//Copyright TurnTool ApS - 1998 - 2008 All Right Reserved.
//You are NOT allowed to use this HTML document for derivative work including parts here of without the written permission from TurnTool.
//This HTML document may not be redistributed in any form.

var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById);
var ns6 = (!document.all && document.getElementById);
var xpcomversion = "2.9.5.5";

var messages = new Array();

function element(id)
{
	if(ie4)	// Explorer 4
		return document.all[id];
	else	// Explorer 5+ Netscape 6+ and Mozilla and Firefox
		return document.getElementById(id);
}

function TNTDoCommand(string)
{
	var tntCtrl = element("TNTCtrl");
	if(tntCtrl && tntCtrl.ready)
		return tntCtrl.TNTDoCommand(string);
	return "";
}

function focus()
{
	var tntCtrl = element("TNTCtrl");
	if(tntCtrl && tntCtrl.ready)
	{
		tntCtrl.focus();
	}
}

var tntWidth = 400;
var tntHeight = 300;

function insertTurnTool(bgcolor,tntloadmsg, coreloadmsg)
{
	bgcolor = (bgcolor == null) ? "#ffffff" : bgcolor;
	tntloadmsg = (tntloadmsg == null) ? "Downloading 3D data file with TurnTool" : tntloadmsg;
	coreloadmsg = (coreloadmsg == null) ? "Downloading TurnTool core" : coreloadmsg;
	
	filepath += 'turntool.tnt';
	createTurnTool(tntWidth,tntHeight, bgcolor, 0, tntloadmsg, coreloadmsg);	// create embed and object tag
	if(TNTsupportedPlatform()==true)
	{
		if (isTurnToolNewestVersion()==false && ns6)
		{
			// this is mozilla only, if missing plugin or plugin is outdated
			//element('InstallTag').innerHTML = '<CENTER><INPUT TYPE=button VALUE="Install TurnTool Viewer" onclick="manualInstallTurnTool();"></CENTER>';
			manualInstallTurnTool();
		}
		if(isTurnToolInstalled() == false && (ie4||ie5))
		{
			//element('InstallTag').innerHTML = '<CENTER><INPUT TYPE=button VALUE="Install TurnTool Viewer" onclick="manualInstallTurnTool();"></CENTER>';
			checkRefresh();
		}
	}
}
function isTurnToolNewestVersion()
{
	var tntInstalled = false;
	if(ie4||ie5)
	{
		try
		{
			var xObj = new ActiveXObject("TNT.TNTCtrl");
			if (xObj)
			{
				tntInstalled = true;
			}
		}
		catch (e)
		{
		}
	}
	else
	{
		var isSupported = navigator.mimeTypes['application/tntfile'];
		isSupported = isSupported && isSupported.enabledPlugin; //in case they disabled it
		if(isSupported)
		{
			var v = navigator.plugins.namedItem("TurnTool XPCOM Plugin");
			if(v && v.description) {
				var version = v.description.replace(/([a-zA-Z]|\s)+/, "").split(".");
				var minversion = xpcomversion.split(".");
				for (var i=0; i<minversion.length; i++)
				{
					if (i >= version.length)
						break;
					if (version[i] > minversion[i])
					{
						tntInstalled = true;
						break;
					}
					if (version[i] < minversion[i])
						break;
					if (i == (minversion.length-1))
						tntInstalled = true;
				}
			}
		}	
	}	
	return tntInstalled;
}
function isTurnToolInstalled()
{
	var tntInstalled = false;
	if (ie4||ie5)
	{
		try
		{
			var xObj = new ActiveXObject("TNT.TNTCtrl");
			if (xObj)
			{
				tntInstalled = true;
			}
		}
		catch (e)
		{
		}
	}
	else
	{
		var isSupported = navigator.mimeTypes['application/tntfile'];
		isSupported = isSupported && isSupported.enabledPlugin; //in case they disabled it
		if(isSupported)
		{
			var v = navigator.plugins.namedItem("TurnTool XPCOM Plugin");
			if(v && v.description) {
				tntInstalled = true;
			}
		}
	}
	return tntInstalled;

}

function installTurnTool() 
{
	element('InstallTag').innerHTML = '<CENTER><INPUT TYPE=button VALUE="Install TurnTool Viewer" onclick="manualInstallTurnTool();"></CENTER>';
	if (ns6)
	{
		return false;
	}
	checkRefresh();
}

