// JavaScript Document
function createT2L(width, height, file, color, transparent)
{
	var DownloadText = "Downloading True2Life Resource";
	var CoreDownloadText = "True2Life Viewer Update Downloading";
	var SetLogo = "None";

  if (ie4||ie5)
  {
  	T2LObject = '<object id="TNTCtrl" width="'+width+'" height="'+height+'" classid="CLSID:402ee96e-2ce8-482d-ada5-ceceea07e16d" codebase="http://www.true2life.co.uk/files/ViewerInstall.exe#version=2,12,0,8"><param name="transparent" value="'+transparent+'"><param name="ctrl_color" value="'+color+'"><param name="tnt_back_color" value="'+color+'"><param name="src" value="'+file+'"> <param name="script" value="ProgressBar.SetDownloadText(\''+DownloadText+'\');ProgressBar.SetCoreDownloadText(\''+CoreDownloadText+'\');TNTCtrl.SetLogo(\''+SetLogo+'\')"></OBJECT>';
  }
  else
  {
	T2LObject = '<embed id="TNTCtrl" script="ProgressBar.SetDownloadText(\''+DownloadText+'\');ProgressBar.SetCoreDownloadText(\''+CoreDownloadText+'\');TNTCtrl.SetLogo(\''+SetLogo+'\')" width="'+width+'" height="'+height+'" src="'+file+'" transparent="'+transparent+'" tnt_back_color="'+color+'" pluginspage="http://www.true2life.co.uk/files/ViewerInstall.xpi" type="application/tntfile"></embed>';
  }
  element('True2Life').innerHTML = T2LObject;
}

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 initT2L(width, height, file, color, transparent)
{
	var DownloadText = "Downloading True2Life Resource";
	var CoreDownloadText = "True2Life Viewer Update Downloading";
	var SetLogo = "None";
	
	var t2lInstalled = isT2LInstalled();
	if (t2lInstalled==false)
	{
		element('InstallTag').innerHTML = '<CENTER><INPUT TYPE=button VALUE="Install True2Life Viewer" onClick="installT2L();"></CENTER>';
		createT2L(width, height, file, color, transparent);
		checkRefresh();
	}
	else
	{
		createT2L(width, height, file, color, transparent);
		
	}	
}

function isT2LInstalled()
{
	var t2lInstalled = false;
	if (ie4||ie5)
	{
		try
		{
			var xObj = new ActiveXObject("TNT.TNTCtrl");
			if (xObj)
			{
				t2lInstalled = true;
			}
		}
		catch (e)
		{
		}
	}
	else
	{
		if(navigator.plugins.namedItem("TurnTool XPCOM Plugin"))
		{
			t2lInstalled = true;
		}
	}
	return t2lInstalled;
}

function installT2L() 
{
	if (ie4||ie5)
	{
		window.location.href = "http://www.true2life.co.uk/files/OfflineInstall.exe";
	}
	else
	{
		var xpi = {'True2life Viewer Installation':'http://www.true2life.co.uk/files/ViewerInstall.xpi'};
		InstallTrigger.install(xpi,installFinish);
	}
}

function installFinish(url, result)
{
	window.location.href = window.location;
}

var checkCounter = 0;
function checkRefresh()
{
	var t2lInstalled = isT2LInstalled();
	if(t2lInstalled==false)
	{
		checkCounter++;
		if (checkCounter==120)
		{
			//checkCounter=0;
			installT2L();
		}
		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 doCommand()
{
	var retval = TNTDoCommand( element("doCommandEdit").value );
	if(retval)
		element("returnval").innerHTML = 'Return Value = "' + retval + '"';
	else
		element("returnval").innerHTML = "";
}
