var PAPERMINT_ACTIVE = false;
var APPLET_STATUS = "";
var SEPARATE_WINDOW = false;
var ERROR = false;
var PAPERMINT_LAUNCHED = false;

function resetApplet(){
	PAPERMINT_ACTIVE = false;
	APPLET_STATUS = "";
	SEPARATE_WINDOW = false;
	ERROR = false;
	PAPERMINT_LAUNCHED = false;
}

var ISFULLSCREEN = false;
var org_position = new Array();
function closeFullscreen(){
	for ( divid in org_position ){
		var n = document.getElementById(divid);			
		if ( n != null ){
			n.style.top = org_position[divid]["TOP"];
			n.style.left = org_position[divid]["LEFT"];
			n.style.width = org_position[divid]["WIDTH"];
			n.style.height = org_position[divid]["HEIGHT"];
			
			if ( n.id == "drupal" ){
				n.style.position = "absolute";
			}else if ( n.id == "applet" ){
				n.style.position = "absolute";
			}
		}
	}
	document.getElementById("fullscreen_bar").style.visibility="hidden";
	ISFULLSCREEN=false;
}

function fullScreen(){	
	if ( ISFULLSCREEN ){
		closeFullscreen();
		ISFULLSCREEN=false;
		return;
	}else{
		ISFULLSCREEN=true;
	}
	
	var viewportwidth;
 	var viewportheight;
	if (typeof window.innerWidth != 'undefined'){
      viewportwidth = window.innerWidth;
      viewportheight = window.innerHeight;
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)      
 } else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0) {
			viewportwidth = document.documentElement.clientWidth;
			viewportheight = document.documentElement.clientHeight;
// older versions of IE       
 }else {
	 viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
	 viewportheight = document.getElementsByTagName('body')[0].clientHeight;
 }
	document.getElementById("applet").style.visibility="visible";
	var par = document.getElementById("applet");
	while ( par.parentNode ){
		if ( par.tagName == "DIV" ){
			if ( par.id.length > 0 ){
				org_position[par.id] = new Array();
				org_position[par.id]["TOP"] = par.style.top;
				org_position[par.id]["LEFT"] =  par.style.left;
				org_position[par.id]["WIDTH"] = par.style.width;
				org_position[par.id]["HEIGHT"]= par.style.height;
				
				
				if ( par.id == "drupal"){
					//par.style.top = 20+"px";
					if ( navigator.appName != "Netscape" ){
						par.style.position = "fixed";
					}
					//par.style.position = "fixed";
					par.style.top = 0+"px";
					par.style.height = '100%';
					par.style.width = '100%';
					par.style.left = 0+"px";					
				}else if ( par.id == "applet" ){
					if ( navigator.appName != "Netscape" ){
						par.style.position = "fixed";
					}
					
					if ( navigator.appName == "Netscape" || navigator.appName == "Chrome" ){
						par.style.top = "-25px";
					}else{
						par.style.top = 20+"px";
					}
					//par.style.height = viewportheight+'px';
					//par.style.width = viewportwidth+'px';		
					par.style.height = (viewportheight-21)+'px';
					par.style.width = (viewportwidth-1)+'px';				
					par.style.left = 0+"px";					
				}else{
					par.style.top = 0+"px";
					par.style.height = '100%';
					par.style.width = '100%';
					par.style.left = 0+"px";	
				}
			}
		}		
		if ( par.parentNode ){
			par = par.parentNode;
		}
	}				
		
	document.applets.papermintclient.width = '100%';
	document.applets.papermintclient.height = '100%';	
	document.getElementById("fullscreen_bar").style.width  = viewportwidth+'px';
	document.getElementById("fullscreen_bar").style.visibility="visible";
}

function onOpenPapermintTab(){
	if ( jreVersion == null ){
		displayInfoPopUp(messages[browserlang]["JAVA_NEEDED"]);
		load( false );
	}else if ( !validJavaVersion ){
		//displayInfoPopUp(messages[browserlang]["WRONG_JAVA_VERSION"]);
		load( false );
	}else{
		hideInfoPopUp();
	}		
	if ( APPLET_STATUS == "FAIL" ){
		restartApplet();
		var imsg = messages[browserlang]["PLEASE_TRUST"];
		if ( !jv1610 ){
			imsg += messages[browserlang]["RESTART_15"];
		}else{
			imsg += messages[browserlang]["RESTART_1610"];
		}
		//displayInfoPopUp( imsg );
		showTrustMessage( true );
	}
	track( "/playPapermint" );
}