function manualInstallTurnTool() 
{
	if (ie4||ie5)
	{
		window.location.href = 'http://www.turntool.com/ViewerInstall.exe';
	}
	else
	{
		var xpi = {'TurnTool Viewer Installation':'http://www.turntool.com/ViewerInstall.xpi'};
		InstallTrigger.install(xpi,installFinish);
	}
	viewBigJavaScreen();
}
function viewBigJavaScreen()
{
	var elm = element('InstallTag');
	var html = elm.innerHTML.toString();
	if(html.search('Vis minimal Java version') != -1)
		return false; // the text is already here...
	html += '<br /><a href="'+window.location;
	if(window.location.toString().search(/\?/) == -1)
		html += '?';
	else
		html += '&';
	html += 'viewer=java" onclick="redirectToJava(); return false;" style="font-size: large">' + 'Vis minimal Java version' +'</a>';
	elm.innerHTML = html;
}

function installFinish(url, result)
{
	if(result >= 0)
	{
		// nice done! No problems!
	}
	else if(result < 0)
	{
		alert('The installation failed, please close all browser windows and try again.\n\nIf the problem continues, try restarting your computer.\n\nIf the problem still continues, please contact your content provider.\n\nIn the meantime, you can view a simplifyed Java version of this solution. Click the "'+'Vis minimal Java version'+'" button at the bottom of the screne.');
	}
}

function redirectToJava()
{
	var str = window.location.href.toString();
	if(str.indexOf('viewer=java') != -1)
		return false;
	if(str.indexOf('?') == -1)
		str += '?';
	else
		str += '&';
	window.location.href = str+'viewer=java';	
}


var checkCounter = 0;
function checkRefresh()
{
	var tntInstalled = isTurnToolInstalled();
	if(tntInstalled==false)
	{
		checkCounter++;
		if (checkCounter==80)
		{
			installTurnTool();
		}
		if (checkCounter==160)
		{
			viewBigJavaScreen();
		}
		setTimeout('checkRefresh()',500);
	}
	else
	{
		window.location.href = window.location;
	}
}

function TNTsupportedPlatform()
{
	if (getArgument('viewer')=='java')
		return false;
	if (navigator.platform.toString().indexOf("Win") != -1)
		return true;
	if (navigator.platform.toString().toLowerCase().indexOf("mac")!=-1 || navigator.platform.toString().toLowerCase().indexOf("linux")!=-1)
		return false;
	return true;
}

function createFilePath(newFloor)
{
	filepath = planedit_3d_path;
	if (!agent)
	{
		agent = getArgument('agent');
		project = getArgument('name');
	}
	if(getArgument('path') != 'false')
	{
		if (agent)
		{
			filepath += agent;
			filepath += "/";
		}
		if (project)
		{
			var tmp = '';
			if(!isNaN(newFloor) && newFloor != 0)
			{
				tmp = '_'+newFloor;
			}
			filepath += project + tmp + "/";
		}
	}
}

var agent = '';
var project = '';
var mode = 3;
var filepath = "files/";

function getArgument(argument)
{
	var filepth  = window.location.search;
	var lastChar  = filepth.indexOf('?') + 1;
	var m = filepth.substring(lastChar,filepth.length);
	var k = m.split('&');
	var a = -1;
	var searchstr = '^'+argument+'=';
	for(var n = 0; n<k.length; n++) 
	{
		if(k[n].toString().search(searchstr) != -1)
		{
			a = n;
			break;
		} 
	}
	if(a != -1)
		return unescape(k[a].split(argument + '=')[1]);
}

