// JavaScript Document

// Add a function after the page loads 
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

// Add a function after the page refreshes 
function addResizeEvent(func) {
  var oldresize = window.onresize;
  if (typeof window.onresize != 'function') {
    window.onresize = func;
  } else {
    window.onresize = function() {
      oldonresize();
      func();
    }
  }
}

// Add a class to an element
function addClass(element,value) {
  if (!element.className) {
    element.className = value;
  } else {
    element.className+= " ";
    element.className+= value;
  }
}

// Insert an new element after a specific element 
function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}

// add JavaScript events to the main navigation to display the drop down menu
function addDropDownMenu()
{
	
	// Make sure the browser understands the DOM methods
	if (!document.getElementById) return false;
	if (!document.getElementsByTagName) return false;
	//make sure the  element exists
	
	if (!document.getElementById("navigation"))
	{
		return false;
	}
	//get main navigation list
	
	var mainNavigation = document.getElementById("navigation");
	var navList = mainNavigation.getElementsByTagName("li");
	
	
	//loop through the list to add events
	for (var i= 0; i < navList.length; i ++)
	{
		//var currentList = navList[i]
		var listLink = 	navList[i].firstChild;
		//if (listLink.nodeName == "A")
	//	{
		addDropDownEvents(listLink,navList[i].getAttribute("id"));
	//	}
	}
	
}

function addDropDownEvents(element, sectionID)
{
	element.onmouseover = function(){cswmShow('Group_'+sectionID, sectionID, 'below');}
	element.onmouseout = function(){cswmHide();}
	element.onfocus = function(){cswmShow('Group_'+sectionID, sectionID, 'below');}
	element.onblur = function(){cswmHide();}
}

function swapImages(el)
{
	
  	var imLo = el.getAttribute("srcover");
  	var imHi = el.getAttribute("src");
	el.setAttribute("src",imLo);
	el.setAttribute("srcover",imHi);
	//return true;
}

function addImageMapEvents(elementID, img)
{
	// Make sure the browser understands the DOM methods
	if (!document.getElementById) return false;
	//make sure the  element exists
	if (!document.getElementById(elementID))return false;
	if (!document.getElementById(img))return false;
	//get element with elementID
	var element = document.getElementById(elementID);
	var imgMap = document.getElementById(img);
	element.onmouseover = function(){swapImages(imgMap);}
	element.onmouseout = function(){swapImages(imgMap);}
	

}

function addSwapImagesBanner()
{
	addImageMapEvents("map_homepage", "img_header");
	
}

