// JavaScript Document

// DropDownMenu by Miha Hribar
// http://hribar.info

function getElementWidth(elem) {
    xPos = parseInt(elem.style.width);
    return xPos;
}

function addLoadEvent(func) {
     var oldonload = window.onload;
     if (typeof window.onload != 'function') {
          window.onload = func;
     } else {
          window.onload = function() {
                oldonload();
                func();
          }
     }
}

function prepareMenu() {
     // first lets make sure the browser understands the DOM methods we will be using
  	if (!document.getElementsByTagName) return false;
  	if (!document.getElementById) return false;
  	
  	// lets make sure the element exists
  	if (!document.getElementById("navmenu-h")) return false;
  	var menu = document.getElementById("navmenu-h");
  	
  	// for each of the li on the root level check if the element has any children
  	// if so append a function that makes the element appear when hovered over
  	var root_li = menu.getElementsByTagName("li");
  	for (var i = 0; i < root_li.length; i++) {
  	     var li = root_li[i];
  	     /*var width_element = li.getElementsByTagName("span");
  	     var our_width = 0;
  	     var tmpwidth;
  	     for (var j = 0; j < width_element.length; j++) {
  	          if(width_element[i] != null){
      	          tmpwidth = getElementWidth(width_element[i]);
                  if(our_width < tmpwidth){
                        our_width = tmpwidth;
                  }
              }
  	     }
  	     if(our_width != 0){
  	          li.style.width = our_width+'px';
  	     }*/
  	     // search for children
  	     var child_ul = li.getElementsByTagName("ul");
  	     if (child_ul.length >= 1) {
  	          // we have children - append hover function to the parent
  	          li.onmouseover = function () {
  	                if (!this.getElementsByTagName("ul")) return false;
  	                var ul = this.getElementsByTagName("ul");
  	                ul[0].style.display = "block";
  	                return true;
  	          }
  	          li.onmouseout = function () {
  	                if (!this.getElementsByTagName("ul")) return false;
  	                var ul = this.getElementsByTagName("ul");
  	                ul[0].style.display = "none";
  	                return true;
  	          }
  	     }
  	}
  	
  	return true;
}

addLoadEvent(prepareMenu);