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 commandes_interieur='<img src="../../images/commandes_vue_interieure.gif">' ;  
var commandes_exterieur='<img src="../../images/commandes_vue_exterieure.gif">' ;

var fullpath = 'http://www.visimmo3d.com/example/';

function afficheimage (adresseimage, index) {
 document.images[index].src = adresseimage;
}

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();
 											
	if (tntInstalled==false)
	{
		popInfo({content:'./turntool.html',width:'560',height:'382',hide:['flashfunction'],title:'t&eacute;l&eacute;chargement plugin',status:'visimmo3d'});
		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;

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

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

function WalkInside(echelleX, skysphere)
{
	TNTDoCommand('SceneGraph.Objects(PhysSphere).SetEnable(true)');
	TNTDoCommand('CameraCtrl.SetCurrent("Camera01")');
	usingCamera02 = false;
	TNTDoCommand('SceneGraph.Physics.Reset()');
	if (echelleX)
	{
		ShowObj("MURS_exterieur");
		if (document.getElementById || (ie4))
	    element("commandes").innerHTML = commandes_interieur;
	  else if (document.layers)
			document.layers["commandes"].innerHTML = commandes_interieur;
	}
	if (skysphere) TNTDoCommand('Objects("' + skysphere + '").SetVisible(true)');			
	element("TNTCtrl").focus();
}


function LookOutside(skysphere)
{
	//element("toit").disabled = false;
	//element("measures").disabled = false;
	
  TNTDoCommand('SceneGraph.ObjectTree(PhysSphere).SetEnable(false)');
  TNTDoCommand('CameraCtrl.SetCurrent("Camera02")');
  usingCamera02 = true;
  //Trick to morph Cameras FOV
  TNTDoCommand('CameraCtrl.Match("Camera02",0)');
		
	if (skysphere) TNTDoCommand('Objects("' + skysphere + '").SetVisible(false)');
	
}

function do360(newCamera)
{
	StartFrame = TNTDoCommand('SceneGraph.Objects("Camera360").GetStartFrame()');
	StopFrame = TNTDoCommand('SceneGraph.Objects("Camera360").GetStopFrame()');
	TNTDoCommand('SceneGraph.Objects("Camera360").StopAnimation()');
	
	TNTDoCommand('SceneGraph.Objects("Camera360").SetEnable(true)');
	TNTDoCommand('CameraCtrl.Match("Camera360",0)');
	TNTDoCommand('CameraCtrl.SetCurrent("Camera360")');

	TNTDoCommand('SceneGraph.Objects("Camera360").PlayAnimation(' + StartFrame +',' + StopFrame + ',true,50)');
}

function do360_2(newCamera)
{
	StartFrame = TNTDoCommand('SceneGraph.Objects("Camera360_top").GetStartFrame()');
	StopFrame = TNTDoCommand('SceneGraph.Objects("Camera360_top").GetStopFrame()');
	TNTDoCommand('SceneGraph.Objects("Camera360_top").StopAnimation()');
	
	TNTDoCommand('SceneGraph.Objects("Camera360_top").SetEnable(true)');
	TNTDoCommand('CameraCtrl.Match("Camera360_top",0)');
	TNTDoCommand('CameraCtrl.SetCurrent("Camera360_top")');

	TNTDoCommand('SceneGraph.Objects("Camera360_top").PlayAnimation(' + StartFrame +',' + StopFrame + ',true,15)');
	setRoofOff();
}

function showterrasse(couleur)
{
	switch(couleur)
	{
		case 'exotique':
		TNTDoCommand('Objects("Exterieur_Terrasse01").SetVisible(true)');
		TNTDoCommand('Objects("Exterieur_Terrasse02").SetVisible(false)');
		break;
		
		case 'gris':
		TNTDoCommand('Objects("Exterieur_Terrasse01").SetVisible(false)');
		TNTDoCommand('Objects("Exterieur_Terrasse02").SetVisible(true)');
		break;
	}
}

usingCamera02 = true;

function GuidedTour()
{
	//element("toit").disabled = true;
	//element("measures").disabled = true;
	
	StartFrame = TNTDoCommand('SceneGraph.ObjectTree(CameraGuided).GetStartFrame()');
	StopFrame = TNTDoCommand('SceneGraph.ObjectTree(CameraGuided).GetStopFrame()');
	TNTDoCommand('SceneGraph.ObjectTree(CameraGuided).StopAnimation()');
	
	TNTDoCommand('SceneGraph.ObjectTree(CameraGuided).SetEnable(true)');
	TNTDoCommand('CameraCtrl.Match("CameraGuided",0)');
	TNTDoCommand('CameraCtrl.SetCurrent("CameraGuided")');

	TNTDoCommand('SceneGraph.ObjectTree(CameraGuided).PlayAnimation(' + StartFrame +',' + StopFrame + ',true, 28)');
}


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 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 makeObjectList()
{
	var rs = '<br/>';
	var objectCount = TNTDoCommand('SceneGraph.GetMeshCount()');
	rs += '<table width="200" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top">';
	rs += '<table width="200" border="0" cellspacing="0" cellpadding="0"><tr bgcolor="#BCC2C4"><td>';
	rs += '<p><b>Objects:</b></p></td><td><p><b>Hide:</b></p></td>';
	var objectStart = objectIndex;
	var objectEnd = objectIndex+24;
	if (objectEnd>objectCount)
		objectEnd=objectCount;
	for (var i=objectIndex; i<objectEnd; i++)
	{
		objectName = TNTDoCommand('SceneGraph.Mesh(' + i + ').GetName()');
		if(i%2 == 0)
			rs += '<tr bgcolor="#EEEEEE"><td><p>';
		else
			rs += '<tr bgcolor="FFFFFF"><td><p>';
		rs += objectName;
		rs += '</p></td><td align="right">';
		rs += '<input type="checkbox" id="checkbox' + i + '" onClick="JavaScript:HideShowObject(' + i + ');" notab>';
		rs += '</td></tr>';
	}
	rs += '</table><center>';
	rs += '<img src="tnt_mouse_help.gif" alt="Left mousebutton: Rotate\r\nRight mousebutton: Zoom\r\nBoth mousebuttons: Pan">';
	rs += '</center></td></tr></table>';
	LevelTable.innerHTML = rs;
}



function loadExternalTextures()
{
	var texcnt = 0;
	texcnt = TNTDoCommand('SceneGraph.GetBitmapCount()');
	var bExternal = 64;
	var bHasAlpha = 32;
	for( var i=0; i<texcnt; i++ )
	{
		var props = 0;
		props = TNTDoCommand('SceneGraph.Bitmap(' + i + ').GetProperties()');
		if(props & bExternal)
		{
			var name = TNTDoCommand('SceneGraph.Bitmap(' + i + ').GetName()');
			if(props & bHasAlpha)
			{
				name += '.png'; // alpha textures are exported as png
			}
			else
			{
				name += '.jpg'; // non-alpha textures are exported as jpg
			}
			TNTDoCommand('SceneGraph.Bitmap(' + i + ').Load("' + name + '")');
		}
	}
}

function cb(source,target)
{
	TNTDoCommand ('SceneGraph.Bitmap("' + source + '").Load("' + target + '")');
	//Firefox workaround for instant switching textures
	SetObjColor("Dummy01", "#FFFFFF");
}

function Resetb(source,target)
{
	TNTDoCommand ('SceneGraph.Bitmap("' + source + '").Reset()');
}


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);								
			}
}


function loadScene(tntfile)
{	
	if (firefox)
		alert ("Fonction non compatible avec firefox");
	else
		element('TNTCtrl').src = (tntfile);
} 

function Walk(camera, physsphere, skysphere)
{
	//element("toit").disabled = true;
	//element("measures").disabled = true;

	TNTDoCommand("SceneGraph.Objects(PhysSphere*).SetEnable(false)");
	TNTDoCommand("Objects(*" + physsphere + "*).SetEnable(true)");
	TNTDoCommand("CameraCtrl.SetCurrent('" + camera + "')'");
	TNTDoCommand('SceneGraph.Physics.Reset()');
	TNTDoCommand('Objects(*arrow*).SetEnable(true)');
		
	if (skysphere) TNTDoCommand('Objects("' + skysphere + '").SetVisible(true)');
	
	element("TNTCtrl").focus();
}
