function menuCascade(targetID) {
    this.id = targetID;
    this.render();
}

menuCascade.prototype.render = function() {
    var menuBar = $(this.id + "_MENUBAR").children;
    for(var i=0; i<menuBar.length; i++) {
        OSGradient.applyGradient({'gradient-end-color':'0x007700','gradient-start-color':'0x' + Element.getStyle(menuBar[i], "backgroundColor").substring(1)}, menuBar[i]);
        if(menuBar[i].menu != null) {
            var menuPane = $(menuBar[i].menu);
            menuPane.style.visibility = "hidden";
            menuPane.style.width = MENU_WIDTH + "px";
            menuBar[i].onmouseover = new Function("menuShow('" + this.id + "', " + menuBar[i].id + ")");
            menuBar[i].onmouseout = new Function("eval(" + this.id + "_TIMEOUT = setTimeout('menuHide(" + this.id + "_MENUBAR)', 350))");

            var menuItems = menuPane.children;
            for(var j=0; j<menuItems.length; j++) {
                var menuItem = $(menuItems[j].id);
                if(menuItem.menu != null) {
                    menuItem.innerHTML += '<SPAN id=' + menuItem.id + '_Arrow class="menuITEMARROW">4</SPAN>';
                    menuFindSub(this.id, menuItem.menu)
                }
                if(menuItem.url != null) menuItem.onclick = new Function("menuClick(" + menuItem.id + ")");
                menuItem.onmouseover = new Function("menuHighlight('" + this.id + "', " + menuItems[j].id + ")");
                menuItem.onmouseout = new Function("eval(" + this.id + "_TIMEOUT = setTimeout('menuHide(" + this.id + "_MENUBAR)', 350))");
            }
        }
        else {
            menuBar[i].onmouseout = new Function("eval(" + this.id + "_TIMEOUT = setTimeout('menuHide(" + this.id + "_MENUBAR)', 350))");
        }
        if(menuBar[i].url != null) menuBar[i].onclick = new Function("menuClick(" + menuBar[i].id + ")");
    }
}

menuCascade.prototype.findID = function(menuStack, iLevel) {
    for(var i=(menuStack.length - 1); i>=0; i--) {
        if(parseInt(menuStack[i].substr(0, 2)) == iLevel) break;
    }
    return menuStack[i];
}

function menuFindSub(targetID, subMenu) {
    var thisSubMenu = $(subMenu);
    var menuItems = thisSubMenu.children;
    for(var j=0; j<menuItems.length; j++) {
        thisSubMenu.style.visibility = "hidden";
        thisSubMenu.style.width = MENU_WIDTH + "px";

        var menuItem = $(menuItems[j].id);
        if (menuItem.menu != null) {
            menuItem.innerHTML += '<SPAN id=' + menuItem.id + '_Arrow class="menuITEMARROW">4</SPAN>';
            menuFindSub(targetID, menuItem.menu);
        }
        if (menuItem.url != null) {
            menuItem.onclick = new Function("menuClick(" + menuItem.id + ")");
        }
        menuItem.onmouseover = new Function("menuHighlight('" + targetID + "', " + menuItems[j].id + ")");
        menuItem.onmouseout = new Function("eval(" + targetID + "_TIMEOUT = setTimeout('menuHide(" + targetID + "_MENUBAR)', 350))");
    }
}

function menuShow(targetID, thisMenuItem) {
    eval("clearTimeout(" + targetID + "_TIMEOUT)");
    menuHide($(targetID + "_MENUBAR"));
    var menuButton = $(thisMenuItem.id);
    menuButton.className = "menuBUTTONMOUSEOVER";
    var menuPane = $(thisMenuItem.menu);
    menuPane.style.visibility = "visible";
    menuPane.style.pixelTop  = thisMenuItem.getBoundingClientRect().top + thisMenuItem.offsetHeight + $(targetID + "_ID").scrollTop - (MENU_TOP - document.body.scrollTop);
    menuPane.style.pixelLeft = thisMenuItem.getBoundingClientRect().left + $(targetID + "_ID").scrollLeft - (MENU_LEFT - document.body.scrollLeft);
}

function menuShowSub(targetID, thisMenuItem) {
    eval("clearTimeout(" + targetID + "_TIMEOUT)");
    parentMenu = $(thisMenuItem.parentElement.id);
    menuHide(parentMenu);
    if(thisMenuItem.menu != null) {
        var menuItem = $(thisMenuItem.menu);
        menuItem.style.visibility = "visible";
        menuItem.style.pixelTop  = thisMenuItem.getBoundingClientRect().top + $(targetID + "_ID").scrollTop - (MENU_TOP - document.body.scrollTop);
        menuItem.style.pixelLeft = thisMenuItem.getBoundingClientRect().right + $(targetID + "_ID").scrollLeft - (MENU_LEFT - document.body.scrollLeft);
        if (menuItem.getBoundingClientRect().right > window.screen.availWidth )
            menuItem.style.pixelLeft = thisMenuItem.getBoundingClientRect().left - menuItem.offsetWidth;
    }
}

function menuHide(thisMenuItem) {
    if(thisMenuItem.hasChildNodes() == true) {
        var subMenu = thisMenuItem.children;
        for(var j =0; j<subMenu.length; j++) {
            if (subMenu[j].className == "menuBUTTONMOUSEOVER") {
                var menuButton = $(subMenu[j].id);
                menuButton.className="menuBUTTON";
            }
            if(subMenu[j].menu != null) {
                var childMenu = $(subMenu[j].menu);
                if(childMenu.hasChildNodes() == true) menuHide(childMenu);
                childMenu.style.visibility = "hidden";
            }
        }
    }
}

function menuHighlight(targetID, thisMenuItem) {
    var parentMenu = $(thisMenuItem.parentElement.id);
    if(parentMenu.hasChildNodes() == true) {
        var menuItems = parentMenu.children;
        for(var i=0; i<menuItems.length; i++) {
            var thisItem = $(menuItems[i].id);
            thisItem.className = "menuITEM";
        }
    }
    thisMenuItem.className = "menuITEMMOUSEOVER";
    window.defaultStatus = thisMenuItem.title;
    menuShowSub(targetID, thisMenuItem);
}

function menuClick(thisMenuItem) {
    var itemURL = $(thisMenuItem).url;
    window.location.href = itemURL;
}

