//Author = Andy Rajamani (AIU0241)

// 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 BROWSER_CD = CheckBrowser();
	var arr = BROWSER_CD.split("|");	
	
	BROWSER_CD = arr[0];
	VERSION = arr[1].substr(0,1);
	
  // Tweak the variables defined in XmlMenuClient.js for fine tuned
  // control over the look and layout of the menus.
  onTextColor             = "#FFFFFF";    //Text color with mouseover
  offTextColor            = "#000000";    //Text color with NO mouseover
  offBorderColor			= "#DDEAF7";
    
  onCellColor             = "#616141";    //Span color with mouseover
  offCellColor            = "#616141";
  
    
  offsetX                 = 0;            //For top level submenu positioning
  offsetY                 = 1;            //For top level submenu positioning
  subOffsetX              = 2;            //For submenu positioning
  subOffsetY              = 0;            //For submenu positioning
  

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 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 flyoutImg				= new Array();
var bIsImgChanged           = false;
var m_imgCount				= 0;
var menutimeout;


// Register onclick event so what if user clicks outside menu, the menus disappear.
//document.onclick = hideAllDivs;
//document.onkeydown = KeyFilter;
	
/**************** END ******************/
	

function SetTimeOut()
{

	menutimeout = window.setTimeout('hideAllDivs();',1000);

}


/*
function KeyFilter()
{
    if (window.event.keyCode == 27) 
    {
		hideAllDivs();
		
	}
}
*/
function hideShowDropDown(hide)
{
    if ((BROWSER_CD == 'IE') && (started == true) && (TOP_NAV_HIDE_DROPDOWN == true))
    {
		var dropDownAll = document.getElementsByTagName("select");
		var style;

		if (hide == true)
			style = 'hidden';
		else
			style = 'visible';
	
	
		for (var i=0; i<dropDownAll.length; i++ )
		{
			dropDownAll[i].style.visibility = style;	
		}
	}
}

function startIt(menu,thisItem,level,root,imgCount) {                     //menu = menu to display,thisItem=coordinates of item to use,level=current depth of menus
    //alert("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,root,imgCount);
    
	hideShowDropDown(true);
}

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/white_right_arrow.jpg";
    }
    var eParent = ((eItem.parentElement)?eItem.parentElement: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.parentElement)?eItem.parentElement:eItem.parentNode);
        eParent.flipped = eItem;
        eItem.flipped = true;
    }
}

function GetLeft(e)
{
	
    var left = e.offsetLeft;
    if ((BROWSER_CD == 'NS') && (VERSION == '6'))
    {
		left = left - e.parentNode.offsetLeft;
	}
    while (e && e.tagName != "BODY")
    {
        if (e.tagName == "TABLE") 
        {
			if ((BROWSER_CD == 'NS') && (VERSION == '6'))
				left += e.offsetLeft - e.parentNode.offsetLeft;
			else
				left += e.offsetLeft;    
         }
        e = ((e.parentElement)?e.parentElement:e.parentNode);
    }
    return left;
}

function GetTop(e)
{
    var top = e.offsetTop;
    
    if ((BROWSER_CD == 'NS') && (VERSION == '6'))
    {		
		top = top - e.parentNode.offsetTop;
	}
    while (e && e.tagName != "BODY")
    {
        if (e.tagName == "TABLE")         
        {
			if ((BROWSER_CD == 'NS') && (VERSION == '6'))
			{				
				top += e.offsetTop - e.parentNode.offsetTop;
			}
			else
				top += e.offsetTop;
          }
            
        e = ((e.parentElement)?e.parentElement:e.parentNode);
    }
    return top;
}



