/*
 * 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)
 */

/* 
 * Original code from mygosuMenu
 * modified by Thomas Buchanan
 * 
 * Main modification is to track which nodes are open
 * We keep an array openNode[] and save it in a cookie
 */
 
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;
        }
        menu = document.getElementById(this.id);
        this.parse(menu.childNodes, this.tree, this.id);
        this.load(menu.childNodes);
        if (menuId = document.getElementById('menu1'))
        {
            menuStyle = menuId.style;
            menuStyle.display = "block";
	}
        
        if (window.attachEvent) {
            window.attachEvent("onunload", function(e) { self.sectionSave(); });
        } else if (window.addEventListener) {
            window.addEventListener("unload", function(e) { self.sectionSave(); }, false);
        }
    }

    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") {
                nodes[i].id = id + "-" + tree.length;
                tree[tree.length] = new Array();
                if (nodes[i].childNodes && this.hasUl(nodes[i].childNodes)) {
                    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.sectionClick('"+nodes[i].id+"');"+
                        "}");
                    }
                } else {
                    var n;
                    if (navigator.appName == "Microsoft Internet Explorer" )
                    {
                    	n = nodes.length - 1;
                    }
                    else
                    {
                    	n = nodes.length - 2;
                    }
                    if (i < n)
                    {
                        nodes[i].className = "box";
                    } else {
                        nodes[i].className = "box-last";
                    }
                    var a;
                    if ( a = this.getA(nodes[i].childNodes) ) {
                    	a.id = nodes[i].id + "-a";
                    	eval("document.getElementById('"+a.id+"').onclick = function () {"+
                    		"self.linkClick('"+nodes[i].id+"');"+
                    		"}");
                    }
                }
            }
            if (nodes[i].tagName.toLowerCase() == "ul") {
                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.sectionClick = function(id) {
        e1 = document.getElementById(id + "-section");
        e2 = document.getElementById(id);
        if (e1.style.display == "none") {
            e1.style.display = "";
            e2.className = "section-open";
            this.openNode[id] = "open";
        } else {
            e1.style.display = "none";
            e2.className = "section";
            this.openNode[id] = "closed";
        }
        self.sectionSave();
    }
    
    this.linkClick = function(id) {
    	this.clickedNode = id;
    	self.linkSave();
    }
    
    this.sectionSave = function() {
        for (node in this.openNode)
        {
            this.cookie.set(node, this.openNode[node]);
        }
    }
    
    this.linkSave = function() {
    	this.cookie.set("clickedNode", this.clickedNode);
    }
    
    this.load = function(nodes) {
    	var current = this.cookie.get("clickedNode");
    	for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].nodeType != 1) {
                continue;
            }
            if (nodes[i].tagName.toLowerCase() == "li") {
            	if (current == nodes[i].id) {
            		e = document.getElementById(nodes[i].id + "-a");
            		e.style.border = "navy 1px dotted";
            	}
		        var nc = this.cookie.get(nodes[i].id);	
    	        if (nc == "open") {
    	        	e = document.getElementById(nodes[i].id + "-section");
    	        	e.style.display = "";
    	          	nodes[i].className = "section-open";
    	        }
    	        if (nodes[i].childNodes) {
    	        	this.load(nodes[i].childNodes);
    	        }
    	    }
    	    else if (nodes[i].childNodes) {
    	        	this.load(nodes[i].childNodes);
    	    }
        }
    }

    
    function Cookie() {
        this.set = function(name, value) {
            document.cookie = name + "=" + escape(value) + "; path=/";
        }
        this.get = function(name) {
        	var dc = document.cookie;
    		var prefix = name + "=";
    		var begin = dc.indexOf("; " + prefix);
    		if (begin == -1)
    		{
    		    begin = dc.indexOf(prefix);
    		    if (begin != 0) return null;
    		}
    		else
    		{
    		    begin += 2;
    		}
    		var end = document.cookie.indexOf(";", begin);
    		if (end == -1)
    		{
    	    	end = dc.length;
    		}
    		return unescape(dc.substring(begin + prefix.length, end));
		}
		this.del = function(name) {
			if (this.cookie.get(name))
    		{
        		document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    		}
		}
    }

    var self = this;
    this.id = id;
    this.tree = new Array();
    this.openNode = new Object();
    this.clickedNode = new Object();
    this.cookie = new Cookie();
    this.init();
}
