function Menu(fileName){
    document.write("<div id=\"menubar\"></div>");
    var menuBar = document.getElementById("menubar");
    var menu, timer;
    load();
    function load(){
        var loader;
        if(window.ActiveXObject) loader = new ActiveXObject("Microsoft.XMLHTTP");
        else loader = new XMLHttpRequest();
        loader.onreadystatechange = function(){
            if(loader.readyState == 4 && loader.status == 200) build(loader.responseText);
        }
        loader.open("get", fileName, true);
        loader.send(null);
    }
    function build(xml){
        var xmlDom;
        try{
            var parser;
            if(window.ActiveXObject){
                parser = new ActiveXObject("Microsoft.XMLDOM");
                parser.async = false;
                parser.loadXML(xml);
                xmlDom = parser.documentElement;
            }else if(window.DOMParser){
                parser = new DOMParser();
                parser.strictErrorChecking=true;
                xmlDom = parser.parseFromString(xml, 'text/xml').documentElement;
            }
        }catch (e){}

        if(xmlDom.childNodes.length > 0 && xmlDom.nodeName == "menubar" && xmlDom.nodeType == 1){
            menu = new Array();
            var menuCount = 0;
            var menuUl = document.createElement("ul");
            menuUl.className = "menu";

            var menuList = xmlDom.childNodes;
            for(var menuIndex = 0; menuIndex < menuList.length; menuIndex++){
                var menuItem = menuList.item(menuIndex);
                if(menuItem.nodeName == "menu" && menuItem.nodeType == 1){
                    var sub = new Array();
                    var subCount = 0;
                    if(menuItem.childNodes.length > 0){
                        var subUl = document.createElement("ul");
                        subUl.className = "sub";
                        var subList = menuItem.childNodes;
                        for(var subIndex = 0; subIndex < subList.length; subIndex++){
                            var subItem = subList.item(subIndex);
                            if(subItem.nodeName == "sub" && subItem.nodeType == 1){
                                var suber = new Array();
                                var suberCount = 0;
                                if(subItem.childNodes.length > 0){
                                    var suberUl = document.createElement("ul");
                                    suberUl.className = "suber";
                                    var suberList = subItem.childNodes;
                                    for(var suberIndex = 0; suberIndex < suberList.length; suberIndex++){
                                        var suberItem = suberList.item(suberIndex);
                                        if(suberItem.nodeName == "suber" && suberItem.nodeType == 1){
                                            var suberName = document.createTextNode(suberItem.getAttribute("name"));
                                            var suberLink = document.createElement("a");
                                            if(suberItem.getAttribute("link").length > 0) suberLink.href = suberItem.getAttribute("link");
                                            suberLink.onmouseover = showSuber;
                                            suberLink.onmouseout = hideSuber;
                                            suberLink.target = "_" + suberItem.getAttribute("target");
                                            suberLink.appendChild(suberName);
                                            var suberLi = document.createElement("li");
                                            suberLi.appendChild(suberLink);
                                            suberUl.appendChild(suberLi);

                                            suber[suberCount] = new Array();
                                            suber[suberCount]["option"] = suberLink;
                                            suberCount++;
                                        }
                                    }
                                    menuBar.appendChild(suberUl);
                                }
                                var subName = document.createTextNode(subItem.getAttribute("name"));
                                var subLink = document.createElement("a");
                                if(subItem.getAttribute("link").length > 0 && suber.length == 0) subLink.href = subItem.getAttribute("link");
                                subLink.onmouseover = showSub;
                                subLink.onmouseout = hideSub;
                                subLink.target = "_" + subItem.getAttribute("target");
                                subLink.appendChild(subName);
                                var subLi = document.createElement("li");
                                subLi.appendChild(subLink);
                                subUl.appendChild(subLi);

                                sub[subCount] = new Array();
                                sub[subCount]["option"] = subLink;
                                sub[subCount]["box"] = suberUl;
                                sub[subCount]["suber"] = suber;
                                subCount++;
                            }
                        }
                        menuBar.appendChild(subUl);
                    }
                    if(menuCount == 0){
                        var mainImg = document.createElement("img");
                        mainImg.src = "images/menu-main.gif";
                        var mainLink = document.createElement("a");
                        mainLink.href = "";
                        mainLink.onmouseover = showMenu;
                        mainLink.onmouseout = hideMenu;
                        mainLink.target = "_self";
                        mainLink.appendChild(mainImg);
                        var mainLi = document.createElement("li");
                        mainLi.className = "main";
                        mainLi.appendChild(mainLink);
                        menuUl.appendChild(mainLi);

                        menu[menuCount] = new Array();
                        menu[menuCount]["option"] = mainLink;
                        menu[menuCount]["box"] = document.createElement("ul");
                        menu[menuCount]["sub"] = new Array();
                        menuCount++
                    }
                    var menuName = document.createTextNode(menuItem.getAttribute("name"));
                    var menuLink = document.createElement("a");
                    if(menuItem.getAttribute("link").length > 0 && sub.length == 0) menuLink.href = menuItem.getAttribute("link");
                    menuLink.onmouseover = showMenu;
                    menuLink.onmouseout = hideMenu;
                    menuLink.target = "_" + menuItem.getAttribute("target");
                    menuLink.appendChild(menuName);
                    var menuLi = document.createElement("li");
                    menuLi.appendChild(menuLink);
                    menuUl.appendChild(menuLi);

                    menu[menuCount] = new Array();
                    menu[menuCount]["option"] = menuLink;
                    menu[menuCount]["box"] = subUl;
                    menu[menuCount]["sub"] = sub;
                    menuCount++
                }
            }
            menuBar.appendChild(menuUl);
        }
    }
    function showMenu(){
        window.clearTimeout(timer);
        collapseMenu();
        this.className = "select";
        for(var menuIndex = 0; menuIndex < menu.length; menuIndex++){
            var option = menu[menuIndex]["option"];
            var box = menu[menuIndex]["box"];
            var sub = menu[menuIndex]["sub"];
            if(option == this){
                if(sub.length > 0){
                    var screenBound = getScreenBound();
                    var parentBound = getBound(option);
                    var childBound = getBound(box);
                    box.style.top = (parentBound["top"] + parentBound["height"]) + "px";
                    if((parentBound["left"] + childBound["width"]) < screenBound["width"]) box.style.left = parentBound["left"] + "px";
                    else box.style.left = (parentBound["left"] + parentBound["width"] - childBound["width"]) + "px";
                    box.style.visibility = "visible";
                }
                break;
            }
        }
    }
    function hideMenu(){
        timer = window.setTimeout(collapseMenu, 100);
    }
    function collapseMenu(){
        for(var menuIndex = 0; menuIndex < menu.length; menuIndex++){
            var option = menu[menuIndex]["option"];
            var box = menu[menuIndex]["box"];
            var sub = menu[menuIndex]["sub"];
            option.className = "";
            if(sub.length > 0){
                box.style.visibility = "hidden";
                collapseSub();
            }
        }
    }
    function showSub(){
        window.clearTimeout(timer);
        collapseSub();
        this.className = "select";
        for(var menuIndex = 0; menuIndex < menu.length; menuIndex++){
            var sub = menu[menuIndex]["sub"];
            if(sub.length > 0){
                for(var subIndex = 0; subIndex < sub.length; subIndex++){
                    var option = sub[subIndex]["option"];
                    var box = sub[subIndex]["box"];
                    var suber = sub[subIndex]["suber"];
                    if(option == this){
                        if(suber.length > 0){
                            var screenBound = getScreenBound();
                            var parentBound = getBound(option);
                            var childBound = getBound(box);
                            box.style.top = parentBound["top"] + "px";
                            if((parentBound["left"] + parentBound["width"] + childBound["width"]) < screenBound["width"]) box.style.left = (parentBound["left"] + parentBound["width"]) + "px";
                            else box.style.left = (parentBound["left"] - childBound["width"]) + "px";
                            box.style.visibility = "visible";
                        }
                        break;
                    }
                }
            }
        }
    }
    function hideSub(){
        timer = window.setTimeout(collapseMenu, 1000);
    }
    function collapseSub(){
        for(var menuIndex = 0; menuIndex < menu.length; menuIndex++){
            var sub = menu[menuIndex]["sub"];
            if(sub.length > 0){
                for(var subIndex = 0; subIndex < sub.length; subIndex++){
                    var option = sub[subIndex]["option"];
                    var box = sub[subIndex]["box"];
                    var suber = sub[subIndex]["suber"];
                    option.className = "";
                    if(suber.length > 0){
                        box.style.visibility = "hidden";
                        collapseSuber();
                    }
                }
            }
        }
    }
    function showSuber(){
        window.clearTimeout(timer);
        collapseSuber();
        this.className = "select";
    }
    function hideSuber(){
        timer = window.setTimeout(collapseMenu, 1000);
    }
    function collapseSuber(){
        for(var menuIndex = 0; menuIndex < menu.length; menuIndex++){
            var sub = menu[menuIndex]["sub"];
            if(sub.length > 0){
                for(var subIndex = 0; subIndex < sub.length; subIndex++){
                    var suber = sub[subIndex]["suber"];
                    if(suber.length > 0){
                        for(var suberIndex = 0; suberIndex < suber.length; suberIndex++){
                            var option = suber[suberIndex]["option"];
                            option.className = "";
                        }
                    }
                }
            }
        }
    }
    function getBound(object){
        var current = object;
        var left = 0, top = 0;
        while(current){
            left += current.offsetLeft;
            top += current.offsetTop;
            current = current.offsetParent;
        }
        var width = object.offsetWidth;
        var height = object.offsetHeight;
        return {
            "left":left,
            "top":top,
            "width":width,
            "height":height
        };
    }
    function getScreenBound(){
        var width = document.body.clientWidth ? document.body.clientWidth : window.innerWidth;
        var height = document.body.clientHeight ? document.body.clientHeight : window.innerHeight;
        return {
            "width":width,
            "height":height
        };
    }
}
