﻿// JScript File

// Global menu element handle:
///////////////////////////////////////
var LiveMenu = null;

// Global menu timeout handle:
///////////////////////////////////////
var Timeout_ID = null;

// Opens or keeps open a given menu
// and shuts any previous menu:
///////////////////////////////////////
function menuOver(MenuID){
  // If DOM1 supported and element exists ...
  if((document.getElementById)&&(document.getElementById(MenuID)!=null)){
    // If this menu is already open ...
    if(LiveMenu==document.getElementById(MenuID)){
      // Do not close it
      clearTimeout(Timeout_ID);
    }
    // Another might still be open ...
    else{
      // If another menu is open ...
      if(LiveMenu!=null){
        // Do not wait, shut it now
        clearTimeout(Timeout_ID);
        hideNow();
      }
    }
    // This is the new 'live' menu, make it visible
    LiveMenu = document.getElementById(MenuID);
    // LiveMenu.style.visibility is
    // initially empty in IE5 until
    // it is assigned by these
    // functions, so must check that
    // it's not null before proceeding...
    if((LiveMenu.style)&&(LiveMenu.style.visibility!=null)){
      LiveMenu.style.visibility = 'visible';
    }
  }
}

// Stops menu links from opening menu
// onmouseover when shut to
// workaround mouse events which are
// not hidden by z-index in Opera 4!
///////////////////////////////////////
function stayOpen(MenuID){
  // If menuOver has not been called or the menu is hidden, do nothing
  if((LiveMenu==null)||((LiveMenu.style)&&(LiveMenu.style.visibility)&&(LiveMenu.style.visibility=='hidden')))return;
  else menuOver(MenuID);
}

// Shuts a given menu in 250
// milliseconds, unless timeout is
// cleared by menuOver()
///////////////////////////////////////
function menuOut(MenuID){
  // If DOM1 supported and a menu is open ...
  if((document.getElementById)&&(document.getElementById(MenuID)!=null)){
    // Get the current live menu
    LiveMenu = document.getElementById(MenuID);
    // Prepare to shut it in 250 milliseconds
    Timeout_ID = window.setTimeout('hideNow();',250);
  }
}

// Called by menu handlers to shut
// previous menu immediately
///////////////////////////////////////
function hideNow(){
  if((LiveMenu.style)&&(LiveMenu.style.visibility)){
    LiveMenu.style.visibility = 'hidden';
  }
}




window.onload = setMenusPosition;
function setMenusPosition()
{
    var i;
    for (i = 1; i < 20; i++)
    {
        if (document.getElementById('topmenu' + i) && document.getElementById('menu' + i)) 
        {
            var refPopupMenu = document.getElementById('menu' + i);
            var refTopMenu = document.getElementById('topmenu' + i);
            var leftPos = findPosLeft(refTopMenu);
            var leftPosContainer = findPosLeft(document.getElementById('container'));
            
            if (leftPos + refPopupMenu.offsetWidth > 984 + leftPosContainer)
            {
                refPopupMenu.style.left = '0px';
                var posRight = leftPos + refTopMenu.offsetWidth - refPopupMenu.offsetWidth - leftPosContainer;
                
                if (posRight < 0)
                    posRight = 0;
                    
                refPopupMenu.style.left = posRight + 'px';
                //alert('posRight: ' + posRight + '; leftPosContainer: ' + leftPosContainer);
            }
        }
    }
}

function findPosLeft(obj)
{
    var curleft = 0;
    do
    {
	    curleft += obj.offsetLeft;
    } while (obj = obj.offsetParent);
	return curleft;
}
