// Function to show a menu
YAHOO.namespace('yulong.dom');

YAHOO.yulong.dom.init = function() {
	
   var move = function(e) {
      var xy = YAHOO.util.Dom.getXY('end');

      YAHOO.util.Dom.setXY('test', xy);
   };
   
   var hook_hideMenu = function(e){
			if(menuStack.length>0)
			{
				for(var ite = 0;ite<menuStack.length;ite++)
				{
					var m = document.getElementById(menuStack[ite]);
					m.style.display = 'none';
				}
				menuStack.length = 0;
			}
   	}
   YAHOO.util.Event.addListener(document, 'click', hook_hideMenu);
   
};

YAHOO.util.Event.addListener(window, 'load', YAHOO.yulong.dom.init);

// create by rocbird.

//自動關 MENU 計時器
var autoOffMenuTimer;

// menuStack 用來儲存打開過的 menu
var menuStack = new Array();

//Menu Class 定義
function MenuItem(s_Name,s_Link,s_Icon,s_CSS,s_Target)
{
	this.sName = s_Name;
	this.sLink = s_Link;
	this.sIcon = s_Icon;
	this.sCSS = s_CSS;
	this.sMenuItemID = '';
	this.clsName = 'MenuItem';
	this.sParent = '';
	this.sTarget = s_Target;
}
function MenuImgItem(s_Name,s_Link,s_Icon,s_ImgPath_Light,s_ImgPath_Dark,s_CSS,s_Target)
{
	this.sName = s_Name;
	this.sLink = s_Link;
	this.sIcon = s_Icon;
	this.sCSS = s_CSS;
	this.sMenuItemID = '';	
	this.sImgPathLight = s_ImgPath_Light;
	this.sImgPathDark = s_ImgPath_Dark;
	this.clsName = 'MenuImgItem';
	this.sParent = '';
	this.sTarget = s_Target;
}
function Menu(s_Name,s_IDX,i_Width,s_ImgPath_Light,s_ImgPath_Dark,s_Link,s_Target)
{	
	this.sMenuName = s_Name;
	this.sMenuID = s_IDX;
	this.items = new Array();
	this.iWidth = i_Width;
	this.clsName = 'Menu';
	
	this.sMenuItemID = '';
	this.sParent = '';

	this.sImgPathLight = s_ImgPath_Light;
	this.sImgPathDark = s_ImgPath_Dark;
	this.sLink = s_Link;
	this.sTarget = s_Target;
}

