var bIE4 = false, bNS4 = false, bNS6 = false;
var iXOffset = 10, iYOffset = 10;

if(navigator.appName == "Microsoft Internet Explorer") {
	bIE4 = true;
	addMask();
}
else if(navigator.appName == "Netscape") {
	bNS6 = true;
}
else {
	bNS4 = true;
}

function popupon(sXYT, oEvent)
{ 
	var iScreenWidth = screen.width;
	var iScreenHeight = screen.height - 300;
	var oLayer;

	if(sXYT.indexOf("|") < 0)
		return;

	var aXY = sXYT.split("|");
	
	var x = parseInt(aXY[0]);
	var y = parseInt(aXY[1]);

	var sTable;
	var i, iWidth, iHeight, iLayerX, iLayerY, iIndex, iThreshIndex, iMetricIndex;
	var aValue, aMetric, aTemp;
	
	var sEvenOdd = "odd";
	
	var onlyException = false;
	if(aNameZ.length > 10)
		onlyException = true;
		
	iIndex = x + (y * aNameX.length);
	
	iWidth = 220;
	iHeight = 200;
		
	//alert(x + "\n" + y + "\n" + aNameX.length + "\n" + iIndex + "\n" + aData[iIndex] + "\n" + aError[iIndex]);

	sTable = "<table>";
	switch(sType)
	{
		case "grid":
			sTable += "<tr><td colspan=\"6\" class=\"header\">" + getTitle(x, y) + "</td></tr>";
			break;
		case "raw":
		case "agg":
			sTable += "<tr><td colspan=\"6\" class=\"header\">" + aNameY[y] + "<br />" + aNameX[x];
			if(sType == "raw" && aAgentName != null && aAgentName[iIndex] != "")
				sTable += "<br />Agent: " +  aAgentName[iIndex];
			if(sType == "raw" && aIP != null && aIP[iIndex] != "")
				sTable += "<br />IP: " +  aIP[iIndex];
			sTable += "</td></tr>";
			break;
	}
	
	aValue = aData[iIndex].split("|");
	if(sType == "raw" && aError != null && aError[iIndex] != "Success")
	{
		iHeight = 50;
		sTable += "<tr><td colspan=\"6\" class=\"error\">" + aError[iIndex] + "</td></tr>";
	}
	else
	{
		if(sType == "grid")
			iThreshIndex = getIndexFromCoordinates("Destination", x, y, 0) + (getIndexFromCoordinates("Origin", x, y, 0) * getWhichArray("Destination").length);
		else
			iThreshIndex = y; // Varies only on agent/target combo
		var showNormal = true;
		aThreshValue = aThresh[iThreshIndex].split("|");
		aTest = aThreshValue[0].split(";");
		if(aTest[0] == "NA" || aTest[0] == 0)
			showNormal = false;
		sTable += "<tr class=\"severity\"><td></td>";
		sTable += "<td>Current</td>";	
		sTable += "<td></td>";
		if(showNormal)
			sTable += "<td>Normal</td>";
		sTable += "<td>Warning</td>";
		sTable += "<td>Critical</td></tr>";
		var count = 0;
				
		for(i = 0; i < aNameZ.length; i++)
		{
			if(sType == "grid")
			{
				iThreshIndex = getIndexFromCoordinates("Destination", x, y, i) + (getIndexFromCoordinates("Origin", x, y, i) * getWhichArray("Destination").length);
				iMetricIndex = getIndexFromCoordinates("Metric", x, y, i);
			}
			else
			{
				iThreshIndex = y; // Varies only on agent/target combo
				iMetricIndex = i; // Perhaps do this (and line before) by modifying DrawChartPopup()
			}
			aThreshValue = aThresh[iThreshIndex].split("|");
			aTemp = aValue[i].split(";");
			aThreshTemp = aThreshValue[iMetricIndex].split(";");
			if(aTemp[1] != "NA")
			{
				switch(sUnitAxis)
				{
					case "nAxis":
						sCurUnit = sUnit;
						break;
					case "xAxis":
						sCurUnit = aUnit[x];
						break;
					case "yAxis":
						sCurUnit = aUnit[y];
						break;
					case "zAxis":
						sCurUnit = aUnit[i];
						break;
				}
				
				switch(aTemp[0])
				{
					case "w":
						aTemp[0] = "null";
						break;
					case "g":
						aTemp[0] = "healthy";
						break;
					case "y":
						aTemp[0] = "warning";
						break;
					case "r":
						aTemp[0] = "critical";
						break;
					default:
						aTemp[0] = "healthy";
						break;
				}
				
				var curClass;
				if(!onlyException || aTemp[0] == "red" || aTemp[0] == "yellow" || count < 5)
				{
					sEvenOdd = (sEvenOdd == "odd") ? "even" : "odd";
					curClass = (sEvenOdd == "odd" ? " class=\"odd\"" : "");
					sTable += "<tr><td";
					if(i == 0)
						sTable += " width=" + parseInt(iWidth - 100);
					sTable += curClass + ">";
					if(i == iCurZ)
						sTable += "<strong>" + aNameZ[i] + "</strong>";
					else
						sTable += aNameZ[i];
					sTable += "</td><td" + curClass + ">";
					sTable += "<img src=\"./common/images/" + aTemp[0] + ".gif\" />&nbsp;" + aTemp[1] + sCurUnit;
					sTable += "</td><td" + curClass + ">&nbsp;&nbsp;";
					if(showNormal)
						sTable += "</td><td" + curClass + ">" + aThreshTemp[0];
					sTable += "</td><td" + curClass + ">" + aThreshTemp[1];
					sTable += "</td><td" + curClass + ">" + aThreshTemp[2] + "</td></tr>";
					if(aTemp[0] == "green" || aTemp[0] == "white")
						count++;
				}
			}
		}
	}
	sTable += "</table>"
	
	if(bIE4)
	{
		iScreenWidth = document.body.offsetWidth;
		iScreenHeight = document.body.offsetHeight;
		oLayer = document.all.popup;
		oLayer.innerHTML = sTable;
		
		oLayer.style.zIndex = "1200";
		
		iLayerX = oEvent.x + iXOffset + document.body.scrollLeft;
		iLayerY = oEvent.y + iYOffset + document.body.scrollTop;
		
		if (iLayerX + iWidth <= iScreenWidth + document.body.scrollLeft)
			oLayer.style.left = iLayerX;
		else
			oLayer.style.left = oEvent.x + document.body.scrollLeft - iXOffset - iWidth;
			
		if (iLayerY + iHeight <= iScreenHeight + document.body.scrollTop)
			oLayer.style.top = iLayerY;
		else
			oLayer.style.top = oEvent.y + document.body.scrollTop - iYOffset - iHeight;

		/// iframe mask
		document.all.pmask.style.height = oLayer.offsetHeight;
		document.all.pmask.style.width =  oLayer.offsetWidth;
		document.all.pmask.style.top = oLayer.style.top;
		document.all.pmask.style.left = oLayer.style.left;
		
		document.all.pmask.style.visibility = "visible";

		oLayer.style.visibility = "inherit";
	}
	else if(bNS4)
	{
		oLayer = document.layers.popup;
		oLayer.document.open();
		oLayer.document.write(sTable);
		oLayer.document.close();

		iLayerX = oEvent.layerX + iXOffset
		iLayerY  = oEvent.layerY + iYOffset;
		
		if (iLayerX + iWidth <= iScreenWidth)
			oLayer.left = iLayerX;
		else
			oLayer.left = oEvent.layerX - iXOffset - iWidth;
			
		if (iLayerY + iHeight <= iScreenHeight)
			oLayer.top = iLayerY;
		else
			oLayer.top = oEvent.layerY + iYOffset - iHeight;
			
		oLayer.visibility = "inherit";
	}
	else if (bNS6)
	{
		oLayer = document.getElementById("popup");
		while(oLayer.hasChildNodes())
			oLayer.removeChild(oLayer.firstChild);
		
		var r = document.createRange();
		r.selectNodeContents(document.body);
		r.collapse(true);
		
		var df = r.createContextualFragment(sTable);
		oLayer.appendChild(df);
		
		iLayerX = oEvent.layerX + iXOffset
		iLayerY = oEvent.layerY + iYOffset;
		
		if (iLayerX + iWidth <= iScreenWidth)
			oLayer.style.left = iLayerX + "px";
		else
			oLayer.style.left = (oEvent.layerX - iXOffset - iWidth) + "px";
		
		if (iLayerY + iHeight <= iScreenHeight)
			oLayer.style.top = iLayerY + "px";
		else
			oLayer.style.top = (oEvent.layerY - iYOffset - iHeight) + "px";
		
		oLayer.style.visibility = "inherit";
	}
}

