var updatePopupReady = false;

function getOS() {
	//for now, sticking to just Mac and Win - later can split out further here
	if(navigator.platform.toLowerCase().indexOf("mac") > -1) {
		return "mac";
	} else {
		return "win";
	}
}
function hideUpdatePopup() {
	$("#update_popup_bg").hide();
	$("#update_popup").hide();
	
	//resetting
	$("#update_progress_popup_panel").hide();
	$("#update_info_popup_panel").show();
}

$(document).ready(function() {
	if(!updatePopupReady) {
		updatePopupReady = true;
		
		//os-based screenshot
		var os = getOS();
		if(os == "mac") {
			$("#download_instructions .launchScreeshot").addClass("osx");
			$("#download_instructions .link").html("Using Windows?");
		}
		$("#download_instructions .link").click(function() {
			if($("#download_instructions .launchScreeshot").hasClass("osx")) {
				$("#download_instructions .launchScreeshot").removeClass("osx");
				$("#download_instructions .link").html("Using Mac OS X?");
			} else {
				$("#download_instructions .launchScreeshot").addClass("osx");
				$("#download_instructions .link").html("Using Windows?");
			}
		});
		
		//buttons
		$("#start_update_popup").roundedButton({ textPadding: "6px 7px 0 22px" });
		$("#cancel_popup").roundedButton();
		
		//button functionality
		$(".popup_close_button").click(function() {
			hideUpdatePopup();
		});
		$("#cancel_popup").click(function() {
			hideUpdatePopup();
		});
		$("#start_update_popup").click(function() {
			if(!$(this).hasClass("disabled")) {
				$("#update_info_popup_panel").hide();
				$("#update_progress_popup_panel").show(function () {
					document.location.href = globalContextPath+"static/downloads/firmware/launch.jnlp";
				});
			}
		});
		
		//show/hide sync button
		$(window).bind("deviceStatus", function(e, data) {
			if($("input[name$='device_id']").val() == null && deviceSerial){
				var thisDeviceSerial = deviceSerial;
			}else{
				var thisDeviceSerial = $("input[name$='device_id']").val();
			}

			if (data.serial != null && data.serial == thisDeviceSerial && data.connected) {
				$("#find_upro_notice").hide();
				$("#start_update_popup").removeClass("disabled");
			} else {
				$("#find_upro_notice").show();
				$("#start_update_popup").addClass("disabled");
			}
		});
		
		//initial popup state
		$("#update_progress_popup_panel").hide();
	}
});
