﻿function popScreenshot(productCode, imgName, imgWidth, imgHeight) {
	window.open("/ScreenshotPage.aspx?src=" + imgName + "&game=" + productCode, "screenshot", "toolbar=no, status=no, resize, width=" + imgWidth + ", height=" + imgHeight);
}

function Pager(inGamesPerPage, inGamesList) {
	this.CurrentPage = 1;
	this.PagesCount = 0;
	this.GamesPerPage = inGamesPerPage;
	this.CurrentGamesList = inGamesList;
	this.itemPrefix = "GameItem_";
	this.displayPagerCount = 3;
	this.displayActivePagePosit = "center";

	this.ChangePage = function(num) {
		if (num <= 0 || num > this.PagesCount || num == this.CurrentPage) {
			return false;
		}

		var prevPage = this.CurrentPage
		this.HideDisplayed();
		this.CurrentPage = num;
		this.Render();
		this.RefreshPagingControl(prevPage, this.CurrentPage);

		return false;
	}
	this.GotoNextPage = function() {
		this.ChangePage(this.CurrentPage + 1);
	}
	this.GotoPrevPage = function() {
		this.ChangePage(this.CurrentPage - 1);
	}
	this.RefreshPagingControl = function(activeberfore, activeafter) {

		if (this.CurrentPage > 1) {
			if (jQuery("#paging_prev").hasClass("disabled") == true) {
				jQuery("#paging_prev").removeClass("disabled");
				jQuery("#paging_bottom_prev").removeClass("disabled");
			}

		} else {
			if (jQuery("#paging_prev").hasClass("disabled") == false) {
				jQuery("#paging_prev").addClass("disabled");
				jQuery("#paging_bottom_prev").addClass("disabled");
			}
		}
		if (this.CurrentPage >= this.PagesCount) {
			if (jQuery("#paging_next").hasClass("disabled") == false) {
				jQuery("#paging_next").addClass("disabled");
				jQuery("#paging_bottom_next").addClass("disabled");
			}
		} else {
			if (jQuery("#paging_next").hasClass("disabled") == true) {
				jQuery("#paging_next").removeClass("disabled");
				jQuery("#paging_bottom_next").removeClass("disabled");
			}
		}

		var showPagesFrom = 0;
		var showPagesTo = 0;

		switch (this.displayActivePagePosit) {
			case "left":
				showPagesFrom = this.CurrentPage;
				showPagesTo = this.CurrentPage + this.displayPagerCount - 1;
				if (showPagesTo > this.PagesCount) {
					showPagesTo = this.PagesCount;
					showPagesFrom = ((showPagesTo - this.PagesCount + 1) > 0 ? showPagesTo - this.PagesCount + 1 : 1);
				}
				break;

			case "right":
				showPagesFrom = this.CurrentPage - (this.displayPagerCount - 1);
				if (showPagesFrom <= 0) {
					showPagesFrom = 1;
					showPagesTo = (this.displayPagerCount > this.PagesCount ? this.PagesCount : this.displayPagerCount);
				} else {
					showPagesTo = this.CurrentPage;
				}
				break;

			case "center":
			default:

				showPagesFrom = this.CurrentPage - Math.ceil((this.displayPagerCount - 1) / 2);

				if (showPagesFrom <= 0) {
					showPagesFrom = 1;
				}
				showPagesTo = showPagesFrom + (this.displayPagerCount - 1);
				if (showPagesTo > this.PagesCount) {
					showPagesTo = this.PagesCount;
					showPagesFrom = ((showPagesTo - this.displayPagerCount + 1) > 1 ? showPagesTo - this.displayPagerCount + 1 : 1);
				}
				break;
		}

		for (var index = 1; index <= this.PagesCount; index++) {
			
			jQuery("#paging_p" + index).html(index);
			jQuery("#paging_bottom_p" + index).html(index);
			
			if (showPagesFrom <= index && index <= showPagesTo) {
				jQuery("#paging_p" + index).removeClass("no_display");
				jQuery("#paging_bottom_p" + index).removeClass("no_display");

				jQuery("#paging_p" + index).removeClass("active");
				jQuery("#paging_bottom_p" + index).removeClass("active");

				if (index == this.CurrentPage) {
					jQuery("#paging_p" + index).html("<strong>" + index + "</strong>");
					jQuery("#paging_bottom_p" + index).html("<strong>" + index + "</strong>");
				}
				continue;
			}
			if (jQuery("#paging_p" + index).hasClass("no_display") == false) {
				jQuery("#paging_p" + index).addClass("no_display");
				jQuery("#paging_bottom_p" + index).addClass("no_display");
			}
		}
	}

	this.ChangeSortingMode = function(mode) {

		var t1, t2;
		if (document.getElementById("SortByPop").getElementsByTagName("a").length > 0) {
			t1 = document.getElementById("SortByPop").getElementsByTagName("a")[0].innerHTML;
			t2 = document.getElementById("SortByName").innerHTML;
		} else {
			t1 = document.getElementById("SortByPop").innerHTML;
			t2 = document.getElementById("SortByName").getElementsByTagName("a")[0].innerHTML;
		}



		if (this.CurrentGamesList.CurrentSortingMode != mode && this.CurrentGamesList.ChangeSorting(mode) == true) {

			this.HideDisplayed();
			var limit = this.CurrentGamesList.GameListByPopularity.length;
			var listFrom = new Array();

			if (mode == 1) {
				listFrom = this.CurrentGamesList.GameListByName;
			}
			if (mode == 0) {
				listFrom = this.CurrentGamesList.GameListByPopularity;
			}

			var SortingTarget = document.getElementById("CategoryGamesList");
			for (var i = 0; i < limit; i++) {
				SortingTarget.appendChild(document.getElementById("GameItem_" + listFrom[i]));
			}


			if (mode == 0) {
				document.getElementById("SortByPop").className = "filter-selector";
				document.getElementById("SortByPop").innerHTML = t1;
				document.getElementById("SortByName").className = "";
				document.getElementById("SortByName").innerHTML = "<a href=\"\" onclick=\"return false;\">" + t2 + "</a>";
			}

			if (mode == 1) {
				document.getElementById("SortByPop").className = "";
				document.getElementById("SortByPop").innerHTML = "<a href=\"\" onclick=\"return false;\">" + t1 + "</a>";
				document.getElementById("SortByName").className = "filter-selector";
				document.getElementById("SortByName").innerHTML = t2;
			}

			this.CurrentPage = -1;
			this.ChangePage(1);

			return false;
		}
	}
	this.Render = function() {
		var limit = this.CurrentPage * this.GamesPerPage;
		var start = limit - this.GamesPerPage;
		var list = this.CurrentGamesList.GetCurrList();
		var allGamesCount = list.length;

		for (; start < limit && start < list.length; start++) {
			this.DisplayRowById(list[start]);
		}

		limit = this.CurrentPage * this.GamesPerPage;
		index = limit - this.GamesPerPage;

		document.getElementById("GameCountStart").innerHTML = (index + 1);
		document.getElementById("GameCountEnd").innerHTML = ((limit > allGamesCount) ? allGamesCount : limit);

		return true;
	}
	this.DisplayRowById = function(rowId) {
		var tmpName = this.itemPrefix + rowId;

		if (jQuery("#" + tmpName).hasClass("no_display") == true) {
			jQuery("#" + tmpName).removeClass("no_display");
		}
	}
	this.HideRowById = function(rowId) {
		var tmpName = this.itemPrefix + rowId;

		if (jQuery("#" + tmpName).hasClass("no_display") == false) {
			jQuery("#" + tmpName).addClass("no_display");
		}
	}
	this.HideDisplayed = function() {		
		var list = this.CurrentGamesList.GetCurrList();
		var limit = list.length
		for (var index = 0; index < limit; index++) {			
			this.HideRowById(list[index]);
		}

		return true;
	}

	this.PagesCount = Math.ceil(this.CurrentGamesList.GetCurrList().length / inGamesPerPage);
}

