/*
** FileName:	menu.js
** Author:		
** Date:		7-AUG-2004
** Ver:			0.2.3
** Path:		
*/

function Browser() {

  var ua, s, i;

  this.isIE    = false;  // Internet Explorer
  this.isOP    = false;  // Opera
  this.isNS    = false;  // Netscape
  this.version = null;

  ua = navigator.userAgent;

  s = "Opera";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isOP = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as Netscape 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }

  s = "MSIE";
  if ((i = ua.indexOf(s))) {
    this.isIE = true;
	this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
  
}

var browser = new Browser();


var activeButton = null;
var menuid = 0;
var itemid = 0;
var mouseOn = false;
var popTimer = 0;
var MENU_TIMOUT = 1500;

function Menu( superM )
{
	var _this = this;
	this.name = "";
//==variables======================
	this.items = [];
	this.thisMenu = null;
	this.superM = superM || null;
	this.isReady = false;
	this.isShow = false;
	this.z_index = 9;
	
//==functions=======================
	this.addItem = addItem;
	this.show = show;
	this.hideSubm = hideSubm;
	this.hideAll = hideAll;
	
//ini the class
	_this.name = "menu" + menuid;
	if( _this.superM != null) _this.z_index = _this.superM.z_index + 1;
	document.writeln("<table id='"+_this.name+"' class='menu' unselectable='on' ");
	document.writeln(" style='position:absolute; z-index:"+ _this.z_index +"; visibility: hidden;' ></table>");
	
	_this.thisMenu = document.getElementById( _this.name );
	
	//------
	var iframeEl = document.createElement("IFRAME");
  	iframeEl.frameBorder = 0;
  	iframeEl.src = "javascript:;";
  	iframeEl.style.display = "none";
  	iframeEl.style.position = "absolute";
  	iframeEl.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
  	_this.thisMenu.iframeEl = _this.thisMenu.parentNode.insertBefore(iframeEl, _this.thisMenu);
	
	//------

	
	if( _this.thisMenu != null ) 
	{
		_this.thisMenu.onmouseover = onOver;
		_this.thisMenu.onmouseout = onOut;
		_this.thisMenu.ondeactivate = onDec;
		_this.isReady = true;
	}
	menuid++;
//end of ini

/*=================================================================
** 
**	
**
**===============================================================*/	
	function addItem( mItem ){
		_this.items[ _this.items.length ] = mItem;
		mItem.ini( _this );
	}//end addItem

/*=================================================================
** 
**	
**
**===============================================================*/	
	function onOver(){
		clearTimeout( popTimer );
		mouseOn = true;	
		if( browser.isIE )
		{
			_this.thisMenu.setActive();
		}
	}//end onOver
	
/*=================================================================
** 
**	
**
**===============================================================*/	
	function onOut(){
		var obj;
		var str = "";
		
		if( browser.isIE )
		{
			obj = window.event.toElement;
		}
		else
		{
			//obj = event.relatedTarget;
		}
		
		
		if( obj != null )
		{
			str =  obj.id.substring( 0,4 );
			if( ! ( str == "menu" || str == "item")) mouseOn = false;
		}
		// this hideAllMenus is public static method
		popTimer = setTimeout("mouseOn = false; hideAllMenus();",MENU_TIMOUT );
		//popTimer = window.setTimeout('timeout_hid()',MENU_TIMOUT );
		
	}//end onOut
	

	
	
/*=================================================================
** 
**	
**
**===============================================================*/	
	function onDec(){
		if( !mouseOn ){
			_this.hideAll();
		}
	}//end onDec
	
/*=================================================================
** 
**	
**
**===============================================================*/	
	function show( b, top, left ){
		if( _this.isReady ){
			var thetop = top  || _this.thisMenu.offsetTop;
			var theleft = left || _this.thisMenu.offsetLeft;
			
			var maxX = ( document.body.clientWidth );
			var maxY = ( document.body.clientHeight );
			
			var thisW = _this.thisMenu.offsetWidth;
			var thisH = _this.thisMenu.offsetHeight;
			
			if( theleft + thisW > maxX ){
				if( _this.superM != null ){
					theleft =  _this.superM.thisMenu.offsetLeft - thisW +3;
				}
				else{
					theleft = maxX - thisW; 
				}
			}
			
			if( thetop + thisH > maxY - 2){
				thetop = maxY - thisH - 4; 
				if( _this.superM == null ) theleft += 30;
			}
			
			if( theleft <0 ) theleft = 0;
			
			_this.thisMenu.style.top = thetop+"px";
			_this.thisMenu.style.left = theleft+"px";
			
			
			if (_this.thisMenu.iframeEl != null)
			{
			  _this.thisMenu.iframeEl.style.left = _this.thisMenu.style.left;
			  _this.thisMenu.iframeEl.style.top  = _this.thisMenu.style.top;
			  _this.thisMenu.iframeEl.style.width  = _this.thisMenu.offsetWidth + "px";
			  _this.thisMenu.iframeEl.style.height = _this.thisMenu.offsetHeight + "px";
			  _this.thisMenu.iframeEl.style.display = "";
			}

			
			
			if( b ){
				if(_this.superM == null || _this.superM.isShow ){
					//_this.thisMenu.style.display = "";
					_this.thisMenu.style.visibility="visible";
					
					_this.thisMenu.iframeEl.style.display = "";
					
					_this.isShow = true;
				}
			}
			else{
				_this.thisMenu.style.visibility="hidden";
				//_this.thisMenu.style.display = "none";
				
				if (_this.thisMenu.iframeEl != null)
      				_this.thisMenu.iframeEl.style.display = "none";
	
				
				_this.isShow = false;
				_this.hideSubm();
			}	
		}
	}// end show
	
/*=================================================================
** 
**	
**
**===============================================================*/	
	function hideSubm(){
		for( var i=0; i< _this.items.length; i++ ){
			if(_this.items[i].subM != null){
				_this.items[i].subM.show(false);
			}
		}	
	}//end hidSubm
/*=================================================================
** 
**	
**
**===============================================================*/	
	function hideAll(){
		_this.show( false );
		if( superM != null) _this.superM.hideAll();
	}//end hideAll

}//end class menu


