// JavaScript Document
/*
Title: c v.2
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: 07/07/2004
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/)

Instructions: Must use valid list markup and include the following code in pages that use the system:
<body onload="MM_preloadImages('images/leaf.gif','images/minus.gif','images/plus.gif'); initialize('menu')">
<ul id="menu">
<div id="breadcrumb"> </div>
<span id="prev"> </span>  | <span id="next"> </span>
<span id="prev2"> </span>  | <span id="next2"> </span>
</body>

Must also use appropriate syling.  Particularly, it should include a style called .nofinger that sets the cursor to default.
*/
var menu, anchors, prev, prev2, next, next2, breadcrumb, leafSrc, plusSrc, minusSrc;
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(menuObj) {
	if (typeof document.insertBefore != 'undefined') {
		menu = document.getElementById(menuObj); //the root list that serves as our menu
		anchors = Array();
		//anchors = menu.getElementsByTagName('a'); //array of all the links in the menu
		//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
		//*To get rid of breadcrumb - 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++) {
			anchors[i] = lis[i].getElementsByTagName('a')[0];
			if (isCurrentPage(i)) menu.current = nodeIndex(anchors[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);
				/*lis[i].getElementsByTagName('input')[0].src = plusSrc;
				lis[i].getElementsByTagName('input')[0].alt = "expand node";
				lis[i].getElementsByTagName('input')[0].onclick = function() {expandNode(this);}*/
				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);
				/*lis[i].getElementsByTagName('input')[0].src = leafSrc;
				lis[i].getElementsByTagName('input')[0].alt = "leaf node";
				lis[i].getElementsByTagName('input')[0].onclick = "";
				lis[i].getElementsByTagName('input')[0].className = "nofinger";*/
			}
		}
		//select the current node in the menu (also sets prev/next and breadcrumb)
		if (anchors[menu.current]!=null){
			selectNode(anchors[menu.current]);
		
		
		//if current node has child, open it
		var currentNode = anchors[menu.current];
		currentNode.style.color="#336699";
		currentNode.style.backgroundColor="#FFFFFF";
		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;
				//ADDED
				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;
		}
		}
	}
}
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;
}
/* Removed for removal of breadcrumb
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;
	}
} -End of removal for breadcrumb */
/*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);
		/*var sublist = this.parentNode.getElementsByTagName('ul')[0];
		if (sublist) {
			if (sublist.style.display == 'none') {
				this.firstChild.alt = "collapse node";
				this.firstChild.src = minusSrc; 
				sublist.style.display = '';
			} else { 
				this.firstChild.src = plusSrc;
				sublist.style.display = 'none';
				this.firstChild.alt = "expand node";
			}
		}*/
	}
}
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";
			//*To get rid of breadcrumb - 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
Altered this rather than include in initialize function,
so DW templates could be used and image locations automatically updated*/
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];
		}
	}
   }
   
}