/**
* D2 Hierarchical Menu System
* Version 2.2.4
*/

/**
* Modify these variables to control menu position and feel.
*/

rootMenuOffsetX = -8;
rootMenuOffsetY = 7;
subMenuOffsetX = 140;
subMenuOffsetY = 0;
subMenuDelay = 200;

// If set to true, the menu will use all available vertical real estate.
compensateVertical = true;

// If set to true, the first tier will reposition horizontally so it is visible.
adjustFirstTier = true;

// If set to true, using the menus will temporarily hide all selects in the document.
autoHideSelects = true;


/**
* Main control code. Do not modify any code below this line.
*/

menuActive = false;

function showSelects() {
    if (autoHideSelects) {
        var i;
        var oSelects = document.getElementsByTagName("select");
        if (oSelects) {
            for (i = 0; i < oSelects.length; i++) {
                oSelects[i].style.visibility = "visible";
            }
        }
    }
}

function hideSelects() {
    if (autoHideSelects) {
        var i;
        var oSelects = document.getElementsByTagName("select");
        if (oSelects) {
            for (i = 0; i < oSelects.length; i++) {
                oSelects[i].style.visibility = "hidden";
            }
        }
    }
}

function showMenu(obj) {
    if (tierSelected) if (tierSelected != obj.id) hideChildren(tierSelected, 0); tierSelected = obj.id; showChildren(obj.id);
}

function hideMenu(obj) {
    hideChildren(obj.id, 250);
}

function getY(obj) {
    var y = 0;
    if (obj) {
        while (obj.offsetParent) {
            y += parseInt(obj.offsetTop);
            obj = obj.offsetParent;
        }
    }
    return y;
}

function getX(obj) {
    var x = 0;
    if (obj) {
        if (document.getElementById || document.all) {
            while (obj.offsetParent) {
                x += obj.offsetLeft;
                obj = obj.offsetParent;
            }
        } else if (document.layers) {
            x += obj.x;
        }
    }
    return x;
}

function getMenuX(id) {
    var obj = getObj(id);
    var x = getX(obj);

    if (document.body.offsetWidth && document.documentElement.offsetWidth) {
        var bodyWidth = Math.min(document.documentElement.offsetWidth, document.body.offsetWidth);
    }

    if (bodyWidth) {
        if (obj.root) {
            if (adjustFirstTier) {
                if ((x + subMenuOffsetX) > bodyWidth - 20) {
                    x -= (subMenuOffsetX - obj.offsetWidth) + 20;
                }
            }
        } else {
            if (x + (obj.offsetWidth + subMenuOffsetX) > bodyWidth) {
                x -= (obj.offsetWidth + subMenuOffsetX - 10);
            }
        }
    }

    if (isNaN(x)) x = 0;
    if (x == 0 && obj.parent == null) {
        x = getX(obj);
    }
    return x;
}

function getMenuY(id) {
    var obj = getObj(id);
    var y = parseInt(obj.offsetTop);
    y = parseInt(y);
    if (isNaN(y)) y = 0;
    if (obj.parent == null) {
        y = getY(obj);
    }
    if (obj.root == obj) {
        y += parseInt(obj.offsetHeight);
    }
    return y;
}

function getObj(id) {
    var obj;
    obj = document.getElementById(id);
    return obj;
}

function setDisplay(id, display) {
    var obj = getObj(id);
    if (display) {
        obj.style.display = 'block';
    } else {
        obj.style.display = 'none';
    }
}


