function getHeight()
{
  if( typeof( window.innerHeight ) == 'number' )
    //Non-IE
    return window.innerHeight;
  else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.offsetHeight ) ) 
    //IE 6+ in 'standards compliant mode'
    return document.documentElement.offsetHeight;
  else
    //IE 4 compatible
    return document.body.offsetHeight;
}

function getTopBottom(el)
{
  var body = document.body;
  var docElem = document.documentElement;
  var scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop;
  var clientTop = docElem.clientTop || body.clientTop || 0;
  var offset = scrollTop - clientTop;

  if (el.getBoundingClientRect)
  {
    var r = el.getBoundingClientRect();
    return {top: Math.round(r.top), bottom: Math.round(r.bottom), offset: offset};
  }
  else
  {
    var top=0;
    var height = el.offsetHeight;
    while(el) {
        top = top + parseInt(el.offsetTop);
        el = el.offsetParent;
    }
    return {top: top - offset, bottom: top+height - offset, offset: offset};
  }
}

function adjustSubmenu(el)
{
  var sub = el.childNodes;
  var winHeight = getHeight();
  var offset;
  var hack = 30;
  for (var i=0; i<sub.length; i++) if ((sub[i].nodeType==1) && (sub[i].tagName=='UL'))
  {
    sub[i].style.top = 'auto';  //сначала надо вернуть на место, а потом рассчитать свободное пространство
    var tb = getTopBottom(sub[i]);
    var parent = sub[i].offsetParent;
    var parentOffset = 0;
    if (tb.bottom > winHeight)
    //не умещается снизу
    {
      if (sub[i].offsetHeight - el.offsetHeight <= tb.top)
      // полно места сверху, размещаем по baseline родителя
      {
        offset = sub[i].offsetHeight - el.offsetHeight;
        var ptb = getTopBottom(el);
        parentOffset = ptb.bottom - winHeight;  // если родитель заходит за нижний край экрана, надо ещё чуточку приподнять...
        if (parentOffset < 0) parentOffset = 0;
        if (parent.tagName=='UL') tb.offset = 0;  // прокрутку имеет смысл учитывать только при отсчёте относительно BODY
        sub[i].style.top = tb.top + tb.offset + hack - getTopBottom(parent).top - offset - parentOffset + 'px';
      }
      else
      // размещаем посередине экрана
      {
        if (parent.tagName=='UL') parentOffset = getTopBottom(parent).top; else parentOffset = - tb.offset;
        sub[i].style.top = Math.round((winHeight - sub[i].offsetHeight) / 2) + hack - parentOffset + 'px';
      }
    }
    else if (tb.top < 0)
    //не умещается сверху
    {
      if (sub[i].offsetHeight > winHeight - tb.top)
      {
        if (parent.tagName=='UL') parentOffset = getTopBottom(parent).top; else parentOffset = - tb.offset;
        sub[i].style.top = Math.round((winHeight - sub[i].offsetHeight) / 2) + hack - parentOffset + 'px';
      }
    }
    var lisub = sub[i].childNodes;
    for (var j=0; j<lisub.length; j++) if ((lisub[j].nodeType==1) && (lisub[j].tagName=='LI'))
      adjustSubmenu(lisub[j]);
  }
}

var jsvhover = function()
{
  var nodes = document.getElementById("main_menu").getElementsByTagName("li");
  for (var i=0; i<nodes.length; i++) 
  {
    nodes[i].onmouseover = function()
    {
      this.className = "jsvhover";
//      this.className += " jsvhover";
      adjustSubmenu(this);
    }
    
    nodes[i].onmouseout = function()
    {
       this.className = this.className.replace("jsvhover", "");
//       this.className = this.className.replace(new RegExp(" jsvhover\\b"), "");
    }
  }
}

if (window.addEventListener)
  window.addEventListener("load", jsvhover, false);
else if (window.attachEvent) 
  window.attachEvent("onload", jsvhover); 
else if (document.getElementById)
  window.onload=jsvhover;

