/* Init */

// Arrays to hold the menu data from parsed flash xml file
var dsMenu = new Array("<a href='/Products_and_Services/index.cfm' style='text-indent:0px;' title='Products and Services'>Products and Services</a>","<a href='/Products_and_Services/Drilling_Solutions/Drilling%20Solutions%20Overview.cfm' style='text-indent:5px;' title='Drilling Solutions'>Drilling Solutions Overview</a>");
var esMenu = new Array("<a href='/Products_and_Services/index.cfm' style='text-indent:0px;' title='Products and Services'>Products and Services</a>","<a href='/Products_and_Services/Environmental_Solutions/Environmental%20Solutions%20Overview.cfm' style='text-indent:5px;' title='Environmental Solutions'>Environmental Solutions Overview</a>");
var ptMenu = new Array("<a href='/Products_and_Services/index.cfm' style='text-indent:0px;' title='Products and Services'>Products and Services</a>","<a href='/Products_and_Services/Production_Technologies/Production%20Technologies%20Overview.cfm' style='text-indent:5px;' title='Production Technologies'>Production Technologies Overview</a>");
var wpMenu = new Array("<a href='/Products_and_Services/index.cfm' style='text-indent:0px;' title='Products and Services'>Products and Services</a>","<a href='/Products_and_Services/Wellbore_Productivity/Wellbore%20Productivity%20Overview.cfm' style='text-indent:5px;' title='Wellbore Productivity'>Wellbore Productivity Overview</a>");
var ifeMenu = new Array();

// Toggle icons
var toggleTo;
var toggleCharacterContract = "<img src='/Images/toggleCharacterContract.gif' width='13' height='13' border='0' align='absmiddle' alt=' [-] ' title=' [-] ' />";
var toggleCharacterExpandPlus = "<img src='/Images/toggleCharacterExpand.gif' width='13' height='13' border='0' align='absmiddle' alt=' [+] ' title=' [+] ' />";
var toggleCharacterExpandAlt = "<img src='/Images/toggleCharacterExpandAlt.gif' width='13' height='13' border='0' align='absmiddle' alt=' [+] ' title=' [+] ' />";
var toggleCharacterExpand = toggleCharacterExpandPlus;
var toggleCharacter = toggleCharacterExpand;

// Set vars to be able to close open menu
var openMenuID = null;
var openMenuChildCount = null;
var openMenuName = null;
var menuID = "";
var el = null;
var newTop;
var checkOpenPosition = true;
var t;
var flashClicked = false;

/* ------------------------------------------------------------------------ */

function thisMovie(movieName) {
	 if (navigator.appName.indexOf("Microsoft") != -1) {
			 return window[movieName];
	 } else {
			 return document[movieName];
	 }
}

// String replacement function
function replaceInString(strInput,strToReplace,strWithThis) {
	var arrSplitString = strInput.split(strToReplace);
	var strReturn = arrSplitString.join(strWithThis);
	return strReturn; 
}

// The following functions basically say if you have (a) ? (b) : (c) then, evaluate A, if true, return B, else return C
function getX(obj) {
  return( obj.offsetParent==null ? obj.offsetLeft : obj.offsetLeft+getX(obj.offsetParent) );
}

function getY(obj) {
  return( obj.offsetParent==null ? obj.offsetTop : obj.offsetTop+getY(obj.offsetParent) );
}

// Reset the menu
function resetMenu() {	
	document.getElementById('solutionsMenu').innerHTML = "";
	openMenuID = null;
	openMenuChildCount = null;
}

// Hide the menus
function hideMenu(fromTimer) {	
	if (fromTimer == true) {
		if (flashClicked == true) {
			thisMovie("FlashPageHeader").sendToActionScript("callResetButtons");
			flashClicked = false;
		}
	}
	document.getElementById('solutionsMenu').style.display = "none";
	openMenuID = null;
	openMenuChildCount = null;
}

// Timer function to hide the menu
function startTimer() {
	clearTimeout(t);
	t = setTimeout("hideMenu(true)",1000);
}

function clearTimer() {
	clearTimeout(t);
}

// If the browser window is re-sized, reset the menus
window.onresize = function() {
	positionContainerBox();
}

// Reset the menus when the window unloads
window.onunload = function() {
	resetMenu();	
}

// Move the container box to the correct position on screen
function positionContainerBox(menuName,xPos,yPos,homePage) {
	
	// Get the true left and top of an object to relationally place the box at
	var relObj = document.getElementById('flashHeaderDiv');
	var moveObj = document.getElementById('solutionsMenuContainer');
	
	var relObjL = getX(relObj);
	var relObjT = getY(relObj);
	var relObjW = relObj.offsetWidth;
	var relObjH = relObj.offsetHeight;
	
	var moveObjX = relObjW + relObjL;
	var moveObjY = relObjT + relObjH;
	var moveObjW = 230;
	
	// Adjust Y for FireFox
	if (parseInt(navigator.appVersion) > 3) {
	 if (navigator.appName == "Netscape") {
	 	if (homePage == "true") {
			moveObjY = moveObjY + 6;
		}
		else {
			moveObjY = moveObjY - 0;	
		}
	 }
	}
	
	moveObj.style.display = "block";
	moveObj.style.left = eval(moveObjX - moveObjW - xPos) + "px";
	moveObj.style.top = eval(moveObjY - yPos) + "px";
	
	// Get browser info (to shrink menu width in IE6 on homepage)
	bName = navigator.appName;
	bVer = parseInt(navigator.appVersion);
	
	if (homePage == false || homePage == "false") {
		if (menuName.toLowerCase() == "ifemenu") {
			document.getElementById('solutionsMenuContainer').style.width = "200px";
		}
		else {
			document.getElementById('solutionsMenuContainer').style.width = "270px";
		}
	}
	else {
		if (bName == "Microsoft Internet Explorer" && typeof document.body.style.maxHeight == "undefined") {
			document.getElementById('solutionsMenuContainer').style.width = "275px";
		}
	}
	
}