function showChildren(id) {
    hideSelects();
    menuActive = true;

    var obj = getObj(id);

    if (!obj.level) obj.level = 1;
    if (!obj.number) obj.number = 1;
    if (!obj.root) obj.root = obj;
    if (!obj.container) obj.container = obj;

    if (obj.delay) {
        clearTimeout(obj.delay);
    }

    if (obj.parent) {
        if (obj.parent.parent != null) {
            obj.parent.className = "menuItem_On";
        }
        if (obj.parent.selected) {
            if (obj.parent.selected != obj) {
                obj.parent.selected.className = "menuItem_Off";
                hideChildren(obj.parent.selected.id, 0);
            }
        }
        obj.parent.selected = obj;
    }

    if (obj.root) {
        if (obj.root.delay) {
            clearTimeout(obj.root.delay);
        }
        obj.root.onmouseout = function() {
            hideMenu(this);
        }
        var oImages = obj.root.getElementsByTagName("img");
        if (oImages) {
            var oImage = oImages[0];
            if (oImage) {
                oImage.onmouseout = null;
            }
        }
    }

    subMenu = getObj(obj.id + "_subMenus");

    if (subMenu != null) {

        obj.subMenus = subMenu.childNodes.length;

        if (obj.root == obj) {
            subMenu.style.top = getMenuY(obj.id) + rootMenuOffsetY + "px";
            subMenu.style.left = getMenuX(obj.container.id) + rootMenuOffsetX + "px";
        } else {
            subMenu.style.top = getMenuY(obj.container.id) + getMenuY(obj.id) + subMenuOffsetY + "px";
            subMenu.style.left = getMenuX(obj.container.id) + subMenuOffsetX + "px";
        }

        if (obj.subMenus > 0) {
            setDisplay(subMenu.id, true);
        }

        for (i = 0; i <= obj.subMenus - 1; i++) {
            tmpObj = getObj(obj.id + "_" + i);
            if (tmpObj) {
                tmpObj.parent = obj;
                tmpObj.level = obj.level + 1;
                tmpObj.number = i;
                tmpObj.root = obj.root;
                tmpObj.container = subMenu;
                tmpObj.style.top = 16 * (i - 1);

                tmpObj.onmouseover = function() {
                    this.className = "menuItem_On";
                    showChildren(this.id);

                    var menuRoot = document.getElementById('menuRoot' + obj.id);
                    if (menuRoot == null) {
                        menuRoot = document.getElementById('menuRoot' + obj.id.substring(0,2));
                    }
                    menuRoot.style['color'] = 'white';
                    var td = getObj(menuRoot.id.substring(9));
                    td.className = "navBarItem_on";
                    td.setAttribute("isHover", "true");
                }

                tmpObj.onmouseout = function() {
                    this.className = "menuItem_Off";
                    hideChildren(this.root.id, subMenuDelay * 2);

                    var menuRoot = document.getElementById('menuRoot' + obj.id);
                    if (menuRoot == null) {
                        menuRoot = document.getElementById('menuRoot' + obj.id.substring(0,2));
                    }
                    menuRoot.style['color'] = 'black';
                    var td = getObj(menuRoot.id.substring(9));
                    td.className = "navBarItem_off";
                    td.setAttribute("isHover", "false");
                }

                tmpObj.onclick = function() {
                    this.className = "menuItem_Off";
                    hideChildren(this.root.id, 0);
                }
            }
        }

        if (compensateVertical) {
            if (document.documentElement.clientHeight) {
                var bodyHeight = document.documentElement.clientHeight;
                if (document.documentElement.scrollTop) {
                    bodyHeight += document.documentElement.scrollTop;
                }
                if (parseInt(subMenu.style.top) + subMenu.offsetHeight > bodyHeight) {
                    subMenu.style.top = (parseInt(subMenu.style.top) - ((parseInt(subMenu.style.top) + subMenu.offsetHeight) - bodyHeight) < 0) ? "0px" : (parseInt(subMenu.style.top) - ((parseInt(subMenu.style.top) + subMenu.offsetHeight) - bodyHeight)) + "px";
                }
            }
        }
    }
}

function hideChildren(id, delay) {
    if (delay == 0) {
        menuActive = false;
    }

    var obj = getObj(id);

    if (obj) {
        if (delay) {
            obj.delay = setTimeout("hideChildren(\'" + id + "\', 0)", delay);
        } else {
            if (obj.root) {
                if (obj == obj.root) {
                    showSelects();
                    if (obj.root.getAttribute("isHover")) {
                        if (hoverOffDiv && obj.root.getAttribute("isHover") == "true") {
                            hoverOffDiv(obj.root);
                        }
                    }
                    var oImages = obj.root.getElementsByTagName("img");
                    if (oImages) {
                        var oImage = oImages[0];
                        if (oImage) {
                            if (hoverOff) {
                                hoverOff(oImage);
                            }
                        }
                    }
                }
            }

            if (obj.subMenus) {
                subMenu = getObj(obj.id + "_subMenus");
                if (obj.selected) {
                    obj.selected.className = "menuItem_Off";
                }
                setDisplay(subMenu.id, false);
            }
            if (obj.selected) {
                hideChildren(obj.selected.id, 0);
            }
        }
    }
}
function OverChange(obj) {
    var menuRoot = document.getElementById(obj.id);
    menuRoot.style['color'] = 'white';
}
function OutChange(obj) {
    var menuRoot = document.getElementById(obj.id);
    menuRoot.style['color'] = 'black';
}
var tierSelected;
