/* JavaScript Document

Filename: 		true2life.js
Version:		1.1 - 10th April 2008
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 "web3d" which will be the container for our 3D viewer
				
				This modified version also manages a secondary control.

Notes;			For project specific code see <project name>.js

History:		1.0 - Initial Creation 
				1.1 - Handle multiple controls

*/


// Global Vars
var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById);
var ns6 = (!document.all && document.getElementById);
var checkCounter = 0;


// 
// Desc: Install and Create the True2Life 3D Viewer
// 
function initT2L(width, height, file, color, transparent, el) {
 var t2lInstalled = isT2LInstalled();
 if (t2lInstalled==false) {
 	element('web3d').innerHTML = '<br><br><br><br><center>You do not have the 3D Viewer installed.<br><br> 1. Please <strong><a href="http://www.true2life.co.uk/files/OfflineInstall.exe" target="_self">click here</a></strong> to download the small (800k) 3d viewer installation.<br><br>2. After downloading please open the downloaded file, this will install it silently.<br><br>3. Close your browser and reopen it.<br><br>4. You will be asked to install the plugin, please accept it.</center>';
 } else {
 	createT2L(width, height, file, color, transparent, el);
 }	
} // func

//
// Swaps the True2Life div with the viewer control.  Code manages two controls.
//
function createT2L(width, height, file, color, transparent, el) {
 var DownloadText = "Downloading True2Life Resource";
 var CoreDownloadText = "True2Life Viewer Update Downloading";
 var SetLogo = "None";
 flashMsg('Unpacking Stove, please wait...');
// Create seperate objects for each AX control so that each can be controlled independantly
if (el=='web3d') {
	 if (ie4||ie5) {
	    T2LObject = '<object id="T2LMain" 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+'\');T2LMain.SetLogo("None");"></OBJECT>';
		// add handler code
		T2LObject += '<sc'+'r'+'ipt LANGUAGE=JavaScript FOR=T2LMain EVENT=TNTEvent(string)>';
		T2LObject += 'parse_s = string.indexOf("(");';
		T2LObject += 'parse_e = string.lastIndexOf(")");';
		T2LObject += 'obj = string.slice(parse_s+1,parse_e);';
		T2LObject += 'if(string.search(/OnMouseEnter/)!=-1){';
		T2LObject += '  Web3DMouseEnter(obj);'
		T2LObject += '} else if(string.search(/OnMouseExit/)!=-1){';
		T2LObject += '  Web3DMouseExit(obj);'
		T2LObject += '} else if(string.search(/OnClick/)!=-1){';
		T2LObject += '  Web3DOnClick(obj);'
		T2LObject += '} else if(string.search(/OnReady/)!=-1){';
		T2LObject += '  Web3DOnReady(obj)};'
		T2LObject += '</sc'+'r'+'ipt>';		
	  } else {
		T2LObject = ''
		T2LObject += '<embed id="T2LMainXP"';
		T2LObject += ' script="ProgressBar.SetDownloadText(\''+DownloadText+'\');';
		T2LObject += ' ProgressBar.SetCoreDownloadText(\''+CoreDownloadText+'\');';
		T2LObject += ' T2LMainXP.SetLogo(\''+SetLogo+'\')"';
		T2LObject += ' width="'+width+'"';
		T2LObject += ' height="'+height+'"';
		T2LObject += ' src="'+file+'"';
		T2LObject += ' transparent="'+transparent+'"';
		T2LObject += ' tnt_back_color="'+color+'"';
		T2LObject += ' pluginspage="http://www.true2life.co.uk/files/ViewerInstall.xpi" type="application/tntfile">';
		T2LObject += ' </embed>';
	  }
		// Add object
		element(el).innerHTML = T2LObject;	 
// Mini3D
} else {
	 if (ie4||ie5) {
	    T2LObject = '<object id="T2LSub" 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+'\');T2LSub.SetLogo(\'None\')"></OBJECT>';
		// add handler code
		T2LObject += '<sc'+'r'+'ipt LANGUAGE=JavaScript FOR=T2LSub EVENT=TNTEvent(string)>';
		T2LObject += 'parse_s = string.indexOf("(");';
		T2LObject += 'parse_e = string.lastIndexOf(")");';
		T2LObject += 'obj = string.slice(parse_s+1,parse_e);';
		T2LObject += 'if(string.search(/OnMouseEnter/)!=-1){';
		T2LObject += '  Mini3DMouseEnter(obj);'
		T2LObject += '} else if(string.search(/OnMouseExit/)!=-1){';
		T2LObject += '  Mini3DMouseExit(obj);'
		T2LObject += '} else if(string.search(/OnClick/)!=-1){';
		T2LObject += '  Mini3DOnClick(obj);'
		T2LObject += '} else if(string.search(/OnReady/)!=-1){';
		T2LObject += '  Mini3DOnReady(obj)};'
		T2LObject += '</sc'+'r'+'ipt>';
		
	  } else {
		T2LObject = '<embed id="T2LSubXP" script="ProgressBar.SetDownloadText(\''+DownloadText+'\');ProgressBar.SetCoreDownloadText(\''+CoreDownloadText+'\');T2LSub.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>';
	  }
	  	// Update page with control.
	    element(el).innerHTML = T2LObject;	  
	}

  
} // func

function Web3D(string) {
 	if(ie4||ie5)	
		 var control = element("T2LMain");
	else	
		 var control = element("T2LMainXP");

	if(control && control.ready) {
	return control.TNTDoCommand(string);
  }
	return "";
} // func


function Mini3D(string) {
 	if(ie4||ie5)	
		var control = element("T2LSub");
	else	
		var control = element("T2LSubXP");

	if(control && control.ready) {
	   return control.TNTDoCommand(string);
	 }

return "";
} // 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 xpcomversion = "2.9.5.1";
   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;
}


//
// Desc: Installs viewer if not found.  Links to latest signed version on our website.
//
function installT2L() 
{
	if (ie4||ie5)
	{
		window.location = "http://www.true2life.co.uk/files/OfflineInstall.exe";
	}
	else
	{
		//window.location = "http://www.true2life.co.uk/files/OfflineInstall.exe";
		var xpi = {'True2life Viewer Installation':'http://www.true2life.co.uk/files/ViewerInstall.xpi'};
		InstallTrigger.install(xpi,installFinish);
	}
}


// 
// When installation finished in FireFox, reload page.
//
function installFinish(url, result) {
	window.location.href = window.location;
	element("web3d").innerHTML = 'Install complete, please restart your browser';
}


//
// 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 focus()
{
	if (!ns6)
	{
		element("web3d").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 initialize() {
  setup();
}

addEvent(window,"load",initialize);

