// JavaScript Document


var activeMenu = 0;
var runOnce = 0;

function showMenu(menuID)
{
	//if this is the first menu to be opened
	if (activeMenu == 0)
	{
		$('ul#ul'+menuID).slideDown(1000);
	}
	else
	{ //if it's not the first menu to be opened and it is not the currently displayed menu, open the new menu and close the old
		if(activeMenu != menuID)
		{
			$('ul#ul'+menuID).slideDown('slow');
			$('ul#ul'+activeMenu).slideUp(2000);
		}
	}
	activeMenu = menuID;
}

function subMenuCheck()
{
	//if the active menu link is a sub link in the menu open the parent menu item and set it as the activemenu
	//var activeLink = document.getElementsByClassName('active');
	$('a.active').parent().parent().slideDown('slow');
	activeMenu = $('a.active').parent().parent().attr('id');
	activeMenu = activeMenu.substr(2);
}
function hideMenu()
{	
	//this is only triggered by the (home) link to hide whatever menu is currently open
	if ('ul'+activeMenu != $('a.active').parent().parent().attr('id'))
	{
		$('ul#ul'+activeMenu).slideUp(2000);
		activeMenu = 0;
		subMenuCheck();
	}
}

function hideAllMenus()
{	//drupal defaults all menu items to be visible we need to hide them, all of the sub menus are contained in ul's, so lets hide them
	var ar_menu = document.getElementsByTagName('ul');
	for(var i=0; i < ar_menu.length; i++)
    {
		//the first menu item is (home) it is contained in a ul but does not have a submenu so we skip it
		if (i != 0)
		{
			ar_menu[i].style.display = 'none';
			activeMenu = 0;
		}
	}
	//we only want to set the id's of the menu elements one time
	if(runOnce == 0)
	{
		for(var i=0; i < ar_menu.length; i++)
		{
			if (i != 0)
			{
				ar_menu[i].setAttribute('id','ul'+i);
				//ar_menu[i].id = 'ul'+i;
				//$(ar_menu[i]).attr('id','ul'+i);
			}
		}
		//add menu events to the menu links
		AddMenuAttributes()
		
	}
	runOnce = 1;
		
}

function AddMenuAttributes()
{
	//get all the li elements on the page
	var ar_li_menu = document.getElementsByTagName('li'); 
	//start the count at 1
	var li_count = 1;
	//for each li element we need to set some attributes
	for (var j=0; j < ar_li_menu.length; j++)
	{
		if (j == 0)
		{
			//if this is the first li element (home link) set a different onmouseover event
			$('li:first').children("a").attr('onMouseover', 'hideMenu()');
			//$('li:first').children("a").onMouseover = hideMenu;
		}
		//select li's based on their class
		if (ar_li_menu[j].className == 'expanded' || ar_li_menu[j].className == 'last expanded')
		{
			//set the id's of the li elements because drupal doesn't and doesn't let us define id's
			ar_li_menu[j].setAttribute('id','li'+li_count);
			//give the li menu links a mouseover event to show the submenu underneath it.
			$('li#li'+li_count).children("a").attr('onMouseover','showMenu('+li_count+')'); //THIS DOESN'T WORK IN IE7
			//$('li#li'+li_count).children("a").onMouseover = showMenu(li_count); //THIS DOESN'T WORK IN ANY BROWSER
			//document.getElementById('li'+li_count).childNodes[0].onMouseover = showMenu(li_count); //THIS DOESN'T WORK IN ANY BROWSER
			li_count++;
		}
	}
	//check if the page we are on is a sublink of one of the main links
	subMenuCheck();
}
