
var showDelay = 300;
var hideDelay = 500;
var showTimer;
var hideTimer;
var currentMenuId;
var allTopEntries = new Object();
var allSubContent = new Object();


function ShowSubMenu(no) {
    //currentMenuId = "SM" + no;
    clearTimeout(hideTimer);
    clearTimeout(showTimer);

    for (prop in allTopEntries) {
        if (allTopEntries[prop].isActive)
            jQuery("#" + prop + "_link").css("background-position", "right -85px");
        else
            jQuery("#" + prop + "_link").css("background-position", "right -1px");
    }

    showTimer = setTimeout("ShowSubMenuDelayed('" + no + "')", showDelay);

    allTopEntries["SM" + no] = new Object();
    if (jQuery("#SM" + no + "_link").hasClass("active"))
        allTopEntries["SM" + no].isActive = true;
    else
        allTopEntries["SM" + no].isActive = false;

    if (allTopEntries["SM" + no].isActive)
        jQuery("#SM" + no + "_link").css("background-position", "right -57px");
    else
        jQuery("#SM" + no + "_link").css("background-position", "right -29px");
}

function ShowSubMenuDelayed(no) {
    currentMenuId = "SM" + no;
    if (BuildMenu(jQuery("#TM" + no))) 
    {
        HideShowNotOverlayableElements("hide");
        jQuery("#cSubmenu").css("display", "block");
    }
    else
    {
        jQuery("#cSubmenu").css("display", "none");
    }
}

function HideShowNotOverlayableElements(showHide) {
    if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7)
        jQuery("select").css("visibility", (showHide == "show" ? "visible" : "hidden"));
}

function StopHideTimer() {
    clearTimeout(hideTimer);
    for (prop in allTopEntries) {
        if (allTopEntries[prop].isActive)
            jQuery("#" + prop + "_link").css("background-position", "right -85px");
        else
            jQuery("#" + prop + "_link").css("background-position", "right -1px");
    }
    if (allTopEntries[currentMenuId].isActive)
        jQuery("#" + currentMenuId + "_link").css("background-position", "right -57px");
    else
        jQuery("#" + currentMenuId + "_link").css("background-position", "right -29px");
}


function HideSubMenu() {
    clearTimeout(showTimer);
    hideTimer = setTimeout("HideSubMenuDelayed()", hideDelay);
}


function HideSubMenuDelayed() {
    for (prop in allTopEntries) {
        if (allTopEntries[prop].isActive)
            jQuery("#" + prop + "_link").css("background-position", "right -85px");
        else
            jQuery("#" + prop + "_link").css("background-position", "right -1px");
    }

    HideShowNotOverlayableElements("show");

    jQuery("#cSubmenu").css("display", "none");
}


