function addEvent(elm, evType, fn, useCapture) {
  // cross-browser event handling for IE5+, NS6 and Mozilla 
  // By Scott Andrew 
  if (elm.addEventListener) { 
    elm.addEventListener(evType, fn, useCapture); 
    return true; 
  } else if (elm.attachEvent) { 
    var r = elm.attachEvent('on' + evType, fn); 
    return r; 
  } else {
    elm['on' + evType] = fn;
  }
}
// This will set the target to _blank for any link whose 'rel' attribute is set to 'external'.
function setLinkTargetToBlank() {
	if (!document.getElementsByTagName) return;
	var all_links = document.getElementsByTagName('a');
	var i = 0;
	for (i=0; i<all_links.length; i++) {
		if (all_links[i].getAttribute('rel') == 'external') {
			all_links[i].setAttribute('target', '_blank');
		}
	}
}
function handleNav(e) {
  //First we get the element
  var el;
  if (window.event && window.event.srcElement) {
  	el = window.event.srcElement;
  } else {
    if (e && e.target) el = e.target;
  }
  
  //If we don't get something, may as well stop here to aivod erorrs.
  if (!el) return;
  
  //Some browsers consider the text node inside the link to be the element.  If so, we go to the parent.
  while (el.nodeName.toLowerCase() != 'a' && el.nodeName.toLowerCase() != 'body') el = el.parentNode;
  if (el.nodeName.toLowerCase() == 'body') return;

  //Now we know that we have the link, but what we really want is the list-item.
  el = el.parentNode;
  
  //Now we need to know if this list-item has a sub list.
  var has_sub_nav = el.getElementsByTagName('ul');
  
  //If so, we display that element. 
  if (has_sub_nav.length > 0) {
	has_sub_nav[0].style.display = 'block';

	//And cancel the link action.
    if (window.event) {
      window.event.cancelBubble = true;
      window.event.returnValue = false;
    }
    if (e && e.stopPropagation && e.preventDefault) {
      e.stopPropagation();
      e.preventDefault();
    }
  }
}
function styleNav() {
	
	//html ID's can't start with a number so we handle that exception here.
	if (nav == '4H') nav = 'fourh';
	//In order for the page headers to be capitalized, the p variable uses caps.  
	//But, javascript is case sensitive and we shouldn't have caps in our ID names, so we fix that here.
	nav = nav.toLowerCase();
	var myNav;
	//The main nav elements don't change so we have to check to make sure the id exists.
	if (myNav = document.getElementById(nav)) {
		//If so, add the additional class name.  I always add a space in case a class name has been added.
		myNav.className += ' selected';
	}
	//I would just do this declaration in the IF but IE throws a tantrum.
	var myContentNav;
	if (myContentNav = document.getElementById(contentNav)) {
		//myContentNav = document.getElementById(contentNav)
		//The first element in the content nav already has a class so we 
		//have to account for that by adding a space before the className.
		myContentNav.className += ' selected';
	}
	//For this one we also check to make sure that the sub and page variables
	//aren't the same so pages with tertiary navs don't get hit twice.  Not that it really matters.
	var mySubNav = document.getElementById(subNav);
	if (mySubNav && (mySubNav != myNav)) {
		//The first element in the content subNav already has a class so we 
		//have to account for that by adding a space before the className.
		mySubNav.className += ' selected';
	}
	
	//This part of the styling is the insertion of the image in the left nav.
	//We detect to see if myNav exists and if the browser can create/add content.
	if (myNav && document.createElement && document.body.insertBefore) {
		//Need to make sure we are in the left nav.  To do that we need the UL element.
		//But, if we are on a sub page, we need the ID of the contentNav to push us back up.
		var myUL; 
		if (myContentNav) {			
			myUL = myContentNav.parentNode;
			myNav = myContentNav;
		} else {
			myUL = myNav.parentNode;
		} 
		
		do {
			myUL = myUL.parentNode;
		} while (myUL.nodeName != 'UL') //This has to be CAPS!
		
		//This just adds the the little line to the left nav.
		if (myUL.className == "showNav") {
			//Create an image.
			var myImg = document.createElement('img');
			//Set the source.
			myImg.src = 'images/left_nav_selected.gif';
			//All the positioning is taken care of with CSS.
			myImg.className = 'selected';
			//To be W3C compliant.
			myImg.alt = '';	
			//Won't work if the image comes after the link, hence, insertBefore.
			myNav.parentNode.insertBefore(myImg, myNav);	
		}
	}
}

// The part that installs the listeners and runs the styling function
function init() {
  //Feature detection.  No point doing anything if we don't have this.
  if (!document.getElementById) return;
  
  //This is the only part that has to match the html.  
  //For this site the ID of the nav in question is 'left_nav'.
  var nav = document.getElementById('left_nav');
  
  //Get all the links in the nav.
  var nav_links = nav.getElementsByTagName('a');

  //And add the listeners.
  for (var i = 0; i < nav_links.length; i++) {
  	addEvent(nav_links[i], 'click', handleNav, false);
  }
  
  //This is just visual not functional.
  styleNav();
  
  setLinkTargetToBlank()
}

addEvent(window, 'load', init, false);