/*
**	class Item
**	
**
*/
function Item( html, subM, mouseclick )
{
	
	var name = "";
	var theItem = null;
	var theRow = null;
	var _this = this;
	var html = html;
	
	this.ownerM = null;
	this.subM = subM || null;
	this.disableOverOut = false;
// methods==================================================
	this.ini = ini;
	this.on_click = mouseclick || new Function();
	this.disabled = disabled;
	this.visible = visible;
//=========================================================
	name = "item" + itemid;	
	itemid++;
/*
	if(subM != null){
		html += "&nbsp;&nbsp;&raquo;"; //face='Wingdings 3'	
	}
*/
	
/*=================================================================
** 
**	
**
**===============================================================*/	
	function ini( menu ){
		if( menu != null ){
			_this.ownerM = menu;
			theRow = _this.ownerM.thisMenu.insertRow(_this.ownerM.thisMenu.rows.length);
			theRow.id = "item_row";
			theRow.unselectable ="on";
			theItem = theRow.insertCell(theRow.cells.length);
			//theItem = theRow.insertCell();			
			theItem.id = "item"+ itemid;
			theItem.unselectable ="on";
			theItem.innerHTML = html;
			
			arrow = theRow.insertCell(theRow.cells.length);
			arrow.unselectable ="on";
			
			if( subM != null ){
				arrow.align = "right";
				arrow.innerHTML = "&rsaquo;";
			}
			
			theItem.className = "itemOut";
			
			if ( theItem != null ){
				theItem.onclick = onClick;
				theItem.onmouseover = onOver;
				theItem.onmouseout = onOut;
			}
		}
	}//end ini	

/*=================================================================
** 
**	
**
**===============================================================*/	
	function disabled(b){
		theRow.disabled = b;
		theRow.cells[0].disabled = b;
		theRow.cells[1].disabled = b;
	}//end disabled

/*=================================================================
** 
**	
**
**===============================================================*/	
	function visible(b){
		if(b){
			theRow.style.display = "";
		}
		else{
			theRow.style.display = "none";
		}		
	}//end visible
/*=================================================================
** 
**	
**
**===============================================================*/
	function onOver(){
		if( !_this.disableOverOut ) this.className = "itemOver";
		_this.ownerM.hideSubm();
		if( _this.subM != null  ){
			if(  !_this.subM.isShow ){
				var top = _this.ownerM.thisMenu.offsetTop + this.offsetTop;
				var left = _this.ownerM.thisMenu.offsetLeft + _this.ownerM.thisMenu.offsetWidth -3;
				_this.subM.show(true,top,left);
			}
		}
	}//end onOver

/*=================================================================
** 
**	
**
**===============================================================*/
	function onOut(){
		if( !_this.disableOverOut ) this.className = "itemOut";
	}//end onOut

/*=================================================================
** 
**	
**
**===============================================================*/
	function onClick(){
		if( _this.subM == null ){
			this.onmouseout();
			_this.ownerM.hideAll();
		}
		_this.on_click();
	}//end onClick
}//end class item

//class separatebar
function separatebar(){
	var name = "";
	var theItem = null;
	var _this = this;
	this.ownerM = null;
	this.disabled = disabled;
	this.visible = visible;
//method===========================================================
	this.ini = ini;
//ini the class====================================================
	name = "item" + itemid;	
	itemid++;
/*=================================================================
** 
**
*/
	function ini( menu ){
		if( menu != null ){
			_this.ownerM = menu;
			theRow = _this.ownerM.thisMenu.insertRow(_this.ownerM.thisMenu.rows.length);
			theRow.id = "item_row";
			theItem = theRow.insertCell(theRow.cells.length);
			theItem.id = name
			theItem.className = "separatebar";
		}
	}
/*=================================================================
** set the visibility 
**
*/
	function visible(b){
		if(b){
			theItem.style.display = "";
		}
		else{
			theItem.style.display = "none";
		}
	}

/*=================================================================
** set the disabled
**
*/
	function disabled(b){
		//not doing any thing
	}//end disabled

}//end class separatebar




function getLeft(el) {

  var x;

  // Return the x coordinate of an element relative to the page.

  x = el.offsetLeft;
  if (el.offsetParent != null)
    x += getLeft(el.offsetParent);

  return x;
}

function getTop(el) {

  var y;

  // Return the y coordinate of an element relative to the page.

  y = el.offsetTop;
  if (el.offsetParent != null)
    y += getTop(el.offsetParent);

  return y;
}

