
// the x.js needed for this script
//xInclude('/include/scripts/x/x_core.js', '/include/scripts/x/x_slide.js', '/include/scripts/x/x_popup.js', '/include/scripts/x/x_timer.js');

// ANDYS CODE FOR THE NEW MENU

var onSub;					// Boolean saying whether you are over a submenu item
var mainOpenId, pageOpenId;	// Remember the ID of each currently open submenu
var pagemenuStartPositionY	// The position in which the pagemenu div starts
var focusItemWhenMenuOpenedId = 0;	// An ID which should be focus()ed when the main menu is opened (focus only works when menu is visible)
var OverMenu = false

// *** DONT CALL THIS FUNCTION DIRECTLY! ***
// Use a wrapper like the two below for each menu using submenus.
function toggleSubmenu (submenu, currentOpenSub) {

	// Only run if you haven't clicked a submenu link or a menu arrow
	if (!onSub) {
		
		// Get objects for image and menu list		
		arrowImg = document.getElementById(submenu + "I");
		subList  = document.getElementById(submenu + "L");

		// Close any currently open menu, unless it is the same one you clicked
		if (currentOpenSub && submenu != currentOpenSub) {
			document.getElementById(currentOpenSub + "L").style.display="none";
			document.getElementById(currentOpenSub + "I").src = document.getElementById(currentOpenSub + "I").src.replace (/up\.gif/, "down.gif");
			document.getElementById(currentOpenSub + "I").alt="Open Submenu";
		}	
	
		// If the list you clicked on is not open, open it... otherwise close it
		if ((subList.style.display != "inline") && (subList.style.display != "block")){
			subList.style.display = "block";
			arrowImg.src = arrowImg.src.replace (/down\.gif/, "up.gif");
			arrowImg.alt="Close Submenu";
			if (xHeight("menucolor") <= xHeight("menudiv")+xTop("menudiv")){
				xHeight("menucolor",xHeight("menudiv")+xTop("menudiv"))// change the height chick here bar
			}			
			return submenu;
	    } else {
			subList.style.display = "none";
			arrowImg.src = arrowImg.src.replace (/up\.gif/, "down.gif");
			arrowImg.alt="Open Submenu";
		}
	
	}
}

// Wrapper for toggleSubmenu, for the main menu.
function togglemainSubmenu (submenu) {
	OverMenu = true
	mainOpenId = toggleSubmenu (submenu, mainOpenId);
}

// Wrapper for toggleSubmenu, for the page menu.
function togglepageSubmenu (submenu) {
	pageOpenId = toggleSubmenu (submenu, pageOpenId);
}

// Make the image passed in highlighted (menu_arrow_over_xxx.gif)
function menuArrowHighlight (imageObj) {
	imageObj.src = imageObj.src.replace (/menu_arrow_up/, "menu_arrow_over_up");
	imageObj.src = imageObj.src.replace (/menu_arrow_down/, "menu_arrow_over_down");
}

// Make the image passed in not highlighted (menu_arrow_xxx.gif)
function menuArrowLowlight (imageObj) {
	imageObj.src = imageObj.src.replace (/menu_arrow_over_up/, "menu_arrow_up");
	imageObj.src = imageObj.src.replace (/menu_arrow_over_down/, "menu_arrow_down");
}

// Set the cookie to record which link was clicked, and optionally which submenu is open
function setMenuCookie (linkHrefId, submenuId, whichMenu) {
	DeleteCookie("cookie_clicked_id");	// delete current menu item highlight cookie
	DeleteCookie("showSubMenuID");		// delete current submenu selected cookie
	DeleteCookie("cookie_menuid");		// Delete current menu id.
	
	SetCookie ("cookie_clicked_id", linkHrefId);// Remember which link was clicked
	
	// If submenu id and which menu identifier were passed into this function then set more cookies
	if (xDef(submenuId) && xDef(whichMenu)) {
		SetCookie ("showSubMenuID", submenuId);	// If a submenu id was passed, remember that too
		SetCookie ("cookie_menuid", whichMenu);	// M if main menu, P if page menu
	}
	
	// Code from old menu system.. Stores products position for going back from applications?
	var pagehref = window.document.location.href.toLowerCase();
	if (pagehref.indexOf('/products/')!= -1) {
		DeleteCookie("productback2","/");				//delete old back location
		SetCookie("productback2", pagehref, null, "/");	//set new back location
	}	
	return true;	// True = follow the hyperlink now that cookie is set
}

