var TerraExplorer;
var CurrentVersion = "4.6.2";
var InstallOK = true;
var OpenFileType = "";
var OpenFileURL = "";

//TODO: Set the path of these variables to your web site
var MyPath = "http://www.3dlisboadakar.com/resources/TEInstall/";

//InstallURL - Determines the path for TerraExplorer install
var InstallURL = "http://www.3dlisboadakar.com/resources/TEInstall/TE.cab";

//InstallVersion - Determines the minimum version for TerraExplorer install
//Format: "X,X,X,X"
var InstallVersion = "4,6,2,7";

//UpdateURL - Determines the default path for TerraExplorer updates
var UpdateURL = "http://www.3dlisboadakar.com/resources/TEInstall/basic.cab";

//ManualDownloadFilePath - Determines the default path for TerraExplorer manual download
var ManualDownloadFilePath = "http://www.3dlisboadakar.com/resources/TEInstall/TerraExplorerSetup.exe";

/*
//StartPageURL - Determines the default path for the start page displayed from Help Menu -> Terraexplorer Home
var StartPageURL;
StartPageURL = "http://www.skylin2esoft.com/Interactive/TerraExplorer/texp_startpage.asp?te";

//QuickGuideURL - Determines the default path for TerraExplorer quick guide help
// Help Menu -> TerraExplorer Help Topics F1
var QuickGuideURL;
QuickGuideURL = "http://www.skylin2esoft.com/Interactive/TerraExplorer/texp_quickguide.asp?te";

//FlightInstructionsURL - Determines the default path for TerraExplorer flight instructions
// Help Menu -> Flight instructions
var FlightInstructionsURL;
FlightInstructionsURL = "http://www.skylin2esoft.com/Interactive/TerraExplorer/texp_flight-instructions.htm";

//ManualDownloadURL - Determines the default path for TerraExplorer manual download
var ManualDownloadURL = "http://www.3dlisboadakar.com/resources/TEInstall/manual_instructions.htm";
*/

// Installation DIV Properties
var InstallDivWidth = 460;
var InstallDivHeight = 170;

// User Messages
var CurrentUserLanguage = "pt";
var InstallDivText, MsgTerraExplorerNotInstalled, MsgAlertUnsuccessfulInstall, MsgUnsuccessfulInstall, MsgCannotInstall;
		
//Inject a div for loading TerraExplorer 
document.write( "<!-- LoadingDiv -->" +
				"<DIV id='LoadingDiv' name='LoadingDiv' " +
				"style='position:absolute;" +
				"width:" + InstallDivWidth + ";" +
				"height:" + InstallDivHeight + ";" +
				"z-index:0;" +
				"left: 20;" +
				"top: 80px;" +
				"background-color: ffffff;" +
				"layer-background-color: ffffff;" +
				"border: 10px solid #808080;" +
				"visibility: hidden'>" +
				"<Span id = 'DivText' name = 'DivText'> "+
				"<center><br><br><br><font face='Verdana, Tahoma, Arial' size='2'><b><span id='InstallDivText'> "+MsgTerraExplorerNotInstalled+" </span></b></font><br><br></center>" +
				"</Span>" +
				"<Span id = 'myObj' name = 'myObj'></Span>" +
				"</DIV>" +
				"<!-- LoadingDiv -->" );

