/*************************************************************************
  This code is from Dynamic Web Coding at http://www.dyn-web.com/
  Copyright 2003 by Sharon Paine 
  See Terms of Use at http://www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/

var currentLayer = 0;
var openedlayers = new Object();

var menuLayers = {
  timer: null,
  activeMenuID: null,
  offX: 0,   // horizontal offset 
  offY: -2,   // vertical offset
  fixX: null,
  fixY: null,
  
  show: function(id, e, x, y, a) { //HKS added a for alignment 1=left
    this.hideall();
  	currentLayer=1;
	openedlayers[id] = 1;
    var mnu = document.getElementById? document.getElementById(id): null;
    if (!mnu) return;
    this.activeMenuID = id;
    if ( mnu.onmouseout == null ) mnu.onmouseout = this.mouseoutCheck;
    if ( mnu.onmouseover == null ) mnu.onmouseover = this.clearTimer;
    viewport.getAll();
	this.fixX = x;
	this.fixY = y;
    this.position(mnu,e, a);
  },
  
  hide: function() {
    this.clearTimer();
    if (this.activeMenuID && document.getElementById && currentLayer==1) 
      this.timer = setTimeout("currentLayer=0;menuLayers.hideall();", 200);
  },
  
  hideall: function() {
    this.clearTimer();
    for (var o in openedlayers)
		document.getElementById(o).style.visibility = "hidden";
  },
  
  position: function(mnu, e, a) { //HKS added a for alignment 1=left
    var x = (this.fixX)?this.fixX:(e.pageX ? e.pageX: e.clientX + viewport.scrollX);
    var y = (this.fixY)?this.fixY:(e.pageY ? e.pageY: e.clientY + viewport.scrollY);
    
	if (a == 1) //position to the left instead of default right
	{
		x = x - mnu.offsetWidth;
	}
    else if ( x + mnu.offsetWidth + this.offX > viewport.width + viewport.scrollX )
	{
	  x = viewport.width + viewport.scrollX - mnu.offsetWidth;
	  //if menu right exceed screen right, then position menu align right
	  //below calculation a bit sala
      //x = x - mnu.offsetWidth - this.offX;
	}
    else x = x + this.offX;
  
    if ( y + mnu.offsetHeight + this.offY > viewport.height + viewport.scrollY )
      y = ( y - mnu.offsetHeight - this.offY > viewport.scrollY )? y - mnu.offsetHeight - this.offY : viewport.height + viewport.scrollY - mnu.offsetHeight;
    else y = y + this.offY;
    
    mnu.style.left = x + "px"; mnu.style.top = y + "px";
    this.timer = setTimeout("document.getElementById('" + menuLayers.activeMenuID + "').style.visibility = 'visible'", 200);
  },
  
  mouseoutCheck: function(e) {
    e = e? e: window.event;
    // is element moused into contained by menu? or is it menu (ul or li or a to menu div)?
    var mnu = document.getElementById(menuLayers.activeMenuID);
    var toEl = e.relatedTarget? e.relatedTarget: e.toElement;
    if ( mnu != toEl && !menuLayers.contained(toEl, mnu) ) menuLayers.hide();
  },
  
  // returns true of oNode is contained by oCont (container)
  contained: function(oNode, oCont) {
    if (!oNode) return; // in case alt-tab away while hovering (prevent error)
    while ( oNode = oNode.parentNode ) 
      if ( oNode == oCont ) return true;
    return false;
  },

  clearTimer: function() {
    if (menuLayers.timer) clearTimeout(menuLayers.timer);
  }
  
}


var subMenuLayers = {
  timer: null,
  activeMenuID: null,
  offX: 0,   // horizontal offset 
  offY: -2,   // vertical offset 
  fixX: null,
  fixY: null,
  
  show: function(id, e, x, y, a) { //HKS added a for alignment 1=left
    this.hideall();
  	currentLayer=2;
	openedlayers[id] = 2;
    var mnu = document.getElementById? document.getElementById(id): null;
    if (!mnu) return;
    this.activeMenuID = id;
    if ( mnu.onmouseout == null ) mnu.onmouseout = this.mouseoutCheck;
    if ( mnu.onmouseover == null ) mnu.onmouseover = this.clearTimer;
    viewport.getAll();
	this.fixX = x;
	this.fixY = y;
    this.position(mnu,e, a);
  },
  
  hide: function() {
    this.clearTimer();
    if (this.activeMenuID && document.getElementById && currentLayer == 2)
	{
	  //currentLayer=1;
	  //document.getElementById(subMenuLayers.activeMenuID).style.visibility = "hidden";
      this.timer = setTimeout("currentLayer=1;document.getElementById('"+subMenuLayers.activeMenuID+"').style.visibility = 'hidden'", 100);
	}
  },
  
  hideall: function() { // hide all level 2 menus only
    this.clearTimer();
    for (var o in openedlayers)
		if (openedlayers[o] == 2) document.getElementById(o).style.visibility = "hidden";
  },
  
  position: function(mnu, e, a) { //HKS added a for alignment 1=left
    var x = (this.fixX)?this.fixX:(e.pageX ? e.pageX: e.clientX + viewport.scrollX);
    var y = (this.fixY)?this.fixY:(e.pageY ? e.pageY: e.clientY + viewport.scrollY);
    
	if (a == 1) //position to the left instead of default right
	{
		x = x - mnu.offsetWidth;
	}
    else if ( x + mnu.offsetWidth + this.offX > viewport.width + viewport.scrollX )
	{
	  x = viewport.width + viewport.scrollX - mnu.offsetWidth;
	  //if menu right exceed screen right, then position menu align right
	  //below calculation a bit sala
      //x = x - mnu.offsetWidth - this.offX;
	}
    else x = x + this.offX;
  
    if ( y + mnu.offsetHeight + this.offY > viewport.height + viewport.scrollY )
      y = ( y - mnu.offsetHeight - this.offY > viewport.scrollY )? y - mnu.offsetHeight - this.offY : viewport.height + viewport.scrollY - mnu.offsetHeight;
    else y = y + this.offY;
    
    mnu.style.left = x + "px"; mnu.style.top = y + "px";
    this.timer = setTimeout("document.getElementById('" + subMenuLayers.activeMenuID + "').style.visibility = 'visible'", 200);
  },
  
  mouseoutCheck: function(e) {
    e = e? e: window.event;
    // is element moused into contained by menu? or is it menu (ul or li or a to menu div)?
    var mnu = document.getElementById(subMenuLayers.activeMenuID);
    var toEl = e.relatedTarget? e.relatedTarget: e.toElement;
    if ( mnu != toEl && !subMenuLayers.contained(toEl, mnu) ) subMenuLayers.hide();
  },
  
  // returns true of oNode is contained by oCont (container)
  contained: function(oNode, oCont) {
    if (!oNode) return; // in case alt-tab away while hovering (prevent error)
    while ( oNode = oNode.parentNode ) 
      if ( oNode == oCont ) return true;
    return false;
  },

  clearTimer: function() {
    if (subMenuLayers.timer) clearTimeout(subMenuLayers.timer);
  }
  
}

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 curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
}

