var boolCMToPos = false;
var boolCMInKlickArea = false;
var boolCMVisible = false;
var isInScrollWindow = 0;

var map = "";

window.onload = function()
{
	setTitle();
	setMail(true);
	setModelInfo();
	showButton("url", true);
	parent.header.lastView = "table";
	if (bSwitchViews){window.setTimeout("showButton('table', true)", 100);}
	checkPrint();
	loadImage();
	actualizeImageArea();
	readCM();
	actualizeTreeView();
	actualizeAS();
	actualizeAS_position();
	highLightObject();
	document.onmousemove = getMouse;
	document.onclick = getMouse;
	parent.header.bSwitch = false;
}

window.onunload = function()
{	
	showButton("table", false);
	showButton("url", false);
	showButton("modelinfo", false);
	showButton("mail", false);
	showButton("print1", false);
	showButton("print2", false);
	parent.header.addHistory(document.title, currModelType, currModelId);
	parent.overview.document.getElementById("container").innerHTML = ""; // ADDED BY SMU
	if (!parent.header.bSwitch){parent.header.bModelLoaded = false}
}

window.onresize = function()
{
	setTitle();
	actualizeAS();	
}

function posObject(objid)
{
	if (window.innerWidth)
	{
		windowWidth = parent.model.window.innerWidth;
		winowHeight = parent.model.window.innerHeight;
	}
	else
	{
		windowWidth = document.body.offsetWidth;
		windowHeight = document.body.offsetHeight;
	}
	currObj = document.getElementById(objid + "Flag");
	objLeft = currObj.offsetLeft;
	objTop = currObj.offsetTop;
	newPosLeft = (windowWidth/2 > objLeft) ? 0 : Math.abs(objLeft - windowWidth/2);
	newPosTop = (windowHeight/2 > objTop) ? 0 : Math.abs(objTop - windowHeight/2);
	this.scrollTo(newPosLeft, newPosTop);
}

function highLightObject()
{
	var currentId = parent.header.actHighLightedObject;
	if (currentId != "")
	{
		highLightElem = document.getElementById(currentId + "Flag");
	
		if (highLightElem)
		{
			highLightElem.style.display = "block";
			document.getElementById(currentId + "Arrow").style.display = "block";
			posObject(currentId);
		}
	}
}

function actualizeAS()
{
  if (self != top)
  {
    if (!parent.overview)
    {
      window.setTimeout("actualizeAS();",100);    
    }
    else
    {
      parent.overview.location.reload();
      window.setTimeout("parent.overview.initAS()",500);
    }
  }
}

function actualizeAS_position()
{
  if (self != top)
  {
    if (!parent.overview)
    {
      window.setTimeout("actualizeAS_position();",100);    
    }
    else
    {
			window.setInterval('parent.overview.AS_position()', 100);
    }
  }
}

function loadImage()
{
	document.getElementById('modelimage').firstChild.src = "../misc/images/model/dummy.gif";
	
	if (!parent || !parent.header)
  {
    window.setTimeout("loadImage()", 100); 
  }
  else
  {
		zoom = parent.header.iZoom;
		imageSrc = eval("modelimage" + zoom + ".src");
		if (imageSrc == "")
		{
			window.setTimeout("loadImage()", 100);
			return
		}
		document.getElementById('modelimage').firstChild.src = imageSrc;
	}
}

function prepareImageMap()
{
	document.getElementById('modelmap').innerHTML = "";
	map = "";
}

