﻿/// <reference name="jquery.js"/>

// namespace
if (typeof GO == "undefined" || !HAT) {
	var GO = {};
}

//Methods
GO.CalculateDimensions =
{
	ChangeDimensions: function() {
	}
}

GO.Effects =
{
	AttachEffects: function() {
		$(".GO-remHref").attr("href", "javascript:void(0);")
		GO.CalculateDimensions.ChangeDimensions();
	},
	Tabs: function() {
		$(".GO-tab-content").hide();
		$("#GO-tab-content-1").show();
		$("#tab-content-1").find(".GO-tab").addClass("GO-tab-on");
		$(".GO-tab").addClass("GO-tab-off");
		$(".GO-tab-link").click(
        function() {
        	var _objId = "#GO-" + $(this)[0].id;
        	$(".GO-tab-content").hide();
        	$(_objId).show();
        	$(".GO-tab").removeClass("GO-tab-on");
        	$(this).find("div").addClass("GO-tab-on");
        	GO.CalculateDimensions.ChangeDimensions();
        }
    )
	}
}
GO.AjaxProgress =
{
	Show: function(sender) {
		$(sender).parent().find('.GO-listingsTable-pagesProgress').show();
	}
}

GO.FlashFunctions =
{
	Switch: function(sender) {

		if ($("#first").is(':visible')) {
			$("#first").hide();
			$("#second").show();
		} else {
			$("#first").show();
			$("#second").hide();
		}
	}
}

GO.ChangeArticle = {
	Init: function() {
		$(".GO-articleLink").click(function(event) {
			event.preventDefault();
			$this = $(this);
			$this.parents(".GO-igOuterContainer").addClass("sfx-hidden");
			$($this.attr("href")).removeClass("sfx-hidden");
		});
	}
}


/*gallery ajax methods*/
function gallery_itemLoadCallback(carousel, state) {
	var _type = "images";
	if (carousel.container.prevObject[0].id.indexOf("video") > 0) {
		_type = "videos";
	}
	if (carousel.prevFirst != null) {
		// Remove the last visible items to keep the list small
		for (var i = carousel.prevFirst; i <= carousel.prevLast; i++) {
			// jCarousel takes care not to remove visible items
			carousel.remove(i);
		}
	}
	var per_page = carousel.last - carousel.first + 1;
	var currPage = 0;
	var f, l;
	var cr = carousel;
	for (var i = carousel.first; i <= carousel.last; i++) {
		var page = Math.ceil(i / per_page);
		if (currPage != page) {
			currPage = page;
			f = ((page - 1) * per_page) + 1;
			l = f + per_page - 1;
			f = f < carousel.first ? carousel.first : f;
			l = l > carousel.last ? carousel.last : l;
			if (carousel.has(f, l)) {
				continue;
			}
			gallery_makeRequest(carousel, f, l, per_page, page, _type);
		}
	}
};

function gallery_itemAddCallback(carousel, first, last, data) {
	var per_page = 2;
	carousel.unlock();
	// Set the size of the carousel
	carousel.size(data.Total);
	for (var i = first; i <= last; i++) {
		var pos = i - 1;
		var idx = Math.round(((pos / per_page) - Math.floor(pos / per_page)) * per_page);
		carousel.add(i, data.Items[idx]);
	}
};

function gallery_makeRequest(carousel, first, last, per_page, page, _type) {
	// Lock carousel until request has been made
	carousel.lock();
	$.get(
        '/GalleryHandler.axd',
        {
        	first: carousel.first,
        	lang: GetLanguage(),
        	last: carousel.last,
        	response: 'json',
        	type: _type
        },
        function(data) {
        	gallery_itemAddCallback(carousel, carousel.first, carousel.last, data);
        },
        'json'
    )
};



/*openGallery(language, section, gallery)*/
function openGallery(l, s, g) {
	var newwindow = window.open("/" + l + "/gallerypopup/default.aspx?l=" + l + "&s=" + s + "&g=" + g, "gallery", "statusbar=no,width=926px,height=746px")
	if (window.focus) { newwindow.focus() }
}
/*openVideo(language, section, gallery)*/
function openVideo(l, s, g) {
	var newwindow = window.open("/" + l + "/videopopup/default.aspx?l=" + l + "&s=" + s + "&g=" + g, "video", "statusbar=no,width=766px,height=583px")
	if (window.focus) { newwindow.focus() }
}
function textCounter(field, cntfield, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	} else {
		document.getElementById(cntfield).innerHTML = maxlimit - field.value.length;
	}
}
function GetLanguage() {
	return new String(document.location).match(new RegExp(".*//.*?\/(\\w\\w\\w)"))[1];
}
function players() {
	alert('players');
}
$('.star').rating({
	callback: function(value, link) {
		$.ajax({
			type: "GET",
			url: "/RatingsHandler.axd",
			data: value,
			success: function(msg) {
			}
		});
	}
});



