function GetCurrentStyle(el, val)
{
  if(el.currentStyle)
    return el.currentStyle[val];
  return document.defaultView.getComputedStyle(el, '')[val];
}

function findPosX(obj)
{
  var curleft = 0;
  while(obj.offsetParent && GetCurrentStyle(obj, 'position')!="absolute")
  {
    curleft+=obj.offsetLeft
    obj=obj.offsetParent;
  }
  return curleft;
}

function findPosY(obj)
{
  var curtop = 0;
  while (obj.offsetParent) // && GetCurrentStyle(obj, 'position')!="absolute"
  {
    curtop+=obj.offsetTop
    obj=obj.offsetParent;
  }
  return curtop;
}

var HideQueue=[];
function HideProc()
{
  var n=HideQueue.shift();
  if(!n)
    return false;
  window.clearTimeout(n.timeout);
  n=n.node;
  if(n.x_visible==0)
    n.style.display="none";
  return true;
}

var ShowQueue=[];
function ShowProc()
{
  var n=ShowQueue.shift();
  window.clearTimeout(n.timeout);
  var node=n.node;
  if(!node)
    return false;
  if(node.x_visible==1)
  {
    var par=node.parentNode;
    if(node.style.display=='' || node.style.display=='none')
      while(HideProc());
    node.style.display="block";
    if(par.className=="rmenu"  || par.className=="lmenu")
    {
//alert('1');
      var yoffs=(findPosY(par)-par.offsetTop)-(findPosY(node)-node.offsetTop);
      if(par.className=="rmenu")
        node.style.left=findPosX(par)+par.offsetWidth-1;
      else
        node.style.left=findPosX(par)-node.offsetWidth+1;
      node.style.top=par.offsetTop+yoffs;
    }
    else
    {
      node.style.left=findPosX(par);
      node.style.top=findPosY(par)+par.offsetHeight;
    }
while(HideProc());
  }
  return true;
}

function RemoveClass(e, c)
{
  var re=new RegExp(c+' *');
  e.className=e.className.replace(re, "");
}
function AddClass(e, c)
{
  e.className+=" "+c;
}


AttachEvent(window, 'load', function()
{
  for(var i=0; i<document.all.length; ++i)
  {
    var it=document.all[i];
    if(it.className=="menu")
    {
      it.onclick=function(e)
      {
        if(!e)
          e=window.event;

        var src=e.srcElement ? e.srcElement : e.target;
        while(src && src!=this)
        {
          if(src.className=='men')
            return true;
          src=src.parentNode;
        }

        var mei;
        for(var i=0; i<this.childNodes.length; ++i)
        {
          var it=this.childNodes[i];
          if(it.className && it.className.match(/^mei/))
            mei=it;
          if(it.className=="men")
            if(it.style.display=='' || it.style.display=='none')
            {
              it.style.display="block";
              AddClass(mei, "mopen");
            }
            else
            {
              it.style.display="none";
              RemoveClass(mei, "mopen");
            }
        }
        e.cancelBubble=true;
        return false;
      }
    }
    if(it.className=="mei" || it.className=="mei curel")
    {
      it.onmouseover=function(e)
      {
        AddClass(this, "mhover");
      }
      it.onmouseout=function(e)
      {
        RemoveClass(this, "mhover");
      }
    }




    if(it.className=="rmenu" || it.className=="dmenu" || it.className=="lmenu")
    {
      it.onmouseover=function()
      {
        var zz=this.childNodes;
        for(j=0;j<zz.length;++j)
        {
          var node=zz[j];
          if(node.className=="men")
          {
            node.x_visible=1;
            ShowQueue.push({'node': node, 'timeout': window.setTimeout(ShowProc, 1)});
          }
        }
      }
      it.onmouseout=function()
      {
        var zz=this.childNodes;
        for(j=0;j<zz.length;++j)
        {
          var node=zz[j];
          if(node.className=="men")
          {
            node.x_visible=0;
            HideQueue.push({'node': node, 'timeout': window.setTimeout(HideProc, 1)});
          }
        }
      }
    }
  }

  var curel=document.getElementById('curel');
  while(curel)
  {
    if(curel.className=='men' && curel.parentNode.className=='menu')
    {
      curel.style.display="block";
      for(var n=curel.previousSibling; n; n=n.previousSibling)
        if(n.className=='mei')
          AddClass(n, 'mopen');
    }
    curel=curel.parentNode;
  }

});

AttachEvent(document, 'click', function()
{
  while(HideProc());
});