function centerWindow()
{
	if (window.screen)
	{
		var windowXcrop = 60;	// space around browser window
		var windowYcrop = 40;
			
		var screenWidth = screen.width;
		var screenHeight = screen.height;
		
		var windowWidth = screenWidth - windowXcrop;
		var windowHeight = screenHeight - windowYcrop;
		if (mode>=0)
		{
			var offsetX = screenWidth/2 - windowWidth/2;
			var offsetY = screenHeight/2 - windowHeight/2;
			if (offsetX<0)
				offsetX=0;
			if (offsetY<0)
				offsetY=0;
			window.resizeTo(windowWidth,windowHeight);
			window.moveTo(offsetX, offsetY);
		}
		screenWidth = getViewabelSize('x');
		screenHeight = getViewabelSize('y');
		var tileFactorW = parseInt((screenWidth - 40) / 32);
		var tileFactorH = parseInt((screenHeight - 210) * 1.777 / 32);
		if (tileFactorH>tileFactorW)
		{
			tileFactorH=tileFactorW;
		}
		if ((tileFactorH+2)<tileFactorW)
		{
			tileFactorW = tileFactorH;
		}
		if (tileFactorH<tileFactorW)
		{
			tileFactorW = parseInt((tileFactorW + tileFactorH + 1) / 2);
		}
		if (tileFactorW<17)
		{
			tileFactorW=17;
		}
		tntWidth = parseInt(tileFactorW*32);
		tntHeight = parseInt(tileFactorW*32/1.7777);
		filesize = tntWidth * tntHeight * 3 / 1000000;
		highfact = 2;
		superfact = 4;
		megafact = 8;
		highWidth = highfact * tntWidth;
		highHeight = highfact * tntHeight;
		superWidth = superfact * tntWidth;
		superHeight = superfact * tntHeight;
		megaWidth = megafact * tntWidth;
		megaHeight = megafact * tntHeight;
		dpi = 300;
		inch2cm = 2.54;  //1 inch = this value in cm

		element('TurnTool').style.width = tntWidth;
		element('TurnTool').style.height = tntHeight;
	}
}

function updateClock()
{
	var thisDate = new Date();
	var hours = parseInt(thisDate.getHours());
	var minutes = parseInt(thisDate.getMinutes());
	TNTDoCommand('Objects(#Clock_Hour).SetRotationLocal(0,0,'+(270-hours*30-(minutes*0.5))+')');
	TNTDoCommand('Objects(#Clock_Minute).SetRotationLocal(0,0,'+(270-minutes*6)+')');
	var seconds = 60-parseInt(thisDate.getSeconds());
	setTimeout("updateClock()",seconds*1000);
}

function OnClick( obj )
{
	var cmd = obj.split('_');
	if(cmd[0].match('#ButtonChgVisible'))
	{
		TNTDoCommand('Objects(' + cmd[1] + '*).SetVisible(false)');
		TNTDoCommand('Objects(*' + cmd[2] + '*).SetVisible(true)');
	}
}

function OnMouseEnter( obj )
{
	if (!moving)
	{
		TNTDoCommand('Objects(' + obj + ').SetSelected(1)');
		TNTDoCommand('Objects(' + obj + ').SetVisible(1)');
	}
}

function OnMouseExit( obj )
{
	TNTDoCommand('Objects(' + obj + ').SetSelected(0)');
	TNTDoCommand('Objects(' + obj + ').SetVisible(0)');
}

var timeoutValue = 0;
function eraseTimeout()
{
	if (timeoutValue!=0)
	{
		clearTimeout(timeoutValue);
		timeoutValue = 0;
	}
}

var timerID = 0;
var oldNavMode = -1;
var currNavMode = -1;
var floorChangeNavMode = -1;
var reclickCounter = -1;
var currCamera = '';

function WalkInside()
{
	TNTDoCommand('SceneGraph.Physics.Reset()');
	TNTDoCommand('Objects(PhysicsSphere).SetEnable(true)');	
	TNTDoCommand('Objects(*_Text).SetVisible(false)');	
	TNTDoCommand('CameraCtrl.SetCurrent("Camera01")');
	TNTDoCommand('CameraCtrl.SetControlable(false)');
	TNTDoCommand('Objects(Camera01).StopAnimation()');
	var frameCount = parseInt(TNTDoCommand('SceneGraph.Objects(Camera01).GetFrameCount()'))-1.0;
	TNTDoCommand('Objects(Camera01).SetFrame('+frameCount+')');
	currCamera = 'Camera01';
	updateCameraLight();
	UnhideInside();
	HideInside();
	TNTDoCommand('Objects(*#HideWhenOutside*).SetVisible(true)');	
	TNTDoCommand('CameraCtrl.SetMoveSpeed(0.0)');
	TNTDoCommand('CameraCtrl.SetMaxHorizontalAngle(3.14)');
	TNTDoCommand('CameraCtrl.SetMinHorizontalAngle(0)');
	TNTDoCommand('Objects(PhysicsSphere).SetEnable(true)');	
	TNTDoCommand('CameraCtrl.SetControlable(true)');
	window.setTimeout("floorChangeInProgress = false;",1000);
	menuSetActive(element('menu_nav_walk'));
	helpSetActive(element('menu_nav_walk'));	
	focus();
}

