// Navigation.js
// This will hide and show naviration based on id. 
// id="navigation" - REQUIRED - this is the element that contains ALL navigation.
// <ul id="[section]" - REQUIRED - this must match the folder name that the file resides in.
//
//First check the name space...

var com;
if (!com || !com.jchristy) { throw new Error("com.jchristy: Required object 'com.jchristy.Support' does not exist."); }
if (!com.jchristy.Handler) { throw new Error("com.jchristy: Required object 'com.jchristy.Handler' does not exist."); }
if (!com.jchristy.Request) { throw new Error("com.jchristy: Required object 'com.jchristy.Request' does not exist."); }

//Name space is ready.

com.jchristy.Navigation = {};
com.jchristy.Navigation.levels = [];
com.jchristy.Navigation._display = function() {
	if (!com.jchristy.Support.features.HTML1) { return; }
	
	var navigation = document.getElementById("navigation");

	if (!navigation) { return; }
		
		
	var section = com.jchristy.Request.section || com.jchristy.Request.sections[0];
	
	var allLists = navigation.getElementsByTagName("ul");
	
	var currentNav;
	
	if (section) { currentNav = document.getElementById(section); }
	
	if (!currentNav && allLists.length > 0) { currentNav = allLists[0]; }
	
	if (!currentNav) { return; }
	
	for (var i = 0; i < allLists.length; i++) {
		if (allLists[i] == currentNav) { continue; }
		allLists[i].style.display = "none";
	}
		
	var activeLink = null;
	var allLinks = currentNav.getElementsByTagName("a");
	
	var section = section || com.jchristy.Request.hostname;
	var pageid = com.jchristy.Request.pageid;
	
	var pattern = new RegExp("\\/" + section + "/" + pageid + "\\.");
	
	if (com.jchristy.Request.nav) { pattern =  new RegExp("nav\\=" + com.jchristy.Request.nav); } 
	
	
	for (var i = 0; i < allLinks.length; i++) {
		var l = allLinks[i];
		if (l.href.charAt(l.href.length - 1) == "#") { 
			if (l.getAttribute("auto")) { 
				com.jchristy.Navigation._setAutoExpand(l); 
			}
			continue; 
		}
		
		if (pattern.test(l.href)) {
			activeLink = l;
		}
	}
	
	if (!activeLink) { return; }
	
	
	if (activeLink.getAttribute("active")) { activeLink.className = activeLink.getAttribute("active"); }
	
	com.jchristy.Navigation.levels[0] = { name: activeLink.firstChild.nodeValue, url: activeLink.href, title: activeLink.title };
	
	var d = activeLink.nextSibling;
	while (d) {
		if (d.nodeName.toLowerCase() == "ul") {
			d.style.display = "block";
			break;
		}
		d = d.nextSibling;
	}
	var u = activeLink.parentNode;
	
	while (u) {
		if (u.nodeName.toLowerCase() == "ul") {
			u.style.display = "block";
			var t = u.previousSibling;
			while (t) {
				if (t.nodeName.toLowerCase() == "a") {
					if (t.getAttribute("active")) { t.className = t.getAttribute("active"); }
					com.jchristy.Navigation.levels.push({ name: t.firstChild.nodeValue, url: t.href, title: t.title });
					break;
				}
				t = t.previousSibling;
			}
		}
		u = u.parentNode;	
	}
	com.jchristy.Navigation.levels.push({ name: currentNav.getAttribute("id"), url: currentNav.getAttribute("href"), title: currentNav.getAttribute("title") });
}
com.jchristy.Navigation._setAutoExpand = function(LINK) {
	var id = LINK.getAttribute("auto");
	if (!id) { return; }
	
	var s = LINK.nextSibling;
	
	while (s) {
		if (s.nodeName.toLowerCase() == "ul") {
			s.id = "_auto_" + id;
			com.jchristy.Handler.add(LINK, "click", com.jchristy.Navigation.autoExpand);
			break;
		}
		
		s = s.nextSibling;	
	}
}
com.jchristy.Navigation.autoExpand = function(e) {
	var target = e.target;
	
	// This is for  safari 1.3 incorectly sets the 
	// text of the link as the target of the event
	if (target.nodeType == Node.TEXT_NODE) {
		target = target.parentNode;
	}
	
	var id = target.getAttribute("auto");
	var nc = target.getAttribute("active");
	var ul = document.getElementById("_auto_" + id);
	
	var hide = document.getElementsByTagName("ul");
	
	for (var i = 0; i < hide.length; i++) {
		if (hide[i] != ul && hide[i].id && hide[i].id.match("_auto_"))	{ 
			hide[i].style.display = "none"; 
			
			var u = hide[i].previousSibling; 
			
			while (u) {
				if (u.nodeName.toLowerCase() == "a") {
					if (u.getAttribute("active")) { u.className = ""; }
					break;
				}
				
				u = u.previousSibling; 
			}
		
		}
	}
	
	if (ul) { ul.style.display = (ul.style.display == "block") ? "none" : "block"; }
	if (nc) { target.className = nc; }
	e.preventDefault();
}
if (document.getElementsByTagName("body").item(0)) {
	com.jchristy.Navigation._display();
} else {
	com.jchristy.Handler.add(window, "load", com.jchristy.Navigation._display);
}

