﻿/*
--------------------------------------------------------------------------------------------------
 Name:	Menu_onMouseOut
 Function:	When mouse move out from the menu
 Input:		
		element	: The menu
 Ouput:
			
---------------------------------------------------------------------------------------------------
*/
function Menu_onMouseOut(element)
{
	try{
		Menu_HiddenSubMenu(element);
		Menu_showOutColor(element);
	}catch(ex)
	{
	}
}
/*
--------------------------------------------------------------------------------------------------
 Name:	Menu_onMouseOver
 Function:	When mouse move into the menu
 Input:		
		element	: The menu	
 Ouput:
---------------------------------------------------------------------------------------------------
*/
function Menu_onMouseOver(element)
{

	try{
		Menu_ShowSubMenu(element);
		Menu_showOverColor(element);
	}catch(ex)
	{
	}
}


/*
--------------------------------------------------------------------------------------------------
 Name:	Menu_showOverColor
 Function:	When mouse move into the menu,change the menu's font color and background color
 Input:		
		element	: The menu	
 Ouput:
---------------------------------------------------------------------------------------------------
*/
function Menu_showOverColor(element)
{
	if(element.M_backgroundColor==null)
	{
		element.M_backgroundColor = element.currentStyle.backgroundColor;

	}
	
	if(	element.M_fontColor==null)
	{
		element.M_fontColor = element.currentStyle.color;

	}
	if(element.M_HightLigthBackColor==null||element.M_HightLigthFontColor==null)
	{
	  	try{
  			element.M_HightLigthBackColor=getHighlightBackColor(element)
			element.M_HightLigthFontColor=getHighlightFontColor(element)
		}catch(ex)
		{
		}
	}
	element.style.backgroundColor =element.M_HightLigthBackColor
	element.style.color=element.M_HightLigthFontColor
}



/*
--------------------------------------------------------------------------------------------------
 Name:	Menu_showOutColor
 Function:	When mouse move out the menu,change the menu's font color and background color to normal color
 Input:		
		element	: The menu	
 Ouput:
---------------------------------------------------------------------------------------------------
*/
function Menu_showOutColor(element)
{
		
	element.style.backgroundColor = element.M_backgroundColor
	element.style.color = element.M_fontColor
}

/*
--------------------------------------------------------------------------------------------------
 Name:	Menu_CreateSubMenu
 Function:	Add a div of submenu to the menu
 Input:		
		element	: The menu	
 Ouput:
---------------------------------------------------------------------------------------------------
*/
function Menu_CreateSubMenu(element)
{	
	var thesubMenuDiv  = eval("window.DIV_"+element.id)
	if(thesubMenuDiv!=null) 
	element.subMenuDiv = thesubMenuDiv
	return;
}



/*
--------------------------------------------------------------------------------------------------
 Name:	Menu_onClick
 Function:	When user click then menu 
 Input:		
		element	: The menu	
 Ouput:
---------------------------------------------------------------------------------------------------
*/
function Menu_onClick(element)
{
	try{
	if( element.href != null && element.href != ""  )
	{
		var thehref= element.href
	 	if(decodeURI(thehref).toLowerCase().indexOf("javascript:")==0)
		{
			try{
				eval(decodeURI(thehref))
			}catch(Ex)
			{
				}
			return;
		}
		if(decodeURI(thehref)== thehref)
		{//If Url is no a encode url.
			
			thehref = encodeURI(thehref)
		}
		if(element.target!="" && element.target!=null&& element.target.toUpperCase!="_SELF")
		{
			window.open(thehref,element.target)
		}
		else
		{
			window.navigate(thehref)
		}
	}
	}catch(ex)
	{
	}

}

