// Navigation.js
// This will hide and show naviration based on page location. 
// <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" <span id="glyph"
// <a href="#auto" id="name"
//
//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 sectionOverride = com.jchristy.Request.section;
	var allLists = navigation.getElementsByTagName("ul");
	
	var currentNav;
	var section;
	
	if (sectionOverride) 
	{ 
		currentNav = document.getElementById(sectionOverride);
		section = com.jchristy.Request.sections[0];
	} 
	else
	{
		for (var i = 0; i < allLists.length; i++)
		{
			var id = allLists[i].id;
			var url = window.location.href;
			
			if (id && url.indexOf("/" + id + "/") != -1) 
			{
				currentNav = allLists[i];
				section = id;
				break;
			}
		}
	}
		
	if (!currentNav && allLists.length > 0) { 
		section = com.jchristy.Request.sections[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";
	}
	
	currentNav.style.display = "block";
		
	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 + "\\/" + "[\\d \\w \\/]*" + pageid + "\\.");
	
	//if (com.jchristy.Request.anchor) { pattern = new  RegExp("\\/" + section + "/" + pageid + "\\.\\w*\#" + com.jchristy.Request.anchor); }
	
	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.indexOf("#auto") != -1) { 
			com.jchristy.Navigation._setAutoExpand(l); 
			continue; 
		}
		
		if (l.href.indexOf("#") != -1)
		{
			if (l.href.substr(l.href.indexOf("#")).length == 1) { continue; }
			
		}
		
		if (pattern.test(l.href)) {
			activeLink = l;
		}
	}
	
	if (!activeLink) { return; }
	
	

	if (activeLink.className != "") 
	{
		activeLink.className += "-highlight";
	} else {
		activeLink.className = "highlight";	
	}
	
	var v = activeLink.firstChild;
					
	while (v.nodeType != Node.TEXT_NODE) {
		v = v.firstChild;
	}
	
	com.jchristy.Navigation.levels[0] = { name: v.nodeValue, url: activeLink.getAttribute("href"), title: activeLink.getAttribute("title") };
	
	var d = activeLink.nextSibling;
	while (d) {
		if (d.nodeName.toLowerCase() == "ul") {
			if (d.className != "hidden") { d.style.display = "block"; }
			break;
		}
		d = d.nextSibling;
	}
	var u = activeLink.parentNode;
	
	while (u) {
		if (u.nodeName.toLowerCase() == "ul") {
			if (u.className != "hidden") { u.style.display = "block"; }
			var t = u.previousSibling;
			while (t) {
				if (t.nodeName.toLowerCase() == "a") {
					
					if (t.className != "") { t.className += "-active"; }
					else { t.className = "active"; }
					
					var v = t.firstChild;
					
					while (v.nodeType != Node.TEXT_NODE) {
						v = v.firstChild;
					}
					
					com.jchristy.Navigation.levels.push({ name: v.nodeValue, url: t.href, title: t.title });
					break;
				}
				t = t.previousSibling;
			}
		}
		u = u.parentNode;	
	}
	
	//Breadcrumbs...

	var l = com.jchristy.Navigation.levels;
	var b = document.getElementById("breadcrumbs");
	
	if (b != null && l.length > 0)  {
		
		var seperator = " > ";
		
		var g = document.getElementById("glyph");
		
		if (g) 
		{
			seperator = g.firstChild.nodeValue;	
		}
		
		var crumbs = document.createElement("span");
		
		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);
			
			var s = document.createElement("span");
				s.className = "seperator";
				s.appendChild(document.createTextNode(seperator));
				
			crumbs.appendChild(s);
		}
		
		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("id");
	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("id");
	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") {
					//need to reset class here
					
					if (u.className == "active") {
						u.className = "";	
					} else if (u.className.indexOf("-active") != -1) {
						u.className = u.className.replace("-active", "");
					}
					break;
				}
				
				u = u.previousSibling; 
			}
		
		}
	}
	
	if (ul) { ul.style.display = (ul.style.display == "block") ? "none" : "block"; }
	
	if (ul.style.display == "none")
	{
		if (target.className == "active") 
		{
			target.className = "";
		} else {
			target.className = target.className.replace("-active", "");	
		}
	}
	var isClosed = (ul.style.display == "none");
	ul = ul.parentNode;
	
	while(ul) {
		if (ul.nodeName.toLowerCase() == "ul") {
			ul.style.display = "block";
		} else if (ul.nodeName.toLowerCase() == "li") {
			var a = ul.firstChild;
			while (a) {
				if (a.nodeName.toLowerCase() == "a") {
					if (a == target && isClosed)
					{	
						a = a.nextSibling;
						continue;	
					}
					if (a.className.indexOf("active") == -1) {
						if (a.className != "") {
							a.className += "-active";	
						} else {
							a.className = "active";	
						}
					}
					break;
				}
				a = a.nextSibling;
			}
		}
		ul = ul.parentNode;
	}
	
	e.preventDefault();
}
if (document.getElementsByTagName("body").item(0)) {
	com.jchristy.Navigation._display();
} else {
	com.jchristy.Handler.add(window, "load", function() { com.jchristy.Navigation._display(); });
}