function GuideTour()
{
	var frameCount = parseInt(TNTDoCommand('SceneGraph.Objects(Camera02).GetFrameCount()'))-1.0;
	TNTDoCommand('Objects(Camera02).PlayAnimation(0,'+frameCount+',true)');
	TNTDoCommand('Objects(PhysicsSphere).SetEnable(false)');
	TNTDoCommand('CameraCtrl.SetCurrent("Camera02")');
	TNTDoCommand('Objects(*_Text).SetVisible(true)');		
	currCamera = 'Camera02';
	updateCameraLight();
	UnhideInside();
	HideInside();
}

function GotoAngle1()
{
	if (currCamera=='Camera01' || typeof floorChangeInProgress == "string")
   	{
		TNTDoCommand('Objects(PhysicsSphere).SetEnable(false)');
   		TNTDoCommand('CameraCtrl.SetCurrent("Camera03")');
		currCamera = 'Camera03';
		updateCameraLight();
		UnhideInside();
   	}
	else
   	{
   		TNTDoCommand('CameraCtrl.Match("Camera03",2000)');
   		eraseTimeout();
		timeoutValue = setTimeout("UnhideInside();",2000);
   	}
	TNTDoCommand('ObjectTree('+currCamera+').SetEnable(true)');
	HideInside();
	timerID = setTimeout('makeMovable()',2100);
}

function GotoAngle2()
{
	if (currCamera=='Camera01' || typeof floorChangeInProgress == "string")
   	{
   		TNTDoCommand('Objects(PhysicsSphere).SetEnable(false)');	
		TNTDoCommand('CameraCtrl.SetCurrent("Camera04")');
		currCamera = 'Camera04';
		updateCameraLight();
		UnhideInside();
	}
	else
	{
		TNTDoCommand('CameraCtrl.Match("Camera04",2000)');
   		eraseTimeout();
		timeoutValue = setTimeout("UnhideInside();",2000);
	}
	HideInside();
	timerID = setTimeout('makeMovable()',2100);
}

function makeMovable()
{
	TNTDoCommand('CameraCtrl.SetControlable(true)');
	TNTDoCommand('CameraCtrl.SetMoveSpeed(3.0)');
}

function UnhideInside()
{
	TNTDoCommand('Objects(*#HideWhenOutside*).SetVisible(true)');
	TNTDoCommand('Objects(*#ShowIfReclicked*).SetVisible(true)');
	TNTDoCommand('Objects(*#HideIfReclicked*).SetVisible(true)');
}

function HideInside()
{
	TNTDoCommand('Objects(*#HideIfInside*).SetVisible(false)');
}

function UnhideOutside()
{
	TNTDoCommand('Objects(*#HideIfInside*).SetVisible(true)');
}

function GotoOverview()
{
	eraseTimeout();
	TNTDoCommand('Objects(*#HideWhenOutside*).SetVisible(false)');
	TNTDoCommand('Objects(*_Text).SetVisible(true)');		
	var cam1ParentNodeIdx = TNTDoCommand('Objects("Camera01").GetParentNodeIndex()');
	var destParentNodeIdx = TNTDoCommand('Objects("Camera05").GetParentNodeIndex()');
	if (cam1ParentNodeIdx == destParentNodeIdx)
	{
		if (currCamera=='Camera01')
		{
			TNTDoCommand('CameraCtrl.Match("Camera05",2000)');
		}
		else
		{
			TNTDoCommand('SceneGraph.Physics.Reset()');
			TNTDoCommand('Objects(PhysicsSphere).SetEnable(true)');	
			TNTDoCommand('CameraCtrl.SetCurrent("Camera01")');
			currCamera = 'Camera01';
			updateCameraLight();
			TNTDoCommand('CameraCtrl.Match("Camera05",1)');
		}
	}
	else
	{
		TNTDoCommand('Objects(PhysicsSphere).SetEnable(false)');
		if (TNTDoCommand('CameraCtrl.GetCurrent()')=='Camera01')
		{
			TNTDoCommand('CameraCtrl.SetCurrent("Camera05")');
			currCamera = 'Camera05';
			updateCameraLight();
		}
		else
		{
			TNTDoCommand('CameraCtrl.Match("Camera05",2000)');			
		}
		UnhideOutside();
	}
	TNTDoCommand('CameraCtrl.SetMaxHorizontalAngle(1.57)');
	TNTDoCommand('CameraCtrl.SetMinHorizontalAngle(0)');		
	focus();
}

