var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById);
var ns6 = (!document.all && document.getElementById);

var agt=navigator.userAgent.toLowerCase();
var firefox = 0;
if (agt.indexOf("firefox") != -1) firefox=1;

var fullpath = '';

isopened = 0;
StartDoorFrame=0;
StopDoorFrame=50;
var INITIAL = 0;
var STATE1 = 1;
var roof_state = INITIAL;

function OnReady(){
		element('TNTCtrl').transparent = 0;
		var mon_url = document.location.href ;
		var is_alliance = mon_url.lastIndexOf('alliance');
		if (is_alliance != -1) 
			TNTDoCommand('SceneGraph.Bitmap("TV_screen_bastides").Load("http://www.visimmo3d.com/advert/alliance/bastides1/logo_bastides1.jpg")');
}

function OnClick(obj){
	obj = obj.toLowerCase();
	
	if(obj == "arrow_up_left"){
		ChangeAngleInteractive('Camera_stairs', 'PhysSphere_Camera_stairs', 2721, 858, 125, 300, 300, 'RDC');
		chargerMap("R1");
	}
	
}

function OnMouseExit(obj){
	obj = obj.toLowerCase();
	TNTDoCommand('Objects("'+ obj +'_border").SetVisible(false)');
}

function OnMouseEnter(obj){
	obj = obj.toLowerCase();
	TNTDoCommand('Objects("'+ obj +'_border").SetVisible(true)');
}

function callWindow(title, width, height)
{
  window.open("",title,"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=" + width + ",height=" + height);
}


///////ROOF///////
function setRoofOn(){
	element("toit").checked = false;
	TNTDoCommand('Objects("roof_*").SetVisible(true)');
	
	roof_state = INITIAL;	
	//setState("ext", ext_wall_state);
}

function toggleRoof(){
	if(roof_state == STATE1)
		setRoofOn();
	else
		setRoofOff();
}

function setRoofOff(){
	element("toit").checked = true;
	TNTDoCommand('Objects("roof_*").SetVisible(false)');
	roof_state = STATE1;
}

var cur_roof_color = 'tuiles';

function switchRoof(roof_color)
{
	if (element("toit").checked == false)
	{
		switch (roof_color)
		{
			case 'tuiles':
				TNTDoCommand('Objects("roof_roof_tuiles").SetVisible(true)');
				TNTDoCommand('Objects("roof_roof_ardoise").SetVisible(false)');
			break;
			case 'ardoise':
			TNTDoCommand('Objects("roof_roof_ardoise").SetVisible(true)');
			TNTDoCommand('Objects("roof_roof_tuiles").SetVisible(false)');
			
			break;
		}
		cur_roof_color= "roof_color";
	}
}

function OpenCloseDoor(doorname,isopened)
{
	TNTDoCommand('Objects('+ doorname + ').StopAnimation()');
	TNTDoCommand('Objects('+ doorname + ').SetEnable(true)');
	
	var CurFrame = TNTDoCommand('Objects('+ doorname +').GetFrame()');
	if (!isopened)
		TNTDoCommand('Objects('+ doorname + ').PlayAnimation(' + CurFrame +',' + StartDoorFrame + ',false)');
	else
		TNTDoCommand('Objects('+ doorname + ').PlayAnimation(' + CurFrame +',' + StopDoorFrame + ',false)');
}


function HideGroup(groupname)
{
	TNTDoCommand('ObjectTree("' + groupname + '").SetVisible(false)');
}

