addLoadEvent(prepare_topnav);

function prepare_topnav() {
	if (!document.getElementById) return false;
	if (!document.getElementsByTagName) return false;

	// Prepare the behavior of the top navigation.
	var header = document.getElementById("header_div");
	var menu = header.getElementsByTagName("ul");
	for (var i=0; i<menu.length; i++) {
		if (menu[i].className == 'header_nav') {

			// Make the dropdowns happen.
			var listItem = menu[i].getElementsByTagName('li');

			for (var j=0; j<listItem.length; j++) {
				listItem[j].onmouseover = function() {
					var liClass = this.className;
					var navBox = document.getElementById(liClass);
					navBox.style.display = "block";
				}
				listItem[j].onmouseout = function() {
					var liClass = this.className;
					var navBox = document.getElementById(liClass);
					navBox.style.display = "none";
				}
				listItem[j].onclick = function() {
					return false;
				}

				// Keep the links raised while using the dropdown...
			}
		}
	}
	
	// Prepare the behavior of the .nav_ext boxes.
	var navBoxes = document.getElementsByTagName('dl');
	for (var i=0; i<navBoxes.length; i++) {
		if (navBoxes[i].className == 'nav_ext') {
			navBoxes[i].onmouseover = function() {
				this.style.display = "block";
				hovering_link(this.getAttribute("id"));
			}
			navBoxes[i].onmouseout = function() {
				this.style.display = "none";
				hovering_link(this.getAttribute("id"));
			}
		}
	}
}

function hovering_link(menuItem) {
	var top_nav = document.getElementById("header_div");
	var top_menu = top_nav.getElementsByTagName("ul");
	for (var h=0; h<top_menu.length; h++) {
		if (top_menu[h].className == "header_nav") {
			var items = top_menu[h].getElementsByTagName("li");
			for (var f=0; f<items.length; f++) {
				if (items[f].className == menuItem) {
					var curClass = items[f].firstChild.className;
					curClass = (curClass == "top_nav") ? "top_nav_hover" : "top_nav";
					items[f].firstChild.className = curClass;
				}
			}
		}
	}
}

//check if the previous sibling node is an element node
function get_previoussibling(n) {
	x=n.previousSibling;
	while (x.nodeType!=1) {
		x=x.previousSibling;
	}
	return x;
}

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

