// addLoadEvent function as seen at http://simon.incutio.com/archive/2004/05/26/addLoadEvent
// allows you to stack functions and apply them to the onload event and also means you 
// can abstract onload functions from the html
// I've added an argument 'arg' for onload functions with an argument
// Could probably be better implemented with 'arguments[]'


function addLoadEvent(func,arg) {
	
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func(arg);
    }
  }
}
 
/*
	Copyright Robert Nyman, http://www.robertnyman.com
	Free to use if this text is included
*/
// ---
function $(strId){
	return document.getElementById(strId);
}
// ---
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];		
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}	
	}
	return (arrReturnElements)
}
// ---
function addClassName(oElm, strClassName){
	var strCurrentClass = oElm.className;
	if(!new RegExp(strClassName, "i").test(strCurrentClass)){
		oElm.className = strCurrentClass + ((strCurrentClass.length > 0)? " " : "") + strClassName;
	}
}
// ---
function removeClassName(oElm, strClassName){
	var oClassToRemove = new RegExp((strClassName + "\s?"), "i");
	oElm.className = oElm.className.replace(oClassToRemove, "").replace(/^\s?|\s?$/g, "");
}
// ---







function setParentArrows() {
 		if(!document.getElementById("nav")) return false;
 		var nav = $('nav');
		// get all the list item element
		var inner = nav.getElementsByTagName('LI');
 		
		var e = [];
		
		var i=0;
		var k=inner.length;
		
		// get rid of all the elements that are a direct child of the outer ul
		while(i<k)
		{
 			(inner[i].parentNode.id != 'nav') ? e.push(inner[i]):null;
			i++;
		}
		
		var j=0;
		var k=e.length;
		
		// if the li has more than two children and the third is Ul then give the li a class 'parent' 
		// and put span in the a element for the arrow
		while(j<k)
		{
  			if(e[j].childNodes.length > 2)
  			{
  				(e[j].childNodes[2].nodeName == 'UL') ? addClassName(e[j],'parent'):null;
				var arrSpan = document.createElement("span");
 				e[j].childNodes[0].appendChild(arrSpan);
   			}
 			j++;
		}
 }

addLoadEvent(setParentArrows);

 

// for drop down navigation - fix for IE which doesn't allow anything:hover
// this adds a class "sfhover" to the li tag onmouseover
function sfHover() {

		if(!document.getElementById("nav")) return false;

		var topItem;
         var sfEls = document.getElementById("nav").getElementsByTagName("LI");
        for (var i=0; i<sfEls.length; i++) {
                sfEls[i].onmouseover=function() {
                        this.className+=" sfhover";
                        
                         
        				var topLevel = this.parentNode;
                        if (topLevel.getAttribute("id") == "nav") {
                        this.className += " currentSection";
                          topItem = this;
                         };
                        
                  }
                sfEls[i].onmouseout=function() {
                        this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
						if(topItem)
						{
                        topItem.className=this.className.replace(new RegExp(" currentSection\\b"), "");
 						}
                         
                }
        }
}

 if (window.attachEvent) window.attachEvent("onload", sfHover);
