/**************************************
/kindly taken from http://www.alistapart.com/articles/dropdowns
/adds over class to appropriate classe, as IE doesnt support li:hover
/**************************************/
IEhoverBug = function() {
  if(!browserIsIE())  //if not Internet explorer which is the only browser where the problem occurs
    return false;
    
    var menuID = "mainmenu";

  if (document.all&&document.getElementById&&document.getElementById(menuID)) {
	  navRoot = document.getElementById(menuID);
	  if (navRoot == null)
	    return false;
	  addHoverClassToLIs(navRoot,1);
	}

  function addHoverClassToLIs(navRoot, level)
  {
    if (navRoot.childNodes.length == 0)
      return false;

	  var i;  //must be declared because it is called recursive (javascript..!)
    for (i=0; i<navRoot.childNodes.length; i++) {
      node = navRoot.childNodes[i];
      if (node.nodeName=="LI" || node.nodeName=="li") {
        node.onmouseover=function() {
	        this.className+=" over"+level;
        }
        node.onmouseout=function() {
	        this.className=this.className.replace(" over"+level, "");
        }
      }

      if (node.childNodes.length > 0)
      {
        var j;
        for (j=0; j<node.childNodes.length; j++) {

          // traverse subtree
          subTree = node.childNodes[j];

          if (subTree != null)
            if (subTree.nodeName=="UL" || subTree.nodeName=="ul") {
              addHoverClassToLIs(subTree,level+1);
            }

        }
      
      }

      // traverse subtree
      //subTree = node.childNodes[1];
      //if (subTree != null)
      //  if (subTree.nodeName=="UL" || subTree.nodeName=="ul") {
      //    addHoverClassToLIs(subTree,level+1);
      //  }


    }
  }
}
if (window.attachEvent) window.attachEvent('onload', IEhoverBug);

// *************************
/** IE script to cover <select> elements with <iframe>s **/
selectHide = function()
{
  if(!browserIsIE())  //if not Internet explorer which is the only browser where the problem occurs
    return false;     //then do nothing
    
	navRoot = document.getElementById("mainmenu");
	if (navRoot == null)
	  return false;

  var ieULs = navRoot.getElementsByTagName('ul');

  for (j=0; j<ieULs.length; j++)
  {
    //insert iframe to hide select dropdowns
    ieULs[j].innerHTML = (ieULs[j].innerHTML + '<div></div><iframe style="filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)" class="iFrameHider" src="about:blank" scrolling="no" frameborder="0"></iframe>');
    var ieMat = ieULs[j].lastChild; //select iFrame in each UL
    ieMat.style.height = ieULs[j].offsetHeight + "px";
  }
}
if (window.attachEvent) window.attachEvent('onload', selectHide);

/*--------------------------------------------------*/
//Internet explorer detect
function browserIsIE()
{
  var detect = navigator.userAgent.toLowerCase();
  if (checkIt('msie'))
    return true;  
  return false;
  
  function checkIt(string)
  {
	  if ((detect.indexOf(string) + 1) && !(detect.indexOf('opera') + 1)) //hvis msie og ikke opera!!! :-)
	    return true;
	  return false;
  }
}

function AdjustWidthForMainMenu(widthModifier) {

    var parents = $('ul.mainmenu').children();
    //var parents = $("li.level2").parent(); 

    for (var j = 0; j < parents.length; j++) {
        var maxWidth = 1;
        var li = parents[j].childNodes;
        for (var i = 0; i < li.length; i++) {
            if (li[i].childNodes[0] != null) { //IE isn't accepting null values
                if (li[i].childNodes[0].lastChild != null) {
                    if (li[i].childNodes[0].lastChild.nodeValue.length > maxWidth) {
                        maxWidth = li[i].childNodes[0].lastChild.nodeValue.length;
                    }
                }
            }           
        }
        for (var i = 0; i < li.length; i++) {
            $(li[i]).css('width', maxWidth * widthModifier + 'px');
        }
    }

}