function stateChange(menu,thisItem,level,root,imgCount) {                                 //menu = menu to display,thisItem=name of span item to use,level=current depth of menus
    //debug("menu="+ menu + ", item=" + thisItem.id + ", level=" + level);
 
   var theImg;

	window.clearTimeout(menutimeout);
  
	var imgName;
	

    if (currentElement) { 
		if (root == 'no')			
		{			
			if (currentElement !=null)
			{		
				if (currentElement.id != '')
				{						
					
					currentElement.style.background = offCellColor;
					currentElement.style.color = offTextColor;
					currentElement.style.borderColor = offBorderColor;
					
					if (imgCount > -1)
					{
						if (menu == "")												
						{
							imgName = 'flyout_' + imgCount
							
							document.images[imgName].src = flyoutImg[0].src;
						}
						else if(bIsImgChanged)
						{
							document.images['flyout_' + m_imgCount].src = flyoutImg[0].src;
						}						
					}
					else if(bIsImgChanged)
					{				
						document.images['flyout_' + m_imgCount].src = flyoutImg[0].src;
					}
					else
					{
						bIsImgChanged = false;
						m_imgCount = -1;
					}
					
				}
			}
			currentElement = null;
		}
    }
    
    //Turn on new item
    currentElement = thisItem;
    
    if (root == 'no')
    {
		thisItem.style.background = onCellColor;
		thisItem.style.color = onTextColor;
		thisItem.style.borderColor = onCellColor;
		
		
		if (imgCount > -1)
		{
			if (menu != "")
			{
				document.images['flyout_' + imgCount].src = flyoutImg[1].src;
				bIsImgChanged = true;
				m_imgCount = imgCount;
			}
		}
	}
	  
       
    if (menu != "") {
    
    
		
        eMenu = document.getElementById(menu);
        
        if (eMenu != null)
        {				
			eItem = thisItem;      	
			
			hideDiv(level);
	    
	                     
			menuArray[menuArray.length] = menu; // add to list of open menus.

			
			bNs6 = ((eItem.parentElement)?false:true);				
							
			var cell = ((eItem.parentElement)?eItem.parentElement:eItem.parentNode);
			
			
			if ((BROWSER_CD == 'NS') && (VERSION == "6"))
			{
				var left
				var positionX
				var positionY
				if (eItem.tagName == "SPAN")			
				{
					left = GetLeft(cell);
					positionX = left;
					positionY = eItem.offsetTop + GetTop(cell);						
				}
				else
				{
					left = (eItem.offsetLeft - cell.offsetLeft) + GetLeft(cell);
					positionX = left;
					positionY = (eItem.offsetTop - cell.offsetTop) + GetTop(cell);
				}
				
				
			}
			else
			{
	        
	        
				var left = eItem.offsetLeft + GetLeft(cell);
				var positionX = left;
				var positionY = eItem.offsetTop + GetTop(cell);
				
				
				
			}

			// if this is a submenu, add width of submenu item.
	        
			if (eItem.tagName == "SPAN") {
				
				positionX += 176;
				
				if (bNs6 == true)
					positionX +=6;
							
	           
				positionY += subOffsetY;

			} 
			else
			{					
				if (BROWSER_CD == 'IE')
					positionY += eItem.offsetHeight; 
				else
					positionY += eItem.offsetHeight - 3;
	         
				if (bNs6 == true)
				{				
					positionY +=3;				
				}
			}
	                  	     		
			eMenu.style.left = positionX + 'px';
			eMenu.style.top = positionY + 'px';
			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 - 3;
					
			
			}
	        
		}
	}
	

    //started = false; //After 1st menu, turn of started variable
				
}

function Select(url, name)
{
    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);    
	
	var eMenu;
	
	currentLevel = currentLevel * 1;
    for (var i = currentLevel; i < menuArray.length; i++) 
    {
        if (menuArray[i] == null) continue;
        
        eMenu = null;
        if (menuArray[i] != null)
			eMenu = document.getElementById(menuArray[i]);
			
        if (eMenu !=null) 
        {
            //UnFlipItem(eMenu);
            eMenu.style.display='none';
        }
    }
	
	if (menuArray.Length != currentLevel)
	{
		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.getElementById(menuArray[i]);
        eMenu.style.display = "none";
        eMenu.style.left = 0;
        eMenu.style.top = 0;
        //UnFlipItem(eMenu);
    }
    
    if (currentElement) { 
		if (currentElement.id != '')
		{
			currentElement.style.background = offCellColor;
			currentElement.style.color = offTextColor;
			currentElement.style.borderColor = offBorderColor;
			
			
			if (bIsImgChanged == true)
			{	
				document.images['flyout_' + m_imgCount].src = flyoutImg[0].src;

				bIsImgChanged = false;
				m_imgCount  = -1;
			}
		}
		
        currentElement = null;
        menuArray = new Array();    
        currentSpanElement = "";        
    }
    
	hideShowDropDown(false);	
    started = false;
    
		
}
