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

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="820" height="484" src="furnituremove.tnt" transparent="1" ctrl_color="#FFFFFF" tnt_back_color="#FFFFFF" 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 "";
}

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

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 changeCamera(newCamera, time)
{
	if (!time) time=2000;
	
	var currentCamera = TNTDoCommand('CameraCtrl.GetCurrent()');
	var srcParentNodeIdx = TNTDoCommand('Objects('+currentCamera+').GetParentNodeIndex()');
	var desParentNodeIdx = TNTDoCommand('Objects('+newCamera+').GetParentNodeIndex()');
	if (srcParentNodeIdx == desParentNodeIdx)
		TNTDoCommand('CameraCtrl.Match("'+newCamera+'",'+ time + ')');
	else
		TNTDoCommand('CameraCtrl.SetCurrent("'+newCamera+'")');
}

var movemode=0;

function OnMouseEnter( obj )
{
	if (movemode==1)
	{
		if (!moving)
		{
			TNTDoCommand('Objects(' + obj + ').SetSelected(1)');
			TNTDoCommand('Objects(' + obj + ').SetVisible(1)');
		}
	}
}

function OnMouseExit( obj )
{
	if (movemode==1)
	{
		TNTDoCommand('SceneGraph.Objects('+ obj +').SetSelected(0)');
		TNTDoCommand('SceneGraph.Objects(' + obj + ').SetVisible(0)');
	}
}

var moving=false;
function OnMoveStart()
{
	moving = true;
	TNTDoCommand('CameraCtrl.SetIgnoreInput(1)');
}

function OnMoveEnd()
{
	moving = false;
	TNTDoCommand('CameraCtrl.SetIgnoreInput(0)');
}

function MoveMode(on,cameraout)
{
		if (on)
		{
			MoveCamera01Dummy(3);
			changeCamera('Camera01', 500);
			TNTDoCommand('SceneGraph.Objects("Box*").SetMouseOverEvent(1)');	
			TNTDoCommand('SceneGraph.Objects("Box*").SetOcclusion(1)');
			TNTDoCommand('Selection.SetMoveDirectionZ(0)');
			TNTDoCommand('Selection.SetLeftMode(1)'); 
			TNTDoCommand('Selection.SetRightMode(5)'); 
			movemode=1;
		}
		else
		{
			TNTDoCommand('SceneGraph.Objects("Box*").SetMouseOverEvent(0)');	
			TNTDoCommand('SceneGraph.Objects("Box*").SetOcclusion(0)');
			TNTDoCommand('CameraCtrl.SetIgnoreInput(0)');
			MoveCamera01Dummy(3);
			changeCamera('Camera01', 500);
			movemode=0;
		}
}

function MoveCamera01Dummy(nbr)
{
	//nbr = number of Box
	//Move Dummy to Barycenter of the nbr Box
	var xPos=0;
	var yPos=0;
	for (i=1; i<=nbr; i++)
	{
	xPos+=parseInt(TNTDoCommand("SceneGraph.Objects('Box0" + i + "').GetPositionWorldX()"));
	yPos+=parseInt(TNTDoCommand("SceneGraph.Objects('Box0" + i + "').GetPositionWorldY()"));
	}
	xPos/=nbr;
	yPos/=nbr;
	TNTDoCommand("SceneGraph.Objects('Dummy02').SetPositionWorld(" + xPos + "," + yPos + ",0)'");
}

