/*
** FileName:	menu.js
** Author:		Jia Yu
** Date:		18-Nov-2006
** Ver:			2.5.8
** Path:		
*/


/*
<modificationlog>
	 <item>
		<date>29-Nov-2006</date>
		<author>Jia</author>
		<description>fix the setOpacity for Menu class with IE. Test the attribute befor set the value.</description>
		<ver>2.5.8</ver>
	</item>
<modificationlog>

*/

var MENU_TIMOUT = 1000;


//class menu bar
function MenuBar( parent, ori )
{
	
	var _this = this;
	var timerID = 0;
	
	this.isShow = true;
	this.zIndex = 5;
	
	var orientation = ori || "h";
	
	if( typeof parent == "object" ) this.parent = parent;
	else if( typeof parent == "string" )this.parent = document.getElementById( parent );
	
	this.thebody = document.createElement("TABLE");
	this.parent.appendChild( this.thebody );

		this.thebody.name = "menubar";
		this.thebody.className  ="menubar";
		this.thebody.cellPadding  = 0;
		this.thebody.cellSpacing = 0;
	this.buttons = new Array();
	var lastShowingButton = null;
	//var lastShowingMenu = null;
	
	//Methods
	this.hideSub = hideSub;
	this.add = add;
	this.addButton = addButton;
	this.hide = hide;
	
	function hideSub()
	{
//		for( i=0;i< _this.buttons.length;i++ )
//		{
//			if(  _this.buttons[i].submenu !=  null ) _this.buttons[i].submenu.hide();
//		}
		//if( lastShowingButton != null  )
//		{
//			if(  lastShowingButton.submenu !=  null ) lastShowingButton.submenu.hide();
//		}
	}
	
	function hide()
	{
		this.setLastShowingButton( null );
	}
	
	this.setLastShowingButton = function ( button )
	{
		if( lastShowingButton != null )
		{
			lastShowingButton.thebody.className = "buttonOut";
			if(  lastShowingButton.submenu !=  null ) lastShowingButton.submenu.hide();
		}
		lastShowingButton = button;
	};
	
	

	function add( theMenu  )
	{
 		var button = new MenuButton( _this , theMenu.disName, null , theMenu, orientation );
		_this.buttons[_this.buttons.length] = button;
		theMenu.parent = _this;
	}
	
	function addButton( str, fun  )
	{
		var button = new MenuButton( _this , str , fun , null, orientation );
		_this.buttons[_this.buttons.length] = button;
	}
	
	
	this.getTimerID = function()
	{
		return _this.timerID;
	};
	
	this.setTimerID = function( num )
	{
		_this.timerID = num;
	};
}
//end class menu bar