// Make sure that the parent link isn't clipped at the top
function checkOpenTitlePosition() {
	if (openMenuID != null) {
		var titleElementY = getY(document.getElementById(openMenuID));
		var compareElementY = getY(document.getElementById('flashHeaderDiv'));
		if (titleElementY < compareElementY + 20) {
			document.getElementById('solutionsMenu').style.top = (document.getElementById(openMenuID).offsetHeight / 2) + "px";
		}	
	}
}

// Build the menu (called from Flash for loop)
function buildMenu(menuName,menuTitle,menuLink,indent,menuChildCount,menuChildNumber) {	
	var childID;
	var newLink;
	
	if (indent == true) {
		// Child Links
		childID = parentID + eval(menuChildNumber);
		newLink = "<a href='/" + menuLink + "' class='solutionsMenuSubItem' style='display:none;' title='" + menuTitle + "' id='" + childID + "'> &middot; " + menuTitle + "</a>";	
	}
	else {
		if (eval(menuChildCount) > 1) {
			// Main Links w/Child Links
			parentID = replaceInString(menuTitle," ","");
			if (menuTitle != "Integrated Fluids Engineering") {
				newLink = "<a href='#' onclick='toggleSubMenu(this.title," + eval(menuChildCount) + ");return false;' style='text-indent:0px;' title='" + menuTitle + "' id='" + parentID + "'>" + toggleCharacter + menuTitle + "</a>";	
			}
			else {
				newLink = "<a href='/" + menuLink + "' style='text-indent:10px;' title='" + menuTitle + "'>" + menuTitle + "</a>";
			}
		}
		else {
			// Main Links
			newLink = "<a href='/" + menuLink + "' style='text-indent:10px;' title='" + menuTitle + "'>" + menuTitle + "</a>";
		}
	}	
	eval(menuName).push(newLink);
}


// Show the menu (called from Flash)
function showMenu(menuName,xPos,yPos,homePage) {
	resetMenu();
	hideMenu();
	flashClicked = true;
	
	document.getElementById('solutionsMenu').innerHTML = "";
	for (i=0;i<eval(menuName).length;i++) {
		document.getElementById('solutionsMenu').innerHTML += eval(menuName)[i];
	}
	document.getElementById('solutionsMenu').style.display = "block";
	positionContainerBox(menuName,xPos,yPos,homePage);
}

// Close the currently open menu
function closeOpenMenu(menuName,childCount,openNewMenu) {
			
	// Hide the children links
	for (i=0;i<eval(openMenuChildCount);i++) {
		el = openMenuID + i;			
		document.getElementById(el).style.display = "none";
	}
	
	// Change the toggle icon
	document.getElementById(menuID).innerHTML = toggleCharacterExpand + openMenuName;
	
	// Reset the open menu variables
	openMenuChildCount = null;
	openMenuID = null;
	openMenuName = null;
	
	document.body.focus();
	
	if (openNewMenu == true) {
		toggleSubMenu(menuName,childCount);	
	}
}

// Show/Hide the sub menu
function toggleSubMenu(menuName,childCount) {
	
	// Check to see if any menus are open
	if (openMenuID != null && openMenuName == menuName) {
		closeOpenMenu(menuName,childCount,false);
		return;
	}
	else if (openMenuID != null && openMenuName != menuName) {
		closeOpenMenu(menuName,childCount,true);
		return;
	}
	else {
		
		menuID = menuName;
		menuID = replaceInString(menuID," ","");
		el;
		
		// Toggle the children links
		for (i=0;i<eval(childCount);i++) {
			el = menuID + i;
			
			// Customize the top and bottom elements
			if (i == 0) {
				//document.getElementById(el).style.padding = "10px 5px 2px 5px";	
				document.getElementById(el).style.borderTop = "solid 1px #bbbbbb";
			}
			if (i == eval(childCount) - 1) {
				//document.getElementById(el).style.padding = "2px 5px 10px 5px";
				document.getElementById(el).style.borderBottom = "solid 1px #bbbbbb";
			}
			
			if (parseInt(navigator.appVersion) > 3) {
				if (navigator.appName == "Netscape") {
					if (document.getElementById(el).style.display == "none") {
						toggleTo = "block";
						toggleCharacter = toggleCharacterContract;
					}
					else {
						toggleTo = "none";
						toggleCharacter = toggleCharacterExpand;
					}
				}
				if (navigator.appName.indexOf("Microsoft")!= -1) {
					if (document.getElementById(el).style.display == "") {
						toggleTo = "none";
						toggleCharacter = toggleCharacterExpand;
					}
					else {
						toggleTo = "";
						toggleCharacter = toggleCharacterContract;
					}
				}
			}
			
			document.getElementById(el).style.display = toggleTo;
		}
		
		// Change the toggle icon
		document.getElementById(menuID).innerHTML = toggleCharacter + menuName;
		
		// Set the open menu variables
		openMenuChildCount = eval(childCount);
		openMenuID = menuID;
		openMenuName = menuName;
		
		document.body.focus();
		
	}
	
}

// Load Solutions Page (called from Flash)
function viewSolutions(solutionURL) {
	document.location = solutionURL;
}



