// 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             = "#FFFFFF";    //Text color with mouseover
var offTextColor            = "#000000";    //Text color with NO mouseover
var onCellColor             = "#77aadd";    //Span color with mouseover
var offCellColor            = "#99CCFF";    //Span color with nomouseover
var offsetX                 = 0;            //For top level submenu positioning
var offsetY                 = 1;            //For top level submenu positioning
var subOffsetX              = 1;            //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 currentExpand          = null;					//for content
var eSynchedNode          = null;					//for tree

function ExpandSection(nnn) //内容扩展
{
//ExpandContentClicked(nnn)
	var icc =null;
	var ctlExpander = event.srcElement;
	if (ctlExpander.id == "btn")
	{
		icc = ctlExpander ;
		ctlExpander = ctlExpander.parentElement;
	}
	else
		icc = ctlExpander.children("btn",0);
	var cc = ctlExpander.parentElement.children("content",0);
	if ((currentExpand != null) && (currentExpand != cc) )
		{
			currentExpand.parentElement.children(0).children("btn",0).src = "media/image/down_arrow.gif";
			currentExpand.className = "NotVisible";
			currentExpand = null;
		}
	if ((cc != null)&& (icc != null))
	{
		if (cc.className == "NotVisible")
		{
			icc.src = "media/image/up_arrow.gif";
			cc.className = "IsVisible";
			currentExpand = cc;
		}
		else
		{
			icc.src = "media/image/down_arrow.gif";
			cc.className = "NotVisible";
			currentExpand = null;
		}
	}

}

function ExpandContentClicked(where)
{
	var icc =null;
	var ctlExpander = event.srcElement;
	if (ctlExpander.id == "btn")
	{
		icc = ctlExpander ;
		ctlExpander = ctlExpander.parentElement;
	}
	else
		icc = ctlExpander.children("btn",0);
	var cc = ctlExpander.parentElement.children("content",0);
	if ((cc != null)&& (icc != null))
	{
		if (cc.className == "NotVisible")
		{
			icc.src = "media/image/up_arrow.gif";
			cc.className = "IsVisible";
		}
		else
		{
			icc.src = "media/image/down_arrow.gif";
			cc.className = "NotVisible";
		}
	}
/*	else
	{	
	var ctlSelectedEntry = ctlExpander.parentElement;
	var colChild = ctlSelectedEntry.children.tags("div");
	var imgChild = ctlExpander.children.tags("img");

	if ((colChild.length > 0)&&(imgChild.length > 0))
		{
			if (colChild[0].className == "NotVisible")
			{
				imgChild[0].src = "media/image/left_arrow.gif";
				colChild[0].className = "IsVisible";
			}
			else
			{
				imgChild[0].src = "media/image/right_arrow.gif";
				colChild[0].className = "NotVisible";
			}
		}
	}
*/	
}


// Register onclick event so what if user clicks outside menu, the menus disappear.
//除注解外，其它为辅助用来被调用的函数
document.onclick = hideAllDivs;
document.onkeydown = KeyFilter;



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 = "media/image/right_arrow.gif";
    }
    var eParent = eItem.parentElement;
    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 = "media/image/left_arrow.gif";
        var eParent = eItem.parentElement;
        eParent.flipped = eItem;
        eItem.flipped = true;
    }
}

function GetLeft(e)
{
    var left = e.offsetLeft;
    while (e && e.tagName.toLowerCase() != "body")
    {
        if (e.tagName.toLowerCase() == "table") 
            left += e.offsetLeft;
        e = e.parentElement;
    }
    return left;
}

function GetTop(e)
{
    var top = e.offsetTop;
    while (e && e.tagName.toLowerCase() != "body")
    {
        if (e.tagName.toLowerCase() == "table") 
            top += e.offsetTop;
        e = e.parentElement;
    }
    return top;
}