//class menu button
function MenuButton( parent , html, mouseclick ,sub_menu, orientation )
{
	//ini
	var _this = this;
	this.parent = parent;
	this.on_Click = mouseclick || new Function();
	this.submenu = sub_menu || null;
	this.orientation = orientation || "h";
	
	if( this.orientation == "v" ) 
	{
		var theRow = this.parent.thebody.insertRow(-1);
		this.thebody = theRow.insertCell(-1);
//  		var arrow = theRow.insertCell( -1 );
//		arrow.className = "buttonOut";
//		arrow.align = "right";
//		arrow.innerHTML = "&raquo;"; 
	}
	else
	{
		var theRow = "";
		if( this.parent.thebody.rows.length == 0 ) 
		{
			theRow = this.parent.thebody.insertRow(-1);
		}
		else
		{
			theRow = this.parent.thebody.rows[0];
		}
		
		this.thebody = theRow.insertCell(-1);
		
		
	}
	
		this.thebody.innerHTML = html;
		this.thebody.className = "buttonOut";
		this.thebody.name = "menuButton";

	this.isDisabled = false;
	this.isVisible = true;
	
	// Methods
	//this.hideSubmenu = hide_sub_menu;
	this.setDisabled = disabled;
	this.setVisible = visible;
	
	//Events
	this.thebody.onclick = onClick;
	this.thebody.onmouseover = onOver;
	this.thebody.onmouseout = onOut;

	function onClick(event)
	{
		if( document.all) event = window.event;
		
		if( !_this.isDisabled && _this.isVisible  )
		{
			if( _this.submenu != null )
			{
				//_this.submenu.show();
			}
			else
			{
				
				if( typeof _this.on_Click == "function"  )
				{	
					_this.on_Click();
				}
				else if( typeof _this.on_Click == "string" )
				{
					eval( _this.on_Click );	
				}
				
			}
		}
		event.cancelBubble = true;
	}
	
	function onOver( event )
	{
		if( document.all) event = window.event;
		
		if( !_this.isDisabled && _this.isVisible )
		{
			//_this.parent.hide();
			_this.parent.setLastShowingButton( _this );
			
			_this.thebody.className = "buttonOver";
			window.clearTimeout( _this.parent.getTimerID() );

			var top ,left;
			
			if ( _this.orientation == "v" )
			{
				top = getTop( _this.thebody );
				left = getLeft( _this.thebody ) + _this.parent.thebody.offsetWidth -3;
			}
			else
			{
				top = getTop( _this.thebody) + _this.thebody.offsetHeight;
				left = getLeft( _this.thebody);
			}
			
			//if( typeof _this.parent.hideSub == "function" ){  _this.parent.hideSub(); }
			
			if( _this.submenu != null ){ _this.submenu.show(top , left)};
			
		}
		event.cancelBubble = true;
	}
	
	function onOut( event )
	{
		if( document.all) event = window.event;
		
		if( !_this.isDisabled && _this.isVisible)
		{
			if( _this.submenu == null )
			{
				_this.thebody.className = "buttonOut";
			}
			else
			{
				_this.parent.setTimerID( setTimeout( _this.submenu.hideAll , MENU_TIMOUT ));
			}
		}
		event.cancelBubble = true;
	}
	
	function disabled(b)
	{
		_this.isDisabled = b;
 		_this.thebody.disabled = b;
	}//end disabled
	
	function visible(b){
		if(b){
			_this.thebody.style.display = "";
		}
		else{
			_this.thebody.style.display = "none";
		}
		_this.isVisible = b;
	}//end visible
	
	function hide_sub_menu()
	{
		if( _this.submenu != null )
		{
			_this.submenu.hide();
		}
	}
	
}



