/* JavaScript Document

Filename: 		true2life.js
Version:		1.0 - 10th Dec 2007
Author:			Geoff Coope
Company:		True2Life Ltd
Contact:		(www.true2life.co.uk) (sales@true2life.co.uk)

Description:	This file handles the installation and configuration of the True2Life ActiveX Viewer
				A div must be in the calling HTML file called "True2Life" which will be the container for our 3D viewer

Notes;			For project specific code see <project name>.js

History:		1.0 - Initial Creation 


*/

// Global Vars
var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById);
var ns6 = (!document.all && document.getElementById);
var checkCounter = 0;

// 
// Desc: Create and Start the True2Life 3D Viewer
// 
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('True2Life').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);
 }	
} // func

//
// Desc: Swaps the True2Life div with the viewer control
//
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/true2life_web3d_viewer_install.exe"><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>';
	T2LObject += '<sc'+'r'+'ipt LANGUAGE=JavaScript FOR=TNTCtrl EVENT=TNTEvent(string)> execScript(string); </sc'+'r'+'ipt>';
  } 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/true2life_web3d_viewer_install.exe" type="application/tntfile"></embed>';
  }
  
 element('True2Life').innerHTML = T2LObject;
 
  
} // func



//
// Desc: Access to DOM based on browser version.
//
function element(id)
{
	if(ie4)	// Explorer 4
		return document.all[id];
	else	// Explorer 5+ Netscape 6+ and Mozilla and Firefox
		return document.getElementById(id);
} // func


//
// Check to see if our viewer is installed. Returns: boolean value.
//
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;
}


//
// Desc: Installs viewer if not found.  Links to latest signed version on our website.
//
function installT2L() 
{
 window.location.href = "http://www.true2life.co.uk/files/true2life_web3d_viewer_install.exe";
}


// 
// When installation finished in FireFox, reload page.
//
function installFinish(url, result) {
	window.location.href = window.location;
}


//
// Desc: Loop check for completion of viewer installation.
//
function checkRefresh()
{
	var t2lInstalled = isT2LInstalled();
	if(t2lInstalled==false) {
	   checkCounter++;
	   if (checkCounter==120)
		   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 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;
	}
}


function prep() {
  initT2L(515,390,'../portfolio/spider/spider_ipod_comp.t2l', '#ffffff', 0);	
}


addEvent(window,"load",prep);


