
var eOpenMenu = null;
var eRightOpenMenu = null;

function findPosX(obj)
{
    var curleft = 0;
    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,iMenu)
  {
    eMenu.style.left = findPosX(eSrc) + 3;
    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 OpenMenuRight(eSrc,eMenu,iMenu)
  {
  	var addX = 0;
  	if (document.all)
  		addX=8;
    eMenu.style.left = findPosX(eSrc) + eSrc.offsetWidth + addX;
    eMenu.style.top =  findPosY(eSrc) - 5;
    eMenu.style.visibility = "visible";
    eRightOpenMenu = eMenu;
			      
  }
    

  function CloseMenu(eMenu){
    if (eMenu == null)
		return;
    eMenu.style.visibility = "hidden";
    if (eMenu == eOpenMenu)
		eOpenMenu = null;    
    else
        eRightOpenMenu = 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 = null;
      var e2Menu = null;
      if (e.id.indexOf("MenuBarItem") != -1)
      {
	      eMenu = getMenu(e.id.replace("MenuBarItem","divMenu"));
      }
      
      if (e.id.indexOf("MenuRightBarItem") != -1)
      {
    	e2Menu = getMenu(e.id.replace("MenuRightBarItem","divMenu"));
      }
      
      if (eOpenMenu && eMenu != eOpenMenu && eMenu != null)
      {
          CloseMenu(eOpenMenu);

		  if (eRightOpenMenu && eMenu2 != eOpenMenu && e2Menu != null)
		  {
			  CloseMenu(eRightOpenMenu);
		  }
      }

      if (eRightOpenMenu && eRightOpenMenu != e2Menu) {
	   	CloseMenu(eRightOpenMenu);
      }
      
      if (eMenu != null)
      {
      	if (eOpenMenu)
		 CloseMenu (eOpenMenu);
        OpenMenu(e,eMenu);
      }
      
      if (e2Menu != null)
      {
         OpenMenuRight(e,e2Menu);
      }
  }

  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("Menu") == -1)
		{
            		CloseMenu(eOpenMenu);
	      		CloseMenu(eRightOpenMenu);
	     	}
        }
   }

  function Menu_out()
  {
      CloseMenu(eOpenMenu);
      CloseMenu(eRightOpenMenu);
  }

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);
    }
    
    if ((sid = isParentId(window.event.srcElement, "MenuRightBarItem")) != 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, "MenuBarItem") == null &&
	    isParentId(window.event.srcElement, "MenuRightBarItem") == null &&
	    window.event.srcElement.id != "context" &&
	    window.event.srcElement.id.indexOf("enu") == -1 &&
	    window.event.srcElement.parentNode.id.indexOf("enu") == -1
    )
    {
        Menu_out();
    }

}


// 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>

For a submenu (only RIGHT!!!)
modify the div for the menubar:
<div onmouseover="if (document.all) MenuBar_over();" ONMOUSEOUT="if (document.all) MenuBar_out();" onselectstart="return false;"  style="position:absolute; top:0; visibility: hidden;" id="divMenuNUM">
</div>

edit the item:
<div id="MenuRightBarItem1">anyitem</div>

<div onselectstart="return false;" style="position:absolute; top:0; visibility: hidden;" id="divMenu2">
everything in the submenu
</div>
 **********************/
