/*--------------------------------------------------------------------------------
-	Script Name:	EcoNavCode
-	Version:		1.0
-	Author:			John Bailey
-	Description:	EcoNavCode is a navagation systems which uses javascript drop
-					down menus.  This code should be included in all pages that 
-					use the menus.  Each menu should be configured in its own file.
--------------------------------------------------------------------------------*/

	/*
		Config Vars
	*/
	var MENUWIDTH = 155
	var pageNavs = new Array()
	var navDispTime
	var BrowserName = navigator.appName;
	if (BrowserName == "Microsoft Internet Explorer")
		var DropMenu = "True"
	else
	{	var DropMenu = "False";
		
	}
	
	
	/*
		Init is the main method in the process.  It is fed a Nav name and adds
		the name to the global list.  The next step is to find all the items in
		the Nav and set there behaviors and hide the Nav.  
	*/

	function Init(navName)
	{
		if(DropMenu == "True")
		{
			var myNav = eval(navName);
			myNav.style.visibility="visible"
		
			addNav(navName)
			document.body.onclick = new Function("hideNav()")
		
			var items = myNav.children
			for(var i = 0;i< items.length;i++)
			{
				items[i].style.visibility = "hidden"
				var items2 = items[i].children
				for(var j = 0;j < items2.length;j++)
				{
					items2[j].style.width=MENUWIDTH
					items2[j].className = items[i].offClass
					items2[j].onmouseover = new Function("highlight("+items2[j].id+");clearTimer()")
					items2[j].onmouseout = new Function("highlight("+items2[j].id+");resetTimer()")
					items2[j].onclick = new Function("go("+items2[j].id+")")
					findSubNavs(items2[j]);
				}
			}
		}
	}
	/*
		finSubNavs is a recursive function that is given a Nav name and will recurse and 
		find all subNavs in the hierarchy.  It also places an image on the item to denote
		the fact there is a subNav.
	*/
	function findSubNavs(navName)
	{
		var subNavs = navName.children
		for(var k =0; k < subNavs.length;k++)
		{
			navName.innerHTML += ">"
			subNavItems = subNavs[k].children
			for(var l =0; l < subNavItems.length;l++)
			{
				subNavItems[l].className = subNavs[k].offClass
				subNavItems[l].onmouseout = new Function("highlight("+subNavItems[l].id+");resetTimer()")
				subNavItems[l].onmouseover = new Function("highlight("+subNavItems[l].id+");clearTimer()")
				subNavItems[l].onclick = new Function("go("+subNavItems[l].id+")")	
				findSubNavs(subNavItems[l]);
			}
			navName.onmouseover = new Function("showSubNav("+navName.id+");clearTimer()");
			navName.onmouseout = new Function("highlight("+navName.id+");resetTimer();hideSubNav("+navName.id+")");
			subNavs[k].style.visibility = "hidden"
			
		}
	}
	/*
		showNav simply places a Nav at a given left position and a given right position.
		It also hides all other Navs.
	*/
	function showNav(navName,left,top)
	{
		var myNav = eval(navName)
		hideNav()
		myNav.fromLeft = left
		myNav.style.left = left
		myNav.style.top = top
		myNav.style.visibility = "visible"	
	}
	/*
		showRightNav is just like show showNav only it the horizantal position
		is from the left and the menu will always stay that far from the the right
	*/
	function showRightNav(navName,right,top)
	{
		hideNav()
		var myNav = eval(navName)
		if(document.body.clientWidth <= 800)
		{
			myNav.style.left = 830 - right
		}
		else
		{
			myNav.style.left = document.body.offsetWidth - right + 50
		}
		myNav.style.top = top
		myNav.style.visibility = "visible"

	}
	/*
		This function shows a subNav when it's parent item is moused over.
		The width is based on a config var 'MENUWIDTH'
	*/
	function showSubNav(parent)
	{
		var myParent = eval(parent)
		var myNav = (myParent.children)[0]
		var test = myParent.parentElement.style.left
		test = test.substring(0,test.indexOf("px"))
		var test2 = parseInt(test) + (myParent.parentElement.style.width.substring(0,myParent.style.width.indexOf("px")) * 2)
		if( test2 > document.body.clientWidth )
		{
			
			//myNav.style.posLeft = -myParent.style.width
		}
		else
		{
			//document.write(	myParent.style.width );
			//myNav.style.posLeft = myParent.width
		}
		myNav.style.visibility = "visible"
	}

	/*
		Hides all the Navs on the page using the global list of Navs.
		If a subNav exists then hideSubNav is called.
	*/
	function hideNav()
	{
		clearTimer()	//	Unessecary but we'll do it just in case
		for(var k = 0;k < pageNavs.length;k++)
		{
			var myNav = eval(pageNavs[k])
			var items = myNav.children
			for(var i = 0;i< items.length;i++)
			{
				items[i].style.visibility="hidden"
				//	Hide subNavs if they exist
				var items2 = items[i].children
				for(var j = 0;j < items2.length;j++)
				{
					hideSubNavs(items2[j]);
				}
			
			}
		}

	}
	function hideSubNav(item)
	{
		clearTimer()	//	Unessecary but we'll do it just in case
		var subNav = item.children
		for(var k = 0;k < subNav.length;k++)
		{
			subNav[k].style.visibility="hidden"
		}

	}
	/*
		Hides the subNavs for a given Nav item.  This is a recursive
		function that will hide all subsaquent submenus.  Very Nice!
	*/
	function hideSubNavs(navName)
	{
		var subNavs = navName.children
		for(var k =0; k < subNavs.length;k++)
		{
			subNavs[k].style.visibility = "hidden"
			var subNavs2 = subNavs[k].children
			for(var l =0; l < subNavs2.length;l++)
			{
				hideSubNavs(subNavs2[l]);
			}
		}

	}
	/*
		Goes to the link listed for a given item
	*/
	function go(itemID)
	{
		if(itemID.link != null)
		{
			location.href = itemID.link
		}
	}

	/*
		Changes the CSS class when an item is mouse over.  A comparison is made 
		between the items current class and the Navs offClass and acts accordingly.
	*/
	function highlight(itemID)
	{
		
		//	Change the items CSS class to the opposite className
		if(itemID.className == itemID.parentElement.offClass)
		{
			itemID.className = itemID.parentElement.onClass
		}
		else
		{ 
			itemID.className = itemID.parentElement.offClass
		}
	}

	/*
		Adds a Nav to the global list of navs
	*/
	function addNav(navName)
	{
		var i = pageNavs.length
		pageNavs[i] = navName
	}


/*----------------------------------------------------------------
	Functions used to keep broweser in check
----------------------------------------------------------------*/

	/*
		Makes sure the window stays at least 800px wide
	*/
	function window_resize()
	{
		if(document.body.clientWidth < 800)
		{
			document.body.Width = 800
		}
	}
	/*
		Trap the window resize events
	*/
	   window.onload = window_resize
	   window.onresize = window_resize

/*----------------------------------------------------------------
	Delay Timer Functions - Used to hide menus after inactivity
----------------------------------------------------------------*/
	/*
		Sets Timeout to 2 secs and then it hides the navs.
		This will occur after a user's mouse exits a Nav. This
		delay takes away the touchiness of just hiding when the
		mouse leaves the Nav.
	*/
	function resetTimer()
	{
		clearTimeout(navDispTime)
		navDispTime = setTimeout("hideNav()", 400);
	}
	/*
		Turns the timeout off in order to keep the Nav up when an
		Item is mousedOver
	*/
	function clearTimer()
	{
		clearTimeout(navDispTime)
	}