function createImageArea(xl, yo, xr, yu, instId, refModId, refInstId, extLink, isHotSpot)
{
	var arrowImageHeight = 42;
	if (!parent || !parent.header)
  {
  	window.setTimeout("createImageArea(" + xl + "," + yo + "," + xr + "," + yu + "," + instId + "," + refModId + "," + refInstId + "," + extLink + "," + isHotSpot + ");", 100);
  	return
  }

	factor = ((parent.header.iZoom) / 2.542373);  
  if (isHotSpot)
  {
  	if (extLink != "")
  	{
  		strAction = ("target='_blank' href=" + extLink);
  	}
  	else
  	{
  		strAction = "href='#contextMenu' onClick='javascript:openWindow(\"" + refModId + "\", \"" + refInstId + "\");'";
  	}
  	map = map + "\n<area shape='rect' coords='" + (factor * xl) + ", " + (factor * yo) + ", " + (factor * xr) + ", " + (factor * yu) + "'" + strAction + "></area>";
  }
  else if (eval("typeof " + instId + "D") != "undefined")
  {
  	if (extLink != "")
  	{
  		strAction = ("onDblClick='javascript:openLink(\"" + encodeURI(extLink) + "\");'");
  	}
  	else
  	{
  		strAction = ("onDblClick='javascript:openWindow(\"" + refModId + "\", \"" + refInstId + "\");'");
  	}
		map = map + "\n<area shape='rect' coords='" + (factor * xl) + ", " + (factor * yo) + ", " + (factor * xr) + ", " + (factor * yu) + "' href='#contextMenu' " + strAction + " onClick='javascript:showCM(\"" + instId + "\");' onMouseOver='javascript:doMouseOver(\"" + instId + "\");' onMouseOut='javascript:doMouseOut();'></area>";
		map = map + "\n<img class='markedObject' id='" + instId + "Flag' src=\"../misc/images/model/dummy.gif\" style='position:absolute;left:" + (factor * xl - 4) + ";top:" + (factor * yo - 4) + ";width:" + ((xr - xl) * factor + 8) + ";height:" + ((yu - yo) * factor + 8) + ";' />"
		map = map + "\n<img id='" + instId + "Arrow' src=\"../misc/images/model/arrow.gif\" style='display:none;position:absolute;left:" + (factor * xr + 4) + ";top:" + (factor * yo - (arrowImageHeight + 4)) + ";' />"		
	}
}

function imageMapsReady()
{
	document.getElementById('modelmap').innerHTML = map;		
}

function openLink(strLink)
{
	killCM();
	extWindow = window.open(decodeURI(strLink), "externalWindow");
	extWindow.focus();	
}

function getMouse(Event)
{
  if (Event)
  {
    oEvent = Event;
  }
  else
  {
  	oEvent = event;
  }
  newX = oEvent.clientX;
  newY = oEvent.clientY;
  
  movObj = document.getElementById("mov");
  cmObj	= document.getElementById("cm");
  
  if (!cmObj || !movObj|| !parent.header)
  {
    window.setTimeout("GetMouse(Event);", 100);            
  }
  else
  {  
		if (oEvent.type == "click" && !boolCMInKlickArea)
		{
			killCM();
		}

    var rightEdge = document.body.clientWidth - newX;
    var bottomEdge = document.body.clientHeight - newY;

		if (boolCMToPos)
		{
			posObj(cmObj);
		}
		
		posObj(movObj);		
  }
  
  function posObj(obj)
  {
		if (rightEdge < obj.offsetWidth)
		{
			obj.style.left = document.body.scrollLeft + newX - obj.offsetWidth - parent.header.Xoffset;
		}
		else
		{
			obj.style.left=document.body.scrollLeft + newX + parent.header.Xoffset;
		}

		if (bottomEdge < obj.offsetHeight)
		{
			obj.style.top=document.body.scrollTop + newY - obj.offsetHeight + parent.header.Yoffset;
		}
		else
		{
			obj.style.top=document.body.scrollTop + newY - parent.header.Yoffset;
		}

		if (obj.style.top.substring(0,1) == "-")
		{
			obj.style.top = document.body.scrollTop;
		}
		
		if (obj.style.left.substring(0,1) == "-")
		{
			obj.style.left = document.body.scrollLeft;
		}		
		
		if (boolCMToPos)
		{
			boolCMToPos = false;
		}
  }
}

function doMouseOver(instId)
{
	boolCMInKlickArea = true;
	msg = eval(instId + "D");
	oMov = document.getElementById("mov");
	
	if (!oMov)
	{
		window.setTimeout("doMouseOver('" + instId + "');", 100);
	}
	else
	{
		if ((typeof oMov.style.show == "undefined") || (oMov.style.show != "visible"))
		{
			popup(unMaskQuotes(msg));
		}
	}
}

function doMouseOut()
{
	boolCMInKlickArea = false;
	killPopup();
}

function popup(msg)
{
  if(msg.length == 0 || boolCMVisible)
  {
    return;
  }

  if (!document.getElementById("mov"))
  {
    window.setTimeout("popup('" + msg + "');", 100);              
  }
  else
  {
		document.getElementById("mov").innerHTML = unMaskTags(msg);
		document.getElementById("mov").style.display = "block";
  }
}

function killPopup()
{
  oMov = document.getElementById("mov");
  if (!oMov)
  {
    window.setTimeout("KillPopup();", 100);              
  }
  else
  {
    oMov.style.display = "none";
  }
}

