/**
 *  This file contains all the functions related to populating and animating the drop down SHOWS and THEMES menus
 */

// @TODO
// drop menu object
// drop menu display functions
// adding arrays and keeping desired order...
// draw

// Global variables
var dropMenuTimeOut 	= 20;
var dropMenuCloseTime	= 0;
var dropMenuItem      	= 0;
var dropDownLink		= 0;

document.onclick = dropMenuClose;

// really stupid
menuArray = new Array();
menuArrayAdditions = 0;


function dropMenuOpen()
{	
	
	dropMenuCancelTimer();
	dropMenuClose();

	dropMenuItem = $(this).find("span").eq(0);
	dropDownLink = $(this).find("a").eq(0);

	dropMenuItem.css("visibility", 	"visible");
	dropMenuItem.css("z-index", 	"555555");

	dropDownLink.css("background", 	"#C00");
	dropDownLink.css("color",		"#FFF");

}

function dropMenuClose()
{	
	if ( dropDownLink )
	{
		dropDownLink.css("background", 	"#FFF");
		dropDownLink.css("color",		"#000");
	}
	if (dropMenuItem) 
	{
		dropMenuItem.css("visibility", "hidden");
	}
}

function dropMenuTimer()
{	
	dropMenuCloseTime = window.setTimeout(dropMenuClose, dropMenuTimeOut);
}

function dropMenuCancelTimer()
{	
	if(dropMenuCloseTime)
	{	
		window.clearTimeout(dropMenuCloseTime);
		dropMenuCloseTime = null;
	}
}


/*
 * Callbacks and population
 */


/**
 * Creates a list for drop menu
 */
function featuredShowsCallback ( jsonData )
{

	var myFeaturedShows;
	var myList;
	var myHTML;

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

	myList = "";

	for ( var i in myFeaturedShows )
	{
		myList += "<li><a href=\"#s=" + myFeaturedShows[i].myKey + "\" onclick=\"clickHandler(this); return false;\">" + myFeaturedShows[i].myName + "</a></li>";	
	}

	myHTML = "<li class=\"dottedBottom\"></li><li></li><li><a href=\"#hp=shows&commandToggleDrawer\" onclick=\"clickHandler(this); return false;\">ALL SHOWS</a></li>" + myList + "</ul>";
// </ul>
	writeExploreMenu ( myHTML, 1, 2 );
	
} // END of featuredShowsCallback()


/**
 * Handles the list of featured themes and places them in the top menu as well as the footer.
 */
function featuredThemesCallback ( jsonData )
{

	var myFeaturedThemes;
	var myHTML;
	var myList;

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

	for (var i in myFeaturedThemes)
	{
		allThemes[i] = myFeaturedThemes[i];
	}

	myList	= "";

	for ( var i in myFeaturedThemes )
	{
		myList += "<li><a href=\"#s=" + myFeaturedThemes[i].myKey + "\">" + myFeaturedThemes[i].myName + "</a></li>";
	}
//<ul id=\"exploreThemesList\">
	myHTML 		= "<ul id=\"exploreShowsList\"><li><a href=\"#hp=themes&commandToggleDrawer\" onclick=\"clickHandler(this); return false;\">ALL THEMES</a></li>" + myList + "";

	writeExploreMenu ( myHTML, 0, 2 );
} // END of featuredThemesCallback()

// @todo, change this, this is stupid
function writeExploreMenu ( someHTML, anOrder, aThreshold )
{

	menuArray[anOrder] = someHTML;
	menuArrayAdditions++;
	allHTML = "";
	
	if (menuArrayAdditions>= aThreshold)
	{
		for ( var i in menuArray )
		{
			
			allHTML += menuArray[i];
		}
		//$("#exploreContainer").append( allHTML );
	}	
	
}