Menu.prototype.AddItem = function(item){
	item.sMenuItemID = this.sMenuID + '_' + this.items.length;	
	item.sParent = this.sMenuID;
	this.items[this.items.length] = item;
}
Menu.prototype.WriteMenu = function(){
	
	var sHTML = '<div id="' + this.sMenuID + '" style="width: '+this.iWidth+'px;display:none;"><table width="150" border="0" cellspacing="0" cellpadding="0">';
	var icount = 0;
	var sIcon;
	var itemID = '';
	var cmdQ = new Array();
	var cmdCount = 0;
	for(icount = 0;icount < this.items.length ; icount++)
	{
		sIcon = (this.items[icount].sIcon)? '<img src="'+this.items[icount].sIcon+'">':'';
		
		switch(this.items[icount].clsName)
		{
			case 'MenuImgItem':
				itemID = this.items[icount].sMenuItemID;
				targetFix = "";
				if(this.items[icount].sTarget != '') targetFix = ' target="' + this.items[icount].sTarget + '" ';
				menuBody = '<a ' + targetFix + ' href="'+ this.items[icount].sLink + '" onMouseOut="doMouseOut(\''+ this.sMenuID +'\');MM_swapImgRestore()" onMouseOver="clearTimeout(autoOffMenuTimer);MM_swapImage(\'img_'+this.items[icount].sMenuItemID+'\',\'\',\''+ this.items[icount].sImgPathLight + '\',1)"><img src="'+ this.items[icount].sImgPathDark + '" alt="'+this.items[icount].sName+'" name="img_'+this.items[icount].sMenuItemID+'" border="0"></a>';
			break;
			case 'MenuItem':
				itemID = this.items[icount].sMenuItemID;
				targetFix = "";
				if(this.items[icount].sTarget != '') targetFix = ' target="' + this.items[icount].sTarget + '" ';
				menuBody = '<a ' + targetFix + ' onMouseOut="doMouseOut(\''+ this.sMenuID +'\');" onMouseOver="clearTimeout(autoOffMenuTimer);" href="'+ this.items[icount].sLink + '">'+this.items[icount].sName+'</a>';
			break;
			case 'Menu':
				if(this.items[icount].sImgPathLight)
				{
					itemID = this.items[icount].sMenuItemID;
					sLink = (this.items[icount].sLink)? this.items[icount].sLink:"javascript:void(0)";
					targetFix = "";
					if(this.items[icount].sTarget != '') targetFix = ' target="' + this.items[icount].sTarget + '" ';
					menuBody = '<a ' + targetFix + ' href="'+sLink+'" onMouseOut="doMouseOut(\''+ this.sMenuID +'\');MM_swapImgRestore()" onMouseOver="clearTimeout(autoOffMenuTimer);showSubMenu(\'td_'+ itemID +'\',\''+ this.items[icount].sMenuID + '\');MM_swapImage(\'img_'+itemID+'\',\'\',\''+ this.items[icount].sImgPathLight + '\',1)"><img src="'+ this.items[icount].sImgPathDark + '" alt="'+this.items[icount].sMenuName+'" name="img_'+itemID+'" border="0"></a>';
					cmdQ[cmdCount++] = 'this.items['+icount+'].WriteMenu();';
				}
				else
				{
					itemID = this.items[icount].sMenuItemID;
					sLink = (this.items[icount].sLink)? this.items[icount].sLink:"javascript:void(0)";
					targetFix = "";
					if(this.items[icount].sTarget != '') targetFix = ' target="' + this.items[icount].sTarget + '" ';
					menuBody = '<a ' + targetFix + ' href="'+sLink+'" onMouseOut="doMouseOut(\''+ this.sMenuID +'\');" onMouseOver="clearTimeout(autoOffMenuTimer);showSubMenu(\'td_'+ itemID +'\',\''+ this.items[icount].sMenuID + '\')">'+this.items[icount].sMenuName+'</a>';
					cmdQ[cmdCount++] = 'this.items['+icount+'].WriteMenu();';
				}
			break;
		}
				
		sHTML += '  <tr id="tr_' + itemID + '">';
		//sHTML += '    <td width="32">'+sIcon+'</td>';
		sHTML += '    <td width="118" id="td_' + itemID + '">' + menuBody + '</td>';
		sHTML += '  </tr>';
	}
	sHTML += '</table></div>';
	//document.frmDebug.tDebug2.value += sHTML;
	document.write(sHTML);
	for(var itemp = 0;itemp<cmdQ.length;itemp++)
	{
		eval(cmdQ[itemp]);
	}
	cmdQ.length=0;
}

function showMenu(el,idx)
{
	collapseMenuTree();
	
	var m = document.getElementById(idx);
	if(m) 
	{
		m.style.display = '';
		//document.frmDebug.tDebug.value = m.innerHTML;
	
		var xy = YAHOO.util.Dom.getXY(el);
	
		var agt=navigator.userAgent.toLowerCase();
	// Note: Opera and WebTV spoof Navigator.  We do strict client detection.
	// If you want to allow spoofing, take out the tests for opera and webtv.
	var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
	              && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
	              && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));

		if(is_nav)
			xy[1]+=15;
		else
			xy[1]+=55;
		YAHOO.util.Dom.setXY(idx,xy);
		menuStack[menuStack.length] = idx;
	}
	clearTimeout(autoOffMenuTimer);
}
function showSubMenu(el,idx)
{
	var root = getRoot(idx);
	collapseMenu(root);
	
	var m = document.getElementById(idx);
	m.style.display = '';
	//document.frmDebug.tDebug.value = m.innerHTML;
	
	var xy = YAHOO.util.Dom.getXY(el);
	xy[0]+=115;
//	xy[1]+=25;
	YAHOO.util.Dom.setXY(idx,xy);
	menuStack[menuStack.length] = idx;
	clearTimeout(autoOffMenuTimer);
}
function collapseMenuTree()
{
	if(menuStack.length>0)
	{
		for(var ite = menuStack.length-1;ite>=0;ite--)
		{
				var m = document.getElementById(menuStack[ite]);
				m.style.display = 'none';
		}
	}
	menuStack.length = 0;
}
function collapseMenu(root)
{
	var disCount = 0;
	if(menuStack.length>0)
	{
		for(var ite = menuStack.length-1;ite>=0;ite--)
		{
			if(menuStack[ite] != root)
			{
				var m = document.getElementById(menuStack[ite]);
				m.style.display = 'none';
				disCount++;
			}
		}
		menuStack.length -= disCount;
	}
}
function doMouseOut(idx)
{
	autoOffMenuTimer = setTimeout('collapseMenuTree()',500);
}
function getRoot(obj_name)
{
	var root = '';
	eval('if('+obj_name+') var o = '+obj_name+';');
	
	if(o)
	{
		if(o.sParent)
		{
			root = getRoot(o.sParent);
		}
		else
		{
			return o.sMenuID;
		}
	}
	return root;
}