// Make columns

// Get list of all shows
// Get list of featured themes
// Get top menu stuff
//  append three columns for all shows
//  append three columns for themes
//  append the last column 


function Footer ()
{

	var SHOW_COLUMNS 	= 3;
	var THEME_COLUMNS 	= 3;
	
	footerObject = this;


	this.myHTML = "";


	// Get the entire list of videos with this seriesKey as a tag
	$.ajax({
		url:    "lib/json.php?getAllShows",
		success: function( data ) 
		{
			footerObject.addColumns (data, "POPULAR SHOWS", SHOW_COLUMNS );
        },
		async:   false
    });

	
	// Get list of featured themes
	$.ajax({
		url:    "lib/json.php?getFeaturedThemes",
		success: function( data ) 
		{
			footerObject.addColumns (data, "POPULAR THEMES", THEME_COLUMNS );
        },
		async:   false
    });	

	// Get list of seasons with this seriesKey
	$.ajax({
		url:    "lib/json.php?getTopMenu",
		success: function( data ) 
		{
			footerObject.addHeaderPages (data );
        },
		async:   false
    });	

	// execute
	this.draw();
		
}


/*
<div class="footLinks">
	SHOWS
	<ul id="footerFeaturedShows" class="footList">
	</ul>
</div>
*/

/**
 * Adds a column of li tags with links to our html cache
 */
Footer.prototype.addColumns = function ( jsonData, aTitle, aColumnCount )
{
	
	var myFeaturedShows;
	var myList = new Array();
	var COLUMNS = aColumnCount;
	var addedCount = 0;
	var totalAddable = 0;

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

	for ( var i = 0; i < COLUMNS; i++ )
	{
		myList[i] = "";
	}
	
	myShows.sort( this.byName );

	for ( var i in myShows )
	{
		if ( myShows[i].myFooterStatus == "E" )
		{
			totalAddable++;
		}		

	}

	for ( var i in myShows )
	{
		currentColumn = Math.floor((COLUMNS / totalAddable) * addedCount);
		if ( myShows[i].myFooterStatus == "E" )
		{
			myList[currentColumn] += "<li><a href=\"#s=" + myShows[i].myKey + "&commandHeaderPageLink\" onclick=\"clickHandler(this); return false;\">" + myShows[i].myName + "</a></li>";	
			addedCount++;
			
		}
	}
	
	this.myHTML += "<div class=\"footLinks\">"+aTitle+"<ul class=\"footList\">"+myList[0]+"</ul></div>";

	for ( var i = 1; i < myList.length; i++ )
	{
		this.myHTML += "<div class=\"footLinks\">&nbsp;<ul class=\"footList\">"+myList[i]+"</ul></div>";
		
	}

}



/**
 * Parses data and spits out top menu recieved from database
 */
Footer.prototype.addHeaderPages = function ( jsonData )
{

	var myData;
	var myList;
	var myHTML;

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

	myList = "";

	for ( var i = 0; i < myData.length; i++ )
	{
		
		// @todo fix exception: skip Share page
		if (myData[i].myKey.toLowerCase() == "share")
		{
			i++;
		}
		myList += "<li><a href=\"#commandHeaderPageLink&hp=" + myData[i].myKey + "\" onclick=\"clickHandler(this);  setWindowFocus( 'head' ); return false;\">" + myData[i].myTitle + "</a></li>";

	}

	// RSS Link
	myList += "<li><a href=\"index.php?q=rss\" onclick=\"clickHandler(this); return false;\">RSS feed</a></li>";
	
	this.myHTML += "<div class=\"footLinks\">INFORMATION<ul class=\"footList\">"+myList+"</ul></div>";

} // END of addHeaderPages


Footer.prototype.byName = function ( anA, aB )
{
	
	anA = anA.myName;
	aB = aB.myName;
	
	if ( anA < aB )
	{
		return -1;
	} else if (aB < anA )
	{
		return 1;
	} else
	{
		return 0;
	}
	
}


Footer.prototype.draw = function ()
{
	
	$("#foot").append(this.myHTML);
	
}
