var TerraExplorer;
var CurrentVersion = "4.6.1";
var InstallOK = true;
var OpenFileType = "";
var OpenFileURL = "";

//TODO: Set the path of these variables to your web site

var MyPath;
MyPath = "http://www.3dfunchal.com/resources/te";
//MyPath = "http://www.skylinesoft.com/interactive/detect";

//InstallURL - Determines the path for TerraExplorer install
var InstallURL;
InstallURL = "http://www.3dfunchal.com/resources/te/TE.cab";
//InstallURL = "http://www.skylinesoft.com/interactive/terraexplorer/install/TE.cab";
//InstallURL = "http://www.skylinesoft.com/interactive/InstallSample/TE.cab";

//InstallVersion - Determines the minimum version for TerraExplorer install
//Format: "X,X,X,X"
var InstallVersion;
InstallVersion = "4,6,1,0";

//UpdateURL - Determines the default path for TerraExplorer updates
var UpdateURL;
UpdateURL = "http://www.3dfunchal.com/resources/te/basic.cab";
//UpdateURL = "http://www.skylinesoft.com/interactive/terraexplorer/install/basic.cab";

//StartPageURL - Determines the default path for the start page displayed from Help Menu -> Terraexplorer Home
var StartPageURL;
StartPageURL = "http://www.skylinesoft.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.skylinesoft.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.skylinesoft.com/Interactive/TerraExplorer/texp_flight-instructions.htm";

//ManualDownloadURL - Determines the default path for TerraExplorer manual download
var ManualDownloadURL;
ManualDownloadURL = "http://www.3dfunchal.com/faq/instalacao.htm";

//Inject a div for loading TerraExplorer 
document.write( "<!-- LoadingDiv -->" +
				"<DIV id='LoadingDiv' name='LoadingDiv' " +
				"style='position:absolute;" +
				"width:340;" +
				"height:220px;" +
				"z-index:99;" +
				"left: 110px;" +
				"top: 100px;" +
				"background-color: #ffffff;" +
				"layer-background-color: #ffffff;" +
				"border: 10px solid #808080;" +
				"visibility: hidden'>" +
				"<Span id = 'DivText' name = 'DivText'> "+
				"<br><br><br><br><br><center><span> Por favor aguarde uns instantes<br>enquanto o <b>Visualizador 3D</b> &eacute; transferido. </span><br><br><span>&nbsp;&nbsp;&nbsp;&nbsp; Esta opera&ccedil;&atilde;o poder&aacute; levar alguns minutos. &nbsp;&nbsp;&nbsp;&nbsp;</span><br><br><br><img src='"+MyPath+"/transferir.gif' border=0></center>" +
				"</Span>" +
				"<Span id='myObj' name='myObj'></Span>" +
				"</DIV>" +
				"<!-- LoadingDiv -->" );

