// This is the client side DHTML JScript code that drives the 
// menu interaction. Requires IE4+
//
// Menu selection takes the 'target' attribute from the XML menu 
// and finds a matching parent frame, a child iframe or open a 
// new named window.  If no target name is provided it navigates 
// the current page to the selected URL.  See Select() function.
//

var currentElement          = null;         //Needed to track current highlighted element
var menuArray               = new Array();  //Tracks what divs are showing so it knows what to hide when a new span is clicked
var onTextColor             = "#FF2805";    //Text color with mouseover
var offTextColor            = "#013C89";    //Text color with NO mouseover
//var onCellColor             = "#77aadd";    //Span color with mouseover
var onCellColor             = "#9FD1FF";    //Span color with mouseover
//var offCellColor            = "#99CCFF";    //Span color with nomouseover
var offCellColor            = "#9FD1FF";    //Span color with nomouseover
var offsetX                 = 0;            //For top level submenu positioning
var offsetY                 = 0;            //For top level submenu positioning
var subOffsetX              = 0;            //For submenu positioning
var subOffsetY              = 0;            //For submenu positioning
var started                 = false;        //Used to track if we just started the menus
var debug                   = false;        //whether to debug menu events.
var timer					= null;
var Trimming				= 73;			// Added by Gloria, it's 80px from the top of the browser.

// Register onclick event so what if user clicks outside menu, the menus disappear.
document.onclick = hideAllDivs;
document.onkeydown = KeyFilter;

var isIE = navigator.userAgent.search("MSIE") > -1;      
var isIE7 = navigator.userAgent.search("MSIE 7") > -1;      
var isFirefox = navigator.userAgent.search("Firefox") > -1;      
var isOpera = navigator.userAgent.search("Opera") > -1;      
var isSafari = navigator.userAgent.search("Safari") > -1;//Google瀏覽器是用這核心

if(isIE)
{
	offsetY = -1;
}
if(isSafari)
{
	offsetY = -4;
}


function KeyFilter()
{
    if (event.keyCode == 27) hideAllDivs();
}

function startIt(menu,thisItem,level) {                     //menu = menu to display,thisItem=coordinates of item to use,level=current depth of menus
    Debug("startmenu="+menu + ", item=" + thisItem.id + ", level=" + level);
    hideAllDivs();
    started = true;                                     //Lets us know we're coming in for the 1st time
    stateChange(menu,thisItem,level);
}

function Debug(msg) {
    if (debug) {
        var row = DEBUG.insertRow();
        var cell = row.insertCell();
        cell.innerText = msg;
    }
}

function UnFlip(eItem)
{
    eItem.flipped = false;
    var eImg = eItem.children["arrow"];
    if (eImg) {
        eImg.src = "images/right_arrow.gif";
    }
    var eParent = eItem.parentNode;
    eParent.flipped = null;
}

function UnFlipItem(eParent)
{
    var eItem = eParent.flipped;
    if (eItem) {
        UnFlip(eItem);
    }
    eParent.flipped = null;
}

function Flip(eItem)
{
    var eImg = eItem.children["arrow"];
    if (eImg) {
        eImg.src = "images/left_arrow.gif";
        var eParent = eItem.parentNode;
        eParent.flipped = eItem;
        eItem.flipped = true;
    }
}

function GetLeft(e)
{
    var left = e.offsetLeft;
    while (e && e.tagName != "BODY")
    {
        if (e.tagName == "TABLE") 
            left += e.offsetLeft;
        e = e.parentNode;
    }
    return left;
}

function GetTop(e)
{
    var top = e.offsetTop;
    var blnReachTop = false;
    while (e && e.tagName != "BODY")
    {
        if (e.tagName == "TABLE") 
            top += e.offsetTop;
        e = e.parentNode;
    }
//	alert (top);
//	top += e.offsetTop;
//	while (e && !blnReachTop)
//	{
//		top += e.offsetTop;
//		if (e.id = "tblMenuBarTop")
///		{
//			blnReachTop = true;
//		}
//		e = e.parentNode;
//	}
    return top;
}

