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 = '';

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 LookOutside(skysphere)
{
  TNTDoCommand('SceneGraph.ObjectTree(PhysSphere*).SetEnable(false)');
  TNTDoCommand('CameraCtrl.SetCurrent("Camera02")');

  //Trick to morph Cameras FOV
  TNTDoCommand('CameraCtrl.Match("Camera02",0)');
		
	if (skysphere) TNTDoCommand('Objects("' + skysphere + '").SetVisible(false)');
	
	TNTDoCommand('Objects(*arrow*).SetEnable(false)');
	TNTDoCommand('Objects(arrow).SetEnable(true)');
}


usingCamera02 = true;


function GuidedTour()
{
	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,20)');
	TNTDoCommand('Objects(*arrow*).SetEnable(false)');		
}


var renderidx = 0;

function Snapshot()
{
	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+'")');
	 			alert("La capture a été placée sur votre bureau : "+pos);
	 			renderidx += 1 ;
	 		}
		}
	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+'")');
		 		alert("La capture a été placée sur C:  : "+pos);
		 		renderidx += 1 ;
			}
		}
}

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


function Walk(camera, physsphere, skysphere)
{
	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();
}

