var allDivs = document.getElementsByTagName("div");

function hideAll()
{
	for(var j=0; j<allDivs.length; ++j)
	{
		var tempStr = allDivs[j].id.substr(0,4);
		
		if(tempStr == 'link')
		{
			allDivs[j].style.borderBottom = '0px';//link
		}
		else if(tempStr == 'menu')
		{
			allDivs[j].style.visibility = 'hidden';//menu
		}
	}
}

function showMenu(menuString)//ie. menuString = 1.2
{
	//split up menu
	var menuArray = menuString.split('.');
	//hide all menus
	hideAll();
	
	//show menu/link
	var tempMString = menuArray[0];
	var zIndex = menuArray.length + 1;
	var menuWidth = allDivs["basemenu"].style.width;
	menuWidth = menuWidth.replace(/\D/g,"");
	
	for(var i=0; i<menuArray.length; ++i)
	{
		//alert(allDivs["link" + tempMString]);

		//show link underline
		allDivs["link" + tempMString].style.borderBottom = '1px solid #c5af7d';//link
		
		//determine parent layer's height
		var numLinks = 3;
		var checkID = "";
		//setup the req expression
			var checkIDRegEx = "";
			if(i==0)//root level
				checkIDRegEx = new RegExp("link\\d+");
			else//sub menu
			{
				checkIDRegEx = new RegExp("link" + menuArray[i-1] + "\\.\\d+");
			}
			
		for(var j=0;j<allDivs.length;++j)
		{		
			checkID = allDivs[j].id.match(checkIDRegEx);//match the root level
			
			if(allDivs[j].id == checkID)
			{
				++numLinks;
			}
		}
		
		//position menu and show it
		var leftSpace = menuWidth * (i + 1);
		allDivs["menu" + tempMString].style.left = leftSpace + 'px';
		allDivs["menu" + tempMString].style.minHeight = numLinks * 20 + 'px';
		allDivs["menu" + tempMString].style.height = numLinks * 20 + 'px';
		allDivs["menu" + tempMString].style.visibility = 'visible';//menu
		allDivs["menu" + tempMString].style.zindex = zIndex--;//menu
		
		if(i + 1 < menuArray.length)
			tempMString += "." + menuArray[i+1];
	}
	
	//store current menu in cookie
	setIDinCookie(menuString);
	
	return false;
}

function autoForward()
{
	if(!document.all && navigator.appVersion.charAt(0) != "5")
	{
		alert("Pop-out menu navigation will not work properly with this browser.");
		//window.location = 'http://www.urlThatDoesntUsePopOutMenu.edu';
	}
	else
	{
		var menuString = getIDfromCookie();
		if(menuString.length > 0)
		{
			showMenu(menuString);
		}
	}
}

function getIDfromCookie()
{
	var NameOfCookie = "menuID";

	if (document.cookie.indexOf(NameOfCookie) != -1) 
	{ 
		begin = document.cookie.indexOf(NameOfCookie+"=");//look for cookie
		if (begin != -1)
		{ 
			begin += NameOfCookie.length+1; 
			end = document.cookie.indexOf(";", begin);
			if (end == -1)
			{
				end = document.cookie.length;
				return unescape(document.cookie.substring(begin, end));
			} 
			return unescape(document.cookie.substring(begin, end));
		}
		return '1'; 
	}
	return '1';
}

function setIDinCookie(idValue) 
{
	var NameOfCookie = "menuID";
	var expiredays = null;

	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
	
	document.cookie = NameOfCookie + "=" + escape(idValue) + 
	((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}