var gb_forceInstaller = false;//true;
var gb_shownBad = false, gb_badPPC, gb_badEnviro, gb_safari64;

var gi_maxAutoInstallIE = 7;
var gb_postInstallNeedsRefresh = false;
var gb_isCheckerPage = false;

function badVersionsCheck() { //return true if bad
	var os = BrowserDetect.OS, ffv;
	if (os == "Windows") {
		if (ffv = getFirefoxVersion()) { //ieq

		}
	} else if (os == "Mac") {
		if (!BrowserDetect.Mac.b_intel) {
			gb_badEnviro = gb_badPPC = true;
			bCanOpen();
			return true;
		} else if (gb_safari) {
			if (BrowserDetect.Mac.vers[1] >= 6) {
				gb_safari64 = true;
				var lyr = objFromID("getPlyrDiv"), td = objFromID("getPlyrTD");

				lyr.style.visibility = "visible";
				td.innerHTML = "Virtual Gallerie currently has an issue with running in Safari running in 64-bit mode on OS X versions 10.6 or later. If your version of Safari is running as a 64-bit application (the default), you will need to quit out and restart it as a 32-bit application. Or you can run on the Mac in Firefox or Chrome.";

				return false;
			}
		}
	}
	return false;
}
function canIEDoAutoInstall() {
	return Pulse.Player.browserSupported && gb_IE && (BrowserDetect.version <= gi_maxAutoInstallIE) && !gb_vista;	
}

function bCanOpen(b_alert_if_unsupported, b_show_installer_even_if_auto_good) {
	var bad_vers = "";
	var b_updatePP = PP_doesWinFFNeedUpdate();

	

	if (gb_badEnviro && gb_shownBad) {
		if(b_alert_if_unsupported) showAlert(getLocalUIString_LI("unsupportedLaunch"));
		return false;
	}

	//if (!pulseInstallCheck(BrowserDetect.browser, BrowserDetect.OS) || 1) {

	if (!b_updatePP && (Pulse.Player.isInstalled || (!b_show_installer_even_if_auto_good && canIEDoAutoInstall()))) return true;

	gb_shownBad = true;

	var br = BrowserDetect.browser, os = BrowserDetect.OS;

	var s = "<center><strong>Pulse Player needed</strong></center><br>Virtual Gallerie products require the Pulse Player plugin in order to run. "

	if (gb_badPPC) {
		s += "<br><br>Unfortunately, Virtual Gallerie only works on Macs with Intel-based chips, or on Windows."
				+ "<br><br> To use Virtual Gallerie on Macs with Intel-based chips, you can use Safari, Chrome, or any version of Firefox. Or on Windows, please use either Firefox version 3.5.x or below, or Internet Explorer";
			if (b_alert_if_unsupported) showAlert(getLocalUIString_LI("unsupportedLaunch"));

	} else if (!Pulse.Player.browserSupported ||(gb_safari && !gb_mac) || (!gb_mac && gb_chrome && !gb_vista)) {
		s += "<br><br>Unfortunately, the Pulse Player is not supported ";

		if (os == "Windows") {
			s += "in " + br + " on Windows. It is currently supported in Windows in Firefox, Chrome (on Vista and above), and Internet Explorer. It is also supported on Macs with Intel-based chips in Firefox, Chrome, and Safari.";
		} else if (os == "Mac") {
			s += "in " + br + " on the Mac. It is currently supported in Firefox, Chrome, and Safari, on Macs with Intel-based chips, or on Windows in Firefox versions 3.5.x and below or Internet Explorer."
		} else {
			s += "on the " + os + " operating system. It is currently supported on Windows in Internet Explorer or Firefox, and on the Mac in Firefox, Chrome, and Safari.";
		}

		s += "<br><br>Please try the Virtual Galleries on one of the abovementioned configurations.";
		if (b_alert_if_unsupported) showAlert(getLocalUIString_LI("unsupportedLaunch"));
	} else {
		if (b_updatePP) s += "It appears that your version is out of date.";
		else s += "It appears that you do not currently have it.";
		
		s += " Please click on the \"Get Pulse Player\" button below to ";

		if (br == "Firefox") {
			s += "start the installer. You will need to restart Firefox as part of the install, but Firefox should automatically prompt you to do so.(After clicking the button, Firefox may present you with a bar at the top of the screen preventing you from installing. If so, please click the 'Allow' (or 'Edit Options', depending on your version of Firefox) button on the right side of the bar to continue).";
		} else if (gb_vista || ((br == "Chrome") && (os == "Windows"))) {
			switch(br) {
			default:
				s += "download and start the installer. You will need to restart your browser after running the installer.  You will need to return to this page to start the demos.";
				break;
			case "Chrome":
			case "Explorer":
				s += "download and start the installer. When you are done running the installer, please refresh (F5) this page and proceed as before.";
				break;
			}
		} else if (br == "Safari") {
 			s += "download and start the installer. You will need to restart Safari after running the installer.  You will need to return to this page to start the demos. After dowloading, the file should automatically decompress and the installer should start. If it doesn't, you will need to open the downloaded \"PulsePlayer5Mac.mpkg.zip\" file, and then open and run the \"PulsePlayer5Mac.mpkg\" file.";
		} else if (br == "Chrome") {
			s += "download the installer.<br><br>After dowloading (a link should appear at the bottom left corner of this window when done), you will need to unzip the downloaded \"PulsePlayer5Mac.mpkg.zip\" file, and then open and run the \"PulsePlayer5Mac.mpkg\" file that it contains.  You will need to restart Chrome after running the installer, and return to this page to start VG.";
 		} else { //it's a non-firefox version of GECKO/Mozilla

 		}

 		s += '<br><br><center><input type="button" class="button-standard" id=getmacpl value="Get Pulse Player" href="#" onclick="getAutoPPlayer();return false;"></center>';

		s += "<br>You'll need administrator access on your machine to install the Pulse Player plugin.";
		
		if (!gb_weveGotWeb) {
			s += "<br><br>You will need an active Internet connection to download the Pulse Player plugin.";
		}
	}

		//check to see if this platform is supported.... and if yes, then do the install, else....
		/* possibilities:
			1) windows
				a) IE: will automatically install when running the content
				b) FF: do it here, and will automatically reopen and return here
			2) mac
				a) Safari: download the installer, run it (or, if not automatic, unzip and run (hmmmm.... do some people not have unzip?
				b) FF: do it here, and will automatically reopen and return here.

		*/
	var lyr = objFromID("getPlyrDiv"), td = objFromID("getPlyrTD");


	lyr.style.visibility = "visible";
	td.innerHTML = s;
	return false;

}


