// Navigation.js
// This will hide and show naviration based on id. 
// <div id="navigation"> - REQUIRED - this is the element that contains ALL navigation.
// <ul id="[section]"> - OPTIONAL - this must match the folder name that the file resides in.
// <div id="breadcrumbs" lang="[language]" - REQUIRED - langauge the breadcrumbs is 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; }
	
	var globalActiveClass = currentNav.getAttribute("active") || null;
	var globalHighlightClass = currentNav.getAttribute("highlight") || null;
	
	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("highlight")) { 
		activeLink.className = activeLink.getAttribute("highlight"); 
	} else if (activeLink.getAttribute("active")) { 
		activeLink.className = activeLink.getAttribute("active"); 
	} else if (globalHighlightClass) {
		activeLink.className = globalHighlightClass;
	} else if (globalActiveClass) {
		activeLink.className = globalActiveClass;
	}

	com.jchristy.Navigation.levels[0] = { name: activeLink.firstChild.nodeValue, url: activeLink.getAttribute("href"), title: activeLink.getAttribute("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"); }
					else if (globalActiveClass) { t.className = globalActiveClass; }
					com.jchristy.Navigation.levels.push({ name: t.firstChild.nodeValue, url: t.href, title: t.title });
					break;
				}
				t = t.previousSibling;
			}
		}
		u = u.parentNode;	
	}
	if (currentNav.getAttribute("href")) {
		com.jchristy.Navigation.levels.push({ name: currentNav.getAttribute("id"), url: currentNav.getAttribute("href"), title: currentNav.getAttribute("title") });
	}
	
	//Breadcrumbs...

	var l = com.jchristy.Navigation.levels;
	var b = document.getElementById("breadcrumbs");
	
	if (b != null && l.length > 0)  {
		
		var homeTitle = b.getAttribute("home") || "Home";
		var homeLink = b.getAttribute("href") || "/";
		var seperator = b.getAttribute("sep") || " >  ";
		
		var crumbs = document.createElement("p");
		
		if (homeTitle) {
			var h = document.createElement("a");
				h.href = homeLink;
				h.appendChild(document.createTextNode(homeTitle));
				
			crumbs.appendChild(h);
			crumbs.appendChild(document.createTextNode(seperator));
		}
		
		for (var i = l.length - 1; i > 0; i--) {
			if (l[i].name == l[i - 1].name) { continue; }
			var a = document.createElement("a");
			if (l[i].title) {
				var t = document.createTextNode(l[i].title);
			} else {
				var t = document.createTextNode(l[i].name);
			}
			
			a.href = l[i].url;
			a.appendChild(t);
			
			crumbs.appendChild(a);
			crumbs.appendChild(document.createTextNode(seperator));
		}
		
		var t = (l[0].title) ? l[0].title : l[0].name;
		var y = document.createElement("strong");
			y.appendChild(document.createTextNode(t))
			crumbs.appendChild(y);
		
			b.appendChild(crumbs);
	}
}
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", function() { com.jchristy.Navigation._display(); });
}