function DetectInstallRun(URL, Type, Lang)//Type = "Fly" || "WebPage" || "FullScreen", Lang = LanguageCode
{
	switch (Lang) {
		case 'pt' :
			InstallDivText=InstallDivText_pt;
			MsgTerraExplorerNotInstalled=MsgTerraExplorerNotInstalled_pt;
			MsgAlertUnsuccessfulInstall=MsgAlertUnsuccessfulInstall_pt;
			MsgUnsuccessfulInstall=MsgUnsuccessfulInstall_pt;
			MsgCannotInstall=MsgCannotInstall_pt;
			break;
		case 'en' :
			InstallDivText=InstallDivText_en;
			MsgTerraExplorerNotInstalled=MsgTerraExplorerNotInstalled_en;
			MsgAlertUnsuccessfulInstall=MsgAlertUnsuccessfulInstall_en;
			MsgUnsuccessfulInstall=MsgUnsuccessfulInstall_en;
			MsgCannotInstall=MsgCannotInstall_en;
			break;
		case 'fr' :
			InstallDivText=InstallDivText_fr;
			MsgTerraExplorerNotInstalled=MsgTerraExplorerNotInstalled_fr;
			MsgAlertUnsuccessfulInstall=MsgAlertUnsuccessfulInstall_fr;
			MsgUnsuccessfulInstall=MsgUnsuccessfulInstall_fr;
			MsgCannotInstall=MsgCannotInstall_fr;
			break;
		case 'es' :
			InstallDivText=InstallDivText_es;
			MsgTerraExplorerNotInstalled=MsgTerraExplorerNotInstalled_es;
			MsgAlertUnsuccessfulInstall=MsgAlertUnsuccessfulInstall_es;
			MsgUnsuccessfulInstall=MsgUnsuccessfulInstall_es;
			MsgCannotInstall=MsgCannotInstall_es;
			break;
		case 'zh' :
			InstallDivText=InstallDivText_zh;
			MsgTerraExplorerNotInstalled=MsgTerraExplorerNotInstalled_zh;
			MsgAlertUnsuccessfulInstall=MsgAlertUnsuccessfulInstall_zh;
			MsgUnsuccessfulInstall=MsgUnsuccessfulInstall_zh;
			MsgCannotInstall=MsgCannotInstall_zh;
			break;
	}
	
	var ret = DetectTE();
	var InnerHTMLText = "";
	var StatusText = "";
	OpenFileURL = URL;
	OpenFileType = Type;
	CurrentUserLanguage = Lang;
	
	if (ret)	
	{
		LoadingDiv.style.visibility = 'visible';
		LoadingDiv.style.left = (window.document.body.clientWidth/2-InstallDivWidth/2);
		LoadingDiv.style.top = (window.document.body.clientHeight/2-InstallDivHeight/2);

		InnerHTMLText = "<OBJECT ID='TE' CLASSID='CLSID:3a4f9191-65a8-11d5-85c1-0001023952c1' " + 
			"onreadystatechange = 'onTEInstalled()' " +
			"onerror = 'InstallOK = false' "+ 
			"style = 'visibility:hidden' "+
			" codebase='" + InstallURL + "#Version=" + InstallVersion +"'>" +
			"</OBJECT>";			
/*
			"<PARAM NAME='UpdateURL' VALUE=" + UpdateURL + ">"	+
			"<PARAM NAME='StartPageURL' VALUE=" + StartPageURL+ ">"	+
			"<PARAM NAME='QuickGuideURL' VALUE=" + QuickGuideURL+ ">"	+
			"<PARAM NAME='FlightInstructionsURL' VALUE=" + FlightInstructionsURL+ ">"	+
*/

			//StatusText = "<br><br><center> Downloading... </center>";
			//LoadingDiv.all ("DivText").innerHTML = StatusText;

			document.all("InstallDivText").innerHTML = InstallDivText;
			LoadingDiv.all ("myObj").innerHTML = InnerHTMLText;
	}
}

function onTEInstalled()
{
	LoadingDiv.style.visibility = 'hidden';
	if (LoadingDiv.readyState == 'complete')  
	{
		if (InstallOK) 
		{
			if (OpenFileType == 'Fly')
			{
			    var TE2 = new ActiveXObject("TerraExplorer2.TerraExplorer");
			    TE2.Load (OpenFileURL);
			}
			else if (OpenFileType == 'WebPage')
			{
				window.parent.location.href =  OpenFileURL;
			}
			else if (OpenFileType == 'FullScreen')
			{
				window.open(OpenFileURL,'','fullscreen');
			}
			else 
				alert ('Error');
		}
		else
		{
			str = MsgCannotInstall
			if (window.confirm(str) == true) 
			{
				//document.location.href = ManualDownloadURL; // change to this when redirecting to a FAQ page.
				document.location.href = ManualDownloadFilePath;
			}						
		}
	}	
}

/*DetectTE - Checks if TerraExplorer is already installed on the client computer.*/
function DetectTE()
{ 
	try
	{
		// Check if TE is installed
		TeInstalled = new ActiveXObject("TEDetect.TETest"); 

		if (TeInstalled != null)
		{
			TeInstalled = null;	// Make sure that the "TEDetect.TETest" ActiveX will not stay alive (otherwise it will interrupt the installation of the new version)

			//Make sure we can run TerraExplorer
			TerraExplorer = new ActiveXObject("TerraExplorer2.TerraExplorer");

			if (TerraExplorer != null)
			{
				TerraExplorer = null;
				return true;
			}
			else
			{
				//It is abnormal to get here. Prompt for manual install 
				alert(MsgAlertUnsuccessfulInstall);

				if ( window.confirm(MsgUnsuccessfulInstall) == true)
				{
					//document.location.href = ManualDownloadUrl; // change to this when redirecting to a FAQ page.
					document.location.href = ManualDownloadFilePath;
				}
			}

		}
		else
		{
			throw -1;	// TE is not installed.
		}

		TerraExplorer = null;
	}
	catch(e)
	{	
		// Try install TE
		ret = window.confirm(MsgTerraExplorerNotInstalled);
		// if ret = OK change cursor
		return ret
	}

	return false;
}