/*
 * This file contains all functions related to changing entire page.
 */


/** Changes the information contained on the page.
  * @param pageKey the key to any one series
  */
function loadPage( pageKey )
{

	currentPage = pageKey;
	
	postControllerObject.change( pageKey );	
	
	if (!upperDrawerIsClosed)
	{	
		toggleUpperDrawer();
	}

	$.ajax({
		url:    "lib/json.php?getSeriesInfo=" + pageKey,
		success: function( data ) 
		{
			grandma.seriesCallback( data, pageKey );
        },
		async:   false
    });	
		
	/*
	 * All exceptions
	 */
	
	if (pageKey == homePage)
	{
		$("#updateLabel").html("NETWORK UPDATES");
		$("#seasonsOrMoreVideos").hide();
	} else
	{
		$("#seasonsOrMoreVideos").show();
		$("#updateLabel").html("UPDATES");
	}
	
} // END of loadPage(key)


/** 
 * Reads all shows from the database and stores them in a global array.
 * Called in index.php
 */
function allShowsCallback( jsonData )
{

	var myData = eval ( "(" + jsonData + ")" );

	for (var i in myData)
	{
		allShows[i] = myData[i];
	}
	
}


/**
 * Changes the page to reflect the currently selected series
 */
function seriesCallback( jsonData, pageKey )
{

	var seriesDescription, myData, bannerTitle, currentPageType;
	var isRecognized;

	myData = eval ( "(" + jsonData + ")" );

	bannerTitle = pageKey.substr(0,1).toUpperCase() + pageKey.substr(1,pageKey.length);

	seriesDescription = myData["mySeriesDescription"];

	// If the current page type is not defined it must be a theme
	currentPageType = myData["myType"] ? myData["myType"] : "T";

	if (!!myData["mySeriesDescription"])
	{
		$("#seriesDescription").html(myData["mySeriesDescription"]);
	} else
	{
		$("#seriesDescription").html("");
	}


	/*
	 * If it's a featured show or theme
	 */
	var isRecognized = false;
	for ( var i in allShows )
	{
		if ( !isRecognized && pageKey.toLowerCase() == this.allShows[i].myKey.toLowerCase())
		{
			isRecognized = true;
			$("#bannerContainer").css("background", "#FFF");
			$("#bannerContainer").html("<img id=\"seriesBanner\" src=\"img/"+myData.mySeriesBanner+"\">");	
		}

	}
	
	if ( isRecognized == false)
	{
		$("#bannerContainer").css("background", "#BBB");
		$("#bannerContainer").html("<div id=\"themeForegroundLetters\">" + bannerTitle + "</div><div id=\"themeBackgroundLetters\">" + bannerTitle + "</div>");	
	}
	
	videoMenuObject.change( "tab", currentPage, currentPageType );	// currentPage is global
	
} // END of seriesCallback
