// General TurnTool script, DOT NOT EDIT, make changes in template.js file.

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 createTurnTool(width, height, file, color, transparent)
{
  if (ie4||ie5)
  {
  	turntoolObject = '<object id="TNTCtrl" width="'+width+'" height="'+height+'" classid="CLSID:402ee96e-2ce8-482d-ada5-ceceea07e16d"';
  	turntoolObject += ' codebase="http://www.turntool.com/ViewerInstall.exe#version=2,12,0,8"><param name="transparent"'; 
  	turntoolObject += ' value="'+transparent+'"><param name="ctrl_color" value="'+color+'"><param name="tnt_back_color"';
  	turntoolObject += ' value="'+color+'"><param name="src" value="'+file+'"></object>';
  	turntoolObject += '<sc'+'r'+'ipt LANGUAGE=JavaScript FOR=TNTCtrl EVENT=TNTEvent(string)> execScript(string); </sc'+'r'+'ipt>';
  }
  else
  {
	turntoolObject = '<embed id="TNTCtrl" width="'+width+'" height="'+height+'" src="'+file+'" transparent="0" ctrl_color="'+color+'"';
	turntoolObject += 'tnt_back_color="'+color+'" pluginspage="http://www.turntool.com/download/" type="application/tntfile"></embed>';
  }
  element('TurnTool').innerHTML = turntoolObject;
}

function initTurnTool()
{
	var tntInstalled = isTurnToolInstalled();
	if (tntInstalled==false)
	{
		element('TurnTool').innerHTML = '<center><input type="button" value="Install TurnTool Viewer" onclick="installTurnTool();"></center>';
		if (ie4||ie5)
		{
			createTurnTool(tntSizeX,tntSizeY,tntSource, tntCtrlColor, tntTransparentOption);
		}
		checkRefresh();
	}
	else
	{
		createTurnTool(tntSizeX,tntSizeY,tntSource, tntCtrlColor, tntTransparentOption);
	}	
}

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/OfflineInstall.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>120)
		{
			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 focus()
{
	if (!ns6)
	{
		element("TNTCtrl").focus();
	}
}

function OnClick( obj )
{
	if(TNTDoCommand('Objects(' + obj + ').GetStopFrame()')!=100)
		TNTDoCommand('Objects(' + obj + ').PlayAnimation(,100)');
	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 )
{
	focus();
	window.status = "Zone Entered: " + zone + ", by physics object: " + physics;	
}

function OnZoneExit( zone, physics )
{
	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 addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj.attachEvent( "on"+type, function() { obj["e"+type+fn](); } );
	}
}

function removeEvent( obj, type, fn )
{
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj["e"+type+fn] );
		obj["e"+type+fn] = null;
	}
}

addEvent(window,"load",initTurnTool);