/*
--------------------------------------------------------------------------------------------------
 Name:	Menu_ShowSubMenu
 Function:	Show submenu div
 Input:		
		element	: The menu	
 Ouput:
---------------------------------------------------------------------------------------------------
*/
function Menu_ShowSubMenu(element)
{	
	try{
		if (element.subMenuDiv==null)
		{
			
			Menu_CreateSubMenu(element)
			
		}
		
		if (element.subMenuDiv!=null)
		{	
			element.subMenuDiv.isShow="true";
			try{
				Menu_ShowMenuDiv(element,element.subMenuDiv)
			   }
			catch(e)
			{
				alert(e.description)
			}
			
			
			
		}
	}catch(ex)
	{
	}
}

/*
--------------------------------------------------------------------------------------------------
 Name:	Menu_HiddenSubMenu
 Function:	Hidden submenu div
 Input:		
		element	: The menu	
 Ouput:
---------------------------------------------------------------------------------------------------
*/
function Menu_HiddenSubMenu(element)
{
	try{
		if (element.subMenuDiv!=null)
		{	
			element.subMenuDiv.isShow = "false"
 	if(element.componentFromPoint(event.x,event.y-3)=="outside" ||element.componentFromPoint(event.x,event.y)=="outside") 
	{
		Menu_HiddenMenuDiv(element.subMenuDiv)
	}
}
	}catch(Ex)
	{
		element.subMenuDiv.isShow = "false"
	}
}

/*
--------------------------------------------------------------------------------------------------
 Name:	getHighlightFontColor
 Function:	Get the menu's hight light font color 
 Input:		
		element	: The menu	
 Ouput:
---------------------------------------------------------------------------------------------------
*/
function getHighlightFontColor(element)
{
	var oParentDiv=eval("window."+element.parentDivID)
	if(oParentDiv==null) return ""; 	
	if(oParentDiv.M_hadGetHFC) return oParentDiv.M_HighlightFontColor;
	if(oParentDiv.M_HighlightFontColor==null)
	{
		oParentDiv.M_HighlightFontColor = getStyleValue(oParentDiv,"Highlight-Font-Color")
	}
	if(oParentDiv.M_HighlightFontColor==null) oParentDiv.M_HighlightFontColor="";
	oParentDiv.M_hadGetHFC = true;
 	return oParentDiv.M_HighlightFontColor
}

/*
--------------------------------------------------------------------------------------------------
 Name:	getHighlightBackColor
 Function:	Get the menu's hight light background color 
 Input:		
		element	: The menu	
 Ouput:
---------------------------------------------------------------------------------------------------
*/
function getHighlightBackColor(element)
{

	var oParentDiv=eval("window."+element.parentDivID)
	if(oParentDiv ==null) return "";
	if(oParentDiv.M_hadGetHBC) return oParentDiv.M_HighlightBackColor
	if(oParentDiv.M_HighlightBackColor==null)
	{
		oParentDiv.M_HighlightBackColor = getStyleValue(oParentDiv,"Highlight-Back-Color")
	}
	if(oParentDiv.M_HighlightBackColor==null) oParentDiv.M_HighlightBackColor=""
	oParentDiv.M_hadGetHBC = true;
	return oParentDiv.M_HighlightBackColor
}