function updateCameraLight()
{
	if (currCamera=='Camera01')
	{
		TNTDoCommand('ObjectTree(Camera01).SetEnable(true)');
	}
	else
	{
		TNTDoCommand('ObjectTree(Camera01).SetEnable(false)');
	}	
	if (currCamera=='Camera02')
	{
		TNTDoCommand('ObjectTree(Camera02).SetEnable(true)');
	}
	else
	{
		TNTDoCommand('ObjectTree(Camera02).SetEnable(false)');
	}	
	if (currCamera=='Camera03')
	{
		TNTDoCommand('ObjectTree(Camera03).SetEnable(true)');
	}
	else
	{
		TNTDoCommand('ObjectTree(Camera03).SetEnable(false)');
	}	
	if (currCamera=='Camera04')
	{
		TNTDoCommand('ObjectTree(Camera04).SetEnable(true)');
	}
	else
	{
		TNTDoCommand('ObjectTree(Camera04).SetEnable(false)');
	}	
	if (currCamera=='Camera05')
	{
		TNTDoCommand('ObjectTree(Camera05).SetEnable(true)');
	}
	else
	{
		TNTDoCommand('ObjectTree(Camera05).SetEnable(false)');
	}
	if (currCamera=='Camera06')
	{
		TNTDoCommand('ObjectTree(Camera06).SetEnable(true)');
	}
	else
	{
		TNTDoCommand('ObjectTree(Camera06).SetEnable(false)');
	}
}

var shotNum = 0;
var sessionID = new String();
function RenderImg(size)
{
	if(!size)
	{
		size = 0;
	}
	var cmd = '"';
	cmd += TNTDoCommand('Core.GetDesktopPath()');
	cmd += 'TURNTOOL-'+agent+'-'+project+'-';
	if(shotNum<10)
	{
		cmd += '0'+shotNum;
	}
	else
	{
		cmd += shotNum;
	}
	var thisDate = new Date();
	var year = thisDate.getFullYear();
	var month = thisDate.getMonth();
	var day = thisDate.getDate();
	var hours = thisDate.getHours();
	var minutes = thisDate.getMinutes();
	var seconds = thisDate.getSeconds();
	var milliseconds = thisDate.getMilliseconds();
	sessionID = year+"-"+month+"-"+day+"-"+hours+"-"+minutes+""+seconds+""+milliseconds;
	cmd += '-'+sessionID+'.bmp",'+size;
	TNTDoCommand('Renderer.SaveImage(' + cmd + ')');
	shotNum++;
}

var dom = (document.getElementById) ? true : false;
var ns5 = ((navigator.userAgent.indexOf("Gecko")>-1) && dom) ? true: false;
var moie5 = ((navigator.userAgent.indexOf("MSIE")>-1) && dom) ? true : false;
var ns4 = (document.layers && !dom) ? true : false;
var moie4 = (document.all && !dom) ? true : false;
var nodyn = (!ns5 && !ns4 && !moie4 && !moie5) ? true : false;

if (nodyn) { event = "nope" }

var enterCount = 0;
var tipFollowMouse= true;	
var tipWidth= 170;
var offX= 0;	// how far from mouse to show tip
var offY= 20; 
var tipFontFamily= "Verdana, Arial, Helvetica, sans-serif";
var tipFontSize= "10px";
var tipFontWeight= "bold";
var tipFontColor= "#000000";
var tipBgColor= "#EEEEEE"; 
var tipBorderColor= "gray";
var tipBorderWidth= 1;
var tipBorderStyle= "ridge";
var tipPadding= 2;

var startStr = '';
var midStr = '<tr><td valign="top" align="center">';
var endStr = '</td></tr></table>';

function initTip() 
{	
	startStr = '<table width="' + tipWidth + '">';	
	tooltip = element('tipDiv');
	tipcss = element('tipDiv').style;
	tipcss.width = tipWidth+"px";
	tipcss.fontFamily = tipFontFamily;
	tipcss.fontSize = tipFontSize;
	tipcss.color = tipFontColor;
	tipcss.fontWeight= tipFontWeight;
	tipcss.backgroundColor = tipBgColor;
	tipcss.borderColor = tipBorderColor;
	tipcss.borderWidth = tipBorderWidth+"px";
	tipcss.padding = tipPadding+"px";
	tipcss.borderStyle = tipBorderStyle;
	if (ns4 && tipFollowMouse)
		document.captureEvents(Event.MOUSEMOVE);
	if(tipFollowMouse)
		document.onmousemove = positionTip;
	
}

