// CSS Menu.
var cssmenuids = ["cssmenu1"] //Enter id(s) of CSS Horizontal UL menus, separated by commas
var csssubmenuoffsetY = 0; //Offset of submenus from main menu. Default is 0 pixels.
var itemHeight = 26;
var itemWidth = 150;

function createcssmenu2() {
    for (var i = 0; i < cssmenuids.length; i++) {
        // get the ul tags within the menu (the sub menus)
        var ultags = document.getElementById(cssmenuids[i]).getElementsByTagName("ul")
        for (var t = 0; t < ultags.length; t++) {

            // see if it is a top level ul tag or not
            var toplevel = ultags[t].parentNode.parentNode.id == cssmenuids[i];
            if (!toplevel) {
                // as usual, firefox has a mind of its own, so is discluded
                if (navigator.userAgent.indexOf('Firefox') ==-1)
                {
                    // put the sub-submenu to the right of the menu
                    ultags[t].style.left = ultags[t].parentNode.offsetLeft + ultags[t].offsetWidth + "px";
                }
                ultags[t].style.top = ultags[t].parentNode.offsetHeight - itemHeight + "px"

            } else {
                // submenu appears below the upper level
                ultags[t].style.top = ultags[t].parentNode.offsetHeight + csssubmenuoffsetY + "px"
            }

            // get the LI above this sub menu and attach functions to it
            ultags[t].parentNode.onmouseover = function() {
                this.style.zIndex = 100
                this.getElementsByTagName("ul")[0].style.visibility = "visible"
                this.getElementsByTagName("ul")[0].style.zIndex = 0
                this.getElementsByTagName("a")[0].className='selected';
            }

            ultags[t].parentNode.onmouseout = function() {
                this.style.zIndex = 0

                this.getElementsByTagName("ul")[0].style.visibility = "hidden"
                this.getElementsByTagName("ul")[0].style.zIndex = 100
                this.getElementsByTagName("a")[0].className='';
            }

        }
    }
}

function createcssmenu3() {
    for (var i = 0; i < cssmenuids.length; i++) {
        // get the ul tags within the menu (the sub menus)
        var menu = document.getElementById(cssmenuids[i]);
        parseMenu(menu, 0);
    }
}

function parseMenu(menu, level) {
    // go through the child nodes of this menu
    for (var i = 0; i < menu.childNodes.length; i++) {
        var item = menu.childNodes[i];
        //item.id = "m" + level + "_" + i;
        if (item.tagName && item.tagName.toUpperCase() == 'UL') {
            if (level == 1) {
                // first level menu - underneath the top menu
                item.style.top = item.parentNode.offsetHeight + csssubmenuoffsetY + "px"
                if (navigator.userAgent.indexOf('MSIE') == -1) {
                    //item.style.top = '23px';
                }
            } else {
                // second level menu - to the side of the top menu
                if (navigator.userAgent.indexOf('Firefox') == -1) {
                    // put the sub-submenu to the right of the menu
                    item.style.left = item.parentNode.offsetLeft + item.offsetWidth + "px";
                }
                if (navigator.userAgent.indexOf('MSIE 6') != -1) {
                    // put the sub-submenu to the right of the menu
                    item.style.left = item.parentNode.offsetLeft + "px";
                }
                item.style.top = item.parentNode.offsetHeight - itemHeight + "px"
            }
        } else if (item.tagName && item.tagName.toUpperCase() == 'LI') {
            // it is a list item but does it have its own children?
            if (item.getElementsByTagName("ul").length > 0) {

                // add a little arrow in
                if (level != 0) item.getElementsByTagName("a")[0].className = "hasMoreMenus";

                item.onmouseover = function() {
                    this.style.zIndex = 100;
                    this.getElementsByTagName("ul")[0].style.visibility = "visible"
                    this.getElementsByTagName("ul")[0].style.zIndex = 0
                    this.getElementsByTagName("a")[0].className = level == 0 ? 'selected' : 'hasMoreMenus_selected';
                }

                item.onmouseout = function() {
                    //setTimeout("hideMenu('" + this.id + "'," + level + ")", 100)
                    this.style.zIndex = 0;
                    this.getElementsByTagName("ul")[0].style.visibility = "hidden"
                    this.getElementsByTagName("ul")[0].style.zIndex = 100
                    this.getElementsByTagName("a")[0].className = level == 0 ? '' : 'hasMoreMenus';
                }
            }
        }
        parseMenu(item, level+1);
    }
}

if (window.addEventListener)
    window.addEventListener("load", createcssmenu3, false)
else if (window.attachEvent)
    window.attachEvent("onload", createcssmenu3)
