// JavaScript Document
//Nav tree code inspired by Matt Kruse's DHTML Tree (http://mattkruse.com) and Christian Heilmann's Unobtrusive Javascript (http://www.onlinetools.org/articles/unobtrusivejavascript/)
//This script supports only W3C-compliant browsers and IE 6+(win)/5+(Mac). Some features will not work in IE-Mac. It will not work in older browsers, but should fail gracefully, displaying the fully expanded tree if the CSS is called via @import


//OPTIONS: change these values to modify the behavior of the tree.
//If true, tree opens to current page, if it is found in the tree.
var openCurrent = true;
//If true, sets a highlight class on the current page, if it is found in the tree.
//Exact appearance of the highlighting is defined in the accompanying stylesheet.
var hiliteCurrent = true;
//If true, saves the current page URL to a cookie when highlighting changes, then sets the open & hilited items to the last page's values when opening a page that isn't in the tree. Does nothing if openCurrent & hiliteCurrent are both false.
var saveLastGood = true;
//List of specific pages where saveLastGood will be disabled
var noHL = new Array("samplePage.html");

//globals for some objects that we'll need numerous times
var pageLinks;
var treeDiv;
var treeItems;
var treeLinks;
var currPage;

//disable hiliting if browser is IE-Mac, because it does some wierd stuff when we try to add the class to links
//unfortunately, this also disables hiliting in Opera-Mac if Opera is reporting itself as IE
if ((navigator.platform.indexOf("Mac") != -1) && (navigator.userAgent.indexOf("MSIE") != -1)) hiliteCurrent = false;



//find all li elements in the tree with the appropriate class names and add event listeners
//also add event listeners for internal links found in the body of the doc and the tree
function initNav(e) {
	//make sure the methods we need are available, if not nothing will work and the user will get 
	//a non-expandable tree, but the links will still work
	if (!document.getElementById || !document.getElementsByTagName) return null;
	
	//set global variables that we'll need numerous times
	pageLinks = document.getElementsByTagName("a");
	treeDiv = document.getElementById('treeroot');
	treeItems = treeDiv.getElementsByTagName('li');
	treeLinks = treeDiv.getElementsByTagName('a');
	currPage = location.protocol + "//" + location.host + location.pathname + location.search; //current page URL, ignoring hash
	
	//add event handlers to internal document links
	for (h=0;h<pageLinks.length;h++) {
		thisLink = pageLinks[h];
		thisURL = thisLink.href;
		if (thisURL.indexOf(currPage) > -1) thisLink.onclick = intClick;
	}

	//add event handlers to nav tree LIs
	var thisPage = false;
	var l;
	var foundPage = false
	for (i=0;i<treeItems.length;i++) {
		l=treeItems[i];
		//attach default event handler to open & close nodes on clicks
		if (l.className=="nodeOpen" || l.className=="nodeClosed") l.onclick=liClick;
		
		//Check whether the current list item contains a link: if it does, check whether it matches the current URL. If it does then open to and/or highlight the item if those options are enabled and save the location to the cookie.
		if (l.getElementsByTagName("a")[0]) {		
			thisLink = l.getElementsByTagName("a")[0];
			
			if ((thisLink.href == location) && !foundPage) {
				//save URL in a cookie for later use with pages that aren't in the tree
				thisPage = true;
				foundPage = true; //set so we won't highlight more than one item
				if (saveLastGood) saveNavState(thisLink.href);
				//highlight current item if found in the tree list
				if (hiliteCurrent) {thisLink.className += " currentA";}
				//open to current item if found in the list
				if (openCurrent) {openToNode(l);}
			}
			
		}
	}
	
	//If the current URL isn't in the tree, then we'll open the tree to the last good URL, if that option is enabled and the page isn't in the exception list.
	//First check whether the current page is one of the exceptions
	var foundExcep = false;
	for (k=0;k<noHL.length;k++) {
		if (location.href.indexOf(noHL[k]) > -1) {
			foundExcep = true;
			saveNavState("");
		}
	}
	
	if (!thisPage && saveLastGood && !foundExcep) {
		//get URL from cookie
		var allCookies = document.cookie;
		var pos = allCookies.indexOf("oseNavState=");
		if (pos != -1) {
			var start = pos + 12;
			var end = allCookies.indexOf(";", start);
			if (end == -1) end = allCookies.length;
			var lastURL = allCookies.substring(start, end);
		}
		for (m=0;m<treeLinks.length;m++) {
			if ((treeLinks[m].href == lastURL) && !foundPage) {
				foundPage = true;
				if (hiliteCurrent) treeLinks[m].className += " currentA";
				if (openCurrent) openToNode(treeLinks[m]);
			}
		}
	}	
}

function openToNode(cNode) {
	if (cNode.id == "treeroot") {return false}
	if (cNode.className == "nodeClosed") cNode.className = "nodeOpen";
	openToNode(cNode.parentNode);
}

function liClick(e) {
	//make sure we are only acting on the list node that triggered the event
	if (e) { //W3C event model
		if (e.target != e.currentTarget) return null;
	}
	else if (window.event) { //IE event model
		if (window.event.srcElement != this) return null;
	}
	else return null;

	//switch classes to change display status
	if (this.className=="nodeOpen") this.className="nodeClosed";
	else if (this.className=="nodeClosed") this.className="nodeOpen";
}

function intClick(e) {
	//This function provides the behavior for internal page links. Since there isn't a page load, we have to do things a little differently	
	if (e) { //W3C event model
		var intLink = e.currentTarget;
	}
	else if (window.event) { //IE event model
		var intLink = window.event.srcElement;
	}
	else return null;
	
	var intTreeLink;
	var prevHiLink;
	var foundIntLink;
	
	//process tree links
	for (i=0;i<treeLinks.length;i++) {
		intTreeLink = treeLinks[i];
		if (intTreeLink.className.match(/\bcurrentA\b/)) prevHiLink = intTreeLink;
		if (intTreeLink.href == intLink.href) foundIntLink = intTreeLink;
	}
	
	if (foundIntLink) {
		//clear highlight class if necessary
		var newClassName = prevHiLink.className.replace(/\bcurrentA\b/, "");
		prevHiLink.className = newClassName;
		//save URL in a cookie for later use with pages that aren't in the tree
		if (saveLastGood) saveNavState(foundIntLink.href);
		//open tree to this node
		if (openCurrent) openToNode(foundIntLink);
		//highlight this node
		if (hiliteCurrent) foundIntLink.className += " currentA";
		
	}
	
}

function saveNavState (cookieURL) {
	document.cookie = "oseNavState=" + cookieURL + "; path=/";
}

window.onload=initNav;