//////////////////////////// doTooltip(evt, num)/////////////////////////////////
function doTooltip(evt, num) 
{
	SetToolTipLanguage();
	tipWidth = messages[num][1].length+100;	
	initTip();
	
	enterCount++;
	curBgColor = tipBgColor;
	curFontColor = tipFontColor;
	var tip = startStr + messages[num][0] + midStr + '<span style="font-family:'+tipFontFamily+'; font-size:'+tipFontSize+'; color:'+curFontColor+'; font-weight:'+tipFontWeight+';">' + messages[num][1] + '</span>' + endStr;
 	tooltip.innerHTML = tip;
 	element('tipDiv').style.visibility = "visible";
 	positionTip(evt);
 	
}
//////////////////////////// positionTip(evt)////////////////////////////////////
function positionTip(evt) 
{
	var mouseX = (ns4||ns5)? evt.pageX: window.event.clientX + document.body.scrollLeft;
	var mouseY = (ns4||ns5)? evt.pageY: window.event.clientY + document.body.scrollTop;
	tipcss.left = (ns4)? mouseX+offX: mouseX+offX+"px";
    	tipcss.top = (ns4)? mouseY+offY: mouseY+offY+"px";    	
}



//////////////////////////// hideTip() //////////////////////////////////////////
function hideTip() 
{
	enterCount--;
	if (enterCount==0)
	 	setTimeout("hideTip_re()",10);	
	document.onmousemove = null;
	 				  	 	
}
//////////////////////////// hideTip_re() ///////////////////////////////////////
function hideTip_re()
{
	if(enterCount==0)
		element('tipDiv').style.visibility = 'hidden';
}

var measureMode = 1;

function setMeasurementMode(mesMode)
{
	if (mesMode>0)
	{
		if (mesMode==1)
		{
			measureMode=2;
			element('MeasureButton').value = lanArray[lanIndex][38];
			TNTDoCommand('Objects(*).SetOcclusion(0)');
			TNTDoCommand('Objects(Collision*).SetOcclusion(1)');
			TNTDoCommand('Measurement.SetLineSize(150)');
			TNTDoCommand('CameraCtrl.SetIgnoreInput(1)');
			TNTDoCommand('Measurement.SetMode(1)');	
			if (currCamera!='Camera06')
			{
				TNTDoCommand('CameraCtrl.SetCurrent("Camera06")');
				currCamera = 'Camera06';
				updateCameraLight();
			}
		}
		else
		{
			measureMode=1;
			element('MeasureButton').value = lanArray[lanIndex][39];
			TNTDoCommand('SceneGraph.Objects(*).SetOcclusion(0)');
			TNTDoCommand('CameraCtrl.SetIgnoreInput(0)');	
			TNTDoCommand('Measurement.SetMode(2)');	
		}
	}
	else
	{
		TNTDoCommand('SceneGraph.Objects(*).SetOcclusion(0)');
		TNTDoCommand('CameraCtrl.SetIgnoreInput(0)');	
		TNTDoCommand('Measurement.SetMode(0)');
		distance = "0.0  ";
		OnMeasureUpdate(distance);
	}
}
var distance = "0.0  ";

function OnMeasureUpdate(distance)
{
	element('MeasureText').innerHTML = lanArray[lanIndex][40] + distance.slice(0,5)+ lanArray[lanIndex][41];
}

var moving = false;

function OnMoveStart()
{
	moving = true;
	TNTDoCommand('CameraCtrl.SetIgnoreInput(1)');
}

function OnMoveEnd()
{
	moving = false;
	TNTDoCommand('CameraCtrl.SetIgnoreInput(0)');
}