function stateChange(menu,thisItem,level) {                                 //menu = menu to display,thisItem=name of span item to use,level=current depth of menus
   
    Debug("menu="+menu + ", item=" + thisItem.id + ", level=" + level);
           
    if (currentElement) { 
        //currentElement.style.background = offCellColor;
        currentElement.style.color = offTextColor;
        //if (currentElement.flipped) UnFlip(currentElement);
        currentElement = null;
    }
            
    //Turn on new item
    currentElement = thisItem;
    //thisItem.style.background = onCellColor;
    thisItem.style.color = onTextColor;
    window.status = thisItem.offsetTop;
   
    if (menu != "") {
        eMenu = document.getElementById(menu);
        eItem = thisItem;
        hideDiv(level);
        menuArray[menuArray.length] = menu; // add to list of open menus.
        
        var cell = eItem.parentNode;   
        var left = eItem.offsetLeft + GetLeft(eItem.parentNode);
        var positionX = left;
        var positionY = eItem.offsetTop + GetTop(eItem.parentNode) + Trimming;
//        var positionY = eItem.Node.offsetHeight + GetTop(eItem.parentNode);

       
        // if this is a submenu, add width of submenu item.
        // 目前已無法用SPAN確認是否為submenu, 因為我將他改成A
        if (eItem.tagName == "SPAN") {
            positionX += eItem.offsetWidth + subOffsetX;
            positionY += subOffsetY;
        } else {
            positionY += eItem.offsetHeight + offsetY;
        }
            
        eMenu.style.left = positionX;
        eMenu.style.top = positionY;
        eMenu.style.zIndex = 1000;	// 設超大, 使其永遠在最上面
        eMenu.style.display = "block";
        
        hideElement("select", eMenu.offsetLeft, eMenu.offsetTop, eMenu.offsetWidth, eMenu.offsetHeight);
        

        // Now that menu is visible check width (width is 0 initially)
        
        //If screen isn't wide enough to fit menu, then pop it up on the left side.
        if ((positionX + eMenu.offsetWidth - document.body.scrollLeft) >= document.body.clientWidth) {
            if (level == 0 || (left - eMenu.offsetWidth) < 0) {
                eMenu.style.left = document.body.clientWidth - eMenu.offsetWidth + document.body.scrollLeft;
            } else {
                eMenu.style.left = left - eMenu.offsetWidth;            
                Flip(eItem);
            }
        }
        //If the menu is too far to the left to display, bump it to the right some
        if (positionX <= document.body.scrollLeft) {
            eMenu.style.left = document.body.scrollLeft;
        }
        //If the menu is too far down, bump the menu up so the bottom equals the body clientHeight property
        if ((positionY + eMenu.offsetHeight) >= document.body.clientHeight) {
            if (started != true) 
                eMenu.style.top = document.body.clientHeight - eMenu.offsetHeight;
        }
        
    }
    
    started = false; //After 1st menu, turn of started variable

}

function Select(url, name)
{
	if (name == '')
	{
		if (url != '#')
		{
//			location.replace(url);
			window.location = url;
		}
	}
	else if (name == "new")
	{
		window.open(url,"","location=yes,toolbar=yes,menubar=yes,status=yes,scrollbars=yes,resizable=yes,width=640,height=480");
	}
	else if (name == "nonToolBar")
	{
		window.open(url,"","location=no,toolbar=no,menubar=no,status=no,scrollbars=yes,resizable=no,width=640,height=480");
	}
	else if (name == "blogWin")
	{
		try
		{
			//ContentMapWin.close();
			BlogWin.close();
		}
		catch(e)
		{
			// ignor it
		}
		BlogWin = window.open(url,"MyETUserBlogIE","location=no,toolbar=yes,menubar=yes,status=yes,scrollbars=yes,resizable=yes,width=800,height=500");
		BlogWin.focus();
	}
    event.returnValue = false;
}

function hideDiv(currentLevel) {
    Debug("hideDiv="+currentLevel);
    
    for (var i = currentLevel; i < menuArray.length; i++) {
        if (menuArray[i] == null) continue;
        Debug("hide="+menuArray[i]);
        var eMenu = document.getElementById(menuArray[i]);
        if (eMenu) {
            UnFlipItem(eMenu);
            eMenu.style.zIndex = 0;
            eMenu.style.display='none';
        }
    }
    menuArray.length = currentLevel;
	
	if (currentLevel == 0)
	{
		showElement("select");
	}
	
}

function hideAllDivs() {
    Debug("hideAllDivs");    
    for (var i = 0; i < menuArray.length; i++) {
        var arrayString = new String(menuArray[i]);
        if (arrayString == "undefined") continue;
        var eMenu = document.getElementById(menuArray[i]);
        eMenu.style.zIndex = 0;
        eMenu.style.display = "none";
        eMenu.style.left = 0;
        eMenu.style.top = 0;
        UnFlipItem(eMenu);
    }
    if (currentElement) { 
        //currentElement.style.background = offCellColor;
        currentElement.style.color = offTextColor;
        currentElement = null;
        menuArray = new Array();    
        currentSpanElement = "";        
    }
    
    showElement("select");

}


function MouseOutSetTimeOut()
{
	timer = setTimeout("hideAllDivs()", 500);
}

function MouseOverClearTimeOut()
{
	clearTimeout(timer);
}

function showElement(elmID)
{
	for (i = 0; i < document.getElementsByName(elmID).length; i++)
	{
		obj = document.getElementsByName(elmID)[i];
		
		if (obj.style.visibility == "hidden")
		{
			obj.style.visibility = "";
		}
	}
}

function hideElement(elmID, menuLeft, menuTop, menuWidth, menuHeight)
{	
	for (i = 0; i < document.getElementsByName(elmID).length; i++)
	{
		obj = document.getElementsByName(elmID)[i];

//		if (! obj || ! obj.offsetParent)
//			continue;

		// Find the element's offsetTop and offsetLeft relative to the BODY tag.
		objLeft   = obj.offsetLeft;
		objTop    = obj.offsetTop;
		objParent = obj.offsetParent;
		while (objParent.tagName.toUpperCase() != "BODY")
		{
			objLeft  += objParent.offsetLeft;
			objTop   += objParent.offsetTop;
			objParent = objParent.offsetParent;
		}

		if ((menuTop + menuHeight) < objTop)
			;
		else if (menuLeft > (objLeft + obj.offsetWidth))
			;
		else if ((menuLeft + menuWidth) < objLeft)
			;
		else
			obj.style.visibility = "hidden";
	}
}