function restartApplet(){
	document.getElementById("applet").innerHTML = "";
	startPapermintAgain();
	hideInfoPopUp();	
}

function updatePage( status ){
	alert( status );
}

//is called from the Java Applet
function updatePage( status , estimatedtime){
	APPLET_STATUS = status;
	
	//alert( ERROR + " | " + status );
	
	if ( ERROR ){
		return;
	}
	if ( status == "START_STAGE" ){
		load( true );
	}else if( status == "STORAGE_STAGE" ){
		showTrustMessage( false );
	}else if ( status == "SUCCESS" ){
		onPapermintLoaded();
		
		if ( document.getElementById("username").value ){
			document.applets.papermintclient.onJavaScript( "username", document.getElementById("username").value );
		}
		if ( document.getElementById("password").value ){
			document.applets.papermintclient.onJavaScript( "password", document.getElementById("password").value );
		}
		
	}else if ( status == "ERROR" ){
		displayInfoPopUp( messages[browserlang]["FAIL"] );
		onPapermintError();
		communicateStatus( messages[browserlang]["STATUS_ERROR"] );
	}else if ( status == "NEVERTRUSTASTRANGER" ){
		
		var imsg = messages[browserlang]["PLEASE_TRUST"];
		if ( !jv1610 ){
			imsg += messages[browserlang]["RESTART_15"];
		}else{
			imsg += messages[browserlang]["RESTART_1610"];
		}
		//displayInfoPopUp( imsg );
		//showTrustWarning();
		showTrustMessage( true );
		load( false );
		ERROR = true;
		communicateStatus( messages[browserlang]["STATUS_ERROR"] );
	}else if ( status == "FAIL" ){
		onPapermintError();
		displayInfoPopUp( messages[browserlang]["FAIL"] );
		communicateStatus( messages[browserlang]["STATUS_ERROR"] );
		
	}else if ( status == "RUNNING_TWICE" ){
		displayInfoPopUp( messages[browserlang]["RUNNING_TWICE"] );
		//onPapermintError();
		onPapermintRunningTwice();
	}else if ( status == "WINDOW_OPEN" || status == "DO_SPAWN_STAGE" ){
		SEPARATE_WINDOW = true;
	}	
	
	if ( estimatedtime > 0 ){
		updateLoadingBar( estimatedtime );
	}		
}

function onPapermintRunningTwice(){
	document.images['papermint_loading'].src = imgpath+"/mint_green_light.gif"
	load( false );
	PAPERMINT_ACTIVE = false;
	ERROR = true;
	showRunningTwiceMessage( true );
}

function onPapermintError(){
	document.images['papermint_loading'].src = imgpath+"/mint_green_light.gif"
	load( false );
	PAPERMINT_ACTIVE = false;
	ERROR = true;
	showErrorMessage( true );
}

/**
Browser Applet detection
**/