function ShowGroup(groupname)
{
	TNTDoCommand('ObjectTree("' + groupname + '").SetVisible(true)');
}


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 initTurnTool()
{
	var tntInstalled = isTurnToolInstalled();
	var installtnt = '<table bgcolor="#A5AAE7" width="100%">' +
	                   '<div style="padding:25px; background-color:#A5AAE7;" >' +
	                   'L\'utilisation du site <b>VISIMMO3D</b> requiert l\'installation d\'un plugin <font color="green"><b>sécurisé</b></font> qui a fait l\'objet d\'une vérification auprès d\'un organisme certifié.<br><br>' +
	                   'Cliquez sur la bannière jaune qui apparait en haut de votre fenêtre et autoriser l\'installation du Plugin ActiveX, puis cliquez Install.' +
	                   '</div>' +
	                   '</table>';

 											
	if (tntInstalled==false)
	{
		element('InstallTag').innerHTML = installtnt;
		checkRefresh();
	}
	else
	{
		if (!(ie4||ie5))
		{
			//element('TurnTool').innerHTML = '<embed id="TNTCtrl" width="640" height="480" src="appart01.tnt" transparent="1" ctrl_color="#D8D8D8" pluginspage="http://www.turntool.com/download/" type="application/tntfile"></embed>';
		}
	}
	
}


function isTurnToolInstalled()
{
	var tntInstalled = false;
	if (ie4||ie5)
	{
		try
		{
			var xObj = new ActiveXObject("TNT.TNTCtrl");
			if (xObj)
			{
				tntInstalled = true;
			}
		}
		catch (e)
		{
		}
	}
	else
	{
		if(navigator.plugins.namedItem("TurnTool XPCOM Plugin"))
		{
			tntInstalled = true;
		}
	}
	return tntInstalled;
}

function installTurnTool()
{
	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);
	}
}

function installFinish(url, result)
{
	window.location.href = window.location;
}

var checkCounter = 0;
function checkRefresh()
{
	var tntInstalled = isTurnToolInstalled();
	if(tntInstalled==false)
	{
		checkCounter++;
		if (checkCounter>20)
		{
			checkCounter=0;
			installTurnTool();
		}
		setTimeout('checkRefresh()',500);
	}
	else
	{
		window.location.href = window.location;
	}
}

function TNTDoCommand(string)
{
	var control = element("TNTCtrl");
	if(control && control.ready)
		return control.TNTDoCommand(string);
	return "";
}

var intervaltimer = 0;
var intervaltimerGuided = 0;

usingCamera02 = true;
usingCameraGuided = false;

function LookOutside(skysphere)
{
	element("toit").disabled = false;
  TNTDoCommand('CameraCtrl.SetCurrent("Camera02")');
  usingCamera02 = true;
  //Trick to morph Cameras FOV
  TNTDoCommand('CameraCtrl.Match("Camera02",0)');
		
	if (skysphere) TNTDoCommand('Objects("' + skysphere + '").SetVisible(false)');
	clearInterval(intervaltimer);
  clearInterval(intervaltimerGuided);
  element("camXY").style.visibility= 'hidden';
  usingCameraGuided = false;
}

function GuidedTourInteractive(echelleX, dx, dy, imgx, imgy, etage)
{
	camera_etage = "CameraGuided";
	
	clearInterval(intervaltimer);
	clearInterval(intervaltimerGuided);
	
	if (etage== 'RDC')
	{
		camera_etage = "CameraGuided_rdc";
		intervaltimerGuided = setInterval(function() {SetUpdatedCameraPos("CameraGuided_rdc", echelleX, dx, dy, imgx, imgy);}, 100);
	}
	else
	{
		camera_etage =  "CameraGuided_r1";
		intervaltimerGuided = setInterval(function() {SetUpdatedCameraPos("CameraGuided_r1", echelleX, dx, dy, imgx, imgy);}, 100);
	}
		
	chargerMap(etage);
		
	StartFrame = TNTDoCommand('SceneGraph.ObjectTree(' + camera_etage + ').GetStartFrame()');
	StopFrame = TNTDoCommand('SceneGraph.ObjectTree(' + camera_etage + ').GetStopFrame()');
	TNTDoCommand('SceneGraph.ObjectTree(' + camera_etage + ').StopAnimation()');
	TNTDoCommand('SceneGraph.ObjectTree(' + camera_etage + ').PlayAnimation(' + StartFrame +',' + StopFrame + ',true)');
	TNTDoCommand('SceneGraph.ObjectTree(' + camera_etage + ').SetEnable(true)');
	TNTDoCommand('CameraCtrl.SetCurrent("' + camera_etage + '")');
	
	usingCameraGuided = true;
}

