/*
 * DO NOT REMOVE THIS NOTICE
 *
 * PROJECT:   mygosuMenu
 * VERSION:   1.2.0
 * COPYRIGHT: (c) 2003,2004 Cezary Tomczak
 * LINK:      http://gosu.pl/dhtml/mygosumenu.html
 * LICENSE:   BSD (revised)
 */

function TreeMenu(id) {

    this.init = function() {
        if (!document.getElementById(this.id)) {
            alert("Element '"+this.id+"' does not exist in this document. TreeMenu cannot be initialized");
            return;
        }
        this.parse(document.getElementById(this.id).childNodes, this.tree, this.id);
    }

    this.NotCustomClass = function (className) {
        /*MODIFICATION TO ORIGINAL SCRIPT
        Date: 2004/07/30
        Author: Guy Thomas
        Purpose of new function: 
        This function checks to see if a node has a custom class (i.e not "section","section-open" or "box")
        */
        switch (className)
        {
            case "" : {return false; break}
            case "section": {return true; break}
            case "section-open": {return true; break}
			//case "section-open": {return false; break}
            case "section-box": {return true; break}
            default: {return false; break }
        }
    }

    this.parse = function(nodes, tree, id) {
        for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].nodeType != 1) {
                continue;
            }
            if (nodes[i].tagName.toLowerCase() == "li") {
				//by laMMo
				if (nodes[i].id.indexOf("c_id") == -1){
                	nodes[i].id = id + "-" + tree.length;
				}
				//alert ("nodes[i].id = " + nodes[i].id);
				//alert (nodes[i].className);
				//alert ("nodes[i].id = " + nodes[i].id + "\n" + "tree.length=" + tree.length);
                tree[tree.length] = new Array();
                if (nodes[i].childNodes && this.hasUl(nodes[i].childNodes)) {
					//by laMMo
					if (nodes[i].className == "section-open"){
						document.cookie = "node[\""+nodes[i].id+"\"]=1";
					}
					if (this.NotCustomClass(nodes[i].className)==true){ // mod by G Thomas 2004/07/30
			                    //nodes[i].className = "section";
					} else {
						//nodes[i].className = "box";
						//alert ("custom class " + nodes[i].className); //by vit
						//alert ("custom value " + nodes[i].value); //by vit
					}
                    var a;
                    if (a = this.getA(nodes[i].childNodes)) {
                        a.id = nodes[i].id + "-a";
                        eval("document.getElementById('"+a.id+"').onclick = function() {"+
                            "self.click('"+nodes[i].id+"');"+
                        "}");
                    }
                } else {
					//alert("box");
					if (this.NotCustomClass(nodes[i].className)==true){ // mod by G Thomas 2004/07/30
						nodes[i].className = "box";
					}
                }

				//by laMMO
				if (a = this.getA(nodes[i].childNodes)) {
	                     	a.id = nodes[i].id + "-a";
					a_id = document.getElementById(nodes[i].id + "-a");
					//alert("node = " + nodes[i].id + " a.id = " + a.id + "\n href = " + a_id.href);
					if (a_id.href.indexOf("#") == -1){
						//by laMMo: highlight current link
						//alert (a_id.href);
						if (currentCategoryHREF == a_id.href){
							a_id.style.background = "ffffcc";
						}
						//alert(document.location.href + "\n" + a_id.href);
						//alert (a_id.href + " = " + a_id.href.indexOf("#"));
					}
				}
				
            }//if (nodes[i].tagName.toLowerCase() == "li")
            if (nodes[i].tagName.toLowerCase() == "ul") {
				//by laMMo
				if (nodes[i].className != "section-open"){
                	nodes[i].style.display = "none";
				}//by laMMo

				//by laMMo
				if (nodes[i].id.indexOf("c_id") == -1){
	                id = id + "-" + (tree.length - 1);
	                nodes[i].id = id + "-section";
				}
				//alert (tree);
                tree = tree[tree.length - 1];
				//alert (tree);
            }
            if (nodes[i].childNodes) {
                this.parse(nodes[i].childNodes, tree, id);
            }
        }
    }//parce
    
	this.click = function(id) {
		
		var nava = (document.layers);
		var iex = (document.all);
		
		//alert ("click " + id);
		e1 = document.getElementById(id + "-section");
		e2 = document.getElementById(id);
		a_id = document.getElementById(id + "-a");
		
		//alert ("style: " + e1.style.display + " name: " + e2.value);
        if (e1.style.display == "none") {
            e1.style.display = "";
			document.cookie = "node[\""+id+"\"]=1";
			if (e2.value == "0" || e2.value == "-1"){
	            e2.className = "section-open";
    		} else {
				e2.className = "open_" + e2.value;
			}
        } else {
            e1.style.display = "none";
			document.cookie = "node[\""+id+"\"]=0";

			//alert ("not none " + e2.value);
			if (e2.value == "0" || e2.value == "-1"){// -1
            	e2.className = "section";
			} else {
				e2.className = "closed_" + e2.value;
			}
            //e2.className = "section";
        }
		//alert (e2.className)
    }//click

		
    this.hasUl = function(nodes) {
        for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].nodeType != 1) {
                continue;
            }
            if (nodes[i].tagName.toLowerCase() == "ul") {
                return true;
            }
            if (nodes[i].childNodes) {
                if (this.hasUl(nodes[i].childNodes)) {
                    return true;
                }
            }
        }
        return false;
    }

    this.getA = function(nodes) {
        for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].nodeType == 1) {
                if (nodes[i].tagName.toLowerCase() == "a") {
                    return nodes[i];
                }
                return false;
            }
        }
    }
	
    var self = this;
    this.id = id;
    this.tree = new Array();
    this.init();
}