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 renderidx = 0;

var CurFrame = 0;

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:'650',height:'450',hide:['flashfunction'],title:'t&eacute;l&eacute;chargement plugin',status:'visimmo3d'});
		checkRefresh();
	}
}

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

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 OnReady(){
		element('TNTCtrl').transparent = 0;
		//Start the fire//
		//TNTDoCommand('Objects("lounge_fire").PlayAnimation(0,7,true)');
		
		//var webOk = false;
		//new Ajax.Request('http://www.visimmo3d.com/testWeb.php',{asynchronous:false,onSuccess:function(r){webOk = true;}});
		//if (webOk)
			//TNTDoCommand('SceneGraph.Bitmap("logo_v3D").Load("http://www.visimmo3d.com/advert/alliance/plm/logo_tv_salon.jpg")');
}


function WalkInside(camera, physsphere)
{
	TNTDoCommand("SceneGraph.Objects(" + physsphere + ").SetEnable(true)");
	TNTDoCommand("CameraCtrl.SetCurrent('" + camera + "')'");
	TNTDoCommand('SceneGraph.Physics.Reset()');
	
	element("TNTCtrl").focus();
}

function LookOutside(skysphere)
{
	//A DECOMMENTER Si Checkbox MASQUER LE TOIT dans le HTML
	//element("toit").disabled = false;
  TNTDoCommand('CameraCtrl.SetCurrent("Camera01")');
  //Trick to morph Cameras FOV
  TNTDoCommand('CameraCtrl.Match("Camera01",0)');
		
	if (skysphere) TNTDoCommand('Objects("' + skysphere + '").SetVisible(false)');
	
}


function do360()
{
	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,25)');
}



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

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


