var nav_current = null;
var nav_hide_delay = null;

function do_nothing(){}

function is_ignorable(nod){
  return (nod.nodeType == 8 || (nod.nodeType == 3 && !/[^\t\n\r ]/.test(nod.data)));
}


function node_after(sib){
  if (sib){
    while ((sib = sib.nextSibling) != null){
      if (!is_ignorable(sib)) return sib;
    }
  }
  return null;
}
function first_child(par){
  if (par && par.nodeType == 1){
    var res=par.firstChild;
    do {
      if (!is_ignorable(res)) return res;
    } while ((res = res.nextSibling) != null);
  }
  return null;
}

function init_menu(){
  var li = document.getElementById('mainnav').childNodes, i = li.length, a;
  while (i--){
    a = first_child(li.item(i));
    while (a != null && a.tagName.toLowerCase() != 'a') a = node_after(a);
    if (a != null){
      a.onmouseover = siteNavHover;
      a.onmouseout = siteNavHideCurrentSetDelay;
    }
  }
}

function siteNavHover(){
  siteNavHideCurrent();
  var ul = node_after(this);
  while (ul != null && ul.tagName.toLowerCase() != 'ul') ul = node_after(ul);
  if (ul != null){
    nav_current = ul;
    nav_current.style.visibility = 'visible';
    nav_current.onmouseover = siteNavHideClearDelay;
    nav_current.onmouseout = siteNavHideCurrentDelay;
  }
}

function siteNavHideCurrent(){
  siteNavHideClearDelay();
  if (nav_current != null){
    nav_current.style.visibility = 'hidden';
    nav_current = null;
  }
}

function siteNavHideCurrentDelay(){
  var tag = this.nodeType == 1? this : this.parentNode;
  if (tag.tagName.toLowerCase() != 'ul') return false;
  siteNavHideCurrentSetDelay();
  return true;
}

function siteNavHideCurrentSetDelay(){
  nav_hide_delay = setTimeout('siteNavHideCurrent()', 500);
}

function siteNavHideClearDelay(){
  if (nav_hide_delay != null){
    clearTimeout(nav_hide_delay);
    nav_hide_delay = null;
  }
}

window.onload = init_menu;

