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

var myPath = location.href;
var pos = myPath.lastIndexOf("/"); 
myPath = myPath.substring(0, pos+1);
//alert (myPath);
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 isTurnToolInstalled()
{
	TNTDoCommand('Objects(*).PlayAnimation(0,400, true)');
	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 onDoCommandKeypress()
{
	if(window.event.keyCode==13) // if return key was pressed
		doCommand();
}

function OnReady()
{
	getObjects();
	cameraButtons();
	animationButton();
	element("TNTCtrl").focus();
	// load textures marked as external with their default textures
	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 + '")');
		}
	}
	window.status = "TurnTool Ready";	
}

function OnClick( obj )
{
	if(TNTDoCommand('Objects(' + obj + ').GetStopFrame()')!=1300)
		TNTDoCommand('Objects(' + obj + ').PlayAnimation(,1300)');
	else
		TNTDoCommand('Objects(' + obj + ').PlayAnimation(,0)');
	window.status = "Object clicked: " + obj;	
}

function OnMouseEnter( obj )
{
	TNTDoCommand('Objects(' + obj + ').SetDiffuseColor(#FF0000)');
	window.status = "Mouse Entered: " + obj;	
}

function OnMouseExit( obj )
{
	TNTDoCommand('Objects(' + obj + ').ResetMaterial()');
	window.status = "Mouse Exited: " + obj;	
}

function OnZoneEnter( zone, physics )
{
	element("TNTCtrl").focus();
	window.status = "Zone Entered: " + zone + ", by physics object: " + physics;	
}

function OnZoneExit( zone, physics )
{
	element("TNTCtrl").focus();
	window.status = "Zone Exited: " + zone + ", by physics object: " + physics;	
}

function OnKeyPress( keyCode, ascii )
{
	window.status = "KeyPress: KeyCode=" + keyCode + " Ascii=" + ascii;
}

function OnKeyRelease( keyCode, ascii )
{
	window.status = "KeyRelease: KeyCode=" + keyCode + " Ascii=" + ascii;
}

function Restart()
{
	TNTDoCommand('SceneGraph.Physics.Reset()');
	TNTDoCommand('Objects(*).StopAnimation()');
	TNTDoCommand('Objects(*).SetFrame(0)');
	cameraName = TNTDoCommand('SceneGraph.Camera(0).GetName()');
	TNTDoCommand('CameraCtrl.SetCurrent("'+ cameraName+'")');
}

function HideShowObject(id)
{
	var checkboxEl = document.getElementById('checkbox0' + id + '');
	objname = TNTDoCommand('SceneGraph.Mesh('+ id + ').GetName()');
	TNTDoCommand('Objects(' + objname + ').SetVisible('+(!checkboxEl.checked)+')');
}

function getObjects()
{
	var rs = '<br/>';
	objectCount = TNTDoCommand('SceneGraph.GetMeshCount()');
	rs += '<table class="box" 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 i = 0;
	while(i < objectCount)
	{
		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="checkbox0' + i + '" onClick="JavaScript:HideShowObject(' + i + ');" notab><br/>';
		rs += '</td></tr>';
		i++;
	}
	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 MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
<!--
function chgpage(formulaire) {
 if (formulaire.listepages.selectedIndex != 0) {
  location.href = formulaire.listepages.options[formulaire.listepages.selectedIndex].value;
 }
}
//-->

<!--
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
 if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
   document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
 else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
//-->

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

<!--
i = 1;
function affiche(numero) {
  i = numero;
  img.src = './images/'+i+'.jpg';
}
// -->


function bouton_aube() {
	
		TNTDoCommand('CameraCtrl.Match("Camera02",2000)');
		TNTDoCommand('Objects(*).PlayAnimation(0,400, true)');

}

function vertical() {
	TNTDoCommand('CameraCtrl.Match("Camera04",2000)');
	TNTDoCommand('Objects(*).PlayAnimation(0,400, true)');
}

function zoom() {
	TNTDoCommand('CameraCtrl.Match("Camera03",2000)');
	TNTDoCommand('Objects(*).PlayAnimation(0,400, true)');	
}

function eclate() {
	TNTDoCommand('CameraCtrl.Match("Camera04",2000)');
	TNTDoCommand('Objects(*).PlayAnimation(400,1050)');		
}


function pale_seule() {
	TNTDoCommand('CameraCtrl.Match("Camera01",2000)');
	TNTDoCommand('Objects(*).PlayAnimation(0,0)');	
}

function pale_fluide() {
	TNTDoCommand('CameraCtrl.Match("Camera02",2000)');
	TNTDoCommand('Objects(*).PlayAnimation(0,55, true)');	
}