function displayApplet ( host , java15, limitMem, startWindow, fullInstall ) {
	var div = document.getElementById("applet");
	if ( !validJavaVersion ){
		return;
	}
	
	var browserName=navigator.appName; 
	var browserVer =navigator.appVersion;
	
	var appletcode = "We could not automatically detect your webbrowser please use the native installers instead!";
	
	var javaParam = '';
	if ( java15==true) {
		javaParam = '<PARAM name="java_version" value="1.5*">';
	}
	var windowParam = '';
	if ( startWindow==true ) {
		windowParam=' -W ';
	}
	var memParam = '256m';
	if ( limitMem==true ) {
		memParam = '56m';
	}
	
	var installParam = '';
	if ( fullInstall ){
		installParam = ' -Call';
	}
	
	var downloadurl = host+"webapp/";
	var libPath = downloadurl+'pmc_manager.jar,'+downloadurl+'lib-ext/pmc_common_1_0.jar,'+downloadurl+'lib-ext/ext-libs-start.jar,'+downloadurl+'lib-ext/commons-logging-1.1.1.jar,'+downloadurl+'lib-ext/resources.jar';
	var launchParams = '-slauncher '+windowParam+' -o'+host+' -aavailplugins.xml -F'+faultLogger+' -dPMC -J'+installParam+' -Pserver='+gameserver+' -Pport='+gameserverport+' -Pcharcreateurl='+charcreateurl+' -Pversion='+versionstring;
	if ( browserName == 'Opera') {
		launchParams = launchParams + ' -S';
	}
	
	browserParam = '<PARAM name="pmbrowser" value="'+browserName+'_'+browserVer+'">';	
	
//	if (browserName=="Microsoft Internet Explorer"){ 
			appletcode = 
				"<applet " +
				' name="papermintclient" '+
				' width="670px" height="600px" '+
				' archive="'+libPath+'"' +
				' type="application/x-java-applet" '+
				' code="com.avaloop.pureclient.PureClient" MAYSCRIPT>' +
				'<param name="archive" value="'+libPath+'"/>'+
				'<param name="code" value="com.avaloop.pureclient.PureClient" />'+
				'<PARAM name="java_arguments" value="-Xmx'+memParam+' -Dsun.java2d.noddraw=true -Dsun.awt.noerasebackground=true">'+
				'<PARAM name="separate_jvm" value="true">'+
				'<param name="codebase_lookup" value="false" />'+
				'<PARAM name="classloader_cache" value="false">'+
				javaParam+
				browserParam+
				'<param name="pluginman" value="'+launchParams+'"/>'+
				'<param name="mayscript" value="yes">'+
				'<param name="scriptable" value="true">'+
			'</applet>';

//	} else { 
// 		if ( (browserName=="Netscape") || (browserName=="Opera") ){
//  		appletcode = 
//  			"<object " +
//  			' width="500px" height="500px" '+
//  			' archive="'+libPath+'"' +
//			' type="application/x-java-applet" '+
//			' code="com.avaloop.pureclient.PureClient">' +
//			'<param name="archive" value="'+libPath+'"/>'+
//			'<param name="code" value="com.avaloop.pureclient.PureClient" />'+
//			'<PARAM name="java_arguments" value="-Xmx'+memParam+' -Dsun.java2d.noddraw=true">'+
//			'<PARAM name="separate_jvm" value="true">'+
//			'<param name="codebase_lookup" value="false" />'+
//			'<PARAM name="classloader_cache" value="false">'+
//			javaParam+
//			'<param name="pluginman" value="'+launchParams+'"/>'+
//			'</object>';
// 		}
//	}
	div.innerHTML = appletcode;
}		

/**

Browser information 

**/

function displayInfo() {
	var div = document.getElementById("info");
	window.onerror=null;

	//colors = window.screen.colorDepth;
	//document.form.color.value = Math.pow (2, colors);
	//if (window.screen.fontSmoothingEnabled == true)
	//	document.form.fonts.value = "Yes";
	//else document.form.fonts.value = "No";
	
	var javaEnabled = "No";	
	if (navigator.javaEnabled() == 1) javaEnabled="Yes";

	div.innerHTML =	"BROWSER: " + navigator.appName + "<br>";
	div.innerHTML =	div.innerHTML + "CODENAME: " + navigator.appCodeName+ "<br>";
	div.innerHTML =	div.innerHTML + "PLATFORM: " + navigator.platform+ "<br>";
	div.innerHTML =	div.innerHTML + "JAVA ENABLED: " + javaEnabled+ "<br>";
	/*
	document.form.version.value = navigator.appVersion;
	document.form.colordepth.value = window.screen.colorDepth;
	document.form.width.value = window.screen.width;
	document.form.height.value = window.screen.height;
	document.form.maxwidth.value = window.screen.availWidth;
	document.form.maxheight.value = window.screen.availHeight;
	document.form.codename.value = navigator.appCodeName;
	document.form.platform.value = navigator.platform;
	document.form.pages.value = history.length;
	if (navigator.javaEnabled() < 1) document.form.java.value="No";
	if (navigator.javaEnabled() == 1) document.form.java.value="Yes";

	if(navigator.javaEnabled() && (navigator.appName != "Microsoft Internet Explorer")) {
		vartool=java.awt.Toolkit.getDefaultToolkit();
		addr=java.net.InetAddress.getLocalHost();
		host=addr.getHostName();
		ip=addr.getHostAddress();		
   }*/
}