function DetectInstallRun (URL, Type)//Type = "Fly" or "WebPage"
{
	var ret = DetectTE();
	var InnerHTMLText = "";
	var StatusText = "";
	OpenFileURL = URL;
	OpenFileType = Type;
	
	if (ret)	
	{
		LoadingDiv.style.visibility = 'visible';

		InnerHTMLText = "<OBJECT ID='TE' CLASSID='CLSID:3a4f9191-65a8-11d5-85c1-0001023952c1' " + 
			"onreadystatechange = 'onTEInstalled()' " +
			"onerror = 'InstallOK = false' "+ 
			"style = 'visibility:hidden' "+
			" codebase='" + InstallURL + "#Version=" + InstallVersion +"'>" +
			"<PARAM NAME='UpdateURL' VALUE=" + UpdateURL + ">"	+
			"<PARAM NAME='StartPageURL' VALUE=" + StartPageURL + ">"	+
			"<PARAM NAME='QuickGuideURL' VALUE=" + QuickGuideURL + ">"	+
			"<PARAM NAME='FlightInstructionsURL' VALUE=" + FlightInstructionsURL + ">"	+
			"</OBJECT>";

			//StatusText = "<br><br><center> Downloading... </center>";
			//LoadingDiv.all ("DivText").innerHTML = StatusText;
			LoadingDiv.all ("myObj").innerHTML = InnerHTMLText;
	}
	else window.close();
}
/*
function DetectInstallRunLanguage (URL, Type, Language)//Type = "Fly" or "WebPage"
{
	var ret = DetectTE();
	var InnerHTMLText = "";
	var StatusText = "";
	OpenFileURL = URL;
	OpenFileType = Type;

	if (Language == "French")
	 {	
		InstallURL = "http://www.skylinesoft.com/interactive/terraexplorer/French/install/TE.cab";
		ManualDownloadURL = "http://www.skylinesoft.com/interactive/terraexplorer/texp_manual-download.asp?Lang=French";
		UpdateURL =  "http://www.skylinesoft.com/interactive/terraexplorer/French/install/basic.cab";
		QuickGuideURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Fr_QuickGuide_300";
		FlightInstructionsURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Fr_FlightInstructions_300";
		StartPageURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Fr_texp_startpage";
	 }
	if (Language == "German")
	 {	
		InstallURL = "http://www.skylinesoft.com/interactive/terraexplorer/German/install/TE.cab";
		ManualDownloadURL = "http://www.skylinesoft.com/interactive/terraexplorer/texp_manual-download.asp?Lang=German";
		UpdateURL =  "http://www.skylinesoft.com/interactive/terraexplorer/German/install/basic.cab";
		QuickGuideURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Gr_QuickGuide_300";
		FlightInstructionsURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Gr_FlightInstructions_300";
		StartPageURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Gr_texp_startpage";
	 }
	if (Language == "Spanish")
	 {	
		InstallURL = "http://www.skylinesoft.com/interactive/terraexplorer/Spanish/install/TE.cab";
		ManualDownloadURL = "http://www.skylinesoft.com/interactive/terraexplorer/texp_manual-download.asp?Lang=Spanish";
		UpdateURL =  "http://www.skylinesoft.com/interactive/terraexplorer/Spanish/install/basic.cab";
		QuickGuideURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Sp_QuickGuide_300";
		FlightInstructionsURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Sp_FlightInstructions_300";
		StartPageURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Sp_texp_startpage";
	 }

	if (ret)	
	{
		LoadingDiv.style.visibility = 'visible';

		InnerHTMLText = "<OBJECT ID='TE' CLASSID='CLSID:3a4f9191-65a8-11d5-85c1-0001023952c1' " + 
			"onreadystatechange = 'onTEInstalled()' " +
			"onerror = 'InstallOK = false' "+ 
			"style = 'visibility:hidden' "+
			" codebase='" + InstallURL + "#Version=" + InstallVersion +"'>" +
			"<PARAM NAME='UpdateURL' VALUE=" + UpdateURL + ">"	+
			"<PARAM NAME='StartPageURL' VALUE=" + StartPageURL+ ">"	+
			"<PARAM NAME='QuickGuideURL' VALUE=" + QuickGuideURL+ ">"	+
			"<PARAM NAME='FlightInstructionsURL' VALUE=" + FlightInstructionsURL+ ">"	+
			"</OBJECT>";

			//StatusText = "<br><br><center> Downloading... </center>";
			//LoadingDiv.all ("DivText").innerHTML = StatusText;
			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.resizeTo('820','550')
				window.parent.location.href =  OpenFileURL;
			}
			else 
				alert ('Error');
		}
		else
		{
			str = "Atenção: Não foi possível executar a instalação automática.\nSe estiver a usar o Microsoft Windows XP com o Service Pack 2 siga estas instruções:\n1. Prima o botão \"Cancelar\". \n2. Prima no aviso de segurança que aparece no topo desta janela\n3. Escolha a opção que permita instalar o objecto ActiveX \n4. Prima em \"Entrar\" novamente.\n\nPara instalar manualmente o Visualizador 3D prima em \"OK\". Para cancelar prima em \"Cancel\"."
			//str = "Error: Cannot run automated installation.\nIf you are using Microsoft Windows XP Service Pack 2 follow these instructions:\n1. Click on the Cancel button. \n2. Click on the security warning that appears at the top of this window\n3. Select the 'Allow this page to install ActiveX controls' option\n4. Click on the demo click again.\n\nTo Manually install TerraExplorer click OK or Cancel to abort."
			if (window.confirm(str) == true) 
			{
				document.location.href = ManualDownloadURL;
			}						
		}
	}	
}

/*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("Atenção: Não foi possível executar o Visualizador 3D.");

				//if ( window.confirm("Error: Can not run automated installation.\nTo Manually install TerraExplorer click OK or Cancel to abort") == true)
				if ( window.confirm("Atenção: Não foi possível executar a instalação automática.\nPara instalar manualmente o Visualizador 3D prima em \"OK\". Para cancelar prima em \"Cancel\".") == true)
				{
					document.location.href = ManualDownloadURL;
				}
			}
		}
		else
		{
			throw -1;	// TE is not installed.
		}

		TerraExplorer = null;
	}
	catch(e)
	{	
		// Try install TE
		ret = window.confirm ("Para entrar na Cidade 3D precisa de instalar o Visualizador 3D.\n Prima em \"OK\" para iniciar a instalação automática.\n\n Atenção: Esta operação poderá levar alguns minutos...");
		//ret = window.confirm ("TerraExplorer Viewer by Skyline Software Systems is not installed.\n Click OK to automatically install TerraExplorer.\n\n Note: This may take a few minutes...");
		// if ret = OK change cursor
		return ret
	}
	return false;
}