/**
 * Dependency, jquery-1.2.6.js
 * Dependency, Framework.js
 * 
 * This is the behavior for the all games page
 * 
 * @author Kevin Sweeney (Fi)
 */

/**
 * Styles dropdown menus contained in the search bar
 */
Framework.prototype.createSearchDropdowns = function(){
	var dropdownClasses	= ['search_modes', 'search_genres', 'search_modes', 'search_genres'];
	var dropdowns		= ['searchModes', 'searchGenres', 'searchModes_bottom', 'searchGenres_bottom'];
	var labels			= [config.selectedMode, config.selectedGenre, 'Most Popular', 'Select other Genre'];
	
	var i = 0;
	var size = dropdowns.length;
	for (i = 0; i < size; i++) 
	{
		$('#' + dropdowns[i]).addClass('skinnedDropdown ' + dropdownClasses[i]);

		$('#' + dropdowns[i] + ' .selectLabel').text(labels[i]);

		if (i == 1)
		{
			$('#' + dropdowns[i] + ' select').change(function()
			{
				var nextText = $(this).children('option').get(this.selectedIndex).text;
				$(this.parentNode).children('.selectLabel').text(nextText);
				config.genreParameter = $(this).children('option').get(this.selectedIndex).value;
				$.getJSON(config.yourGamesPaginationFeed + config.genreParameter + "?format=json&page=0&sort=" + config.sortParameter,{},function(data)
				{
					config.currentPage = 0;
					config.numItems = data.relatedgames.list.length;

					$("#Pagination").pagination(config.numItems, {
	 					num_edge_entries: 1,
		 				num_display_entries: 2,
	 					items_per_page:6,
	 					prev_text:"prev",
		 				next_text:"next",
						callback: pageselectCallback
					});
					pageselectCallback(config.currentPage, config.currentjq);
				});			
			});
		} else if (i == 0)
		{
			$('#' + dropdowns[i] + ' select').change(function()
			{
				var nextText = $(this).children('option').get(this.selectedIndex).text;
				$(this.parentNode).children('.selectLabel').text(nextText);
				config.sortParameter = $(this).children('option').get(this.selectedIndex).value;
				var requestUrl = '';
				if(config.genreParameter != '' && config.genreParameter != '/genre-')
				{
					requestUrl = config.yourGamesPaginationFeed + config.genreParameter + "?format=json&page=0&sort=" + config.sortParameter;
				} else
				{
					requestUrl = config.yourGamesPaginationFeed + "?sort=" + config.sortParameter + "&format=json&page=0";
				}
				$.getJSON(requestUrl,{},function(data)
				{
					config.currentPage = 0;
					config.numItems = data.relatedgames.list.length;

					$("#Pagination").pagination(config.numItems, {
	 					num_edge_entries: 1,
		 				num_display_entries: 2,
	 					items_per_page:6,
	 					prev_text:"prev",
		 				next_text:"next",
						callback: pageselectCallback
					});

					pageselectCallback(config.currentPage, config.currentjq);
				});			
			});
		} else
		{
			var nextText = $(this).children('option').get(this.selectedIndex).text;
			$(this.parentNode).children('.selectLabel').text(nextText);
		}
	}
}

var EA = new Object();

/**
 * DOM ready function. This function will fire when the DOM has been loaded. This function
 * is normally fired before all images have been loaded.
 */
$(function(){

	EA.framework = new Framework();
	
	if(EA.framework.getCookied()|location.search.indexOf('YourGames')>0) {
		
		$('#allGamesTabsContent1').removeClass('hidden');
		$('#allGamesTabsContent0').addClass('hidden');
		
		$('#allGames li:eq(0)').removeClass('selected');
		$('#allGames li:eq(1)').addClass('selected');
		
		EA.gameTabs = new TabView("allGames", 1);
		
	}
	else {
		EA.gameTabs = new TabView("allGames", 0);
	}
	
	if (!EA.framework.oldBrowser()) {
		EA.framework.createSearchDropdowns();
	}
	
	EA.framework.setupSearch();
	EA.framework.buildAccordions();
	
	equalizeHeights();
	

	
	
});

