// SubNavigation.js
// This will hide and show naviration based on id. 
// id="subnavigation" - 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.SubNavigation = {};
com.jchristy.SubNavigation.language = "";
com.jchristy.SubNavigation.levels = [];
com.jchristy.SubNavigation.display = function() {
	if (!com.jchristy.Support.features.HTML1) { return; }
	
	var subnav = document.getElementById("subnavigation");
	
	if (subnav) {
		
		var section = com.jchristy.Request.section || com.jchristy.Request.sections[0];
		var allUl = subnav.getElementsByTagName("ul");
		var currentNav = (section) ? document.getElementById(section) : allUl[0];
		
		if (currentNav) { com.jchristy.SubNavigation.language = currentNav.getAttribute("lang"); }
		
		for (var i = 0; i < allUl.length; i++) {
			if (allUl[i] == currentNav) { continue; }
			allUl[i].style.display = "none";
		}
		
		
		if (currentNav) {
			var activeLink = null;
			var pageid = com.jchristy.Request.pageid;
			var allLinks = currentNav.getElementsByTagName("a");
			
			var searchString = (pageid.charAt(0) != "*") ? "\\/" + pageid + "\\." : "\\" + pageid;
			
			var pattern = new RegExp(searchString);
			
			for (var i = 0; i < allLinks.length; i++) {
				if (allLinks[i].href.charAt(allLinks[i].href.length - 1) == "#") { continue; }
				
				if (pattern.test(allLinks[i].href)) {
					activeLink = allLinks[i];
					break;
				}
			}
			if (activeLink) {
				if (activeLink.getAttribute("active")) { activeLink.className = activeLink.getAttribute("active"); }
				com.jchristy.SubNavigation.levels[0] = { name: activeLink.firstChild.nodeValue, url: activeLink.href, title: activeLink.getAttribute("title") };
				var a = activeLink.nextSibling;
				
				while (a) {
					if (a.nodeName.toLowerCase() == "ul") {
						a.style.display = "block";
						
						var o = a.firstChild;
						
						while (o) {
							
							if (o.nodeType == Node.TEXT_NODE) { 
								o = o.nextSibling;
								continue; 
							}
							if (o.nodeName.toLowerCase() == "li") { 
								
								var r = o.firstChild;
								
								while (r) {
									if (r.nodeName.toLowerCase() == "a" && r.href.charAt(r.href.length - 1) != "#" && pattern.test(r.href)) {
										if (r.getAttribute("active")) { r.className = r.getAttribute("active"); }
										com.jchristy.SubNavigation.levels[0] = { name: r.firstChild.nodeValue, url: r.href, title: r.getAttribute("title") };
										activeLink = o;
										break;
									}
									r = r.nextSibling;
								}

							}
							o = o.nextSibling;	
						}
					}
					a = a.nextSibling;
				}
				var p = activeLink.parentNode;
				
				while (p) {
					if (p.nodeName.toLowerCase() == "ul") { 
						p.style.display = "block";
						var s = p.previousSibling;
						while (s) {
							if (s.nodeName.toLowerCase() == "a") {
								com.jchristy.SubNavigation.levels.push({ name: s.firstChild.nodeValue, url: s.href});
								if (s.getAttribute("active")) { s.className = s.getAttribute("active"); }
							}	
							s = s.previousSibling;
						}
					}
					p = p.parentNode;
				}
				
				com.jchristy.SubNavigation.levels.push({ name: currentNav.getAttribute("title"), url: currentNav.getAttribute("href") });
			}
		}
	}

}
if (document.getElementsByTagName("body").item(0)) {
	com.jchristy.SubNavigation.display();
} else {
	com.jchristy.Handler.add(window, "load", com.jchristy.SubNavigation.display);
}
