/* Copyright 2007 All About Eyecare Optometrists Incorporated.  All rights reserved. */

var menuLingerTimeout = 100;//how many millis for the menu to linger before disappearing.
var menuItemArr = []; //each menu item is held in an array, indexed by id.
var menuTimeoutArr = []; //each menu item timeout is held in an array, indexed by id.

/**
Sets the menu with the class name and the unordered list.
@param className The class name of the list items.
@param uListIn The name of the Unordered List <UL> of menu items, as specified by the id="" property.
*/
function setMenu(className, uListIn) {
if (document.getElementById) {
    //gets the list of elements that are tagged <ul>
	uList = document.getElementById(uListIn).getElementsByTagName('ul');
	var i,j; //counters
    var currItem, elementArr;
	var arrIndex = menuItemArr.length;
	for (i = 0; i < uList.length; i++) {
        //go through the list
		if (currItem = uList[i].getAttribute('id')) {
			menuItemArr[arrIndex] = currItem;
			currItem = uList[i].parentNode;
			currItem.className = className;

            //create function references for what to do when onMouseOver
			fPtrOnMouseOver = new Function('showMenu(\'' + menuItemArr[arrIndex] + '\');');
            //assign it to the currItem.
			currItem.onmouseover = fPtrOnMouseOver;
            //create function references for what to do when onMouseOut
			fPtrOnMouseOut = new Function('hideMenuAfterTimeout(\'' + menuItemArr[arrIndex] + '\');');
            //assign it to the currItem.
			currItem.onmouseout = fPtrOnMouseOut;

            //go through each hyperlink that is attached to this element
			elementArr = currItem.getElementsByTagName('a');
			for (j = 0; j < elementArr.length; j++) {
                //assign the same function pointers to each element.
				elementArr[j].onfocus = fPtrOnMouseOver;
				elementArr[j].onblur = fPtrOnMouseOut;
			}
			arrIndex++;
		}
	}
}}

/**
* shows the menu
*/
function showMenu(menuId) {
    var i;
    //go through each menu item
	for (i = 0; i < menuItemArr.length; i++) {
		if (document.getElementById(menuItemArr[i]).style.display != 'none') {
			if (menuItemArr[i] != menuId) {
                hideMenu(menuItemArr[i]);
            } else {
                clearMenu(menuItemArr[i]);
            }
		}
	}
	document.getElementById(menuId).style.display = 'block';
}

/**
* Hides the menu right away.
* @param menuId The id of the menu item to hide.
*/
function hideMenu(menuId) {
	clearMenu(menuId);
    //hide the menu item by accessing style properties.
	document.getElementById(menuId).style.display = 'none';
}

/**
* Triggers hideMenu after the timeout value specified by menuLingerTimeout 
* @param menuId The id of the menu item to hide.
*/
function hideMenuAfterTimeout(menuId) {
	menuTimeoutArr[menuId] = setTimeout('hideMenu(\'' + menuId + '\');', menuLingerTimeout);
}

/**
* clears the menu of the timeout request
* @param menuId The id of the menu item to clear.
*/
function clearMenu(menuId) {
	if (menuTimeoutArr[menuId]) {
		clearTimeout(menuTimeoutArr[menuId]);
		menuTimeoutArr[menuId] = null;
	}
}

