// JavaScript Document
/*
USAGE:
addLoadEvent(function_name); 

Where 'function_name' is the name of the function you wish to run onload

DESCRIPTION:
Adds a function to the onload call dynamically.
*/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

/*
USAGE:
toggleDisplay(element_id, [new_element_state(show|hide)]);

Where 'element_id' is the id of the element you wish to change.
Optional: 'new_element_state' is the new state of the element.

DESCRIPTION:
This function sets the class of the selected element to "show" or "hide" depending on current state of the element OR the state given by 'new_element_state'.
The primary purpose of this function is to show or hide elements on a web page.
*/
function toggleDisplay (elementId, elementState){
	var element = document.getElementById(elementId);
	if(elementState == "show" || elementState == "hide"){ 
		switch(elementState){
			case 'show':
				element.className = "show";
			break;
			case 'hide':
			default:
				element.className = "hide";
			break;
		}
	} else {
		switch(element.className.toLowerCase()){
			case 'hide':
				element.className = "show";
			break;
			case 'show':
			default:
				element.className = "hide";
			break;
		}
	}
}

/*
USAGE:
externalLinks(); OR
addLoadEvent(externalLinks); // USING THE LOADEVENT FUNCTION ABOVE!

DESCRIPTION:
This function sets the target attribute for a link via the DOM as it is no longer valid to use the target="" attribute.
To enable, just put rel="external" in the anchor instead, and run this function on body load.

This function
*/

function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external"){
			anchor.target="_blank";
		}
		if (anchor.getAttribute("href") && anchor.getAttribute("class") == "pdf"){
			anchor.target="_blank";
		}
	}
}

/*
USAGE:
sfHover(element_id);

Where 'element_id' is the id of the element you wish to enable the hover on.

DESCRIPTION:
This function sets the class of the selected element to "sfHover" whilst the mouse cursor is over the element.
This enables IE and other older browsers to have hover effects on LI and other non-anchor elements.
*/
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
addLoadEvent(externalLinks);