/*
 * PROJECT:   mygosuMenu
 * VERSION:   1.3.0
 * COPYRIGHT: (c) 2003,2004 Cezary Tomczak
 * LINK:      http://gosu.pl/software/mygosumenu.html
 * LICENSE:   BSD (revised)
 */

function ClickMenu(id) {

    this.init = function() {
        if (!document.getElementById(this.id)) {
            alert("Element '"+this.id+"' does not exist in this document. ClickMenu cannot be initialized");
            return;
        }
        this.parse(document.getElementById(this.id).childNodes, this.tree, this.id);
        this.load();
    }

    this.parse = function(nodes, tree, id) {
        for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].nodeType != 1) {
                continue;
            }
            if ("box1" == nodes[i].className.substr(0, 4)) {
                nodes[i].id = id + "-" + tree.length;
                tree[tree.length] = new Array();
                eval('document.getElementById("'+nodes[i].id+'").onmouseover = function() { document.getElementById("'+nodes[i].id+'").className = (self.id_openbox == "'+nodes[i].id+'" ? "box1-open-hover" : "box1-hover") };');
                eval('document.getElementById("'+nodes[i].id+'").onmouseout = function() { document.getElementById("'+nodes[i].id+'").className = (self.id_openbox == "'+nodes[i].id+'" ? "box1-open" : "box1") };');
                eval('document.getElementById("'+nodes[i].id+'").onclick = function() { self.click("'+nodes[i].id+'"); }');
            }
            if ("section" == nodes[i].className) {
                id = id + "-" + (tree.length - 1);
                nodes[i].id = id + "-section";
                tree = tree[tree.length - 1];
            }
            if ("box2" == nodes[i].className.substr(0, 4)) {
                nodes[i].id = id + "-" + tree.length;
                tree[tree.length] = new Array();
                nodes[i].onmouseover = new Function('document.getElementById("'+nodes[i].id+'").className = "box2-hover";');
                nodes[i].onmouseout = new Function('document.getElementById("'+nodes[i].id+'").className = "box2";');
            }
            if (nodes[i].childNodes) {
                this.parse(nodes[i].childNodes, tree, id);
            }
        }
    }

    this.click = function(id) {
        var id_openbox = this.id_openbox;
        if (this.id_openbox) {
            this.hide();
            document.getElementById(id_openbox).className = (id_openbox == id ? "box1-hover" : "box1");
        }
        if (id_openbox != id) {
            this.show(id);
            var className = document.getElementById(id).className;
            if ("box1-hover" == className) {
                document.getElementById(id).className = "box1-open-hover";
            }
        }
    }

    this.show = function(id) {
        if (document.getElementById(id + "-section")) {
            document.getElementById(id + "-section").style.display = "block";
            this.id_openbox = id;
        }
    }

    this.hide = function() {
        document.getElementById(this.id_openbox + "-section").style.display = "none";
        this.id_openbox = null;
    }

    this.save = function() {
        this.cookie.set(this.id, this.id_openbox);
    }

    this.load = function() {
        var id_openbox = this.cookie.get(this.id);
        if (id_openbox) {
            this.show(id_openbox);
            document.getElementById(id_openbox).className = "box1-open";
        }
    }

    function Cookie() {
        this.get = function(name) {
            var cookies = document.cookie.split(";");
            for (var i = 0; i < cookies.length; i++) {
                var a = cookies[0].split("=");
                if (a[0] == name) {
                    return a[1];
                }
            }
            return null;
        }
        this.set = function(name, value) {
            document.cookie = name + "=" + escape(value);
        }
    }

    var self = this;
    this.id = id;
    this.tree = new Array();
    this.cookie = new Cookie();
    this.id_openbox = null;
    this.init();
}