function instCM(name)
{
	var attrList = new Array();
	var allAttrNames = new Array();
	var attrCounter = 0;
	
	this.name = name;
		
	function attribute(name, type)
	{
		var linkList = new Array();

		this.name = name;
		
		this.type = type;

		function link(url, name, icon)
		{
			this.url = url;
			this.name = name;
			this.icon = icon;
		}

		this.addLink = 	function(url, name, icon)
		{
			linkList.push(new link(url, name, icon));
		}

		this.getNumberOfLinks =	function()
		{
			return linkList.length;
		}

		this.getLinkName =	function(index)
		{
			if (index >= linkList.length)
			{
				return false;
			}
			else
			{
				return linkList[index].name;
			}
		}

		this.getLinkUrl =	function(index)
		{
			if (index >= linkList.length)
			{
				return false;
			}
			else
			{
				return linkList[index].url;
			}
		}
		
		this.getLinkIcon = function(index)
		{
			if (index >= linkList.length)
			{
				return false
			}
			else
			{
				return linkList[index].icon;
			}
		}
	}
	
	this.addAttribute =	function(name, type)
	{
		attrList[name] = new attribute(name, type);
		attrCounter = attrCounter + 1;
		allAttrNames.push(name);
	}
	
	this.addLink = function (attrname, url, linkName, icon)
	{
		attrList[attrname].addLink(url, linkName, icon);
	}
		
	this.getNumberOfAttributes = function()
	{
		return attrCounter;
	}
	
	this.getNamesOfAllAttributes = function()
	{
		return allAttrNames;
	}
	
	this.getType = function(attrname)
	{
		return attrList[attrname].type;
	}
	
	this.getNumberOfLinks = function(attrname)
	{
		return attrList[attrname].getNumberOfLinks();
	}
	
	this.getLink = function(attrname, index)
	{
		var result = new Array();
		result[0] = attrList[attrname].getLinkName(index); 
		result[1] = attrList[attrname].getLinkUrl(index);
		result[2] = attrList[attrname].getLinkIcon(index);
		return result;
	}	
}

function showCM(instId)
{
	oCM = document.getElementById("cm")

	if (!oCM)
	{
		window.setTimeout("showCM('" + instId +"');", 100);
	}
	else
	{
		killPopup();
		killCM();
		boolCMToPos = true;
		var erg = "";
		
		try
		{
			var currCM = eval(instId + "CM");
		}
		catch (e)
		{
			return;
		}
		
		if (currCM.name == "")
		{
			return;
		}
		
		var numberAttrNames = currCM.getNumberOfAttributes();
		var allAttrNames = currCM.getNamesOfAllAttributes();

		for (var i = 0; i < numberAttrNames; i++)
		{
			var strTarget = "";
			var currAttrName = allAttrNames[i];
			var currAttrType = currCM.getType(currAttrName);
			var numberOfLinks = currCM.getNumberOfLinks(currAttrName);
			
			if (i==0)
			{
				var strRule = "";
			}
			else
			{
				var strRule = "<hr />";
			}
			
			if (currAttrType == "ext")
			{
				var strTarget = "target='_blank'"
			}
			
			erg = erg + strRule + "<span class='cmName'>" + currAttrName + "</span>";

			for (var j = 0; j < numberOfLinks; j++)
			{
				var currLink 		= currCM.getLink(currAttrName, j);
				var linkName 		= currLink[0];
				var linkAddress	= currLink[1];
				var linkIcon 		= currLink[2];

				erg = erg + "<a href='" + linkAddress + "' " + strTarget + " class='cmEntry' onmouseover='this.className=\"cmEntryActive\"' onmouseout='this.className=\"cmEntry\"'><img class='cmIcon' src='../misc/images/icons/" + linkIcon + "' />" + linkName + "</a>";
			}
		}
		
		if (numberAttrNames > 0)
		{
			erg = erg + "<hr />"
		}
		
		erg = erg + "<a href='javascript:openWindow(\"" + currModelId + "\", \"" + instId + "\");' class='cmEntry' onmouseover='this.className=\"cmEntryActive\"' onmouseout='this.className=\"cmEntry\"'>" + top.strCMEntryDetails + "</a>";
	
		oCM.innerHTML = unMaskTags(erg);
		oCM.style.display = "inline";
		boolCMVisible = true;
	}
}

function killCM()
{
	oCM = document.getElementById("cm");
	if (!oCM)
	{
		window.setTimeout("killCM();", 100);
	}
	else
	{
		oCM.style.display = "none";
		boolCMVisible = false;
	}
}