/*
 * GLOBALS
 */
// index of the topmenu currently on
var indexOn = null;
// css class name for topmenu on state, including space
var TOPMENU_ON_CLASSNAME = " onstate";
// The standard interval for menu actions.
var MENU_TIMEOUT_INTERVAL = 250;
// Timer for hiding a submenu.
var menuOffTimer;

function getTopmenuLinkId(index) {
    return "topmenu_link_" + index;
}

function getSubmenuId(index) {
    return "submenu_" + index;
}

function getTopmenuActiveIndicatorId(index) {
    return "topmenu_active_indicator_" + index;
}



/*
 * Called onmouseover of a top menu button. Turn top and submenus
 * on and off as appropriate.
 */
function topmenuOver(index) {
    handleMenuAlreadyOn(index);
    turnTopmenuOn(index);
    turnSubmenuOn(index);
}


/*
 * Checks to see if any menu items are already in the on state, and
 * handles them.
 */
function handleMenuAlreadyOn(index) {
    // If any menu items were previously on during this page view
    if(indexOn != null) {
        if (indexOn == index) {
            // We're mousing back into the menu that's already on
            clearTimeout(menuOffTimer);
        } else {
            // Turn the previously on menu items off immediately
            turnOffMenuItems(indexOn)
        }
    }
}


/*
 * Set a topmenu item to the on state.
 */
function turnTopmenuOn(index) {
    indexOn = index;
    // Add onstate class to topmenu item, unless it already has it
    var el = document.getElementById(getTopmenuLinkId(index));
    if (el.className.indexOf(TOPMENU_ON_CLASSNAME) == -1) {
        el.className += TOPMENU_ON_CLASSNAME;
    }
    document.getElementById(getTopmenuActiveIndicatorId(index)).style.display = 'none';
}


/*
 * Set a submenu item to the on state.
 */
function turnSubmenuOn(index) {
    document.getElementById(getSubmenuId(index)).style.visibility = 'visible';
}


/*
 * Returns an Array of <a> element nodes within a submenu.
 *
 * Args:
 *  - submenu_id: id of the submenu to get element nodes from.
 */
function getSubmenuLinkNodes(submenu_id) {
    submenu = document.getElementById(submenu_id);
    submenuNodes = submenu.childNodes;
    submenuLinkNodes = new Array();
    for (var i = 0; i < submenuNodes.length; i++) {
        if (submenuNodes[i].nodeType == 1 &&
            submenuNodes[i].tagName.toLowerCase() == "a") {
            submenuLinkNodes.push(submenuNodes[i]);
        }
    }
    return submenuLinkNodes;
}


/*
 * Return the width of the widest link in the nodes arg.
 *
 * Args:
 *  - submenuLinkNodes: An Array of <a> element nodes.
 */
function getSubmenuWidth(submenuLinkNodes, topmenu_link_id) {
    // padding + border
    var WIDTH_OFFSET = 11;
    var widest = document.getElementById(topmenu_link_id).scrollWidth - WIDTH_OFFSET;
    for (var i = 0; i < submenuLinkNodes.length; i++) {
        if (submenuLinkNodes[i].scrollWidth > widest) {
            widest = submenuLinkNodes[i].scrollWidth
        }
    }
    if (topmenu_link_id.charAt(topmenu_link_id.length-1) == "0") {
      return widest + 1;
    } else {
      return widest;
    }
}


/*
 * Called onmouseout of a topmenu item. Kicks off the timer that turns
 * menu items off. 
 */
function topmenuOut(index) {
    // A timer will be kicked off here.
    startMenuOffTimer(index);
}


/*
 * Flip the topmenu item back to non-over state and hide the submenu.
 */
function turnOffMenuItems(index) {
    turnTopmenuOff(index);
    turnSubmenuOff(index);
}


/*
 * Turn topmenu item off by removing the onstate class.
 */
function turnTopmenuOff(index) {
    var el = document.getElementById(getTopmenuLinkId(index));
    el.className = el.className.replace(TOPMENU_ON_CLASSNAME, "");
    document.getElementById(getTopmenuActiveIndicatorId(index)).style.display = '';
}


/*
 * Turn the submenu off by setting its visibility to hidden.
 */
function turnSubmenuOff(index) {
    document.getElementById(getSubmenuId(index)).style.visibility = 'hidden';
}


/*
 * Starts timer that ultimately calls code to turn off menu items.
 */
function startMenuOffTimer(index) {
    var code_string = "turnOffMenuItems('" + index + "', '" + getSubmenuId(index) + "')";
    menuOffTimer = setTimeout(code_string, MENU_TIMEOUT_INTERVAL);
}


/*
 * Called onmouseover of submenu. Cancels the timer for turning menu items off, which
 * is triggered on the mouseout event of the topmenu link.
 */
function submenuOver() {
    clearTimeout(menuOffTimer);
}


/*
 * Called onmouseout of submenu. Starts the timer for turning the submenu off.
 */
function submenuOut(index) {
    startMenuOffTimer(index);
}
