// JavaScript Document
/*
Title: Accessible Dynamic Navigation System v.3
Creator: Robert Fentress (rfentres@vt.edu)
Acknowledgements: Thanks to Richard Ishida, Lee Roberts, Martin Honnen, and James Craig for suggestions for improvements to this script
Date: 09/14/2005
Description: This is a Javascript-based dynamic web page navigation system that was designed to meet accessibility standards such as Section 508 and W3C WAI WCAG.  When viewed without javascript enabled, it degrades to a series of nested unordered lists.  Though this causes previous, next, and breadcrumb navigation features to be disabled, these are non-essential to navigating the site and probably redundant for those using a screen reader.
Copyright Holder: Virginia Polytechnic Institute & State University, Institute for Distance & Distributed Learning
License: This work is licensed under a Creative Commons License (http://creativecommons.org/licenses/by-sa/1.0/)
*/

//This bit necessary to prevent users from seeing the menu until its proper nodes have been hidden/displayed (Thanks to Bobby van der Sluis for this idea)
if (document.getElementById && document.getElementsByTagName && document.createElement) {
	document.write('<style type="text/css">#menu {display: none;}</style>');
	window.onload = initialize;
}
var menu, anchors, prev, prev2, next, next2, breadcrumb, leafSrc, minusSrc, plusSrc;
var dirIndex = Array("index.htm","index.html","index.php"); //pages server will deliver if only dir name is provided, must adjust for your particular server configuration.  Not important if you use explicit references.