/*
--------------------------------------------------------------------------------------------------
 Name:	Menu_ShowMenuDiv
 Function:  Show the menu div.
 Input:		
		node -  parent menu div.	
		element - This menu div.
 Ouput:
---------------------------------------------------------------------------------------------------
*/
function Menu_ShowMenuDiv(node,element)
{
	try{
		if (element.BtmIframe==null)
		{
			element.BtmIframe = document.createElement("Iframe")	
			element.BtmIframe.style.position = "absolute"
			element.BtmIframe.scrolling ="no"
			element.BtmIframe.border ="0"
			document.body.appendChild(element.BtmIframe)		
		}
		var oParentMenuDiv
		if(element.style.visibility=="visible")
		{
			return;
		}
		element.BtmIframe.style.display="block"	
		if(node != null){
			var rect1=node.getBoundingClientRect()
			var rect2=element.getBoundingClientRect()
			switch (element.pop){
			case 2:
			case "2":
			break;
			case 1:
			case "1":
				var top=rect1.top-2	
				var left=rect1.right-2
				element.style.left=left+document.body.scrollLeft
				element.style.top=top+document.body.scrollTop
				break ;
			case 0 :
			case "0":
			default :	
				var top=rect1.bottom-2	
				//var left=rect1.left+((rect1.right-rect1.left)-(rect2.right-rect2.left))/2-2
				var left=rect1.left -2
				element.style.left=left+document.body.scrollLeft
				element.style.top=top+document.body.scrollTop
				if(element.offsetWidth<node.offsetWidth)
				{
					element.style.width = node.offsetWidth+2
				}
				break ;
			}
		
		}			
		if(element.parentMenuDiv==null)
		{
			element.parentMenuDiv = eval("window.DIV_"+element.parentID)
		}
		if(element.parentMenuDiv!=null)
		{
			element.parentMenuDiv.isActive="true";
		}
		element.style.visibility="visible"
		element.style.display="block"
		if(element.pop==2 || element.pop=="2")
		{	
				var rect1=node.getBoundingClientRect()
				var rect2=element.getBoundingClientRect()
				var top=rect1.top-2	
				var left=rect1.left+2-element.clientWidth 		
				element.style.left=left+document.body.scrollLeft
				element.style.top=top+document.body.scrollTop
				
		}
		

		CheckNodePostion(element)		
		element.BtmIframe.style.left =element.style.left
		element.BtmIframe.style.top = element.style.top
		element.BtmIframe.style.width = element.clientWidth
		element.BtmIframe.style.height = element.clientHeight	
		//Menu_hidden_win(element)
	}catch(ex)
	{
	}
}

/*
--------------------------------------------------------------------------------------------------
 Name:	Menu_HiddenMenuDiv
 Function:  Hidden this menu div.
 Input:		
		element - This menu div.
 Ouput:
---------------------------------------------------------------------------------------------------
*/
function Menu_HiddenMenuDiv(element)
{		
		try{
		if(element.isActive == "true")
		{
			return
		}
			
		if(element.alwaysShow) return;
		if(element.componentFromPoint(event.x,event.y)!="outside") return //if the mouse still in the menu div
		var subMenuList =  element.getElementsByTagName("SPAN")  //得到层包含的所有子菜单
		var i
		for(i=0;i<subMenuList.length;i++)
		{
			
			if(subMenuList[i].parentMenuDiv==element)//如果子菜单的父是本层
			{
				if(subMenuList[i].subMenuDiv!=null) //如果子菜单有弹出层
				{
					//如果鼠标在子菜单的弹出层中则不隐藏
					if(subMenuList[i].subMenuDiv.componentFromPoint(event.x,event.y)!="outside") 
					{
						return
					}
				}
				
			}
		}
		
		element.style.left = document.body.scrollLeft- element.clientWidth-1000
		element.style.top  = document.body.scrollTop- element.clientHeight-1000
		if(element.BtmIframe!=null)element.BtmIframe.style.display="none"
		element.style.visibility="hidden"
		element.style.display="none"
		if(element.parentMenuDiv==null)
		{

			element.parentMenuDiv = eval("window.DIV_"+element.parentID)
			
		}
		
		if(element.parentMenuDiv!=null)
		{
			 element.parentMenuDiv.isActive="false"
			 Menu_HiddenMenuDiv(element.parentMenuDiv);
		}	
		
		//	Menu_show_win(element)
	return true;
	}catch(ex)
	{
	}
}

