var fShow = "visible";
var fHide = "hidden";

document.body.onclick=hideAll;
document.body.onscroll=hideAll;
document.body.onmousemove=updateIt;

function setTopNavHighlight() {
	var topNavLinks = [["","home_id"],["index.html","home_id"],["our_business","our_business_id"],["in_the_community","community_id"],["news","news_id"],["investor_relations","investor_relations_id"],["careers","careers_id"]];
	var finalString;
	var url = window.location.href;

	if (url.indexOf('investors.mirant.com') != -1) {
		if (url.indexOf('releases.cfm') != -1) {
			finalString = 'news';
		} else {
				finalString = 'investor_relations';
		}
	} else if (url.indexOf('jobs-mirant.icims.com') != -1) {
			finalString = 'careers';
	} else {
			var index1 = url.indexOf('//');
			var string1 = url.substring(index1+2);
			var index2 = string1.indexOf('/');
			var string2 = string1.substring(index2+1);
			var index3 = string2.indexOf('/');
			finalString = string2.substring(0,index3);
	}
	
	for (var x = 0; x < topNavLinks.length; x++) {
		if (finalString == topNavLinks[x][0]) {
			document.getElementById(topNavLinks[x][1]+'_cell').style.backgroundColor = '#FFFFBA';
			document.getElementById(topNavLinks[x][1]+'_underline').style.backgroundImage = 'url(/images/pix_ffcf31_2px.gif)';
			document.getElementById(topNavLinks[x][1]+'_underline').style.backgroundRepeat = 'repeat-x';
			document.getElementById(topNavLinks[x][1]+'_underline').style.backgroundPosition = 'top';
			break;
		}
	}
}

function Menu() {
	this.bgColor     = "#FFFFFF";
	this.fontColor   = "#003366";
	this.addItem    = addItem;
	this.addSubItem = addSubItem;
	this.showMenu   = showMenu;
	this.mainPaneBorder = 0;
	this.subMenuPaneBorder = 0;
	this.subMenuPaneWidth = 170;

	lastMenu = null;
	rightY = 0;
	leftY = 0;
	rightX = 0;	
	leftX = 0;

	HTMLstr = "";
	HTMLstr += "<!-- MENU PANE DECLARATION BEGINS -->\n";
	HTMLstr += "\n";
	HTMLstr += "<table id='MainTable' align='left' cellspacing='0' cellpadding='0' bgcolor='"+this.bgColor+"' style='border-top: 1px solid #003366'>\n";	
	HTMLstr += "<tr id='mainRow'>\n";
	HTMLstr += "<!-- MAIN MENU STARTS -->\n";
	HTMLstr += "<td width='5' height='18'>&nbsp;</td>\n"; 
	HTMLstr += "<!-- MAIN_MENU -->\n";
	HTMLstr += "<!-- MAIN MENU ENDS -->\n";
	HTMLstr += "</tr>\n";
	HTMLstr += "<tr><td id='bottomLine' height='1' colspan='13' style=\"background-image: url(/images/pix_003366.gif); background-repeat: repeat-x; background-position: top\"></td></tr>\n";	
	HTMLstr += "<tr height='2' style='border-top: 1px solid #003366'>";	
	HTMLstr += "<td id='spacer1'></td>";
	HTMLstr += "<td id='home_id_underline'></td>";
	HTMLstr += "<td id='spacer2'></td>";
	HTMLstr += "<td id='our_business_id_underline'></td>";
	HTMLstr += "<td id='spacer3'></td>";
	HTMLstr += "<td id='community_id_underline'></td>";
	HTMLstr += "<td id='spacer4'></td>";
	HTMLstr += "<td id='news_id_underline'></td>";
	HTMLstr += "<td id='spacer5'></td>";
	HTMLstr += "<td id='investor_relations_id_underline'></td>";
	HTMLstr += "<td id='spacer6'></td>";
	HTMLstr += "<td id='careers_id_underline'></td>";
	HTMLstr += "<td id='spacer7'></td>";
	HTMLstr += "<td id='spacer8'></td>";		
	HTMLstr += "</tr>";
	HTMLstr += "</table>\n";
	HTMLstr += "\n";
	HTMLstr += "<!-- SUB MENU STARTS -->\n";
	HTMLstr += "<!-- SUB_MENU -->\n";
	HTMLstr += "<!-- SUB MENU ENDS -->\n";
	HTMLstr += "\n";
	HTMLstr += "<!-- MENU PANE DECALARATION ENDS -->\n";
}