//class menu
function Menu( disName )
{
	var _this = this;
	this.parent = null;
	this.disName = disName;
	this.items = new Array();
	
	this.lastOverItem = null;
	//this.minWidth = 90;
	
	this.shadow = document.createElement("DIV");
	document.body.appendChild( this.shadow );
	this.shadow.className = "shadow";
	this.iframeEl = "";
	this.isShow = false;
	
	this.thebody = document.createElement("TABLE");
	//this.parent.appendChild( this.thebody );
	document.body.appendChild( this.thebody );
	this.thebody.appendChild( document.createElement("tBody"));
	
	this.thebody.style.left = "0px";
	this.thebody.style.top = "0px";
	
	//this.thebody.style.zIndex = 50;
	
	//Methods
	this.hide = hideme;
	this.hideSub = hideSub;
	this.hideSup = hideSup;
	this.hideAll = hideAll;
	this.setMinwidth = setMinwidth;
	this.addItem = addItem;
	this.addSubMenu = addSubMenu;
	this.addSeparatebar = addSeparatebar;
	this.setOpacity = setOpacity;
	this.showpop = showpop;
	this.show = showme;
	this.add = add;
	
	//ini the menu body

	this.thebody.className = "menu";
	this.thebody.name ="menu";
	//this.thebody.unselectable = "on";
	this.thebody.cellPadding  = 0;
	this.thebody.cellSpacing = 0;

	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.iframeEl = this.thebody.parentNode.insertBefore(iframeEl, this.thebody);
	

	//0-100
	function setOpacity( value ){
		if( document.all ){
			if( _this.thebody.filters.alpha ) _this.thebody.filters.alpha.opacity = value;
		}
		else{
			value = value * 0.01;
			_this.thebody.style.opacity = value;
		}	
	}
	
	function setMinwidth( intNum )
	{
		_this.minWidth = intNum;
		_this.thebody.style.minWidth = intNum +"px";
	}

	function hideme()
	{
		if( _this.isShow )
		{
			hideSub();
			_this.isShow = false;
			//_this.thebody.style.display = "none";
			_this.shadow.style.visibility = "hidden";
			_this.thebody.style.visibility="hidden";
			_this.iframeEl.style.dispaly = "none";
			if( _this.lastOverItem != null ) _this.lastOverItem.itemOut();
		}
	}

	//Hide all supermenus
	function hideSup()
	{
		var obj = _this;
		
		while( typeof obj.parent.thebody != "undefined" )
		{
			if( typeof obj.parent.hide == "function"  )
			{
				if( obj.parent.isShow ) obj.parent.hide();
			}
			
			obj = obj.parent;
		}
		
	}
	
	function hideSub()
	{
		for( i=0;i<_this.items.length;i++ )
		{
			if(  _this.items[i].submenu != null  )  _this.items[i].submenu.hide();
		}
	}
	
	//event
	this.thebody.onmouseover =  function( event )
	{
		if( document.all ) event = window.event;

		window.clearTimeout( _this.getTimerID() );
		event.cancelBubble = true;
	};
	
	//event
	this.thebody.onmouseout = function( event )
	{
//		var obj;
//		if( document.all )
//		{
//			obj = window.event.toElement;
//			event = window.event;
//		}
//		else
//		{
//			obj = event.relatedTarget;
//		}


		if( document.all ) event = window.event;
		if( _this.lastOverItem != null ) 
		{
			if(  _this.lastOverItem.submenu == null )
			{
				_this.lastOverItem.itemOut();
			}
		}
		_this.setTimerID( setTimeout( _this.hideAll , MENU_TIMOUT ) ); 
		event.cancelBubble = true;
	};
	
	function hideAll()
	{
		_this.hideSub();
		_this.hide();
		_this.hideSup();
	}
	
	function add( mItem )
	{
		_this.items[ _this.items.length ] = mItem;
		
		if( _this.thebody.offsetWidth < _this.minWidth ) 
		{
			_this.thebody.style.width = _this.minWidth+"px";
		}
	}
	
	function addItem( html, mouseclick ,sub_menu, iconHTML )
	{
		var mItem = new MenuItem( _this , html, mouseclick ,sub_menu, iconHTML );
		
		_this.items[ _this.items.length ] = mItem;
		
		if( _this.thebody.offsetWidth < _this.minWidth ) 
		{
			_this.thebody.style.width = _this.minWidth+"px";
		}
		return mItem;
	}
	
	function addSubMenu( submenu, iconHTML )
	{
		_this.items[ _this.items.length ] = new MenuItem( _this , submenu.disName , null ,submenu, iconHTML );
		
		submenu.thebody.style.zIndex = _this.thebody.style.zIndex +1;
		
		if( _this.thebody.offsetWidth < _this.minWidth ) 
		{
			_this.thebody.style.width = _this.minWidth+"px";
		}
	}
	
	function addSeparatebar()
	{
		_this.items[ _this.items.length ] =  new Separatebar( _this );
	}
	
	function showpop( obj )
	{
		_this.show( getTop( obj )+ obj.offsetHeight , getLeft( obj ) );
	}
	
	
	function showme( top, left )
	{
		
		var thetop = top;
		var theleft = left;
		
		var maxW = ( window.innerWidth ? window.innerWidth : document.body.clientWidth );
		var maxH = ( window.innerHeight ? window.innerHeight : document.body.clientHeight );
		
		var thisW = _this.thebody.offsetWidth;
		var thisH = _this.thebody.offsetHeight;
		
		if( theleft + thisW > maxW )
		{
			if( _this.parent.thebody.name == "menu" )
			{
				if( _this.parent != null )
				{
					theleft = _this.parent.thebody.offsetLeft - thisW +3;
				}
				else{
					theleft = maxW - thisW; 
				}
			}
			else 
			{
				theleft = maxW - thisW - 3; 
			}
		}
		
		if( thetop + thisH > ( maxH - 2 ))
		{
			thetop = maxH - thisH - 4; 
			if( _this.parent == null ) theleft += 30;
		}
		
		if( theleft <0 ) theleft = 0;
		
		
		_this.thebody.style.top = thetop+"px";
		_this.thebody.style.left = theleft+"px";
		
		
		if (_this.iframeEl != null)
		{
		  _this.iframeEl.style.left = theleft+"px";
		  _this.iframeEl.style.top  = thetop+"px";
		  _this.iframeEl.style.width  = "0px";
		  _this.iframeEl.style.height = "0px";
		  _this.iframeEl.style.display = "";
		}
		
		//_this.thebody.style.display = "block";
		if( _this.parent.thebody.style.zIndex < 100 ) _this.parent.thebody.style.zIndex = 100;
		
		_this.thebody.style.zIndex = _this.parent.thebody.style.zIndex + 2;
		_this.thebody.style.visibility = "visible";
		
		//shadow
		if (_this.shadow != null)
		{
			_this.shadow.style.width = _this.thebody.offsetWidth + "px";
			_this.shadow.style.height = _this.thebody.offsetHeight + "px";
			_this.shadow.style.top = (thetop+2)+"px";
			_this.shadow.style.left = (theleft+2)+"px";
			_this.shadow.style.zIndex = _this.thebody.style.zIndex - 1;
			_this.shadow.style.visibility = "visible";
		}
		//---
		_this.isShow = true;	
	}
	
	this.getTimerID = function()
	{
		if( _this.parent != null )
		{
			return _this.parent.getTimerID();
		}
	};
	
	this.setTimerID = function( num )
	{
		if( _this.parent != null )
		{
			return _this.parent.setTimerID( num );
		}
	};
	
	
}