function getAutoPPlayer() {
	if ((BrowserDetect.browser == "Explorer") && !canIEDoAutoInstall()) {
		var b_explorer = (BrowserDetect.browser == "Explorer");
		addPostInstallClickInfo(b_explorer ? "Internet Explorer" : BrowserDetect.browser, b_explorer);
		window.location = gs_rootPathForPluginspage + "/english/5.2/win/PulsePlayer5.2Win.exe";
	} else if (gb_safari || gb_chrome) {
		addPostInstallClickInfo(BrowserDetect.browser, gb_chrome && !gb_mac);
		if (gb_mac) {
			window.location = gs_rootPathForPluginspage + "/english/5.2/macx/PulsePlayer5Mac.mpkg.zip";
		} else {
			window.location = gs_rootPathForPluginspage + "/english/5.2/win/PulsePlayer5.2ChromeWin.crx";
		}
	} else {
		Pulse.Player.Install.startInstall();
    }
	return true;
}

function addPostInstallClickInfo(brz, b_no_restart) {
    var msg = !b_no_restart
    	? "When the installer has finished running, please restart " + brz + ", and return to this page. You may just want to copy the address from the address field before you quit, so you can paste it back in after restarting " + brz + ". Thanks!"
    	: "When the installer has finished running, please refresh this page (F5)" + (!gb_isCheckerPage ? " and proceed as before." : " and it should tell you the player has installed.") + " If it still asks you to get the Player after installing and refreshing, you may need to restart " + brz + ". Thanks!";

    td = objFromID("getPlyrTD");
    td.innerHTML = msg;

    gb_postInstallNeedsRefresh = b_no_restart;
}



function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
function getLocalUIString_LI(id) {
	switch(id) {
	case "unsupportedLaunch":
		return "We are sorry but you can not lunch Virtual Gallerie with your current Browser/OS configuration. Please see the note in the red-outlined box.";
		break;
	default:
		return id;
		break;
	}
}