function chargerMap(map){
	switch(map){
		case "RDC" : 
		element('plan_rdc').style.zIndex=2;
		element('plan_1er').style.zIndex=1;
		break;
		case "R1" : 
		element('plan_rdc').style.zIndex=1;
		element('plan_1er').style.zIndex=2;
		break;
	}
}


function ChangeAngleInteractive(camera, physsphere, echelleX, dx, dy, imgx, imgy, etage)
{
	TNTDoCommand("SceneGraph.Objects(" + physsphere + ").SetEnable(true)");
	TNTDoCommand("CameraCtrl.SetCurrent('" + camera + "')'");
	usingCamera02 = false;
	TNTDoCommand('SceneGraph.Physics.Reset()');
	
	chargerMap(etage);
	
	clearInterval(intervaltimer);
	clearInterval(intervaltimerGuided);
	intervaltimer = setInterval(function() {SetUpdatedCameraPos(camera,echelleX, dx, dy, imgx, imgy);}, 100);
	element("TNTCtrl").focus();
	usingCameraGuided = false;
}

//Fonction pour repère camera sur plan
function SetUpdatedCameraPos(cam, echelleX, dx, dy, imgx, imgy)
{
		pos_cam_x = Math.ceil(TNTDoCommand("SceneGraph.Objects(" + cam + ").GetPositionWorldX()")); 
		pos_cam_y = Math.ceil(TNTDoCommand("SceneGraph.Objects(" + cam + ").GetPositionWorldY()"));
	
		orient_cam_z = Math.ceil(TNTDoCommand("SceneGraph.Objects(" + cam + ").GetRotationWorldZ()"));
		
		angleidx = Math.round(orient_cam_z/30);
		if (angleidx == 12) (angleidx = 0) ;
		imgrotatecursor = "<img src='./images/curseur_map_" + angleidx + ".gif' border=0>" ;
		
		ratioech = echelleX/imgx;
		deltaX = dx;
		deltaY = dy;
		if (document.all && !window.opera && window.XMLHttpRequest)
			{
				deltaX -= 10;
				deltaY -= 2;
			}
		if (firefox)
		{
			deltaX -= 11;
			deltaY -= 3;
		}
		
		pos_ico_x=Math.ceil(pos_cam_x/ratioech + deltaX); 
		pos_ico_y=Math.ceil(-pos_cam_y/ratioech + deltaY);
		
		if (pos_cam_x <= 0) pos_ico_x=deltaX;
			else
				if
					(pos_ico_x >= deltaX + imgx) pos_ico_x=deltaX+imgx;
		if (pos_cam_y >= 0) pos_ico_y=deltaY;
			else
				if
					(pos_ico_y >= deltaY + imgy) pos_ico_y=deltaY+imgy;
		
	if (document.getElementById || (ie4))
   {
    element("camXY").style.left = pos_ico_x;
		element("camXY").style.top = pos_ico_y;
		element("camXY").innerHTML =  imgrotatecursor ;
		element("camXY").style.visibility= 'visible';
   }
   else if (document.layers)
   {
   	document.layers["camXY"].left = pos_ico_x;
		document.layers["camXY"].top = pos_ico_y;
		document.layers["camXY"].innerHTML =  imgrotatecursor ;
		document.layers["camXY"].style.visibility= 'visible';
   }
}

var renderidx = 0;