function initMove()
{
	TNTDoCommand('Objects(*).SetOcclusion(0)');
	TNTDoCommand('Objects(*).SetMouseOverEvent(0)');	
	TNTDoCommand('Objects(*#A*).SetEnable(true)');	
	TNTDoCommand('Objects(*#A*).SetVisible(false)');	
	TNTDoCommand('Objects(*#A*).SetOcclusion(1)');
	TNTDoCommand('Objects(*#A*).SetMouseOverEvent(1)');	
	TNTDoCommand('Objects(Collision*).SetOcclusion(1)');
	TNTDoCommand('Selection.SetMoveDirectionZ(0)');	
	TNTDoCommand('Selection.SetRotationSpeedX(0.0)');	
	TNTDoCommand('Selection.SetRotationSpeedY(0.0)');
	TNTDoCommand('Selection.SetRightMode(5)');
	TNTDoCommand('CameraCtrl.SetIgnoreInput(0)');	
	if (currCamera!='Camera06')
	{
		TNTDoCommand('CameraCtrl.SetCurrent("Camera06")');
		currCamera = 'Camera06';
		updateCameraLight();
		TNTDoCommand('ObjectTree("Camera06").SetEnable(true)');
	}
}

var currTab = 1;
function changeTab(newTab)
{
	if (currTab != newTab)
	{
		// ---- Init Tab ----
		if (newTab==6)
		{
			changeNavMode(5);	
		}
		else if (newTab==7)
		{
			changeNavMode(6);
		}
		currTab = newTab;
	}
	else
	{
		if ((currTab==6)||(currTab==7))
		{
			TNTDoCommand('CameraCtrl.Match("Camera06",2000)');
		}
	}
}
function getRenderSettings()
{	
	document.renderform.renderagent.value = agent;
	document.renderform.rendername.value = project;
	var cameraName = TNTDoCommand('CameraCtrl.GetCurrent()');
	document.renderform.rendercameraname.value = cameraName;
	var fov = TNTDoCommand('Objects('+cameraName+').GetFOV()');
	if(isNaN(fov) || fov=="")
	{
		document.renderform.renderfov.value = "90.0";
	}
	else
	{
		document.renderform.renderfov.value = fov;
	}
	document.renderform.renderxpos.value = TNTDoCommand('Objects('+cameraName+').GetPositionWorldX()');
	document.renderform.renderypos.value = TNTDoCommand('Objects('+cameraName+').GetPositionWorldY()');
	document.renderform.renderzpos.value = TNTDoCommand('Objects('+cameraName+').GetPositionWorldZ()');
	document.renderform.renderxrot.value = TNTDoCommand('Objects('+cameraName+').GetRotationWorldX()');
	document.renderform.renderyrot.value = TNTDoCommand('Objects('+cameraName+').GetRotationWorldY()');
	document.renderform.renderzrot.value = TNTDoCommand('Objects('+cameraName+').GetRotationWorldZ()');
	document.renderform.renderpm.value = escape(getArgument('pm'));
	document.renderform.renderpmmail.value = escape(getArgument('pmmail'));
	document.renderform.renderlocation.value = "cubicasa_"+lanArray[lanIndex][0];
	document.renderform.submit();
}

function createTurnTool(width, height, color, transparent, tntDownloadText, coreDownloadText)
{
	if(TNTsupportedPlatform()==true)
	{
		if (ie4||ie5)
		{
			turntoolObject = '<object id="TNTCtrl" width="'+width+'" height="'+height+'" classid="CLSID:402ee96e-2ce8-482d-ada5-ceceea07e16d" codebase="http://www.turntool.com/ViewerInstall.exe#version=2,95,4,0"><param name="transparent" value="'+transparent+'"><param name="ctrl_color" value="'+color+'"><param name="script" value="ProgressBar.SetDownloadText(\''+tntDownloadText+'\');ProgressBar.SetCoreDownloadText(\''+coreDownloadText+'\');TNTCtrl.SetLogo(\'None\');"><param name="src" value="'+filepath+'"></object>';
			turntoolObject += '<sc'+'r'+'ipt LANGUAGE=JavaScript FOR=TNTCtrl EVENT=TNTEvent(string)> execScript(string); </sc'+'r'+'ipt>';
		}
		else
		{
			turntoolObject = '<embed id="TNTCtrl" width="'+width+'" height="'+height+'" src="'+filepath+'" transparent="0" ctrl_color="'+color+'" script="ProgressBar.SetDownloadText(\''+tntDownloadText+'\');ProgressBar.SetCoreDownloadText(\''+coreDownloadText+'\');TNTCtrl.SetLogo(\'None\');" pluginspage="http://www.turntool.com/ViewerInstall.xpi" pluginsurl="http://www.turntool.com/ViewerInstall.xpi" type="application/tntfile"></embed>';
		}
		if (isTurnToolInstalled()==true && element('InstallTag'))
			element('InstallTag').style.display = 'none';
	}
	else
	{
		turntoolObject = createJavaObject(width, height);
	}
	element('TurnTool').innerHTML = turntoolObject;
}