// Highlights the menu item with the id passed in using the highlight class.
// Optional subMenuId=id of submenu to open and whichMenu=m or p for main menu or page menu
function setMenuItemActive (linkHrefId, subMenuId, whichMenu) {
	// If it is a valid ID
	if (xGetElementById(linkHrefId)) {
		// If it's a submenu opener with a hyperlink give it special class as it has other css styles
		if (xGetElementById(linkHrefId).className == 'linkingSub') {
			xGetElementById(linkHrefId).className = 'linkingSubHL'
		} else {
			xGetElementById(linkHrefId).className = 'highlight';
		}

		// If a submenu id AND an identifier of the menu it is on was passed in
		if (xDef(subMenuId) && xDef(whichMenu)) {
			
			if (whichMenu.toUpperCase() == "M") {	// If it is on the main menu, toggle correct submenu.
				togglemainSubmenu (subMenuId);
				focusItemWhenMenuOpenedId = linkHrefId;	// Focus this link when the main menu is opened
				// Attempt to highlight a corresponding entry on the pagemenu (if one exists) - they should have the same ID in the buildmenu call
				setMenuItemActive (linkHrefId.slice(0,-1) + "p")
			} else {
				togglepageSubmenu (subMenuId);
			}
			// Scroll to the highlighed item in 0 ms (otherwise cannot focus item)
			window.setTimeout("try{xGetElementById(\"" + linkHrefId + "\").focus()}catch(e){}",2)
		}
		// If this link is on pagemenu, Attempt to highlight a corresponding entry on the main menu (if one exists) - they should have the same ID in the buildmenu call
		if (linkHrefId.charAt(linkHrefId.length-1) == 'p') {
			
			setMenuItemActive (linkHrefId.substr(0,linkHrefId.length-1) + "m")
		}
	}
}

// --------------  set the location of the menu ---------
function setLocation(){
	// find location of click here bar (menucolor) and set menudiv to the same location
	var topOfBar = xPageY("menucolor")+menuHeightAdjust
	if (topOfBar < xScrollTop()) {		// If the top of menu bar is above the screen
		topOfBar = xScrollTop() + 7		// Set top of menu to 7px below top of screen
	}
	return topOfBar
}

// Update the position of the pagemenu, called every 30 ms once SetSubMenuLocation() is called
function updatePagemenuPosition() {
	// Find where the Y location of the menu would be if it was moved now
	pagemenuNewY = setLocation();
	pagemenuCurrentY = xPageY("submenuscolldiv");
	//
	// If menu is above where pagemenu started, slide to start position instead
	if(pagemenuNewY < pagemenuStartPositionY){
		pagemenuNewY = pagemenuStartPositionY;
	}	

	// Calculate a difference which can be added to current position to make menu slide	
	var dy = (pagemenuNewY - pagemenuCurrentY) / 7;	// number controls speed/accel

	// Round number up or down so that it doesnt get stuck on 0.xxx (cant move by 0.5!)
	if (dy > 0) {
		dy = Math.ceil(dy);
	} else {
		dy = Math.floor(dy);
	}
	
	// Move by amount calculated above.. gives smooth slide effect
	xTop("submenuscolldiv", pagemenuCurrentY + dy);
	//xMoveTo("submenuscolldiv", xPageX("sublayout")+10, pagemenuCurrentY + dy);

}		
var pagemenuStartPositionX = 30;
var loopStop = 0;
// Start the interval timer to run the pagemenu scrolling function above
function SetSubMenuLocation() {
	
	// If we can get the top of the pagemenu div, set up interval scrolling

if (loopStop == 0){
			setTimeout("SetSubMenuLocation()",100);
		}else{
		if (document.GetElementById("submenuscolldiv")) {
			// Remember where the pagemenu was when page loaded (now)
			pagemenuStartPositionY = xPageY("submenuscolldiv");
			if (xPageX("TE_BreadCrumb")>0){
				pagemenuStartPositionX = xPageX("TE_BreadCrumb")+14
			}else{
				pagemenuStartPositionX = xPageX("submenuscolldiv");
			}
			// Set pagemenu's coords to where it is at the moment so it doesnt jump when slided
			xMoveTo("submenuscolldiv", pagemenuStartPositionX, pagemenuStartPositionY);
			// Run interval function every so often to slide pagemenu (in ms)
			setInterval("updatePagemenuPosition()", 30)
			
		}else{
			loopStop += 1
			if (loopStop <= 20){
				setTimeout("SetSubMenuLocation()",10);
		}
		}
	}
}
//Kick off the scrolling function in footer !!!

