function onlinePendingChanges() {
	return "WARNING: some of your changes have not been sent to server and will be lost!";
}

function offlineNotAutosaved() {
	return "WARNING: some of your changes have not been sent to the server.\n" + 
				 "To save a local copy on this computer, please click cancel to stay on the current page and click on \"save\" to save the map on this computer.\n" +
				 "We don't recommend saving maps on a public computer. To modify your autosave settings or delete autosaved maps, please click on \"Open autosaves\" next time you log in.";
}

function offlineAutosaved() {
	connect.ProxiedConnectionServer.instance.doAutosave();
	return "WARNING: some of your changes have not been sent to the server.\n" +
				 "They have been stored on this computer. Next time you log in, you can open the map by clicking \"Open autosaves\" and then click \"Save as\" to save them on the server.\n" +
				 "We don't recommend saving maps on a public computer. To modify your autosave settings or delete autosaved maps, please click on \"Open autosaves\" next time you log in."; 
}

var trial = false;

function bohr_DoFSCommand(command, args) {
	if ("setBeforeUnload" == command) {
		if ("noPendingChanges" == args) {
			setBeforeUnload(null);
		} else if ("onlinePendingChanges" == args) {
			setBeforeUnload(onlinePendingChanges);
		} else if ("offlineNotAutosaved" == args) {
			setBeforeUnload(offlineNotAutosaved);
		} else if ("offlineAutosaved" == args) {
			setBeforeUnload(offlineAutosaved);		
		}
	} else if ("updateWindowTitle" == command) {
		updateWindowTitle(args);
	} else if ("setTrialMode" == command) {
		setTrialMode();
	} else if ("closeWindow" == command) {
		closeWindow();
	}
}

function setBeforeUnload(handler) {
	window.onbeforeunload = handler;
}
function updateWindowTitle(title) {
	document.title = title;
}
function setTrialMode() {
	trial = true;
}
function closeWindow() {
	if (BrowserDetect.browser == "Explorer") {
			this.focus();
			self.opener = this;
			self.close(); 
	} else if (BrowserDetect.browser == "Firefox") {
			setTimeout(function () { 
				window.open("","_parent","");
				window.close(); 
			}, 50);
	} else {
			window.close(); 				
	}
}

function doOnUnloadActions() {
	if (typeof(connect.ProxiedConnectionServer.instance.doLogout) == 'function') {
		connect.ProxiedConnectionServer.instance.doLogout();
	}
    if (typeof(connect.ProxiedConnectionServer.instance.notifyAboutUnload) == 'function') {
		connect.ProxiedConnectionServer.instance.notifyAboutUnload();
	}
	if (trial && typeof(connect.ProxiedConnectionServer.instance.doRecordTrialStatistics) == 'function') {
		connect.ProxiedConnectionServer.instance.doRecordTrialStatistics();
	}
}

function addAlternativeContent() {
	var version = swfobject.getFlashPlayerVersion();
	var versionText = "You do not have the Flash plugin installed.";
	if (version["major"] > 0) {
		versionText = "You have Flash player "+ version['major'] +"."+ version['minor'] +"."+ version['rev'] +" installed.";
	}

	var text = "<table width='50%' align='center'><tr><td>";
	text += "<h1>You need to upgrade your Flash Player</h1>";
	text += "<p>" + versionText + "</p>";	
	text += "<p>You need to have Adobe Flash Player version 7 or later installed in order to use " + document.title + ".</p>";	
	text += "<p><a href='http://www.adobe.com/go/getflashplayer'>Click here</a> to download and install the latest Adobe Flash Player.</p>";
	text += "<p>If your IT department has a policy against installing software on your machine, you can try to convince them to upgrade the Flash player, because old versions <a href='http://www.adobe.com/support/security/bulletins/apsb06-11.html'>have known security issues</a>.</p>";
	text += "<p>If you are not allowed to update Adobe Flash Player in Internet Explorer, you can try to install the excellent <a href='http://www.getfirefox.com'>Firefox</a> browser instead.</p>";
	text += "</td></tr></table>";
	document.getElementById("flashcontent").innerHTML = text;
}

function embeddBohrSwf(loader) {
	var playerMinVersion = "6.0.0";
	// for the backwards compatilibity
	var map = swfobject.getQueryParamValue("mmap");
	if (map == "") {
		map = swfobject.getQueryParamValue("map");
	}
	
	var flashvars = {
						amf: swfobject.getQueryParamValue("amf"),
						cmd: swfobject.getQueryParamValue("cmd"),
						browser: BrowserDetect.browser,
						browserVersion: BrowserDetect.version,
						clientOS: BrowserDetect.OS,
						notifierID: swfobject.getQueryParamValue("clientID"),
						email: swfobject.getQueryParamValue("email"),
						password: swfobject.getQueryParamValue("password"),
						mmap: map,
						owner: swfobject.getQueryParamValue("owner"),
						location: document.location.protocol + "//" + document.location.host + document.location.pathname,
						refresh: swfobject.getQueryParamValue("refresh"),
						key: swfobject.getQueryParamValue("key"),
						trial: swfobject.getQueryParamValue("trial"),
						mapid: swfobject.getQueryParamValue("mapid"),
						publishKey: swfobject.getQueryParamValue("publishKey"),
						editKey: swfobject.getQueryParamValue("editKey"),
						viewKey: swfobject.getQueryParamValue("viewKey"),
						focus: swfobject.getQueryParamValue("focus"),
						flash9: swfobject.getQueryParamValue("flash9"),
                        date: swfobject.getQueryParamValue("date"),
						isVista: BrowserDetect.isVista,
						showExit: (null != window.opener),
						MMredirectURL: window.location,
						MMplayerType: BrowserDetect.browser == "Explorer" && BrowserDetect.OS == "Windows" ? "ActiveX" : "PlugIn",
						MMdoctitle: document.title
					};
	var params = {
						scale: "noscale",
						menu: true,
						allowScriptAccess: "always",
						SeamlessTabbing: false,
						allowFullScreen: true
					};
	var attributes = {
						id: "bohr",
						name: "bohr",
						xiRedirectUrl: window.location.href
					};

	swfobject.embedSWF(loader, "flashcontent", "100%", "100%", playerMinVersion, null, flashvars, params, attributes);

	if (swfobject.hasFlashPlayerVersion(playerMinVersion)) {
		swfobject.addDomLoadEvent(setFocusOnFlash); 
	} 
	
	window.onunload = doOnUnloadActions;
	setBeforeUnload(null);
}

function setFocusOnFlash() { 
	var bohrSwf = document.getElementById("bohr"); 
	if (bohrSwf) { 
		bohrSwf.focus(); 
	} 
}