var eOpenMenu = null;

function findPosX(obj)
{
    var curleft = -1;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else
    if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj)
{
    var ctop = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            ctop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else
    if (obj.y)
        ctop += obj.y;

    return ctop;
}

  function OpenMenu(eSrc,eMenu)
  {
//    alert (findPosX(eSrc));
    eMenu.style.left = findPosX(eSrc) ;
    
//    alert(eMenu.style.left);
    if (document.all)
        eMenu.style.top =  findPosY(eSrc) + eSrc.offsetHeight;
    else
        eMenu.style.top =  findPosY(eSrc) + eSrc.offsetHeight;
    eMenu.style.visibility = "visible";
    eOpenMenu = eMenu;
  }

  function CloseMenu(eMenu){
    if (eMenu == null)
	return;
    eMenu.style.visibility = "hidden";
    eOpenMenu = null;
  }

  function getMenu(id)
  {
    if (document.all)
        return document.all[id];
    // netscape style
    return document.getElementById(id);

  }

  // open the menu bar
  function MenuBar_over(e)
  {
      if (e == null || e.id == null)
        return;
	
      var eMenu = getMenu(e.id.replace("MenuBarItem","divMenu"));
      if (eOpenMenu && eOpenMenu != eMenu){
          CloseMenu(eOpenMenu);
      }
      if (eMenu)
      {
          OpenMenu(e,eMenu);
      }
  }

  function MenuBar_out(e)
  {
    var eTo = window.event.toElement;

    if (eOpenMenu && eTo && !eOpenMenu.contains(eTo) && "tblMenuBar" != eTo.id){
           if (eTo.id != null && eTo.id != "")
           {
                if(eTo.id.indexOf("MenuBar") == -1)
              CloseMenu(eOpenMenu)}
          }
        }

  function Menu_out()
  {
      CloseMenu(eOpenMenu);
  }

function setupEventObject(e) {
    // Map NS event object to IEs
    if (e==null || document.all != null) return // IE/Oparea returns
    window.event = e
    window.event.fromElement = e.target
    window.event.toElement = e.target
    window.event.srcElement = e.target
    window.event.x = e.x
    window.event.y = e.y
    // Route the event to the original element
    //window.event.srcElement.handleEvent(e);
}

// check if we are over a menu element
function doMouseOver (e)
{
    setupEventObject(e);

    if (window.event.srcElement.id == null)
        return;

    if ((sid = isParentId(window.event.srcElement, "MenuBarItem")) != null)
    {
        MenuBar_over(sid);
    }

}
// checks if our parent has a certain name (indexof)
function isParentId (e, name)
{
    var cur = e;
    while (cur != null)
    {
      if (cur.id != null && cur.id.indexOf(name) != -1)
      {
        return cur;
      }
      cur = cur.parentNode;
    }
    return null;
}

// check if we are out of the menu
function doMouseOut (e)
{
    setupEventObject(e);
    if (window.event.srcElement.id == null)
        return;
    if ( isParentId(window.event.srcElement, "Menu") == null)
    {
        Menu_out();
//      alert ("gotcha menu out: " + sid);
    }

}

// this function has to be executed once (f.e. onload='StartCapture()')!
function StartCapture()
{
    if (window.document.captureEvents!=null)  // NS - capture events
    {
        window.document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT)
    }
    window.document.onmouseover = doMouseOver;
    window.document.onmouseout = doMouseOut;
}

/**** HTML USAGE ******

The menu will be aligned right under the MenuBarItem. The NUM in the Menu and MenuBarItem 
must be the same in order to be displayed correct.

<div id="MenuBarItemNUM">a title</div>

<div onselectstart="return false;" style="position:absolute; top:0;" id="divMenuNUM">
all the stuff in the menu
<div>

 **********************/


/****************************flaggen script****************************************/
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