function stateChange(menu,thisItem,level) //响应鼠标，改变菜单状态如颜色，弹出等，startIt调用，
{                                 //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  write offsettop to statebar:)
    currentElement = thisItem;
    thisItem.style.background = onCellColor;
    thisItem.style.color = onTextColor;
    window.status = thisItem.offsetTop;
   
    if (menu != "") {
        eMenu = document.all(menu);
        eItem = thisItem;
        hideDiv(level);
        menuArray[menuArray.length] = menu; // add to list of open menus.

        var cell = eItem.parentElement;   
        var left = eItem.offsetLeft + GetLeft(eItem.parentElement);
        var positionX = left;
        var positionY = eItem.offsetTop + GetTop(eItem.parentElement);
        
        // if this is a submenu, add width of submenu item.
        if (eItem.tagName.toLowerCase() == "span") {
            positionX += eItem.offsetWidth + subOffsetX;
            positionY += subOffsetY;
        } else {
            positionY += eItem.offsetHeight + offsetY;
        }
            
        eMenu.style.left = positionX;
        eMenu.style.top = positionY;
        eMenu.style.display = "block";

        // 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)// //响应点击,doSection原型，不再使用
{
    if (name != '') {
        if (parent != window && parent.frames[name] != null) {            
            parent.frames[name].url = url;
        } else if (document.all.item(name)) {
            document.all.item(name).src = url;
        } else {
            window.open(url, name);
        }
    } else {
        location.href = url;
    }
    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.all(menuArray[i]);
        if (eMenu) {
            UnFlipItem(eMenu);
            eMenu.style.display='none';
        }
    }
    menuArray.length = currentLevel;
}

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.all(menuArray[i]);
        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 = "";        
    }
}
//used for tree expand and not
function ExpanderClicked() //树结构扩展
{
	var ctlExpander = event.srcElement;
	var ctlSelectedEntry = ctlExpander.parentElement;
	var colChild = ctlSelectedEntry.children.tags("div");
	if(colChild.length > 0)
	{
		var strCSS;
		var ctlHidden = ctlSelectedEntry.all("hidorExpanded");
		if(ctlHidden.value == "1")
		{
			//Entry was expanded and is being contracted
			//ctlExpander.innerHTML = "+ ";
			ctlExpander.src ="media/image/book_close.gif";
			ctlHidden.value = "0";
			strCSS = "TreeNotVisible";
		}
		else
		{
			ctlExpander.src ="media/image/book_open.gif";
			ctlHidden.value = "1";
			strCSS = "TreeIsVisible";
		}
		for(var intCounter=0;intCounter<colChild.length; intCounter++)
		{
			colChild[intCounter].className = strCSS;
		}
	}
}

function Loadtoc( )
{
    //if( self == top ) location.replace( "index.aspx" );
		var theform = document.forms(0);
		var xx = 0;
		if (0 != theform.__index.value) 
					eSynchedNode = document.all(theform.__index.value-1+1);
//			eSynchedNode = document.all(theform.__index.value);
    if( eSynchedNode )
    {
      var eUdiv = ( eSynchedNode ).parentElement.parentElement;
      var em = null;
      var ctlHidden = null;
			if (eUdiv!= null) {
			    while (eUdiv.tagName.toLowerCase() == "div")
				{
					em = eUdiv.children.tags("img");
					if  (em.length > 0) em[0].click();//so use ExpanderClick yo expand
				/*
					ctlHidden = eUdiv.children("hidorExpanded",0);
					em = eUdiv.children.tags("img);
					if  (em.length > 0) em[0].src = "media/image/book_open.gif";
					if (null != ctlHidden) 
						ctlHidden.value = "1";
					if (eUdiv.className == "NotVisible") eUdiv.className = "IsVisible";
					var colChild = eUdiv.children.tags("div");
					for(var intCounter=0;intCounter<colChild.length; intCounter++)
					{
					if (colChild[intCounter].className == "NotVisible") 
						colChild[intCounter].className = "IsVisible";
					}*/
					eUdiv = eUdiv.parentElement;
        }
       }
        window.scrollTo(0,eSynchedNode.offsetTop-(document.body.clientHeight/2));
    }
}

function TreedoSection (url, name) //响应树结构点击，使用隐藏input,__cmd保存选择的参数，触发隐藏的button事件，其它使用frame
{
    var theform;
		theform = document.forms(0);
		eSynchedNode = event.srcElement;
		if (null != eSynchedNode)  theform.__index.value = eSynchedNode.sourceIndex;
   if (name != '') 
   {
			theform.__cmd1.value = url;
			theform.__cmd2.value = 0;
			if (name != 'default') theform.__cmd2.value = name;
			theform.submit();
    }
    event.returnValue = false;	
	
}
function MenudoSection (url, name) //响应菜单点击，使用隐藏input,__cmd保存选择的参数，触发隐藏的button事件，其它使用frame
{
    var theform;
		theform = document.forms(0);
		eSynchedNode = null;
		theform.__index.value = 0;
   if (name != '') 
   {
			theform.__cmd.value = url;
			theform.__cmd1.value = "0";
			theform.submit();
    }
    event.returnValue = false;	
	
}
// for indexed context



function popOpen(url)
{
document.open(url, '_blank','height=500,width=400,resizable=yes,status =0,titlebar =0,toolbar=0'  );
}
function PartOpen(url, name)
{
//location.href = url;
window.open(url, name);
}