function addItem(idItem, text, cellWidth, expandable, location, altLocation) {
	var careers_id;
	var mainItem = eval('idItem');
	var MENUitem = "";
	var Lookup = "<!-- ITEM "+idItem+" -->";

	if (HTMLstr.indexOf(Lookup) != -1) {
		alert(idParent + " already exist");
		return;
	}

	MENUitem += "\n<!-- ITEM "+idItem+" -->\n";
	MENUitem += "<td id='"+idItem+"_cell' height='18' nowrap valign='middle' ";
	
	if (expandable) {
		MENUitem += "onmouseover=\"hideAll(); document.getElementById('"+idItem+"_cell').style.backgroundColor = '#FFFFBA';  displaySubMenu('"+idItem+"')\" onmouseout=\"this.style.backgroundColor = ''; document.getElementById('"+idItem+"_underline').style.backgroundImage = ''; setTopNavHighlight()\" >\n";	
	} else {
			MENUitem += "onmouseover=\"hideAll(); this.style.backgroundColor = '#FFFFBA'; document.getElementById('"+idItem+"_underline').style.backgroundImage = 'url(/images/pix_ffcf31_2px.gif)'; document.getElementById('"+idItem+"_underline').style.backgroundRepeat = 'repeat-x'; document.getElementById('"+idItem+"_underline').style.backgroundPosition = 'top'\" onmouseout=\"this.style.backgroundColor = ''; document.getElementById('"+idItem+"_underline').style.backgroundImage = ''; setTopNavHighlight()\" >\n";		
	}
	
	MENUitem += "<div id='"+idItem+"' >\n";
	MENUitem += "<a style=\"vertical-align: 'middle'\" ";
	MENUitem += "class=top_nav_bold onmouseover=\"this.style.color = '"+this.fontColor+"'\" ";
	
	if (location != null) {
		MENUitem += "href='"+location+"' ";
	} else {
			if (altLocation != null) {
				MENUitem += "href='"+altLocation+"' ";
			} else {
				MENUitem += "href='.' ";
				MENUitem += "onclick=\"return false;\" "
			}
	}
	
	MENUitem += ">&nbsp;&nbsp;";
	MENUitem += text;
	MENUitem += "&nbsp;&nbsp;</a>\n";
	MENUitem += "</div>\n";
	MENUitem += "</td>\n";
	MENUitem += "<!-- END OF ITEM "+idItem+" -->\n\n";
	
	if (mainItem != careers_id) MENUitem += "<td height='18' width='5'>&nbsp;</td>\n";
	
	MENUitem += "<!-- MAIN_MENU -->\n";
	HTMLstr = HTMLstr.replace("<!-- MAIN_MENU -->\n", MENUitem);
}

function addSubItem(idParent, text, location) {
	var MENUitem = "";
	
	Lookup = "<!-- ITEM "+idParent+" -->";
	if (HTMLstr.indexOf(Lookup) == -1) {
		alert(idParent + " not found");
		return;
	}
	
	Lookup = "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->";
	if (HTMLstr.indexOf(Lookup) == -1) {
		MENUitem += "\n";
		MENUitem += "<div id='"+idParent+"submenu' style='position: absolute; margin-top: -3px; visibility: hidden; z-index: 1; font: "+this.menuFont+";'>\n";
		MENUitem += "<table cellspacing='0' border='0' cellpadding='0' bgcolor='"+this.bgColor+"' ";
		MENUitem += "style='border: 1px solid #FFCC33; border-top: 2px solid #FFCC33' ";
		MENUitem += "onmouseover=\"document.getElementById('"+idParent+"_cell').style.backgroundColor='#FFFFBA';\" \n";
		MENUitem += "onmouseout=\"document.getElementById('"+idParent+"_cell').style.backgroundColor= '"+this.bgColor+"';  document.getElementById('"+idParent+"_underline').style.backgroundImage = ''; setTopNavHighlight()\"> \n";
		MENUitem += "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
		MENUitem += "</table>\n";
		MENUitem += "</div>\n";
		MENUitem += "\n";
		MENUitem += "<!-- SUB_MENU -->\n";
		HTMLstr = HTMLstr.replace("<!-- SUB_MENU -->\n", MENUitem);
	}

	Lookup = "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
	MENUitem = "<tr><td style='padding: 3px 3px 3px 3px' nowrap onmouseover=\"this.style.backgroundColor = '#FFFFBA'\" onmouseout=\"this.style.backgroundColor = ''; setTopNavHighlight()\"><a class='top_nav_regular' href='"+location+"' onmouseover=\"this.style.color = '"+this.fontColor+"'\">"+text+"</a></td></tr>\n";
	MENUitem += Lookup;
	HTMLstr = HTMLstr.replace(Lookup, MENUitem);
}

function showMenu() {
	document.writeln(HTMLstr);
}