//url: "http://int-game-on.mrmpweb.co.uk/services/PathFindingService.asmx/RetrieveMapHighScores",
//type: "POST",

GO.getMapHighScore = function(vmapId) {
	var vlocation = $("html").attr("lang").toLowerCase();
	$.ajax({
		url: "http://game-on.intel.com/services/PathFindingService.asmx/RetrieveMapHighScores",
		type: "GET",
		data: ({ locale: vlocation, mapId: vmapId }),
		dataType: "xml",
		success: function(xml) {
			var countScore = 0;
			var html = "<table class='GO-leaderboard-table first'>";
			$(xml).find("HighScoreEntry").each(function() {
				countScore++;
				var rowCol = ((countScore % 2) == 0) ? "even" : "odd";
				html += "<tr class='" + rowCol + "'><td class='countscore'>" + countScore + "</td><td class='name'>" + $(this).find("Name").text() + "</td><td class='score'>" + $(this).children("Score").text() + "</td></tr>";
				if (countScore == 10) { html += "</table><table class='GO-leaderboard-table last'>"; }
			});
			html += "</table>";
			$("#GO-leaderboard-AJAX").html(html);
		}
	});
};

GO.mapSelectorSettings = {
	mapPos: 1,
	maxMaps: 3
}

GO.initMapHighScore = function() {
	var mapID = (document.location.hash.length > 0) ? document.location.hash.substring(1, document.location.hash.length) : 1;
	GO.mapSelectorSettings.mapPos = mapID;
	GO.mapdescriptionChange(mapID);
	GO.getMapHighScore(mapID);
	GO.mapSelectorClickBind();
};

GO.mapdescriptionChange = function(id) {
	$(".GO-leaderboard-mapdescription div").hide();
	$(".GO-leaderboard-mapdescription div:eq(" + (GO.mapSelectorSettings.mapPos - 1) + ")").show();
}

GO.mapSelectorClickBind = function() {
	$(".GO-leaderboard-mapselector img").attr("src", "/images/geekout/inside-the-game/leaderboard-map" + GO.mapSelectorSettings.mapPos + ".jpg");
	$(".GO-leaderboard-mapselector .GO-leaderboard-nextbtn").click(function(event) {
		event.preventDefault();
		GO.mapSelectorSettings.mapPos++;
		GO.mapSelectorSettings.mapPos = (GO.mapSelectorSettings.mapPos > GO.mapSelectorSettings.maxMaps) ? 1 : GO.mapSelectorSettings.mapPos;
		GO.mapdescriptionChange(GO.mapSelectorSettings.mapPos);
		$(".GO-leaderboard-mapselector img").attr("src", "/images/geekout/inside-the-game/leaderboard-map" + GO.mapSelectorSettings.mapPos + ".jpg");
		GO.getMapHighScore(GO.mapSelectorSettings.mapPos);

		return false;
	});

	$(".GO-leaderboard-mapselector .GO-leaderboard-prevbtn").click(function(event) {
		event.preventDefault();
		GO.mapSelectorSettings.mapPos--;
		GO.mapSelectorSettings.mapPos = (GO.mapSelectorSettings.mapPos < 1) ? GO.mapSelectorSettings.maxMaps : GO.mapSelectorSettings.mapPos;
		GO.mapdescriptionChange(GO.mapSelectorSettings.mapPos);
		$(".GO-leaderboard-mapselector img").attr("src", "/images/geekout/inside-the-game/leaderboard-map" + GO.mapSelectorSettings.mapPos + ".jpg");
		GO.getMapHighScore(GO.mapSelectorSettings.mapPos);
		return false;
	});
}

GO.openLeaderboardPage = function(mapID) {
	mapID = (mapID == null || mapID == "undefined") ? 1 : mapID;
	document.location = "../leaderboard/default.aspx#" + mapID;
}

GO.backBtn = function() {
	$(".GO-backtextbtn").click(function(e) {
		e.preventDefault();
		history.go(-1);
		return false;
	});
};

$(document).ready(function() {
	GO.Effects.Tabs();
	GO.Effects.AttachEffects();
	GO.backBtn();
	//GO.ChangeArticle.Init();
	$('.GO-gallery-list').jcarousel({
		itemLoadCallback: gallery_itemLoadCallback
	});
	$("#test").click(GO.FlashFunctions.Switch);
});