/**
 * Ensures that containers displayed in the same "row" have 
 * matching heights. The current container looks at the 
 * adjacent sibling containers and if their heights are not
 * equal, it stretches the shorter of the two.
 */
function equalizeHeights() {
	
	$('.genreList').each(function(index){
		
		if ($(this).prev().height() > $(this).height()) {
			$(this).children('.listContainer').height($(this).prev().height() - 15); // 15px is the bottom padding (caused by header image offset) (sorry for the magic number!)
		}
		else if ($(this).next().height() > $(this).height()) {
			$(this).children('.listContainer').height($(this).next().height() - 15);
		}
		
	});
	
}


$(document).ready(function(){
	if ($('#YGNG').get(0)) {
		buildHeaderGames();
	}
	});



function buildHeaderGames() {

	var NewGamesCode ="";
    $.getJSON(config.yourGamesPaginationFeed + config.genreParameter + "?format=json&page=0&sort=2" + config.hardwareParameter + config.carrierParameter,
        function(data){
          $.each(data.relatedgames.list, function(i,item){
		  NewGamesCode += "<div style='clear:both;'><div style='float:left;padding-right:10px;'><img src='/Web/refresh/image/YG"+(i+1)+".gif'>&nbsp;</div><div style='float:left; width:160px;'>";		  
		   NewGamesCode += "<a class='YGHprod' href='"+item.productPage+"'>"+item.header+"</a><br/>"		   
		   	if (item.comingSoon == "true"){
				NewGamesCode += "Coming Soon!"			
	 		} else {	
			buylink = item.buy;
			if (item.tryLink != "null"){
					
					NewGamesCode += "<a rel='nofollow facebox' class='YGHbuy' href='javascript:jQuery.facebox({ ajax: \""+buylink +"\"})'>Buy it</a> | <a class='YGHtry' href='"+item.tryLink+"'>Try it</a>";
				}else{
					NewGamesCode += "<a rel='nofollow facebox' class='YGHbuy' href='javascript:jQuery.facebox({ ajax: \""+buylink +"\"})'>Buy it</a>";
				}
			}	
			NewGamesCode += "<br/><br/></div></div>";				   
            if ( i == 4 ) return false;
          });
		  	$("#YGNG")[0].innerHTML=NewGamesCode;
        });
	var MPGamesCode ="";
    $.getJSON(config.yourGamesPaginationFeed + config.genreParameter + "?format=json&page=0&sort=0" + config.hardwareParameter + config.carrierParameter,
        function(data){
          $.each(data.relatedgames.list, function(i,item){
		  MPGamesCode += "<div style='clear:both;'><div style='float:left;padding-right:10px;'><img src='/Web/refresh/image/YG"+(i+1)+".gif'>&nbsp;</div><div style='float:left; width:160px;'>";		  
		   MPGamesCode += "<a class='YGHprod' href='"+item.productPage+"'>"+item.header+"</a><br/>"		   
		   	if (item.comingSoon == "true"){
				MPGamesCode += "Coming Soon!"			
	 		} else {	
			buylink = item.buy;
				if (item.tryLink != "null"){
					MPGamesCode += "<a rel='nofollow facebox' class='YGHbuy' href='javascript:jQuery.facebox({ ajax: \""+buylink +"\"})'>Buy it</a> | <a class='YGHtry' href='"+item.tryLink+"'>Try it</a>";
				}else{
					MPGamesCode += "<a rel='nofollow facebox' class='YGHbuy' href='javascript:jQuery.facebox({ ajax: \""+buylink +"\"})'>Buy it</a>";
				}
			}	
			MPGamesCode += "<br/><br/></div></div>";				   
            if ( i == 4 ) return false;
          });
		  	$("#YGMP")[0].innerHTML=MPGamesCode;
        });

	}