function GamesList(ListByPop, ListByName, sortMode) {
	this.GameListByPopularity = ListByPop;
	this.GameListByName = ListByName;
	this.CurrentSortingMode = sortMode;


	this.GetCurrList = function() {
		if (this.CurrentSortingMode == 0) {
			return this.GameListByPopularity;
		} else {
			return this.GameListByName;
		}
	}
	this.ChangeSorting = function(mode) {
		if (mode == 1 && mode != this.CurrentSortingMode) {
			if (this.GameListByName.length != this.GameListByPopularity.length) {

				var tempArray = new Array();
				var tempKeyValue;

				for (var i = 0; i < GamesName.length; i++) {
					tempArray.push(GamesName[i]);
				}

				tempArray.sort(this.GamesNameComparer);


				for (var i = 0; i < GamesName.length; i++) {
					tempKeyValue = GamesName.indexOf(tempArray[i]);
					this.GameListByName.push(this.GameListByPopularity[tempKeyValue]);
				}

			}
		} else if (mode != 0) {
			return false;
		}

		this.CurrentSortingMode = mode;
		return true;
	}

	this.GamesNameComparer = function(g1, g2) {
		var g1n = g1.toLowerCase();
		var g2n = g2.toLowerCase();

		return ((g1n < g2n) ? -1 : ((g1n > g2n) ? 1 : 0));
	}
}


[ ].indexOf || (Array.prototype.indexOf = function(v, n) {
	n = (n == null) ? 0 : n; var m = this.length;
	for (var i = n; i < m; i++)
		if (this[i] == v)
		return i;
	return -1;
});