/*
 * Source : http://simonwillison.net/2004/May/26/addLoadEvent/
 *
 */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

if (document.getElementById) {
  var is_IE = false;

  if (document.all && navigator.appVersion.match(/MSIE/)) {
    /MSIE ([^;]+);/.exec(navigator.appVersion);
    is_IE = true;
    IEVersion = parseFloat(RegExp.$1);
  }
  function hover_init() {
    var mainmenu = document.getElementById('mainmenu');
    if (mainmenu && mainmenu.childNodes) {
      for (i = 0 ; i < mainmenu.childNodes.length ; i++) {
        child = mainmenu.childNodes[i];
        if (child.nodeType == 1 && child.nodeName == 'LI') {
          child.onmouseout = function () {
            if (this.className.match(/\s*hover\s*/)) {
              this.className = this.className.replace(/\s*hover\s*/, '');
              var submenu = this.getElementsByTagName('ul');
              if (submenu.length > 0)
                submenu[0].style.left = '-999em';
            }
          } // li.onmouseout()
          child.onmouseover = function () {
            if (! this.className.match(/\s*hover\s*/)) {
              this.className = this.className + ' hover';
              var submenu = this.getElementsByTagName('ul');
              if (submenu.length > 0) {
                var left = 0;
                if (is_IE && IEVersion < 8) {
                  var el = this;
                  do {
                    left += el.offsetLeft;
                    el = el.offsetParent;
                  } while(el.offsetParent.nodeName != 'HTML');
                } else {
                  left = this.offsetLeft;
                }
                left = left + (this.offsetWidth - submenu[0].offsetWidth) / 2;
                submenu[0].style.left = left+ 'px';
              }
            }
          } // li.onmouseover()
        } // if nodeName == <li>
      } // foreach mainmenu.childNodes
    } // if mainmenu && mainmenu.childNodes

    var buttons = document.getElementsByTagName('button');
    if (buttons) {
      for(i = 0 ; i < buttons.length ; i++) {
        buttons[i].onmouseout = function() {
          if(this.className.match(/\s*button_submit_hover\s*/))
            this.className = this.className.replace(/\s*button_submit_hover\s*/, ' button_submit ')
        } // button.onmouseout()
        buttons[i].onmouseover = function() {
          if(this.className.match(/\s*button_submit\s*/))
            this.className = this.className.replace(/\s*button_submit\s*/, ' button_submit_hover ')
        } // button.onmouseover()
      } // foreach buttons
    } // if buttons
  }
  addLoadEvent (hover_init);
}

