
//cookie name constants
//currently selected tab id (LI id)
var L1_SELECTED = "L1_SELECTED";

//cookie to indicate whether sub menu is visible or not
var SUB_MENU_HIDDEN = "SUB_MENU_HIDDEN";

var COOKIE_CHECK = "COOKIE_CHECK";

//show hide control - bar appears across bottom of submenu 
var show_hide_control_container = "#show-hide-navigation";
var show_hide_control = "#show-hide-navigation-link";

//toolbar container - contains LI items that control link containers
var navigation_menu = "#main-navigation-menu";

//element that triggers the subenu display event handler
var navigation_menu_controller = "#main-navigation-menu ul li";

//wrapper for child DIVS containing links related to clicked link
var navigation_submenu = "#main-navigation-submenu";
var navigation_submenu_container = "#main-navigation-submenu-container";

//the suffix is used to associate link container DIVS to their controlling LI hyperlink
//e.g. <li id="international-eu-students"> controls <div id="international-eu-students-links" class="second-level">
var submenu_links_container_suffix = "-links";

//class assigned to active tab
var active_tab_class = "active";

//set up a variable to hold id of tab link clicked - null at first but written to by cookie	
var currentTab = null;

$(document).ready(function() {

	//this can only happen if cookies are enabled
	if(cookieSetCheck())
	{
		//event handlers		
		$(navigation_menu_controller).click(function() {
			handleTabClick($(this), "true");
			return false;
		});

		$(show_hide_control).click(function() {	
			toggleSubMenu();
			return false; 		
		});
		
		//check if selected tab cookie exists - need to reshow it
		if ($.cookie(L1_SELECTED)!=null && $.cookie(L1_SELECTED)!="null")
		{
			handleTabClick($("#" + $.cookie(L1_SELECTED)), false);			
		}
	}	
});

//check to see whether cookies can be set
function cookieSetCheck()
{	
	$.cookie(COOKIE_CHECK,"init", {expires: null, path: '/'} );	

	if($.cookie(COOKIE_CHECK) != null)
	{				
		return true;
	}
	return false;
}

function setMenuHiddenCookie(state)
{
	$.cookie(SUB_MENU_HIDDEN,state, {expires: null, path: '/'} );
}

function setTabSelectedCookie(tabId)
{
	$.cookie(L1_SELECTED, tabId, {expires: null, path: '/'} );	
}

function setActiveTabBg(tabId)
{			
	$("#" + tabId + " a").addClass(active_tab_class);
}

function clearActiveTabBg(tabId)
{
	$("#" + tabId + " a").removeClass(active_tab_class);
}

function changeShowHideText(txt)
{
	$(show_hide_control).html(txt)
}

function displayShowHideControl()
{
	$(show_hide_control_container).show();
}

function hideAllSubmenuLinkContainers()
{
	$(navigation_submenu + " div").hide();
}

function displaySelectedSubmenuLinkContainer(id)
{
	hideAllSubmenuLinkContainers();
	var relatedMenuDiv = "#" + id + submenu_links_container_suffix;	
	$(navigation_submenu_container).show();
	$(relatedMenuDiv).show();
}

function slideMenuUp()
{
	$(navigation_submenu).slideUp();
	setMenuHiddenCookie("true");
	changeShowHideText("Show menu")
}

function slideMenuDown()
{
	$(navigation_submenu).slideDown();
	setMenuHiddenCookie("false");
	changeShowHideText("Hide menu");	
}


function showMenu()
{	
	$(navigation_submenu).show();
	setMenuHiddenCookie("false");
	changeShowHideText("Hide menu");
}

function hideMenu()
{
	$(navigation_submenu).hide();
	setMenuHiddenCookie("true");
	changeShowHideText("Show menu")
}

function subMenuHidden()
{
	if($.cookie(SUB_MENU_HIDDEN)=="true" || $.cookie(SUB_MENU_HIDDEN)=="init")
	{
		return true;
	}	
	return false;
}

function toggleSubMenu() 
{
	var currDisplay = $(navigation_submenu).css("display");				

	if (currDisplay == "block")
	{
		slideMenuUp();
	}
	else
	{
		slideMenuDown();
	}
}


//if a tab is already selected then make sure it stays open reselect it

function handleTabClick(theObject, doSlide)
{	
	//get value of tab from cookie
	currentTab = $.cookie(L1_SELECTED);	
	
	//object ID passed by event trigger
	var thisID = theObject.attr("id");

	//clear current tab active class
	clearActiveTabBg(currentTab);	

	//set new tab active class
	setActiveTabBg(thisID);

	//set new tab cookie balue
	setTabSelectedCookie(thisID);

	//display the show hide bar
	displayShowHideControl();
	
	// Get the name of the clicked tab and use it to derive the name of the sub menu links container									
	//hide all DIVs then show the one clicked
	
	displaySelectedSubmenuLinkContainer(thisID);

	//boolean to determine whether to display the submenu or not
	var affectMenuDisplay = false;

	if((subMenuHidden() && thisID != currentTab) || !subMenuHidden())
	{
		affectMenuDisplay = true;
	}
	
	if(affectMenuDisplay)
	{
		//check if menu has been hidden by user			
		if (doSlide)
		{
			slideMenuDown();		
		}			
		else
		{
			showMenu();							
		}	
	}
	
	//reset selected tab cookie
	setTabSelectedCookie(thisID);		
}