function displaySubMenu(idMainMenu) {
	var menu;
	var submenu;
	var submenuText = 'submenu';

	//alert(navigator.platform);	
	//alert(navigator.userAgent);
		
	menu = document.getElementById(idMainMenu);
	submenu = document.getElementById(idMainMenu + submenuText);
	submenu.style.left = calculateSumOffset(menu, 'offsetLeft') + 'px';
	
	if ((navigator.platform).indexOf("Mac") != -1) {			
		submenu.style.top  = (calculateSumOffset(menu, 'offsetTop') + 22) + 'px';
		submenu.style.visibility = fShow;

		if (lastMenu != null && lastMenu != submenu) hideAll();
		
		leftX  = parseInt(document.getElementById(idMainMenu + submenuText).style.left);
		rightX = leftX + document.getElementById(idMainMenu + submenuText).offsetWidth;
		leftY  = parseInt(document.getElementById(idMainMenu + submenuText).style.top) - 22;
		rightY = parseInt(document.getElementById(idMainMenu + submenuText).style.top) + document.getElementById(idMainMenu + submenuText).offsetHeight;
		
		//alert(leftX);
		//alert(rightX);
		//alert(leftY);
		//alert(rightY);
		//alert(document.documentElement.scrollTop);
	} else	if ((navigator.appName).indexOf("Microsoft") != -1 && (navigator.userAgent).indexOf("Opera") == -1) {			
		submenu.style.top  = (calculateSumOffset(menu, 'offsetTop') + 20) + 'px';
		submenu.style.visibility = fShow;

		if (lastMenu != null && lastMenu != submenu) hideAll();
		
		leftX  = parseInt(document.getElementById(idMainMenu + submenuText).style.left);
		rightX = leftX + document.getElementById(idMainMenu + submenuText).offsetWidth;
		leftY  = parseInt(document.getElementById(idMainMenu + submenuText).style.top) - 20;
		rightY = parseInt(document.getElementById(idMainMenu + submenuText).style.top) + document.getElementById(idMainMenu + submenuText).offsetHeight;
		
		//alert(leftX);
		//alert(rightX);
		//alert(leftY);
		//alert(rightY);
		//alert(document.documentElement.scrollTop);
	} else if ((navigator.userAgent).indexOf("Opera") != -1) {
			menu = document.getElementById(idMainMenu);
			submenu = document.getElementById(idMainMenu + submenuText);
			submenu.style.left = calculateSumOffset(menu, 'offsetLeft') + 'px';
			submenu.style.top  = (calculateSumOffset(menu, 'offsetTop') + 17) + 'px';
			submenu.style.visibility = fShow;

			if (lastMenu != null && lastMenu != submenu) hideAll();
		
			leftX  = parseInt(document.getElementById(idMainMenu + submenuText).style.left);
			rightX = leftX + document.getElementById(idMainMenu + submenuText).offsetWidth;
			leftY  = parseInt(document.getElementById(idMainMenu + submenuText).style.top) - 17;
			rightY = parseInt(document.getElementById(idMainMenu + submenuText).style.top) + document.getElementById(idMainMenu + submenuText).offsetHeight;
			//alert(leftX);
			//alert(rightX);
			//alert(leftY);
			//alert(rightY);
			//alert(document.documentElement.scrollTop);		
	} else {
			menu = document.getElementById(idMainMenu);
			submenu = document.getElementById(idMainMenu + submenuText);
			submenu.style.left = calculateSumOffset(menu, 'offsetLeft') + 'px';
			submenu.style.top  = (calculateSumOffset(menu, 'offsetTop') + 18) + 'px';
			submenu.style.visibility = fShow;

			if (lastMenu != null && lastMenu != submenu) hideAll();
		
			leftX  = parseInt(document.getElementById(idMainMenu + submenuText).style.left);
			rightX = leftX + document.getElementById(idMainMenu + submenuText).offsetWidth;
			leftY  = parseInt(document.getElementById(idMainMenu + submenuText).style.top) - 18;
			rightY = parseInt(document.getElementById(idMainMenu + submenuText).style.top) + document.getElementById(idMainMenu + submenuText).offsetHeight;
			//alert(leftX);
			//alert(rightX);
			//alert(leftY);
			//alert(rightY);
			//alert(document.documentElement.scrollTop);		
	}
	
	lastMenu = submenu;
}

function hideAll() {
	if (lastMenu != null) {
		lastMenu.style.visibility = fHide;
		lastMenu.left = 0;
	}
}

function calculateSumOffset(idItem, offsetName) {
	var totalOffset = 0;
	var item = eval('idItem');
	
	do {
		totalOffset += eval('item.'+offsetName);
		item = eval('item.offsetParent');
	} while (item != null);
	
	return totalOffset;
}

function updateIt(e) {
	var pixsFromTop = 0;
	var pixsFromLeft = 0;
	
	if (document.documentElement && document.documentElement.scrollTop) {
		pixsFromTop = document.documentElement.scrollTop;
	} else if (document.body) {
			pixsFromTop = document.body.scrollTop;
	}
	if (document.documentElement && document.documentElement.scrollLeft) {
		pixsFromLeft = document.documentElement.scrollLeft;
	} else if (document.body) {
			pixsFromLeft = document.body.scrollLeft;
	}
	
	if (document.all)	{
		var x = window.event.clientX + pixsFromLeft;
		var y = window.event.clientY + pixsFromTop;

		if (x > rightX || x < leftX) hideAll();
		else if (y > rightY || y < leftY) hideAll();
	} else {
		var x = e.pageX;
		var y = e.pageY;

		if (x > rightX || x < leftX) hideAll();
		else if (y > rightY || y < leftY) hideAll();
	}
}