function initialize() {
	MM_preloadImages(document.getElementById("leaf").src,document.getElementById("minus").src,document.getElementById("plus").src);
	menuObj = "menu";
	var clone = document.getElementById(menuObj).cloneNode(true);
	var cloneDiv = document.getElementById('cloneDiv');
	cloneDiv.replaceChild(clone, cloneDiv.firstChild);
	menu = document.getElementById(menuObj); //the root list that serves as our menu
	firstAnchors = menu.getElementsByTagName('a'); //array of all the links in the menu
	anchors = new Array();
	for (var n=0;n<firstAnchors.length;n++) {
		if (firstAnchors[n].getElementsByTagName("img").length ==0) anchors.push(firstAnchors[n]);
	}	
	prev = document.getElementById('prev'); //the previous link
	prev2 = document.getElementById('prev2'); //the previous link
	next = document.getElementById('next'); //the next link
	next2 = document.getElementById('next2'); //the next link
	breadcrumb = document.getElementById('breadcrumb'); //where the breadcrumb trail goes
	
	//set the current node in the menu as the page we are on
	for (var n=0;n<anchors.length;n++) {
		if (isCurrentPage(n)) menu.current = nodeIndex(anchors[n]);
	}	
	
	//set the initial state of all nodes
	var lis = menu.getElementsByTagName("li");
	for (var i=0;i<lis.length;i++) {
		var sublist = lis[i].getElementsByTagName('ul')[0];
		//close all nodes that have have children, setting the appropriate src image and alt attribute
		if (sublist) {
			expandButton = document.createElement('a');
			expandButtonImg = document.createElement('img');
			expandButtonImg.src = plusSrc;
			expandButtonImg.alt = "expand node";
			expandButtonImg.border = "0";
			expandButton.href = "javascript:;";
			expandButton.onclick = function() {expandNode(this);}
			if (document.all) {
				expandButton.onkeyup = function() {keyExpand(this);}
			} else {
				expandButton.addEventListener("keypress", mzKeyExpand, true)
			}
			expandButton.appendChild(expandButtonImg);
			lis[i].insertBefore(expandButton, lis[i].firstChild);
			sublist.style.display = 'none';
		//if no children, set image as a leaf node
		} else {
			leafNode = document.createElement('img');
			leafNode.src = leafSrc;
			leafNode.alt = "leaf node";
			lis[i].insertBefore(leafNode, lis[i].firstChild);
		}
	}
	//select the current node in the menu (also sets prev/next and breadcrumb)
	selectNode(anchors[menu.current]);
	
	//if current node has child, open it
	var currentNode = anchors[menu.current];
	var firstParent = currentNode.parentNode;
	var currentUL = firstParent.getElementsByTagName("ul")[0];
	if (currentUL) {
		var firstImg = firstParent.getElementsByTagName("a")[0];
		if (firstImg.firstChild.alt == "expand node" || firstImg.firstChild.alt == "collapse node") {
			firstImg.firstChild.src = minusSrc;
		}
		currentUL.style.display = "";
	}
	//open all parents of the current page
	while (currentNode != menu) {
		if (currentNode.tagName == "UL") {
			currentNode.style.display = "";
		}
		if (currentNode.tagName == "LI") {
			var currentAnchor = currentNode.getElementsByTagName("a")[0];
			var currentImg = false;
			if (currentAnchor.firstChild.alt == "expand node" || currentAnchor.firstChild.alt == "collapse node") {
				currentAnchor = currentNode.getElementsByTagName("a")[1];
				currentImg = currentNode.getElementsByTagName("a")[0].firstChild;
			}
			if (currentImg && (currentAnchor != anchors[menu.current])) {
				currentImg.src = minusSrc;
			}
		}
		currentNode = currentNode.parentNode;
	}
	menu.style.display="block";
}
function isCurrentPage (n) {
	var yes = false;
	if (location==anchors[n].href) yes=true;
	for (var i=0;i<dirIndex.length;i++) {
		if ((location+dirIndex[i]==anchors[n].href)||(location==anchors[n].href+dirIndex[i])) yes=true;
	}
	return yes;
}
function setBreadcrumb() {
	var currentNode = anchors[menu.current];
	while (breadcrumb.firstChild) breadcrumb.removeChild(breadcrumb.firstChild);
	while (currentNode != menu) {
		if (currentNode.tagName == "LI") {
			var currentLink = currentNode.getElementsByTagName('a')[0];
			if (currentLink.firstChild.alt == "expand node" || currentLink.firstChild.alt == "collapse node") {
				currentLink = currentNode.getElementsByTagName("a")[1];
			}
			var currentIndex = nodeIndex(currentLink);
			var breadcrumbLink = currentLink.cloneNode(true);
			var delimiter = document.createTextNode(" > ");
			if (breadcrumb.childNodes.length) breadcrumb.insertBefore(delimiter, breadcrumb.firstChild);
			breadcrumb.insertBefore(breadcrumbLink, breadcrumb.firstChild);
		}
		currentNode = currentNode.parentNode;
	}
}
function setPrev() {
	while (prev.firstChild) prev.removeChild(prev.firstChild);
	while (prev2.firstChild) prev2.removeChild(prev2.firstChild);
	var lt = document.createTextNode("< ");
	var prevNum = menu.current-1;
	var prevLink;
	var prevText = document.createTextNode("Prev");
	
	if (prevNum < 0) {
		prevLink = document.createElement("span");
	} else {
		prevLink = anchors[prevNum].cloneNode(true);
		prevLink.removeChild(prevLink.firstChild);
	}
	prevLink.appendChild(prevText);
	prev.appendChild(lt.cloneNode(true));
	prev2.appendChild(lt);
	prev.appendChild(prevLink.cloneNode(true));
	prev2.appendChild(prevLink);
}
function setNext() {
	while (next.firstChild) next.removeChild(next.firstChild);
	while (next2.firstChild) next2.removeChild(next2.firstChild);
	var gt = document.createTextNode(" >");
	var nextNum = menu.current+1;
	var nextLink;
	nextText = document.createTextNode("Next");
	if (nextNum == anchors.length) {
		nextLink = document.createElement("span");
	} else {
		nextLink = anchors[nextNum].cloneNode(true);
		nextLink.removeChild(nextLink.firstChild);
	}
	nextLink.appendChild(nextText);
	next.appendChild(nextLink.cloneNode(true));
	next2.appendChild(nextLink);
	next.appendChild(gt.cloneNode(true));
	next2.appendChild(gt);
}
function expandNode(x) {
	var sublist = x.parentNode.getElementsByTagName('ul')[0];
	if (sublist) {
		if (sublist.style.display == 'none') {
			x.firstChild.alt = "collapse node";
			x.firstChild.src = minusSrc; 
			sublist.style.display = '';
		} else { 
			x.firstChild.src = plusSrc;
			sublist.style.display = 'none';
			x.firstChild.alt = "expand node";
		}
	}
}
function keyExpand (x) {
	if (event.keyCode == 32) {
		expandNode(x);
	}
}
function mzKeyExpand (key_pressed) {
	if (key_pressed.which == 32) {
		expandNode(this);
	}
}
function selectNode(x) {
	anchors[menu.current].className = "deselected";
	for (var n=0;n<anchors.length;n++) {
		if (isCurrentPage(n)) menu.current = nodeIndex(anchors[n]);
	}
	anchors[menu.current].className = "selected";
	setBreadcrumb()
	setPrev();
	setNext();	
}
function nodeIndex (x) {
	for (var i=0;i<anchors.length;i++) {
		if (anchors[i] == x) return i;
	}
}
/*Macromedia function altered by Robert Fentress*/
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; 
	leafSrc=a[0];
	minusSrc=a[1];
	plusSrc=a[2];
	for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}