// 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.4.0.0";

function element(id)
{
	return document.getElementById(id);
}

function createTurnTool(width, height, file, color, transparent)
{
	var turntoolObject = '<div id="InstallTag"><center><input type="button" value="Install TurnTool Viewer" onclick="installTurnTool();" /></center></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 TurnTool Resource\');ProgressBar.SetCoreDownloadText(\'Downloading TurnTool 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 TurnTool Resource\');ProgressBar.SetCoreDownloadText(\'Downloading TurnTool Core\');TNTCtrl.SetLogo(\'None\');" src="'+file+'" transparent="'+transparent+'" ctrl_color="'+color+'"';
		turntoolObject += 'tnt_back_color="'+color+'" pluginspage="http://www.turntool.com/ViewerInstall.xpi" pluginsurl="http://www.turntool.com/ViewerInstall.xpi" 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()
{
	createTurnTool(tntSizeX,tntSizeY,tntSource, tntCtrlColor, tntTransparentOption);	// create embed and object tag
	if(TNTsupportedPlatform()==true)
	{
		if (isTurnToolNewestVersion()==false && ns6)
		{
			// this is mozilla only, if missing plugin or plugin is outdated
			element('InstallTag').innerHTML = '<CENTER><INPUT TYPE=button VALUE="Install TurnTool Viewer" onclick="manualInstallTurnTool();"></CENTER>';
			manualInstallTurnTool();
		}
		if(isTurnToolInstalled() == false && (ie4||ie5))
		{
			element('InstallTag').innerHTML = '<CENTER><INPUT TYPE=button VALUE="Install TurnTool Viewer" onclick="manualInstallTurnTool();"></CENTER>';
			checkRefresh();
		}
	}	
}
function isTurnToolNewestVersion()
{
	var tntInstalled = false;
	if(ie4||ie5)
	{
		try
		{
			var xObj = new ActiveXObject("TNT.TNTCtrl");
			if (xObj)
			{
				tntInstalled = true;
			}
		}
		catch (e)
		{
		}
	}
	else
	{
		var isSupported = navigator.mimeTypes['application/tntfile'];
		isSupported = isSupported && isSupported.enabledPlugin; //in case they disabled it
		if(isSupported)
		{
			var v = navigator.plugins.namedItem("TurnTool XPCOM Plugin");
			if(v && v.description) {
				tntInstalled = true;
			}
		}	
	}	
	return tntInstalled;
}
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() 
{
	element('InstallTag').innerHTML = '<CENTER><INPUT TYPE=button VALUE="Install TurnTool Viewer" onclick="manualInstallTurnTool();"></CENTER>';
	if (ns6)
	{
		return false;
	}
	checkRefresh();
}

function installFinish(url, result)
{
	if (result>=0)
	{
		element('TurnTool').innerHTML = 'You must restart your browser to view the content of this page.<br />Please reopen this page after restart of your browser.';
		alert('The installation requires that you restart your browser');
	}
	else if(result == -210)
	{
		alert('You must install the TurnTool Viewer to see the 3d content of this page.\n\nFor more information on the TurnTool Viewer, visit http://www.turntool.com');
	}
	else
	{
		alert('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.');
	}
}

var checkCounter = 0;
function checkRefresh()
{
	var tntInstalled = isTurnToolInstalled();
	if(tntInstalled==false)
	{
		checkCounter++;
		if (checkCounter>120)
		{
			checkCounter=0;
			installTurnTool();
		}
		if(ie4||ie5)
		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 Restart()
{
	TNTDoCommand('SceneGraph.Physics.Reset()');
	TNTDoCommand('Objects(*).StopAnimation()');
	TNTDoCommand('Objects(*).SetFrame(0)');
	cameraName = TNTDoCommand('SceneGraph.Camera(0).GetName()');
	TNTDoCommand('CameraCtrl.SetCurrent("'+ cameraName+'")');
}

function TNTsupportedPlatform()
{
	if (navigator.platform.toString().indexOf("Win") != -1)
		return true;
	if (navigator.platform.toString().toLowerCase().indexOf("mac")!=-1 || navigator.platform.toString().toLowerCase().indexOf("linux")!=-1)
		return false;
	return true;
}

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);