function Snapshot2x()
{
	renderidx += 1 ;
	if (ie4||ie5){
		 	var pos =  documentname() + '_' + renderidx +'.bmp';
		 	if(confirm('Enregistrer la vue 3D actuelle sur votre bureau en tant que "'+pos+' " ?')){
				TNTDoCommand('Renderer.SaveImage("'+pos+'",1)');
	 			alert("La capture a été placée sur votre bureau : "+pos);
	 		}
		}
	else{
			var pos =  'C:\\'+documentname() + '_' + renderidx +'.bmp';
			if(confirm('Enregistrer la vue 3D actuelle sur votre disque en tant que "'+pos+' " ?')){
				TNTDoCommand('Renderer.SaveImage("'+pos+'",1)');
		 		alert("La capture a été placée sur C:  : "+pos);
			}
		}
}

function documentname()
{
	var doc = document.location.href;
	var tab = doc.split('/');
	var page = tab[tab.length-1];
	tab = page.split('.');
	var nom = tab[0];
	return(nom);
}

var showhide2 = false;

var hideshow = false;

var CurFrame = 0;
var animated = false;

function doCommand()
{
	var retval = TNTDoCommand( element("doCommandEdit").value );
	if(retval)
		element("returnval").innerHTML = 'Return Value = "' + retval + '"';
	else
		element("returnval").innerHTML = "";
}

var objectIndex=0;


function TntCtrlBrowser(typeobj, typebg)
{
	var installationIE = '<OBJECT id="TNTCtrl" width="640" height="420" classid="CLSID:402ee96e-2ce8-482d-ada5-ceceea07e16d" codebase="http://www.turntool.com/ViewerInstall.exe#version=2,12,0,8">' +
 											'<param name="transparent" value="1">' +
 											'<param name="tnt_back_color" value="' + typebg + '">' +
 											'<param name="ctrl_color" value="#000000">' +
 											'<param name="src" value="' + typeobj + '.tnt">' +
 											'<param name="script" value="ProgressBar.SetDownloadText(\'Téléchargement de la scène 3D en cours (made by VISIMMO 3D)\');ProgressBar.SetCoreDownloadText(\'Mise à jour du plugin 3D\');TNTCtrl.SetLogo(\'None\');">' +
 											'</OBJECT>';
 											
 var installationMozilla = "<embed id='TNTCtrl' width='640' height='420' src='" + typeobj + ".tnt' transparent='1' ctrl_color='#0000FF' " +
 													 "tnt_back_color='" + typebg + "' pluginspage='http://www.turntool.com/download/' type='application/tntfile'></embed>'" ;
 	
 			if (ie4||ie5)
 			{								
 			document.write(installationIE);								
			}
			else
			{
			document.write(installationMozilla);								
			}
}

function TntCtrlBrowserSize(typeobj, typebg, tailleX, tailleY)
{
	var installationIE = '<OBJECT id="TNTCtrl" width="' + tailleX + '" height="' + tailleY + '" classid="CLSID:402ee96e-2ce8-482d-ada5-ceceea07e16d" codebase="http://www.turntool.com/ViewerInstall.exe#version=2,12,0,8">' +
 											'<param name="transparent" value="1">' +
 											'<param name="tnt_back_color" value="' + typebg + '">' +
 											'<param name="ctrl_color" value="#000000">' +
 											'<param name="src" value="' + typeobj + '.tnt">' +
 											'<param name="script" value="ProgressBar.SetDownloadText(\'Téléchargement de la scène 3D en cours (made by VISIMMO 3D)\');ProgressBar.SetCoreDownloadText(\'Mise à jour du plugin 3D\');TNTCtrl.SetLogo(\'None\');">' +
 											'</OBJECT>';
 											
 var installationMozilla = '<embed id="TNTCtrl" width="' + tailleX + '"  height="' + tailleY +  '"src="' + typeobj + '.tnt" transparent="1" ctrl_color="#0000FF"' +
 													 'tnt_back_color=\'' + typebg + ' pluginspage=\'http://www.turntool.com/download/\' type=\'application/tntfile\'></embed>' ;
 	
 			if (ie4||ie5)
 			{								
 			document.write(installationIE);								
			}
			else
			{
			document.write(installationMozilla);								
			}
}

