// See also do_navigation_tree().

var display = "block";
var depth = 2;

function init_navtree() {
	// START CLOSED.
	e = document.getElementById("navigation");
	e = e.getElementsByTagName("ul");
	for (i=0; i<e.length; i++) {
		if (e[i].parentNode.tagName.toLowerCase() == "ul") {
			e[i].style.display = "none";
		}
	}
	
	// OPEN BREADCRUMB.
	// A link gets tagged as current_node if it links to the current page.
	// We move up until we find the encapsulating <li> element.
	e = document.getElementById("current_page");
	while (e && e.tagName.toLowerCase() != "li") { 
		e = e.parentNode; 
	}
	if (e) {
		e.id = "current_node";
	}
	// If the next element is <ul>, it is a subbranch that must be open.
	if (e && e.nextSibling && e.nextSibling.tagName.toLowerCase() == "ul") {
		e.nextSibling.style.display = display;
	}
	if (e) {sub = e.getElementsByTagName("ul");
	if (sub.length > 0)	sub[0].style.display = display;}
	// All of the parent <ul> elements must be open.
	for (i=0; i<depth; i++) {
		if (e) {
			e = e.parentNode;
			if (e && e.tagName && e.tagName.toLowerCase() == "ul") {
				e.style.display = display;
			}
		}
	}
	
	// TOGGLE NODE WHEN CLICKED.
	e = document.getElementById("navigation");
	e = e.getElementsByTagName("li");
	for (i=0; i<e.length; i++) {
		// Each <li> in the navigation uses the link cursor.
		e[i].style.cursor = "pointer";
		// Disable text selecting.
		e[i].onselectstart = function() { return false; };
		e[i].unselectable = "on";
    	e[i].style.MozUserSelect = "none";
		// Attach a click behavior to each <li> in the navigation.
		e[i].onclick = function () {
		    // Close open menus not in this branch.
		    e = document.getElementById("navigation");
        	e = e.getElementsByTagName("ul");
        	for (i=0; i<e.length; i++) {
        		if (e[i].parentNode.tagName.toLowerCase() == "ul" && 
        		    e[i] != this.nextSibling && 
        		    e[i] != this.parentNode &&
        		    e[i] != this.parentNode.parentNode &&
        		    e[i].style.display == display) {
        			e[i].style.display = "none";
        		}
        	}
			// If the element following this <li> is a <ul>,
			// this is a subbranch that needs to be toggled open or closed.
			e = this.nextSibling;
			if (e && e.tagName.toLowerCase() == "ul") {
				if (e.style.display == display) {
					e.style.display = "none";
				} else {
					e.style.display = display;
				}
			}
		}
	}

}

init_navtree();