// Menu Scripts for St. Luke Hospitals 2005
// Copyright Health Alliance of Greater Cincinnati
//alert("loading scripts");

var menuState; //global variable holding the current menuState
var ie4 = document.all; //detect ie4+
if (ie4 != null) ie4 = true;
else ie4 = false;

//parallel lists of sub-menus and their corresponding links
var menuLayers = ["menu_general", "menu_women", "menu_teens", "menu_children", "menu_senior", "menu_physicians", "menu_volunteers", "menu_education"];
var menuLinks = ["btn_general", "btn_women", "btn_teens", "btn_children", "btn_senior", "btn_physicians", "btn_volunteers", "btn_education"];

var tempVar;

function openMenu( whatMenu ) {
	//opens a sub-menu, where whatMenu is a layer name as a string
	whatLayer = getLayer(whatMenu);
	whatLayer.className = "openLayer";
	whatLayer.style.visibility = "visible";
	whatLayer.style.overflowY = "visible";
	whatLayer.style.overflowX = "hidden";

	//resets the style of the submenu's link
	whatLink= getLink(whatMenu);
	whatLink.className = "arrowDown";
	setGlobalMenuState( whatMenu );
	
	//if not ie4, reload the page
	if (!ie4) location.reload();
}

function closeMenu( whatMenu ) {
	//closes a sub-menu, where whatMenu is a layer name as a string
	whatLayer = getLayer(whatMenu);
	whatLayer.className = "closedLayer";
	
	if (ie4) whatLayer.style.height = 1;
	else whatLayer.style.height=0;
	
	whatLayer.style.visibility = "hidden";
	whatLayer.style.overflowY = "hidden";
	whatLayer.style.overflowX = "hidden";
	
	//resets the style of the submenu's link
	whatLink= getLink(whatMenu);
	whatLink.className = "arrowRight";
	
	setGlobalMenuState( null );
}

function setGlobalMenuState( newState ) {
	//sets the global menuState variable and saves a cookie to preserve state
	menuState = newState;
	makeCookie( newState );
}

function clickMenu( whatMenu ) {
	//if (ie4) {
		//apply this script to menu buttons
		//save the initial menu state
		var initMenuState = menuState;
		//close the old menu if needed
		if (initMenuState != null & initMenuState != "null" & initMenuState != "undefined") {
			closeMenu( initMenuState );
		}
		//open the new menu
		if (initMenuState != whatMenu) {
			openMenu( whatMenu );
		}
	//}
}

function initMenu( whatMenu ) {
	//call this script on page load
	menuState = null;  //initialize global menuState to null
	//if (ie4) {
	
	//read the cookie
	var storedState = getCookie()
	//check to see if a menu state has been passed
	if (whatMenu != null) {
		openMenu( whatMenu );
	}
	//else check the cookie. if found, read and apply the state
	else {
		if (storedState != null & storedState != "") whatMenu = storedState;
	}
		
	//close the sub-menus
	for (var i=0; i<menuLayers.length; i++ ) {
		if ( menuLayers[i] != whatMenu ) closeMenu(menuLayers[i]);
	}
	setGlobalMenuState( whatMenu );
}

function getLink (whatMenu) {
	//returns the menu link based on a menu name. uses parallel lists at the top of this document.
	var i=0;
	var result = null;
	var returnVal = null;
	while (result == null & i<menuLayers.length) {
		if (menuLayers[i] == whatMenu) result = i;
		i++;
	}
	if (result != null) {
		var whatLink = menuLinks[result];
		var whatObject = document.getElementById(whatLink);
		returnVal = whatObject;
	}
	return returnVal;
}

function makeCookie( newState ) {
	//write newState to the cookie
	writeCookie( "storedState", newState )
}

function getCookie() {
	//read the cookie and return the value if applicable
	return readCookie( "storedState" )
}
function getLayer( whatMenu ) {
	//returns a layer object derived from the layer name as a string
	//return eval("document.all."+ whatMenu )
	return(document.getElementById(whatMenu));
}