function createJavaObject(width, height)
{
	turntoolObject = '<applet id="TNTCtrl" name="ptviewer" code="ptviewer.class" width="'+width+'" height="'+height+'" mayscript="true">';
	turntoolObject += '<param name="codebase" value="files/">';
	turntoolObject += '<param name="file"  value="'+filepath.replace('turntool.tnt','')+'pano_camera03.jpg">';
	turntoolObject += '<param name="pano0" value="{file='+filepath.replace('turntool.tnt','')+'pano_camera03.jpg}{fovmin=40}{fovmax=95}">';
	turntoolObject += '<param name="pano1" value="{file='+filepath.replace('turntool.tnt','')+'pano_camera04.jpg}{fovmin=40}{fovmax=95}">';
	turntoolObject += '<param name="debugmode" value="false">';
	turntoolObject += '<param name="accelmouse" value="false">';
	turntoolObject += '<param name="mousesense" value="800">';
	turntoolObject += '<param name="fovmin" value="40">';
	turntoolObject += '<param name="fovmax" value="95">';
	turntoolObject += '<param name="wait" value="files/promo-tnt.jpg">';
	turntoolObject += '<param name="barcolor" value="7a8e48">';
	turntoolObject += '<param name="bgcolor" value="ffffff" >';
	turntoolObject += '</applet>';
	return turntoolObject;
}

function panoImage(panoid)
{
	element('TNTCtrl').newPanoFromList(panoid);
}
var isFullscreen = false;
function fullscreenmode()
{
	var fullscreenmode = getArgument('fullscreen');
	if(	fullscreenmode == "true" )
	{
		isFullscreen = true;
		window.moveTo(0,0)
		window.resizeTo(screen.width,screen.height-30)
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			tntWidth = window.innerWidth;
			tntHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			tntWidth = document.documentElement.clientWidth;
			tntHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			tntWidth = document.body.clientWidth;
			tntHeight = document.body.clientHeight;
		}
		var html = '<div id="TurnTool">CubiCasa.com</div><div id="InstallTag"></div>';
		if (document.documentElement && document.documentElement.scrollTop)
			document.documentElement.body.innerHTML = html;
		else if (document.body)
			document.body.innerHTML = html;	
		return true;
	}
	return false;
}

function openTNT(who)
{
	var wid = 'width=700,height=700,';
	if(who.indexOf('fullscreen=true') != -1)
		wid = 'width='+ screen.availWidth + ',height=' + screen.availHeight + ',';
	window.open(who,'tnt',wid+'status=yes,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=yes').focus();
	return false;
}

function getViewabelSize(xaxis)
{
	var winWidth = tntWidth;
	var winHeight = tntHeight;
	if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			winWidth = window.innerWidth;
			winHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			winWidth = document.documentElement.clientWidth;
			winHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			winWidth = document.body.clientWidth;
			winHeight = document.body.clientHeight;
	}
	if(xaxis == true || xaxis.toString().toLowerCase() == 'x')
		return winWidth;
	return winHeight;
}
function getAbsoluteFilePath()
{
	var fp = window.location.href.toString();
	//fp = fp.replace('/','\\');
	var slash = fp.lastIndexOf('/');
	if(slash == -1)
	{
		slash = fp.lastIndexOf('\\');
		fp = fp.replace('\\\\','//');
	}
	var parms = fp.indexOf('?');
	if(parms == -1)
		parms = fp.indexOf('&');
	if(parms == -1)
		parms = -2;
	if((parms > 0 && slash < parms) || slash > 0)
		return fp.substring(0,slash);
	return '';
}

var currentFloor = 0;
var floorChangeInProgress = true;
function changeFloor(zone)
{
	if(zone.search(/^#StairUp|^#StairDown/) != -1 && floorChangeInProgress == false)
	{
		floorChangeInProgress = zone;
		if(zone.search(/^#StairUp/i)!= -1)
		{
			currentFloor++;	
		}
		else
		{
			currentFloor--;
		}
		createFilePath(currentFloor);
		element('TNTCtrl').src = getAbsoluteFilePath()+'/'+filepath + 'turntool.tnt';
	}
}

function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}
var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);

function removeEvent( obj, type, fn )
{
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj["e"+type+fn] );
		obj["e"+type+fn] = null;
	}
}