function popupoff() 
{
	if (bIE4) {
		oLayer = document.all.popup;
		document.all.pmask.style.visibility = "hidden";
	}else if(bNS4)
		oLayer=document.layers.popup;
	else if(bNS6)
		oLayer=document.getElementById("popup");

	if(oLayer != null)
	{
		if(bIE4 || bNS6)
			oLayer.style.visibility = "hidden";
		else if(bNS4)
			document.popup.visibility = "hide";
	}
}

function getTitle(x, y)
{
	var aOrder = sOrder.split("|");
	var sTemp = "";
	for(var i = 0; i < aOrder.length; i++)
	{
		sTemp += getVar(aOrder[i], x, y);
		if(i + 1 == Math.round(aOrder.length / 2))
			sTemp += "<br />";
		else if(i < aOrder.length -1)
			sTemp += " - ";
	}
	
	return sTemp;
}

function getVar(sAxis, x, y)
{
	switch(sAxis)
	{
		case "xAxis":
			return aNameX[x];
		case "yAxis":
			return aNameY[y];
		case "zAxis":
			return zValue;
		case "nAxis":
			return nValue;
	}
	return null;
}

function getIndexFromCoordinates(sName, x, y, z)
{
	if(xAxis == sName)
		return x;
	if(yAxis == sName)
		return y;
	if(zAxis == sName)
		return z;
	if(nAxis == sName)
		return nIndex;
	return 0;
}

function getWhichArray(sName)
{
	if(xAxis == sName)
		return aNameX;
	if(yAxis == sName)
		return aNameY;
	if(zAxis == sName)
		return aNameZ;
	return new Array(nValue);
}

function addMask()
{
	// iframe mask
	var pmask = document.createElement("IFRAME");
	pmask.src = "javascript:void(0)";
	pmask.frameBorder = 0;
	pmask.id = "pmask";
	pmask.style.position = "absolute";
	pmask.style.visibility = "hidden";
	pmask.style.left = "0px";
	pmask.style.top = "-2000px";
	pmask.style.zIndex = "1500";
	pmask.style.width = "100px";
	pmask.style.height = "100px";
	pmask.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=0)";
	pmask.allowTransparency = false;
	document.body.appendChild(pmask);
}