/*
--------------------------------------------------------------------------------------------------
 Name:	Menu_hidden_win
 Function:  Hidden those objects they  will show on the uperstratum . 
 Input:		
		element - This menu div.
 Ouput:
---------------------------------------------------------------------------------------------------
*/
function Menu_hidden_win(element)
{
	try{
		var thedivrect=element.getBoundingClientRect();
		var HidWin=new Array;		
		var Selects=document.getElementsByTagName("SELECT");		
		var Sindex
		for(Sindex=0;Sindex<Selects.length;Sindex++)
		{
			try{
				cRect=Selects[Sindex].getBoundingClientRect();
				if(checkRandRect(cRect,element.getBoundingClientRect()))
				{				
					var theselect=Selects[Sindex]
					if(theselect.style.visibility!="hidden"&&theselect.style.display!="none")
					{
						theselect.style.visibility="hidden";
						HidWin[HidWin.length]=theselect;						
					}			
				}
			}catch(e)
			{}
		}
		element.oHidWinList=HidWin;
	}catch(ex)
	{
	}
}

/*
--------------------------------------------------------------------------------------------------
 Name:	Menu_show_win
 Function: Make these objects that head been  hiddened by this menu div.
 Input:		
		element - This menu div.
 Ouput:
---------------------------------------------------------------------------------------------------
*/
function Menu_show_win(element)
{
	try{
	var i
	if(element.oHidWinList!=null){
		for(i=0;i<element.oHidWinList.length;i++)
		{
			element.oHidWinList[i].style.visibility="inherit"
		}
	}
	}catch(ex)
	{
	}
}

/*
--------------------------------------------------------------------------------------------------
 Name:	checkRandRect
 Function: Check if one rect is across with another
 Input:		
		R1 - rect.
		R2 - rect.
 Ouput:
		boolean
---------------------------------------------------------------------------------------------------
*/
function checkRandRect(R1,R2)
{
	try{
		if(R1.left>R2.right || R2.left>R1.right )
		{
			return false
		}
		if(R1.top>R2.bottom ||R2.top>R1.bottom)
		{
			return false
		}
		return true
	}catch(e)
	{
	
	}
}

/*
--------------------------------------------------------------------------------------------------
 Name:	getStyleValue
 Function: Get a extented value by a key from element 's style .
 Input:		
		element - the html element ,
		sKey - the key .
 Ouput:
		boolean
---------------------------------------------------------------------------------------------------
*/
function getStyleValue(element,sKey)
{	
	var CssID = "maincss"
	var sValue
	try{
		if(CssID!=null && CssID!="" && sKey!=null && sKey!="") 
		{
			if(element.myCssText==null)//If had get the myCssText not need get it again
			{
				ostyleSheet =document.styleSheets(CssID) //Get styleSheet from document				
				if(ostyleSheet!=null)
				{					
					var i ,orule					
					for(i=0;i<ostyleSheet.rules.length;i++)
					{						
						if(ostyleSheet.rules[i].selectorText ==("."+element.className))
						{
							orule = ostyleSheet.rules[i]
							break;
						}
					}
					if(orule!=null)
					{
						element.myCssText = orule.style.cssText
					}
					
				}
			}		
			if(element.myCssText!=null&& element.myCssText!="")
			{			
				var istart = element.myCssText.indexOf(sKey+": ")
				if(istart >=0)
				{
					sValue = element.myCssText.substring(istart+sKey.length+2,element.myCssText.indexOf(";",istart+sKey.length)>0?element.myCssText.indexOf(";",istart+sKey.length):element.myCssText.length)
				}
				else
				{
					sValue = null;
				}
			}
			else
			{
				sValue =  null;
			}
		}
		else
		{
			sValue = null;
		}
	}catch(Ex)
	{
		sValue = null;
	}	
	return 	sValue
}

/*
--------------------------------------------------------------------------------------------------
 Name:	CheckNodePostion
 Function: 保持对象的下边在窗口底边上部
 Input:		
	node html对象		
 Ouput:
		
---------------------------------------------------------------------------------------------------
*/
function CheckNodePostion(node)		
{
	try{	
		var rect1=node.getBoundingClientRect()
		if(rect1.bottom>document.body.clientHeight)
		{
			node.style.top = parseInt(document.body.clientHeight)- parseInt(node.clientHeight) + parseInt(document.body.scrollTop)
		}
	}catch(ex)
	{
	}	
}