function BuildMenu(elm) {
    if (allSubContent[currentMenuId]) {
        SetMenuWidthAndHeight(allSubContent[currentMenuId].noOfColumns, allSubContent[currentMenuId].noOfRows, elm);
        jQuery("#cSubmenuContent").html(allSubContent[currentMenuId].html);
        return true;
    }
    else
    {
        var mc = "<div>";
        var children;
        try {
            children = document.getElementById(currentMenuId).childNodes;
        }
        catch (e) {
            return false;
        }
        var noOfColumns = 1;
        var noOfRows = 0;
        var noOfRowsMax = -1;

        for (var i = 0; i < children.length; i++) {
            
            // nodeType == 1 are all nodes that are real tags (ELEMENT_NODE)
            // DOM nodes that are whitespace only have a different nodeType an can be ignored
            // FF returns whitespace as DOM nodes, which would break this code
            if (children[i].nodeType != 1) continue;
            
            var newColumn = children[i].className;
            if (newColumn == "newColumn") {
                mc += "</div><div>";
                noOfColumns++;
                if (noOfRows > noOfRowsMax)
                    noOfRowsMax = noOfRows;
                noOfRows = 0;
            }
            var fchild = getFirstChild(children[i]);
            var title = fchild.innerHTML;
            var noTop = fchild.className;
            var cClass = "";
            if (title && title.toLowerCase().indexOf("<br>") != -1) {
                cClass = noTop=="" ? "topTwoLines" : "twoLines";
                noOfRows = noOfRows + 2;
            }
            else {
                cClass = noTop=="" ? "top" : "";
                noOfRows++;
            }
            var href = jQuery(fchild).attr("href"); //children[i].firstChild["href"];
            var target = jQuery(fchild).attr("target"); //children[i].firstChild["target"];
            if (target != "") target = ' target="' + target + '"';
            if (href != "" && href != null)
            {
                mc += '<a href="' + href + '"' + target + ' class="' + cClass + '">' + title + '</a>';
            }
            else 
            {
                mc += '<span class="' + cClass + '">' + title + '</span>';
            }

            var subEntries = jQuery(children[i]).children("ul.level3").children();
            
            // Test for sub entries
            if (subEntries.length > 0) {
                for (var j = 0; j < subEntries.length; j++) {

                    if (subEntries[j].nodeType != 1) continue;
                    
                    var firstChild = getFirstChild(subEntries[j]);
                    
                    var html = firstChild.innerHTML; 
                    if (!html) continue;
                    
                    var subItemClass = "";
                    if (html.toLowerCase().indexOf("<br>") != -1) {
                        subItemClass = ' class="twoLines"';
                        noOfRows = noOfRows + 2;
                    }
                    else {
                        noOfRows++;
                    }
                    var subItemHref = jQuery(firstChild).attr("href"); // subEntries[j].firstChild["href"];
                    var subItemTarget = jQuery(firstChild).attr("target"); // subEntries[j].firstChild["target"];
                    if (subItemTarget != "") subItemTarget = ' target="' + subItemTarget + '"';
                    mc += '<a href="' + subItemHref + '"' + target + subItemClass + '>' + html + '</a>';
                }
            }
        }
        mc += "</div";

        if (noOfRows > noOfRowsMax)
            noOfRowsMax = noOfRows;

        SetMenuWidthAndHeight(noOfColumns, noOfRowsMax, elm);

        jQuery("#cSubmenuContent").html(mc);
        allSubContent[currentMenuId] = new Object();
        allSubContent[currentMenuId].html = mc;
        allSubContent[currentMenuId].noOfColumns = noOfColumns;
        allSubContent[currentMenuId].noOfRows = noOfRowsMax;

        return true;
    }
}

function SetMenuWidthAndHeight(columns, rows, topMenuElement) {
    var columnWidth = 228;
    var rowHeight = 24;
    var posX = jQuery(topMenuElement).position().left + 11;

    var width = 8 + ((columnWidth + 8) * columns);
    var height = 16 + (rowHeight * rows);

    jQuery("#cSubmenuContainer").css("width", width + "px");
    jQuery("#cSubmenu_ds4").css("width", (width + 2) + "px");
    jQuery("#cSubmenu_ds3").css("width", (width + 4) + "px");
    jQuery("#cSubmenu_ds2").css("width", (width + 6) + "px");
    jQuery("#cSubmenu_ds1").css("width", (width + 8) + "px");
    jQuery("#cSubmenu").css("width", (width + 10) + "px");

    jQuery("#cSubmenuContainer").css("height", height + "px");
    jQuery("#cSubmenuContent").css("height", (height - 16) + "px");
    jQuery("#cSubmenu_ds4").css("height", (height) + "px");
    jQuery("#cSubmenu_ds3").css("height", (height + 2) + "px");
    jQuery("#cSubmenu_ds2").css("height", (height + 4) + "px");
    jQuery("#cSubmenu_ds1").css("height", (height + 6) + "px");
    jQuery("#cSubmenu").css("height", (height + 8) + "px");

    if (columns < 4) 
    {
        if ((posX + width) > 952)
            jQuery("#cSubmenu").css("left", (posX - ((posX + width) - 952) + 11) + "px");
        else
            jQuery("#cSubmenu").css("left", posX + "px");
    }
    else
    {
        jQuery("#cSubmenu").css("left", "11px");
    }
}


function getFirstChild(elm)
{
    if ( !elm.childNodes.length )
    {
        return null;
    }
    var children = elm.childNodes.length;
    for ( var i = 0; i <= children; ++i )
    {
        var child = elm.childNodes[i];
        if (child)
        {
            var inner = elm.childNodes[i].innerHTML;
            if (inner)
            {
                return elm.childNodes[i];
            }            
        }
    }
    return null;
}
