/*
 * 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, act) {
	this.cookiePath = "";
    this.cookieDomain = "";
    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.parse = function(nodes, tree, id) {
    	var act = this.active;
    	this.treecookie = new TreeCookie();
        for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].nodeType != 1) {
                continue;
            }
            if (nodes[i].tagName.toLowerCase() == "li") {
                nodes[i].id = id + "-" + tree.length;
                tree[tree.length] = new Array();
                cookie_id = nodes[i].id + "-section";
                if (nodes[i].childNodes && this.hasUl(nodes[i].childNodes)) {
                    //keep open   
                   
                   if(this.treecookie.get(cookie_id)=="opened")
                    nodes[i].className = "section-open";
                    else
                    nodes[i].className = "section";
                    
                    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+"','"+id+"');"+
                        "}");
                       
                    }
                } else {
                
                 if(nodes[i].title==this.act)
                    nodes[i].className = "box-active";
                 else
                 	nodes[i].className = "box";
                }
            }
            
            if (nodes[i].tagName.toLowerCase() == "ul") {
               //keep open
               if(this.treecookie.get(cookie_id)=="opened")
               nodes[i].style.display = "inline";
               else
               nodes[i].style.display = "none";
                id = id + "-" + (tree.length - 1);
                nodes[i].id = id + "-section";
                tree = tree[tree.length - 1];
            }
            if (nodes[i].childNodes) {
                this.parse(nodes[i].childNodes, tree, id);
            }
        }
    }

    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;
            }
        }
    }

    this.click = function(id, menuid) {
        e1 = document.getElementById(id + "-section");
        e2 = document.getElementById(id);
        if (e1.style.display == "none") {
            e1.style.display = "inline";
            e2.className = "section-open";
            this.treecookie.set(e1.id, "opened", 3600*24*30, this.cookiePath, this.cookieDomain);
        } else {
            e1.style.display = "none";
            e2.className = "section";
            this.treecookie.del(e1.id);
        }
         for (var i = 0; i < 100; i++) {
         if(document.getElementById(menuid + "-" + i) && document.getElementById(menuid + "-" + i + "-section")){
         eo1 = document.getElementById(menuid + "-" + i + "-section");
         eo2 = document.getElementById(menuid + "-" + i);
        	if(id != eo2.id) {
        	// this.treecookie.set(i, eo2.id, 3600*24*30, this.cookiePath, this.cookieDomain);
       		 if (eo1.style.display == "inline") {
            	eo1.style.display = "none";
            	eo2.className = "section";
            	document.location = "#"+menuid;
            	this.treecookie.del(eo1.id);
       		} 
       		}
       		}
         } //endfor
        
    }
    
    
    function TreeCookie() {
        this.get = function(name) {
            var cookies = document.cookie.split(";");
            for (var i = 0; i < cookies.length; ++i) {
                var a = cookies[i].split("=");
                if (a.length == 2) {
               /* if(a[0].trim())
                    a[0] = a[0].trim();
                if(a[1].trim())
                
                    a[1] = a[1].trim();*/
                    if (Trim(a[0]) == name) {
                        return unescape(Trim(a[1]));
                    }
                }
            }
            return "";
        };
        
        this.set = function(name, value, seconds, path, domain, secure) {
            var cookie = (name + "=" + escape(value));
            if (seconds) {
                var date = new Date(new Date().getTime()+seconds*1000);
                cookie += ("; expires="+date.toGMTString());
            }
            cookie += (path    ? "; path="+path : "");
            cookie += (domain  ? "; domain="+domain : "");
            cookie += (secure  ? "; secure" : "");
            document.cookie = cookie;
        };
        this.del = function(name) {
            document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT";
        };
    }
	
	function Trim(TRIM_VALUE){
		if(TRIM_VALUE.length < 1){
		return"";
		}
		TRIM_VALUE = RTrim(TRIM_VALUE);
		TRIM_VALUE = LTrim(TRIM_VALUE);
		if(TRIM_VALUE==""){
		return "";
		}
		else{
		return TRIM_VALUE;
		}
	} //End Function

	function RTrim(VALUE){
		var w_space = String.fromCharCode(32);
		var v_length = VALUE.length;
		var strTemp = "";
		if(v_length < 0){
		return"";
		}
		var iTemp = v_length -1;

		while(iTemp > -1){
		if(VALUE.charAt(iTemp) == w_space){
		}
		else{
		strTemp = VALUE.substring(0,iTemp +1);
		break;
		}
		iTemp = iTemp-1;

		} //End While
		return strTemp;

	} //End Function

	function LTrim(VALUE){
		var w_space = String.fromCharCode(32);
		if(v_length < 1){
		return"";
		}
		var v_length = VALUE.length;
		var strTemp = "";
		var iTemp = 0;
		while(iTemp < v_length){
		if(VALUE.charAt(iTemp) == w_space){
		}
		else{
		strTemp = VALUE.substring(iTemp,v_length);
		break;
		}
		iTemp = iTemp + 1;
		} //End While
	return strTemp;
	} //End Function


    var self = this;
    this.id = id;
    this.act = act;
    this.tree = new Array();
    this.treecookie = new TreeCookie();
    this.init();
    
   
}

 