//class menu item
function MenuItem( parent , html, mouseclick ,submenu, iconHTML )
{
	//ini
	var _this = this;
	this.parent = parent;
	
	this.on_Click = mouseclick || new Function();
	this.submenu = submenu || null;
	if( this.submenu != null ) 
	{
		this.submenu.parent = this.parent;
	}
	
	this.thebody = document.createElement("TR");
	this.parent.thebody.tBodies[0].appendChild( this.thebody );
	
	//this.parent.tBodies[0].appendChild( this.thebody );
		this.thebody.unselectable ="on";
		this.thebody.className = "itemOut";
		this.thebody.name = "menuItem";
	this.isDisabled = false;
	this.isVisible = true;
	// Methods
	this.setDisabled = disabled;
	this.setVisible = visible;
	
	//Events
	this.thebody.onclick = onClick;
	this.thebody.onmouseover = onOver;
	this.thebody.onmouseout = onOut;
	
	//前面的图片预留
	var theIcon = this.thebody.insertCell( -1 );
	theIcon.className = "icon";
	theIcon.innerHTML = iconHTML || "";

	//中间的文字部分
	var theCell = this.thebody.insertCell( -1 );
	//theCell.unselectable ="on";
	theCell.className = "menucell";
	theCell.innerHTML = html;
	
	//左侧的箭头
	var arrow = this.thebody.insertCell( -1 );
	arrow.className = "menuArrow";
	//arrow.unselectable ="on";
	
	if( this.submenu != null )
	{
		arrow.align = "right";
		arrow.innerHTML = "&raquo;"; //&raquo; &raquo;
		this.submenu.parent = this.parent;
	}

	function onClick( event )
	{
		if( document.all) event = window.event;
		
		if( !_this.isDisabled && _this.isVisible  )
		{
			if( _this.submenu != null )
			{
				
			}
			else
			{
				_this.parent.hideAll();
				if( typeof _this.on_Click == "function"  )
				{	
					_this.on_Click();
				}
				else if( typeof _this.on_Click == "string" )
				{
					eval( _this.on_Click );	
				}
			}
		}
		//event.cancelBubble = true;
	}
	
	function onOver( event )
	{
		if( document.all) event = window.event;

		if(  _this.parent.lastOverItem !=null ) _this.parent.lastOverItem.itemOut();
		
		_this.parent.lastOverItem = _this;
		
		if( !_this.isDisabled && _this.isVisible  )
		{
			
			_this.thebody.className = "itemOver";
			theCell.className = "menucellOver";
			arrow.style.color = "#333333";
			
			if( typeof _this.parent.hideSub == "function" )  _this.parent.hideSub();
			
			if( _this.submenu != null )
			{
				var top = _this.parent.thebody.offsetTop + _this.thebody.offsetTop +1;
				var left = _this.parent.thebody.offsetLeft + _this.parent.thebody.offsetWidth -3;
				_this.submenu.show(top , left);
			}
		}
		//event.cancelBubble = true;
	}
	
	this.itemOut = function()
	{
		_this.thebody.className = "itemOut";
		theCell.className = "menucell";
		arrow.style.color = "";
	};
	
	
	function onOut( event )
	{
		if( document.all) event = window.event;
		if( !_this.isDisabled && _this.isVisible )
		{
			//if( _this.submenu == null )
//			{
//				_this.itemOut();
//			}
//			else
//			{
//				
//				
//			}
		}
		//event.cancelBubble = true;
	}
	
	
	function disabled(b)
	{
		_this.isDisabled = b;
 		_this.thebody.disabled = b;
	}//end disabled
	
	function visible(b){
		if(b){
			_this.thebody.style.display = "";
		}
		else{
			_this.thebody.style.display = "none";
		}
		_this.isVisible = b;
	}//end visible
	

}



