

var timeout;
var currentMenuNum;
var currentSubMenuNum;

function OverNavigation() {
    clearTimeout(timeout);
}

function OutOfNavigation(timeOutCallback) {
    timeout = setTimeout(timeOutCallback, 200);
}

function showMenu(menuNum, subMenuNum, isCurrent) {
    // A '-1' passed in indicates to not show any menu items.
    if (menuNum == '-1')
        return;

    if (isCurrent) {
        currentMenuNum = menuNum;
        currentSubMenuNum = subMenuNum;
    }

    var link = document.getElementById("navmenu_" + menuNum);
    var menu = document.getElementById("submenu_" + menuNum);

    //If the navigation item isn't rendered, just exit since there is nothing to show.
    if (link == null)
        return;

    // hide all sub menus
    var subnavcontainer = $(".topsubnav").eq(0);
    var subnavmenus = subnavcontainer.find("div");

    for (i = 0; i < subnavmenus.length; i++) {
        subnavmenus[i].style.display = "none";
    }

    // remove 'selected' class from all top level menu links
    var topnav = $("#topnav");
    var links = topnav.find("a");

    for (i = 0; i < links.length; i++) {
        $(links).eq(i).removeClass("pending");
    }

    // add selected class to selected link and show sub menu
    if (menuNum == currentMenuNum) {
        $('[ID$=topnavul] li .selected').removeClass('selected');
        setShowMenu(menuNum, subMenuNum);
        $(link).addClass("selected");
    } else {
        $(link).addClass("pending");
    }

    $(link).removeClass("persisted");
    $(menu).show();

    //If the SubMenu index is 0 , this means we only are at the top level nav, so don't do anything
    //with the sub nav.	
    if (subMenuNum == '0')
        return;

    //Set the a tag class from the UL parent node.  <UL><LI><A/></UL>
    //var subnavitem = menu.childNodes[subMenuNum].childNodes[0];			
    var subnavitem = menu.getElementsByTagName("a")["subnavmenu_" + subMenuNum];
    if (subMenuNum == currentSubMenuNum) {
        $('[ID$=topsubnav] .selected').removeClass('selected');
        $(subnavitem).addClass("selected");
    } else {
        $(subnavitem).addClass("pending");
    }
}

function hideMenu(menuNum) {
    var menu = document.getElementById("submenu_" + menuNum);
    var link = document.getElementById("navmenu_" + menuNum);
    $(menu).hide();

    //If the navigation item isn't rendered, just exit since there is nothing to remove.
    if (link == null)
        return;

    $(link).removeClass("selected");
}

function persistMenu(menuNum, subMenuNum) {
    var menu = document.getElementById("submenu_" + menuNum);
    var link = document.getElementById("navmenu_" + menuNum);

    //If the navigation item isn't rendered, just exit since there is nothing to persist.
    if (link == null)
        return;

    $('[ID$=topnavul] li .selected').removeClass('selected');
    setShowMenu(menuNum, subMenuNum);
    $(link).addClass("selected");
}

function setShowMenu(navbarId, subMenuId) {

    var timeoutExpiredEvent = 'showMenu(' + navbarId + ',' + subMenuId + ')';

    if ($('#ctl00_Header_navigation')[0]) {
        $('#ctl00_Header_navigation')[0].onmouseout = function () {
            OutOfNavigation(timeoutExpiredEvent);
        };
    }
}

(function createControlEvents() {

    $('.hrefDisabled').die('click');

    $('.hrefDisabled').live('click', function () {

        $('[ID$=topnavul] li .selected').removeClass('selected');

        $(this).addClass('selected');

        if ($(".selected").length > 0 && $(".selected").get(0).id && $(".selected").get(0).id.length > 8) {
            var navbarId = $(".selected").get(0).id.slice(8);
            setShowMenu(navbarId, 0);
        }
    });

})();
