/* COMPATIBILITY */
function xDef(a) {
  if (a==null)
    return false;
  return (typeof(a)!='undefined');
}

function getBodyScrollTop() {
  return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function getBodyScrollLeft() {
  return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
}

function getWindowHeight() {
  var a, b, c;
  a = document.body.clientHeight;
  b = c = 999999999;
  if (xDef(document.documentElement) && document.documentElement.clientHeight)
    b = document.documentElement.clientHeight;
  if (xDef(window.innerHeight))
    c = window.innerHeight;
  return Math.min(a,b,c);
}

function getWindowWidth() {
  var a, b, c;
  a = document.body.clientWidth;
  b = c = 999999999;
  if (xDef(document.documentElement) && document.documentElement.clientWidth)
    b = document.documentElement.clientWidth;
  if (xDef(window.innerWidth))
    c = window.innerWidth;
  return Math.min(a,b,c);
}

function recurse_register(el, p) {
  var e, i;
  el.onmouseover = menu_over;
  el.onmouseout = menu_out;
  if (e = document.getElementById(p)) {
    e.onmouseover = menu_over;
    e.onmouseout = menu_out;
  }
  i = 1;
  while (e = document.getElementById(p+i)) {
    recurse_register(e, (p+i) * 100);
    i++;
  };
}

function GetTopLeft(elm) {
  var x, y, e;
  x = elm.offsetLeft;
  y = elm.offsetTop;
  e = elm.offsetParent;
  while(e) {
    x += e.offsetLeft;
    y += e.offsetTop;
    e = e.offsetParent;
  }
  return {Top:y, Left:x};
}


function menu_over(ev) {
  if (menu_timer_id) {
    clearTimeout(menu_timer_id);
    menu_timer_id = 0;
  }
  if (typeof(ev)=='undefined') ev = window.event;
  menu_hide_except(parseInt(this.id / 100), this.id);
  if (this.className=='item')
    this.className = 'selected';
  var e, cid = parseInt(this.id);
  if (e = document.getElementById(cid*100)) { // submenu
    e.style.display = 'block';
    var TL = GetTopLeft(this);
    var pl = TL.Left;
    var pt = TL.Top;
    var pr = pl + this.offsetWidth;
    var pb = pt + this.offsetHeight;
    var ww = getWindowWidth();
    var wh = getWindowHeight();
    var bst = getBodyScrollTop();
    var bsl = getBodyScrollLeft();
    var x, y;
    if (cid < 100) {// top level
      x = Math.min(pl, ww + bsl - e.offsetWidth - 4); // x - very good  4 is IE hack ???
      if (pb + e.offsetHeight < wh + bst)   // y - very good
        y = pb;
      else if (pt - e.offsetHeight > bst)
        y = pt - e.offsetHeight;
      else
        y = wh - e.offsetHeight + bst;
    } else { // any low level
      if (pr + e.offsetWidth < ww + bsl)
        x = pr - 4;                 // IE hack (IE не учитывает border)
      else if (pl - e.offsetWidth > bsl)
        x = pl - e.offsetWidth + 2; // IE hack (IE не учитывает border)
      else
        x = ww - e.offsetWidth;          // x - very good
      if (pt + e.offsetHeight < wh + bst) // y - very good
        y = pt;
      else if (pb - e.offsetHeight > bst)
        y = pb - e.offsetHeight;
      else
        y = wh - e.offsetHeight + bst;
    }
    if (e.offsetParent) {
      TL = GetTopLeft(e.offsetParent);
      x -= TL.Left;
      y -= TL.Top;
    }
    e.style.left = String(x) + 'px';
    e.style.top = String(y) + 'px';
  } // submenu
  var par = this;
  while ((e = par) && (cid >= 100)) {
    cid = parseInt(cid / 100);
    par = document.getElementById(cid);
    e.style.display = 'block';
  };
  if (typeof(ev.stopPropagation) != 'undefined')
   ev.stopPropagation();
  ev.cancelBubble = true;
  return true;
}


function menu_out(ev) {
  if (this.className=='selected')
    this.className = 'item';
  if (menu_timer_id) {
    clearTimeout(menu_timer_id);
    menu_timer_id = 0;
  }
  menu_timer_id = setTimeout('menu_hide_except(0,0)', menu_timeout);
  if (typeof(ev)=='undefined') ev = window.event;
  if (typeof(ev.stopPropagation) != 'undefined')
   ev.stopPropagation();
  ev.cancelBubble = true;
  return true;
}


function menu_hide_except(parent_id, except_id) {
  var e,i,p;
  p = parent_id * 100;
  i = 1;
  while (e = document.getElementById(p+i)) {
    if (p+i != except_id)
      menu_hide_except(p+i, 0);
    i++;
  };
  if (!except_id)
    if (e = document.getElementById(p))
      e.style.display = 'none';
}


if (document.register_menu)
  document.register_menu();