//window.onresize = setLeftPageMenu;

function setLeftPageMenu() {
	// reset the manu menu location
/*	xTop("menudiv",setLocation()+5)
	xLeft("menudiv",xOffsetLeft("TE_outerDiv")+20)
	*/// reset the pagemenu
	pagemenuStartPositionX = xPageX("TE_BreadCrumb");
	if (pagemenuStartPositionX < 36) {pagemenuStartPositionX = 36}
	xMoveTo("submenuscolldiv", pagemenuStartPositionX, pagemenuStartPositionY);

}

// change this function to use show hide need to change style sheet frist
function showmenu(show){

	if(!OverMenu){
		if (show){
			if (dojo.byId("menudiv").style.visibility == "visible"){
				xHide("menudiv")
			} else {
				// set the location of the menu then show it
				xTop("menudiv",setLocation()+5)
				xLeft("menudiv",xOffsetLeft("TE_outerDiv")+20)
				xShow("menudiv")
				if (xHeight("menucolor") <= xHeight("menudiv")+xTop("menudiv")){
					xHeight("menucolor",xHeight("menudiv")+xTop("menudiv"))// change the height chick here bar
				}
				// If there is a item to scroll to when menu opened, do it!
				if (focusItemWhenMenuOpenedId != 0) {
					window.setTimeout("try{dojo.byId(" + focusItemWhenMenuOpenedId + ").focus()}catch(e){}",0)
					focusItemWhenMenuOpenedId = 0;
				}
			}
		} else {	// false passed in = close the menu
			xHide("menudiv")
		}
	}else{
		OverMenu = false
	}
}

// Deletes all cookies relating to menu, then uses parameters to set the given item id(s) active.
function highlightMenuItems(cookie_clicked_id, cookie_menuid, showSubMenuID) {
	
	// delete and reset cookie that holds the clicked item used to diplay item on page load
	// (refresh page should remove highlighted item!
	var deleteCookieCount = 0
	try{
		if (cookie_clicked_id != ""){
			do
			{
				DeleteCookie("cookie_clicked_id")
				deleteCookieCount++
			}while (GetCookie ("cookie_clicked_id") != "" && deleteCookieCount < 5)
		}
		if (cookie_menuid != ""){
			deleteCookieCount = 0
			do
			{
				DeleteCookie("cookie_menuid")
				deleteCookieCount++
			}while (GetCookie ("cookie_menuid") != "" && deleteCookieCount < 5 )			
		}
		if (showSubMenuID != ""){
			deleteCookieCount = 0
			do
			{
				DeleteCookie("showSubMenuID")
				deleteCookieCount++
			}while (GetCookie ("showSubMenuID") != "" && deleteCookieCount < 5 )			

		}
	}catch(S){}
	
	// Just incase the highlight and submenu opening code breaks...
	try {
		// If no submenu id, just pass the href id, otherwise pass them both (open submenu too)
		if (showSubMenuID.length == 0) {
			setMenuItemActive(cookie_clicked_id);
		} else {
			setMenuItemActive(cookie_clicked_id, showSubMenuID, cookie_menuid);
		}
	} catch (E) { }
	
}
function setPageHeight(){
try{
	if (dojo.byId("menucolor") && (dojo.byId("menucolor").style.height <= dojo.html.getViewportHeight() - 180)){
		dojo.byId("menucolor").style.height = (dojo.html.getViewportHeight() - 180);
	}
}catch(e){}
try{
	if (dojo.byId("Page_menutd") && (dojo.byId("Page_menutd").style.height <= dojo.html.getViewportHeight() - 280)){
		dojo.byId("Page_menutd").style.height = (dojo.html.getViewportHeight() - 180);
	}
}catch(e){}	
try{		
	if (dojo.byId("subLayout") && (dojo.byId("subLayout").style.height <= dojo.html.getViewportHeight() - 280)){
		dojo.byId("subLayout").style.height = (dojo.html.getViewportHeight() - 180);
	}
}catch(e){}


	return true
}
		