//class separatebar
function Separatebar( parent )
{
//ini the class====================================================
	var _this = this;
	
	this.parent = parent;
	this.isVisible = true;
	
	this.thebody = document.createElement("TR");
	this.parent.thebody.tBodies[0].appendChild( this.thebody );
		this.thebody.unselectable ="on";
//		this.thebody.className = "separatebar";
		this.thebody.name = "separatebar";		
		this.thebody.insertCell(-1);
		var cell = this.thebody.insertCell(-1);
		cell.className = "separatebar";
		this.thebody.insertCell(-1);
	
	// Methods
	this.setVisible = setVisible;
	
/*=================================================================
** set the visibility 
**
*/
	function setVisible(b){
		if(b){
			_this.thebody.style.display = "";
		}
		else{
			_this.thebody.style.display = "none";
		}
		_this.isVisible = b;
	}


}//end class separatebar



//class Color Pad
function ColorPad( parent , mouseclick )
{
	//ini
	var _this = this;
	this.parent = parent;
	
	this.on_Click = mouseclick || new Function();

	this.thebody = this.parent.thebody.tBodies[0];
	//this.parent.thebody.tBodies[0].appendChild( this.thebody );
	this.parent.setOpacity(100);
	
	var therow = this.parent.thebody.insertRow( -1 );
	var icon = therow.insertCell( -1 );
	icon.className = "icon";
	
	var theCell = therow.insertCell( -1 );
	var arrow = therow.insertCell( -1 );
	
	arrow.className = "menuArrow";
	
	var theTable = document.createElement("TABLE");
	theCell.appendChild( theTable );
	theTable.style.width = "145px";
	theTable.cellPadding = 0;
	theTable.cellSpacing = 0;
	theTable.style.padding = "2px";
	
	
	
	for(var k=0;k<3;k++ )
	{
		for(var i=0;i<6;i++)
		{
		var row = theTable.insertRow( -1 );
			
			for( var j=0;j<12;j++ )	
			{
				var cell = row.insertCell( -1 );
				cell.onmouseover = onOver;
				cell.onmouseout = onOut;
				cell.onclick = onClick;
				cell.className = "colorCell";
				if( j < 6 )
				{
					cell.style.backgroundColor = "#"+twoDigs(tohex(k*51))+twoDigs(tohex(i*51))+twoDigs(tohex(j*51));
				}
				else
				{
					cell.style.backgroundColor = "#"+twoDigs(tohex((k+3)*51))+twoDigs(tohex(i*51))+twoDigs(tohex((j-6)*51));
				}
				
				cell.title = cell.style.backgroundColor;
			}
		}
	}
	lastRow = theTable.insertRow( -1 );
	
	for(var i=0;i<12;i++)
	{
		var cell = lastRow.insertCell( -1 );
		cell.onmouseover = onOver;
		cell.onmouseout = onOut;
		cell.onclick = onClick;
		cell.className = "colorCell";		
		cell.style.backgroundColor = "#"+twoDigs(tohex(i*21.25))+twoDigs(tohex(i*21.25))+twoDigs(tohex(i*21.25));
		cell.title = cell.style.backgroundColor;
	}

	this.isDisabled = false;
	this.isVisible = true;
	// Methods
	//this.hideSubmenu = hide_sub_menu;
	this.setDisabled = disabled;
	this.setVisible = visible;
	
	//Events
	//this.thebody.onclick = onClick;
	//this.thebody.onmouseover = onOver;
	//this.thebody.onmouseout = onOut;

	function onClick( event )
	{
		if( document.all) event = window.event;
		
		if( !_this.isDisabled && _this.isVisible )
		{
			_this.on_Click();
			alert( this.style.backgroundColor );
			_this.parent.hideAll();
		}
		//event.cancelBubble = true;
	}
	
	function onOver( event )
	{
		if( document.all) event = window.event;
		
		if( !_this.isDisabled && _this.isVisible )
		{
			this.className = "colorCellOver";
			//alert( this.style.backgroundColor );
			//this.style.borderColor="#000000";
		}
		//event.cancelBubble = true;
	}
	
	function onOut( event )
	{
		if( document.all) event = window.event;
		if( !_this.isDisabled && _this.isVisible )
		{
//			_this.thebody.className = "colorCell";
			this.className = "colorCell";
			//this.style.borderColor="#DDDDDD";
		}
		//event.cancelBubble = true;
	}
	
	
	function disabled(b)
	{
		_this.isDisabled = b;
 		_this.thebody.disabled = b;
	}//end disabled
	
	function visible(b){
		if(b){
			_this.thebody.style.display = "";
		}
		else{
			_this.thebody.style.display = "none";
		}
		_this.isVisible = b;
	}//end visible

}



