﻿
var HoverMenu = {

    oTimer: null,
    oDivToBeHidden: null,
    oMenus: []
    ,
    addLoadEvent: function(func) {
        var oldonload = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = func;
        }
        else {
            window.onload = function() {
                oldonload();
                func();
            }
        }
    }
    ,
    registerMenu: function(id) {
        var bInList = false;
        for (var i = 0; i < HoverMenu.oMenus.length; i++) {
            if (HoverMenu.oMenus[i].id == id) {
                bInList = true;
                break;
            }
        }
        if (!bInList) {
            HoverMenu.oMenus.push(document.getElementById(id));
        }
    }
    ,
    getCoords: function(el) {
        var iTop = 0, iLeft = 0;
        var current = el;
        while (current) {
            iTop += current.offsetTop;
            iLeft += current.offsetLeft;
            current = current.offsetParent;
        }
        var iBottom = iTop + el.offsetHeight;
        var iRight = iLeft + el.offsetWidth;
        var coords = { top: iTop, bottom: iBottom, left: iLeft, right: iRight };
        return coords;
    }
    ,
    toggleMenu: function(el, bShow) {
        el.style.visibility = bShow ? 'visible' : '';
        el.style.display = bShow ? 'block' : '';
    }
    ,
    clearHideTimer: function() {
        if (HoverMenu.oTimer) {
            window.clearTimeout(HoverMenu.oTimer);
        }
        HoverMenu.oDivToBeHidden = null;
        HoverMenu.oTimer = null;
    }
    ,
    hideAllMenus: function(divIdThatWillBeShown) {
        HoverMenu.clearHideTimer();
        for (var i = 0; i < HoverMenu.oMenus.length; i++) {
            // If divIdThatWillBeShown is passed in, it will contain the ID of the DIV that
            // is about to be shown.  If may already be visible if the mouse is moving from
            // the menu to the link.  In this case, don't turn off that menu DIV.
            if (!divIdThatWillBeShown || HoverMenu.oMenus[i].id != divIdThatWillBeShown) {
                HoverMenu.toggleMenu(HoverMenu.oMenus[i], false);
            }
        }
    }
    ,
    showMenu: function(oLink, divId) {
        HoverMenu.registerMenu(divId);
        HoverMenu.hideAllMenus(divId);
        var oDiv = document.getElementById(divId);

        // If the mouse is moving from the menu to the link, the menu will already be
        // visible.  In this case, there is no need to turn the menu back on.
        if (oDiv.style.visibility == 'visible') { return; }

        var linkCoords = HoverMenu.getCoords(oLink);
        oDiv.style.top = (linkCoords.bottom + 0) + "px";
        oDiv.style.left = (linkCoords.left -6) + "px";
        HoverMenu.toggleMenu(oDiv, true);
    }
    ,
    onMenuHover: function(oDiv) {
        if (HoverMenu.oTimer && HoverMenu.oDivToBeHidden && HoverMenu.oDivToBeHidden.id == oDiv.id) {
            HoverMenu.clearHideTimer();
        }
    }
    ,
    fullyHideMenu: function(oDiv) {
        HoverMenu.hideAllMenus();
    }
    ,
    hideMenu: function(oDiv) {
        // oDiv may be the actual Div, or it may be the Div ID.
        if (typeof oDiv === "string") {
            oDiv = document.getElementById(oDiv);
        }
        HoverMenu.oDivToBeHidden = oDiv;
        HoverMenu.oTimer = window.setTimeout(function() { HoverMenu.fullyHideMenu(oDiv); }, 150);
    }
};

function toggleShowMoreLinks(oLink, sTableId) {

    var sShowingMoreLinksCssClass = "showing-more-links";
    var oTable = jQuery("#" + sTableId);
    if (oTable && oTable.length > 0) {
        if (oTable.hasClass(sShowingMoreLinksCssClass)) {
            oTable.removeClass(sShowingMoreLinksCssClass);
            oTable.find("tr.ShowInitiallyHiddenRow").removeClass("ShowInitiallyHiddenRow").addClass("RowFallsOutsideInitialVisibility");
            oLink.innerHTML = "( Show More Links )";
        } else {
            oTable.addClass(sShowingMoreLinksCssClass);
            oTable.find("tr.RowFallsOutsideInitialVisibility").removeClass("RowFallsOutsideInitialVisibility").addClass("ShowInitiallyHiddenRow");
            oLink.innerHTML = "( Show Less Links )";
        }
    }
    return false;
}

