function Menu(fileName){
    document.write("<div id=\"menubar\"></div>");
    var menuBar = document.getElementById("menubar");
    menuBar.style.indexZ = "3";
    menuBar.style.position = "absolute";

    document.write("<img id=\"bird\" src=\"images/bird.gif\" />");
    var bird = document.getElementById("bird");
    bird.style.indexZ = "2";
    bird.style.position = "absolute";

    document.write("<a href=\"http://www.lichnost.ru/\"><img id=\"home\" src=\"images/home-button.gif\" /></a>");
    var home = document.getElementById("home");
    home.style.indexZ = "4";
    home.style.position = "absolute";

    document.write("<a href=\"mailto:kotva@mail.ru;\"><img id=\"mail\" src=\"images/mail-button.gif\" /></a>");
    var mail = document.getElementById("mail");
    mail.style.indexZ = "4";
    mail.style.position = "absolute";

    document.write("<a href=\"http://www.lichnost.ru/map/\"><img id=\"map\" src=\"images/map-button.gif\" /></a>");
    var map = document.getElementById("map");
    map.style.indexZ = "4";
    map.style.position = "absolute";

    var template = document.getElementById("template");
    template.style.indexZ = "2";

    var timer;
    var menuLeft;
    var menuTop;
    getPosition();
    document.body.onresize = getPosition;
    var menu = build(fileName);
    function getPosition(){
        var screenBound = getScreenBound();
        var screenWidth = screenBound['width'];

        var bodyLeft = ((screenWidth - 1000) / 2) < 0 ? 0 : ((screenWidth - 1000) / 2);

        bodyLeft += (/MSIE (5\.5|6|7).+Win/.test(navigator.userAgent) ? 8 : 0);

        menuLeft = bodyLeft + 90;
        menuTop = 246;
        menuBar.style.left = menuLeft + "px";
        menuBar.style.top = menuTop + "px";

        bird.style.left = bodyLeft + 25 + "px";
        bird.style.top = 700 + "px";

        home.style.left = bodyLeft + 830 + "px";
        home.style.top = 254 + "px";

        mail.style.left = bodyLeft + 850 + "px";
        mail.style.top = 254 + "px";

        map.style.left = bodyLeft + 870 + "px";
        map.style.top = 254 + "px";

        template.style.left = bodyLeft + 478 + "px";
        template.style.top =  0 + "px";
    }
    function build(fileName){
        var xmlDom;
        if(window.ActiveXObject){
            xmlDom = new ActiveXObject("Microsoft.XMLDOM");
            xmlDom.async = false;
            xmlDom.load(fileName);
        }else{
            var xmlHttp = new window.XMLHttpRequest();
            xmlHttp.open("GET", fileName, false);
            xmlHttp.send(null);
            xmlDom = xmlHttp.responseXML;
        }
        var menuList = xmlDom.documentElement;
        var menuUl = document.createElement("ul");
        menuUl.className = "menu";
        var menu = new Array();
        var menuCount = 0;
        for(var menuIndex = 0; menuIndex < menuList.childNodes.length; menuIndex++){
            var subList = menuList.childNodes.item(menuIndex);
            if(menuList.childNodes.item(menuIndex).nodeType == 1){
                var subUl= document.createElement("ul");
                subUl.className = "sub";
                var sub = new Array();
                var subCount = 0;
                for(var subIndex = 0; subIndex < subList.childNodes.length; subIndex++){
                    var suberList = subList.childNodes.item(subIndex);
                    if(subList.childNodes.item(subIndex).nodeType == 1){
                        var suberUl = document.createElement("ul");
                        suberUl.className = "suber";
                        var suber = new Array();
                        var suberCount = 0;
                        for(var suberIndex = 0; suberIndex < suberList.childNodes.length; suberIndex++){
                            if(suberList.childNodes.item(suberIndex).nodeType == 1){
                                var name = suberList.childNodes.item(suberIndex).getAttribute("name");
                                var link = suberList.childNodes.item(suberIndex).getAttribute("link");
                                var image = suberList.childNodes.item(suberIndex).getAttribute("image");
                                var suberName = document.createTextNode(name);
                                var suberLink = document.createElement("a");
                                if(link.length > 0) suberLink.href = link;
                                if(image.length > 0) suberLink.style.backgroundImage = "url(\"" + image + "\")";
                                suberLink.onmouseover = showSuber;
                                suberLink.onmouseout = hideSuber;
                                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 name = subList.childNodes.item(subIndex).getAttribute("name");
                        var link = subList.childNodes.item(subIndex).getAttribute("link");
                        var image = subList.childNodes.item(subIndex).getAttribute("image");

                        var subName = document.createTextNode(name);
                        var subLink = document.createElement("a");
                        if(link.length > 0 && suber.length == 0) subLink.href = link;
                        if(image.length > 0) subLink.style.backgroundImage = "url(\"" + image + "\")";
                        subLink.onmouseover = showSub;
                        subLink.onmouseout = hideSub;
                        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"] = suber.length > 0 ? suberUl : null;
                        sub[subCount]["suber"] = suber;
                        subCount++;
                    }
                }
                menuBar.appendChild(subUl);

                var name = menuList.childNodes.item(menuIndex).getAttribute("name");
                var link = menuList.childNodes.item(menuIndex).getAttribute("link");
                var image = menuList.childNodes.item(menuIndex).getAttribute("image");

                var menuName = document.createTextNode(name);
                var menuLink = document.createElement("a");
                if(link.length > 0 && sub.length == 0) menuLink.href = link;
                if(image.length > 0) menuLink.style.backgroundImage = "url(\"" + image + "\")";
                menuLink.onmouseover = showMenu;
                menuLink.onmouseout = hideMenu;
                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"] = sub.length > 0 ? subUl : null;
                menu[menuCount]["sub"] = sub;
                menuCount++
            }
        }
        menuBar.appendChild(menuUl);
        return menu;
    }
    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 && sub.length > 0){
                var screenBound = getScreenBound();
                var parentBound = getBound(option);
                var childBound = getBound(box);
                box.style.top = (parentBound["top"] + parentBound["height"] - menuTop) + "px";
                if((parentBound["left"] + childBound["width"]) < screenBound["width"]) box.style.left = parentBound["left"] - menuLeft + "px";
                else box.style.left = (parentBound["left"] + parentBound["width"] - childBound["width"]) - menuLeft + "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 && suber.length > 0){
                        var screenBound = getScreenBound();
                        var parentBound = getBound(option);
                        var childBound = getBound(box);
                        box.style.top = parentBound["top"] - menuTop + "px";
                        if((parentBound["left"] + parentBound["width"] + childBound["width"]) < screenBound["width"]) box.style.left = (parentBound["left"] + parentBound["width"] - menuLeft) + "px";
                        else box.style.left = (parentBound["left"] - childBound["width"] - menuLeft) + "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
        };
    }
}
function XMLDom(name){
    var xmlDom;
    if(window.ActiveXObject)
        xmlDom = new ActiveXObject("Microsoft.XMLDOM");
    else if(document.implementation && document.implementation.createDocument)
        xmlDom = document.implementation.createDocument("", "", null);

    xmlDom.async = false;
    xmlDom.load(name);
    this.getXML = function(){
        return xmlDom;
    }
}
