/**
 * An array of XML objects representing directories on disk
 *
 * Used to store the directory XML client side so that we don't have to read from
 * the disk on each onMouseOver event via AJAX. This is an associative array with
 * the array index being the directory path.
 * 
 * @access global
 * @since 1.0 - Aug 27, 2007
 * @var array An array of XML objects representing directories on disk
 */
var directories = [];
var execute = true;

function printLinks(links) {
	if(links.length < 1) return;
	if(links.length == 1 && links[0].getAttribute("path").match("index.htm") == "index.htm") return;
	var divID = "divFloatNav" + links[0].getAttribute("path").replace(/\//g, "");
	if(document.getElementById(divID)) return;
	var str = "<div id=\"" + divID + "\" class=\"floatNav\"><ul>";

        for(var i = 0; i < links.length; i++) {
		if(links[i].nodeName == "link") {
			var id = "liNav" + links[i].getAttribute("path").replace(/\//g, "");

			str += "<li id=\"" + id + "\"><a " + 
				"onMouseOver=\"showNav('" + links[i].getAttribute("path").substr(1) + "', '" + id + "', event)\" " +
				"href=\"" + links[i].getAttribute("path") + "\">";

			if(links[i].getAttribute("title") == "") {
				var children = links[i].childNodes;

				for(var j = 0; j < children.length; j++) {
					if(children[j].nodeName == "link" && 
						children[j].getAttribute("path").match("index.htm") == "index.htm") {

						str += children[j].getAttribute("title");
					}
				}
			} else {
				str += links[i].getAttribute("title");
			}

			str += "</a></li>";
		}
	}

	str += "</ul></div>";

	return str;
}

function showNav(directory, id) {
	var li = document.getElementById(id);
	killDivs(li.parentNode);
	execute = true;

	if(directories[directory] == null) {
		var XMLHttpRequestObject = false;
		if(window.XMLHttpRequest) {
			XMLHttpRequestObject = new XMLHttpRequest();
		} else if(window.ActiveXObject) {
			XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
		}

		if(XMLHttpRequestObject) {
			XMLHttpRequestObject.open("GET", "/menu.php?dir=" + directory);
			XMLHttpRequestObject.onreadystatechange = function() {
				if(XMLHttpRequestObject.readyState == 4 &&
					XMLHttpRequestObject.status == 200) {
					var xml = XMLHttpRequestObject.responseXML;
					if (xml.firstChild.childNodes.length == 0) {
						xml = new ActiveXObject("Microsoft.XMLDOM");
						xml.loadXML(XMLHttpRequestObject.responseText);
					}
					directories[directory] = xml;
					var str = printLinks(xml.getElementsByTagName("links")[0].childNodes);
					if(str)
						li.innerHTML += str;
				}
			}

			XMLHttpRequestObject.send(null);
		} else {
			alert("AJAX isnt working.");
		}
	} else {
		if(directories[directory].firstChild.childNodes.length > 0) {
			var str = printLinks(directories[directory].getElementsByTagName("links")[0].childNodes);
			if(str)
				li.innerHTML += str;
		}
	}
}

function killDivs(obj) {
	for(var i = 0; i < obj.childNodes.length; i++) {
		if(obj.childNodes[i].tagName == "DIV" && obj.childNodes[i].className == "floatNav") {
			obj.removeChild(obj.childNodes[i]);
		}

		if(obj.childNodes[i] && obj.childNodes[i].childNodes.length > 0) {
			killDivs(obj.childNodes[i]);
		}
	}

	execute = false;
	register();
}

function register() {
	// Horizontal Nav Items
	//document.getElementById("aNavAbout").onmouseover = function() { showNav("about", "liNavAbout"); };
	//document.getElementById("aNavSponsors").onmouseover = function() { showNav("sponsors", "liNavSponsors"); };
	document.getElementById("aNavInitiative").onmouseover = function() { showNav("initiatives", "liNavInitiative"); };
	document.getElementById("aNavMagazine").onmouseover = function(event) { showNav("avmag", "liNavMagazine"); };
	//document.getElementById("aNavSubmit").onmouseover = function(event) { showNav("submit", "liNavSubmit"); };
	//document.getElementById("aNavJoin").onmouseover = function(event) { showNav("join", "liNavJoin"); };
	//document.getElementById("aNavDonate").onmouseover = function(event) { showNav("donate", "liNavDonate"); };

	if(document.getElementById("divLeftNav")) {
		// Left Nav Items
		document.getElementById("aNavGoing").onmouseover = function() { showNav("going", "liNavGoing"); };
		document.getElementById("aNavReturn").onmouseover = function() { showNav("return", "liNavReturn"); };
		document.getElementById("aNavAcademics").onmouseover = function() { showNav("academics", "liNavAcademics"); };
		document.getElementById("aNavCareers").onmouseover = function() { showNav("careers", "liNavCareers"); };
		document.getElementById("aNavStories").onmouseover = function() { showNav("stories", "liNavStories"); };
		document.getElementById("aNavGreen").onmouseover = function() { showNav("green", "liNavGreen"); };
		document.getElementById("aNavEngage").onmouseover = function() { showNav("engage", "liNavEngage"); };
		document.getElementById("aNavCitizen").onmouseover = function() { showNav("globalcitizen", "liNavCitizen"); };
		document.getElementById("aNavNews").onmouseover = function() { showNav("news", "liNavNews"); };
		document.getElementById("aNavLectures").onmouseover = function() { showNav("lectures", "liNavLectures"); };
		document.getElementById("aNavTopics").onmouseover = function() { showNav("topics", "liNavTopics"); };
	}

	// Content
	document.getElementById("divContent").onmouseover = function() {
		if(execute) {
			killDivs(this.parentNode);
		}
	}
}

register();

