var AUTO_LAUNCH_COOKIE_NAME = "autoLaunch";
var MAINTENANCE = false;

var popup;

function body_onLoad(){

	//need to set the divs that hold the content for our bottom section
	//to the same size
	var toolsDescription = document.getElementById("toolsDescription");
	var toolDescription = document.getElementById("toolDescription");
	var whatsNew = document.getElementById("whatsNew");
	
	var toolsDescriptionContent = document.getElementById("toolsDescriptionContent");
	var toolDescriptionContent = document.getElementById("toolDescriptionContent");
	var whatsNewContent = document.getElementById("whatsNewContent");
	
	if (toolsDescriptionContent != null && toolDescriptionContent != null && whatsNewContent != null ) {
	
		var toolsDescriptionHeight = toolsDescription.clientHeight;
		var toolDescriptionHeight = toolDescription.clientHeight;
		var whatsNewHeight = whatsNew.clientHeight;
		var maxHeight = 0;
		var maxHeightPx;
		
		if (toolsDescriptionHeight > toolDescriptionHeight && toolsDescriptionHeight > whatsNewHeight) {
			maxHeight = toolsDescriptionHeight;
		} else if (toolDescriptionHeight > toolsDescriptionHeight && toolDescriptionHeight > whatsNewHeight) {
			maxHeight = toolDescriptionHeight;
		}
		else {
			maxHeight = whatsNewHeight;
		}
		
		maxHeightPx = maxHeight.toString() + 'px';
		toolsDescription.style.height = maxHeightPx;
		toolDescription.style.height = maxHeightPx;
		whatsNew.style.height = maxHeightPx;
		
		
		maxHeightPx = (maxHeight - 20).toString() + 'px';
		toolsDescriptionContent.style.height = maxHeightPx;
		toolDescriptionContent.style.height = maxHeightPx;
		whatsNewContent.style.height = maxHeightPx;
	}
	
	var maintenanceContent = document.getElementById("maintenanceContent");
	var launchContent = document.getElementById("launchContent");
	var autoLaunchCheck = document.getElementById("chkAutoLaunch");
	
	if (MAINTENANCE && maintenanceContent != null && launchContent != null) {
		maintenanceContent.style.display = "block";
		launchContent.style.display = "none";
	} else {
		var validPlayer = checkFlashPlayer();

		if (validPlayer) {
			var autoLaunch = getCookie(AUTO_LAUNCH_COOKIE_NAME, false);

			if(autoLaunchCheck != null){
				autoLaunchCheck.checked = autoLaunch;
			}

			if (autoLaunch) {
				launch();
			}
		}
	}
}

function btnLaunch_click() {
	launch();
}

function btnRegister_click() {
	window.location = '/pearls_registration';
}

function depressLaunchButton(){
	document.getElementById("btnLaunch").src = 'assets/launch_button_depressed2.gif';
}

function restoreLaunchButton(){
	document.getElementById("btnLaunch").src = 'assets/launch_button2.gif';
}

function checkFlashPlayer() {
	// Major version of Flash required
	var requiredMajorVersion = 10;
	// Minor version of Flash required
	var requiredMinorVersion = 1;
	// Minor version of Flash required
	var requiredRevision = 0;

	// Version check for the Flash Player that has the ability to start Player Product Install (6.0r65)
	var hasProductInstall = DetectFlashVer(6, 0, 65);

	// Version check based upon the values defined in globals
	var hasRequestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);

	if ( hasProductInstall && !hasRequestedVersion ) {
		if(document.getElementById("altContent") != null){
			document.getElementById("altContent").style.display = "block";
		}

		// DO NOT MODIFY THE FOLLOWING FOUR LINES
		// Location visited after installation is complete if installation is required
		var MMPlayerType = (isIE == true) ? "ActiveX" : "PlugIn";
		var MMredirectURL = window.location;
		document.title = document.title.slice(0, 47) + " - Flash Player Installation";
		var MMdoctitle = document.title;

		AC_FL_RunContent(
			"src", "v1/playerProductInstall",
			"FlashVars", "MMredirectURL="+MMredirectURL+'&MMplayerType='+MMPlayerType+'&MMdoctitle='+MMdoctitle+"",
			"align", "middle",
			"id", "altContent",
			"quality", "high",
			"bgcolor", "#869ca7",
			"name", "altContent",
			"allowScriptAccess","sameDomain",
			"type", "application/x-shockwave-flash",
			"pluginspage", "http://www.adobe.com/go/getflashplayer"
		);

	} else if (hasRequestedVersion) {
		if(document.getElementById("launchControls") != null){
			document.getElementById("launchControls").style.display = "block";
		}

	} else {  // flash is too old or we can't detect the plugin
		if(document.getElementById("altContent") != null){
			var alternateContent = 'This content requires the Adobe Flash Player. '
			+ '<a href=http://www.adobe.com/go/getflash/>Get Flash</a>';

			document.getElementById("altContent").innerHTML = "<b>" + alternateContent + "</b>";
			document.getElementById("altContent").style.display = "block";
		}
	}

	return hasProductInstall && hasRequestedVersion;
	return true;
}

function chkAutoLaunch_click() {
	var autoLaunch = document.getElementById("chkAutoLaunch").checked;
	
	if (autoLaunch) {
		launch();
	}

	setCookie(AUTO_LAUNCH_COOKIE_NAME, (autoLaunch ? "true" : ""));
}

function createPopup(){
	popup = window.open("/v1/WoccuPlatform.php", 'winPearls', 'location=0, width=1024, height=768, resizable=1');

	if (popup == null) {
		alert("The tools window has been blocked by your popup blocker.  Please modify your settings to allow popups from pearlsweb.woccu.org and click \"Launch the application.\"")
	}
}

function getCookie(name, defaultValue) {
	var value = defaultValue;

	if (document.cookie.length > 0) {
		var startIndex = document.cookie.indexOf(name + "=");

		if (startIndex != -1) {
			startIndex = startIndex + name.length + 1;

			endIndex = document.cookie.indexOf(";", startIndex);

			if (endIndex == -1) {
				endIndex = document.cookie.length;
			}

			value = unescape(document.cookie.substring(startIndex,endIndex));
		}
	}

	return value;
}

function launch(){
	if (popup != null) {
		if (popup.closed == true) {
			popup = null;

			createPopup();
		} else {
			popup.focus();
		}
	} else {
		createPopup();
	}
}

function setCookie(name, value) {
	document.cookie = name + "=" + escape(value) + ";expires=09/05/2037";
}