//public function contains
//A contains B
function contains(a , b)
{
	while(b) 
	{
		if (a == b) return true;
		b = b.parentNode;
	}
	return false;
}

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;
}


function twoDigs( num )
{
	num = num+"";
	if( num.length <=1 ) num = "0"+num;
	if( num.length >2 ) num = num.substring( 0,2 );
	return num;
}


function tohex( num )
{
	return Number( num ).toString(16); 
}
function todec( num )
{
	return  parseInt( num ,16);
	
}

function Bulid_Menu( menudata )
{
	var _this = this;
	
	if( typeof menudata == "object" ) this.menudata = menudata;
	else if( typeof menudata == "string" )this.menudata = document.getElementById( menudata );
	
	this.parentNode = this.menudata.parentNode;
	
	this.menudata.style.display = "none";
	

	var menubar = new MenuBar( this.parentNode );
	
	
	var add = function( parent, submenu )
	{
		var text = submenu.getElementsByTagName("A")[0].innerHTML;
		
		var subs = submenu.getElementsByTagName("UL");
		
		if( subs.length > 0 )
		{
			var m = new Menu( text );
	
	
			for(var i=0; i< subs[0].childNodes.length; i++ )
			{
				
				if( subs[0].childNodes[i].tagName == "LI" )
				{
					add( m, subs[0].childNodes[i] );
				}
				 
			}
			
			if( parent == menubar )
			{	
				menubar.add( m );
			}
			else
			{
				parent.addSubMenu( m );	
			}
			
		}
		else
		{
			if( parent == menubar )
			{
				menubar.addButton( text, function(){  
					
					window.location = submenu.getElementsByTagName("A")[0].href;
					
				 } );
			}
			else
			{
				parent.addItem( text , "window.location='"+submenu.getElementsByTagName("A")[0].href+"';" , null   );
			}
		}
		
	}
	
	var menus = this.menudata.getElementsByTagName("UL");
	
	for(var i=0; i< menus[0].childNodes.length;i++  )
	{
		if( menus[0].childNodes[i].tagName == "LI" )
		{
			add( menubar,menus[0].childNodes[i] );
		}
	}
	
	
	
}

