// 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);

var xpcomversion = "1.3.0.0";
var checkCounter = 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 createTurnTool(width, height, file, color, transparent)
{
	var turntoolObject = '<div id="installTag"><div class="inWinNote"><h1 style="margin: 25px 0px 0px 0px;">3D Plugin Installation</h1><p>Internet Explorer Users: There is usually a small delay before the plugin installation is activated.</p><div id="colA"><p>1. Click \'run\' when the following warning popup appears</p><img src="images/secWarning_savefile.gif" /></div><div id="colB"><p>2. Click \'run\' when the following warning popup appears</p><img src="images/secWarning_runfile.gif" /></div><div class="ffContentKeeper"></div></div></div>';
	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" value="'+color+'">';
		turntoolObject += '<param name="script" value="ProgressBar.SetDownloadText(\'Downloading T3D Resource\');ProgressBar.SetCoreDownloadText(\'Downloading T3D Core\');TNTCtrl.SetLogo(\'None\');">';
		turntoolObject += '<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+'" script="ProgressBar.SetDownloadText(\'Downloading T3D Resource\');ProgressBar.SetCoreDownloadText(\'Downloading T3D Core\');TNTCtrl.SetLogo(\'None\');" src="'+file+'" transparent="'+transparent+'" ctrl_color="'+color+'"';
		turntoolObject += 'tnt_back_color="'+color+'" pluginspage="http://www.turntool.com/download/" type="application/tntfile"></embed>';
	}
	element('TurnTool').innerHTML = turntoolObject;
	if(isTurnToolInstalled())
		element('installTag').style.display = 'none';
		
}
function manualInstallTurnTool()
{
	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 initTurnTool()
{
	var tntInstalled = isTurnToolInstalled();
	if (tntInstalled==false)
	{
		if (ie4||ie5)
		{
			createTurnTool(tntSizeX,tntSizeY,tntSource, tntCtrlColor, tntTransparentOption);
		}
		else
		{
			element('TurnTool').innerHTML = '<div id="installTag"><div class="inWinNote"><h1 style="margin: 25px 0px 0px 0px;">3D Plugin Installation</h1><p>Internet Explorer Users: There is usually a small delay before the plugin installation is activated.</p><div id="colA"><p>1. Click \'run\' when the following warning popup appears</p><img src="images/secWarning_savefile.gif" /></div><div id="colB"><p>2. Click \'run\' when the following warning popup appears</p><img src="images/secWarning_runfile.gif" /></div><div class="ffContentKeeper"></div></div></div>';
			installTurnTool();
		}
		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
	{
		var v = navigator.plugins.namedItem("TurnTool XPCOM Plugin");
		if(v && v.description) {
			var version = v.description.replace(/([a-zA-Z]|\s)+/, "").split(".");
			var minversion = xpcomversion.split(".");
			for (var i=0; i<minversion.length; i++)
			{
				if (i >= version.length)
					break;
				if (version[i] > minversion[i])
				{
					tntInstalled = true;
					break;
				}
				if (version[i] < minversion[i])
					break;
				if (i == (minversion.length-1))
					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)
{
   if (result>=0)
   {
      element('TurnTool').innerHTML = '<p class="inWinNote">You must restart your browser to view the content of this page.<br />Please reopen this page after restart of your browser.</p>';
      alert('The installation requires that you restart your browser');
   }
   else if(result == -210)
   {
      alert('You must install the 3D plugin to see the 3D content of this page.');
   }
   else
   {
      alert('<p class="inWinNote">The installation failed, please close all browser windows and try again.\n\nIf the problem continues, try restarting your computer.\n\nIf the problem still continues, please contact your content provider.</p>');
   }
}

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()
{
	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);