/* 
	adding "here" class to appropriate links
	choose the structure containing the "here" by id 
 */
function markHere (id) {
	// Find the anchor tag
	// Set the container
	var container = document.getElementById(id);
	
	// Get links within the container
	var links = container.getElementsByTagName('a');
	var linkUrl = "";
	// for each item, look for the filename
	for (var i = 0; i < links.length; i++) {
		linkUrl = links[i].href.substring(0, links[i].href.lastIndexOf('?'));
		if ((linkUrl.lastIndexOf('/') == linkUrl.length - 1) && (document.URL.indexOf('index') != -1)) {
			// Chop the doc url down
			docUrl = document.URL.substring(0, document.URL.lastIndexOf('/') +1);
		} else {
			if (document.URL.indexOf('?') != -1) {
				docUrl = document.URL.substring(0, document.URL.lastIndexOf('?'));
			} else {
				docUrl = document.URL;
			}
		}
		//document.write(docUrl + ": " + linkUrl + "<br />");
		if (linkUrl == docUrl) {
			links[i].className = "here";
		}
	}
}

function hideHere (id, here, element) {
	// Set the container
	var container = document.getElementById(id);
	// Find the element with the right class
	var elementArray = container.getElementsByTagName(element);
	for (var i = 0; i < elementArray.length; i++) {
		// If the element has the class specified by here
		if (elementArray[i].className.indexOf(here) != -1) {
			elementArray[i].style.display = "none";
		}
	}
}

function getNextElement (element) {
	var nextSib = element.nextSibling;
  while (nextSib.nodeType != 1) {
    nextSib = nextSib.nextSibling;
  }
  return nextSib;
}

function flexNav (containerId, labelId, labelTag) {
	// Set the container
	var container = document.getElementById(containerId);
	
	// Make sure that new nav is visible
	var showNav = getNextElement(document.getElementById(labelId));
	showNav.style.display = "block";
	
	// Set up the labels
	var labels = container.getElementsByTagName(labelTag);
	for (var i = 0; i < labels.length; i++) {
		if (labels[i].id != labelId && labels[i].firstChild.nodeName != "A") {
			// Make class hidden
			labels[i].className = "hide";
			
			// Identify the label text
			var labelText = labels[i].removeChild(labels[i].lastChild);
			// Add the anchor
			var ank = labels[i].appendChild(document.createElement("a"));
			// Put the text within the anchor
			ank.appendChild(labelText);
						
			// set ank value?
			ank.className = labels[i].id;
			// Make it do this on click
			ank.onclick = function() { flexNav(containerId, this.className, labelTag); }
			// Hide the accompanying  content
			// Get the subnav element - assume it's a direct element sibling of the label
			var subNav = getNextElement(labels[i]);
			// Change display style to none
			subNav.style.display = "none";
		} else if (labels[i].id == labelId && labels[i].firstChild.nodeName == "A") {
			// Remove anchor if need to
			// Record the text
			var labelText = document.createTextNode(labels[i].firstChild.firstChild.nodeValue);
			// Remove the anchor tag
			labels[i].removeChild(labels[i].firstChild);
			// Add back in the text
			labels[i].appendChild(labelText);
			labels[i].removeAttribute("class");
			labels[i].removeAttribute("className");
			
		}
	}
}