function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
	window.onload = function() {
		oldonload();
		func();
		}
	}
}

function addClass(element,value) {
	if (!element.className) { 
			element.className = value; 
		} else { 
			newClassName = element.className; 
			newClassName+= " "; 
			newClassName+= value; 
			element.className = newClassName; 
	} 
} 

function HasClassName(objElement, strClass)
{
// if there is a class
if ( objElement.className )
  {

  // the classes are just a space separated list, so first get the list
  var arrList = objElement.className.split(' ');

  // get uppercase class for comparison purposes
  var strClassUpper = strClass.toUpperCase();

  // find all instances and remove them
  for ( var i = 0; i < arrList.length; i++ )
     {

     // if class found
     if ( arrList[i].toUpperCase() == strClassUpper )
        {

        // we found it
        return true;

        }

     }

  }

// if we got here then the class name is not there
return false;

}

function getElementsByClassName(oElm, strTagName, oClassNames){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var arrRegExpClassNames = new Array();
	if(typeof oClassNames == "object"){
		for(var i=0; i<oClassNames.length; i++){
			arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
		}
	}
	else{
		arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
	}
	var oElement;
	var bMatchesAll;
	for(var j=0; j<arrElements.length; j++){
		oElement = arrElements[j];
		bMatchesAll = true;
		for(var k=0; k<arrRegExpClassNames.length; k++){
			if(!arrRegExpClassNames[k].test(oElement.className)){
				bMatchesAll = false;
				break;
			}
		}
		if(bMatchesAll){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function slideoutSection(id){
	$("div.section").each(function(i){
		if ($(this).attr("id") == id){
			$(this).slideDown("slow");
		} else {
			$(this).slideUp("slow");
		}
	});
}

function showSection(id){
	var divs = document.getElementsByTagName("div");
	for (var i=0;i<divs.length;i++){
		if (divs[i].className.indexOf("section") == -1) continue;
		if (divs[i].getAttribute("id") != id){
			divs[i].style.display = "none";
		} else {
			divs[i].style.display = "block";
		}
	}
}

function highlightSubNav(whichLink){
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("subNav")) return false;
	var nav = document.getElementById("subNav");
	var links = nav.getElementsByTagName("a");
	for (var i=0;i<links.length;i++){
		/* check if link[i] is the clicked link */
		if(links[i].getAttribute("href") == whichLink){
			links[i].style.background = "#2a303c url(../images/subNav_arrow.png) no-repeat 5px center";
			links[i].style.textIndent = "25px";
			links[i].style.cursor = "default";
			links[i].style.color = "#fff";
		} else {
			links[i].style.color = "#2a303c";
			links[i].style.textIndent = "20px";
			links[i].style.cursor = "pointer";
			/* check if the list item is the last one */
			if(links[i].parentNode.className.indexOf("lastItem") == -1){
				/* add the dotted line image */
				links[i].style.background = "#b3b3b3 url(../images/subNav_dot_divider.png) no-repeat center bottom";	
			}else{
				/* remove the dotted line image */
				links[i].style.background = "#b3b3b3";						
 			}
		}
	}
}


function prepareSubNav(){
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("subNav")) return false;
	var nav = document.getElementById("subNav");
	var links = nav.getElementsByTagName("a");
	for (var i=0;i<links.length;i++){
		var sectionId = links[i].getAttribute("href").split("#")[1];
		if (!document.getElementById(sectionId)) continue;
		document.getElementById(sectionId).style.display = "none";				
		links[i].destination = sectionId;
		links[i].onclick= function(){
			slideoutSection(this.destination);
			highlightSubNav(this.getAttribute("href"));
			return false;
		}		
	}	
	var firstItem = links[0].getAttribute("href").split("#")[1];
	document.getElementById(firstItem).style.display = "block";

}