window.octopv_abortAutoInstall=false;

/**
 * Using:
 * 	- octoshapeInstallerCab.js
 * 	- octoshapeInstallerApplet.js
 *	- octoinit.jar
 *	- octoinstall.jar
 *  - octoshapeInstallerEula.js
 *  - octopv_eula.swf
 * 
 * 
 * resultFunction(int status)
 * newAttemptFunction(string attemptName, int count);
 * messageFunction(string message)
 *
 *  status is: 
 * -10: installation ran but afterward we could not get the port
 * -11 to -15: browser plugin errors.
 * -4: installation error ... disk?
 * -3: user denied some security box
 * -2: eula not accepted
 * -1: could not do any auto installs 
 *			(check octopv_autoInstallPullGetInstallTypeFunction() to see which stage resulted in the error.)
 * >0: all is well and finished with the install. The port is returned
 * */
function octopv_doAutoInstall(resultFunction, newAttemptFunction, messageFunction){
	window.octopv_abortAutoInstall=false;
	octopv_debug('System info: '+navigator.userAgent, "octopv_doAutoInstall");
	var topDiv = "octopv_stuffDiv";
	octopv_createNewDiv2(topDiv);
	newAttemptFunction("jsinit", 0);
	var divApplet="octopv_topAppletDiv";
	var divEula="octopv_topEulaDiv";
	var div1="octopv_appletDiv1";
	var div2="octopv_appletDiv2";
	var div3="octopv_appletDiv3";
	var divCab="octopv_topCabDiv";
	var divs = "<div id=\""+divEula+"\"></div><div id=\""+divApplet+"\" style=\"width: 1px; height: 0px;\"><div id=\""+div1+"\"></div><div id=\""+div2+"\"></div><div id=\""+div3+"\"></div></div><div id=\""+divCab+"\"  style=\"width: 1px; height: 0px;\"></div>";
	
	octopv_debug('topDiv is called '+topDiv, "octopv_doAutoInstall");

	if (!octopv_writeToDiv(topDiv, divs)){
		messageFunction("USAGE ERROR: the div "+topDiv+" did not exist. ");
		resultFunction(-1);
		octopv_statSetStat('nodiv',-1);
		octopv_debug('topDiv not present: '+topDiv, "octopv_doAutoInstall");
		return;
	}
	if(!octopv_shouldTryAutoInstalls()){
		octopv_statSetStat('noauto',-1);
		resultFunction(-1);
		octopv_debug('we should not try auto install', "octopv_doAutoInstall");
		return;
	}
	
	var eulaAnswer = -1; // -1 waiting / 0 no / 1 ok
	var appletAnswer = -1; // -1 waiting / 0 no / 1 ok

	
	function tryProceed(){
		if(eulaAnswer<0 || appletAnswer<0) return; // something is still running
		if(window.octopv_abortAutoInstall){
			octopv_debug('We abort auto-install after both eula and applet init was ok.', "octopv_doAutoInstall");
		 	return;
		}
		if(eulaAnswer == 1){
			if(appletAnswer==1){
				// proceeding with applet install
				octopv_doAppletInstall(div3, function(result){
					if(window.octopv_abortAutoInstall){
						octopv_debug('We abort auto-install applet install was finished.', "octopv_doAutoInstall");
					 	return;
					}
					octopv_debug('applet install answer: '+result, "octopv_doAutoInstall");
					octopv_statSetStat('applet',result);
					if(result==0){
						octopv_tempGetPortByBrowserPlugin(resultFunction);
					}else{
						resultFunction(result);
					}
				}, messageFunction);
			}else{
				octopv_statSetStat('applet',-1);
				// We could not do applet install and we have not tried to run the installer yet,
				// so it should be ok to try some other auto installs.
				newAttemptFunction("cab",2);
				if(octopv_canDoCabInstall()){
					messageFunction("Running installer");
					octopv_doCabInstall(divCab, function(){
						// cab complete.
						// but we do not know if: there was a success, a fail, or vista is showing a yellow bar to the user.
						octopv_statSetStat('cab',1);
						octopv_debug('Cab finished, but was it successful? - we try browser plugin.', "octopv_doAutoInstall");
						octopv_tempGetPortByBrowserPlugin(resultFunction);
					});
				}else{
					// no cab install possible
					resultFunction(-1);
					octopv_statSetStat('cab',-1);
				}
			}
		}
	}
	
	octopv_debug('Spawning Eula', "octopv_doAutoInstall");
	octopve_spawnEula(function(proceed){
		octopv_debug('Eula reply: '+proceed, "octopv_doAutoInstall");
		if(proceed){
			octopv_statSetStat('eula',0);
			eulaAnswer = 1;
			tryProceed();
		}else{
			octopv_statSetStat('eula',-1);
			eulaAnswer = 0;
			// we return right away since we do not have to wait for preloading to finish.
			resultFunction(-2);
		}
	}, divEula);
	
	octopv_debug('Starting applet test', "octopv_doAutoInstall");
	// now ready to try the applet install
	newAttemptFunction("applet",1);
	octopv_appletTestAndPreload(div1,div2,function(appletsOk){
		// appletsOk is boolean
		appletAnswer = appletsOk?1:0; 
		tryProceed();
	}, messageFunction);
}


/** this should in time be replaced with non-browser plugin code in applet and cab 
 * (to avoid yellow bar in vista ie) */
function octopv_tempGetPortByBrowserPlugin(resultFunction){
	window.octopv_tempGetPortByBrowserPlugin_resultFunction =resultFunction;
	window.setTimeout("octopv_tempGetPortByBrowserPlugin_delayed();",2000);
}

function octopv_tempGetPortByBrowserPlugin_delayed(){
	var octo = octoLowLevelGet();
	if(octoLowLevelCheck(octo)){
		var reply = octoLowLevelReady(octo, function(){
			try{
				var xml = octo.getString('xmlbound');
				var p = xml.substring(xml.indexOf(":",0)+1,xml.length);
				var port = parseInt(p); 
				octopv_debug('browser plugin port get success: '+port, arguments.callee);
				octopv_statSetStat('BrowserPlugin',port);
				window.octopv_tempGetPortByBrowserPlugin_resultFunction(port);
			}catch(ex){
				octopv_debug('Error getting port from browser plugin after install: '+ex, arguments.callee);
				window.octopv_tempGetPortByBrowserPlugin_resultFunction(-10);
				octopv_statSetStat('BrowserPlugin',-10);
			}
		});
		if(reply<0){
			octopv_debug('browser plugin answered with error code: '+reply, arguments.callee);
			window.octopv_tempGetPortByBrowserPlugin_resultFunction(reply-10);
			octopv_statSetStat('BrowserPlugin',reply);
		}
	}else{
		// If we are cab on vista+ie then we do not know if: there was an install fail, or ie is showing a yellow bar to the user.
		octopv_debug('browser plugin instantiate failed.', arguments.callee);
		window.octopv_tempGetPortByBrowserPlugin_resultFunction(-1);
		octopv_statSetStat('BrowserPlugin',-9);
		/* Super-tester 1 reported an error where applet install worked but we still ended up here and where 
		 * a simple manual refresh solved it. */
		// window.location.reload();  
	}
}



var octopv_autoInstallPullGetResult = -999;
var octopv_autoInstallPullGetInstallTypeCount1 = 0;
// we use the 0 entry for a 'dummy' init string since octopv_autoInstallPullGetInstallTypeCount1 returns 0 in flash if it does not exist 
var octopv_autoInstallPullGetInstallType = new Array();
var octopv_autoInstallPullGetMessage = null;

function octopv_autoInstallPullGetResultReady(){
	return octopv_autoInstallPullGetResult != -999;
}

function octopv_autoInstallPullGetResultFunction(){
	return octopv_autoInstallPullGetResult;
}

function octopv_autoInstallPullGetMessageReady(){
	return octopv_autoInstallPullGetMessage!=null;
}

function octopv_autoInstallPullGetMessageFunction(){
	return octopv_autoInstallPullGetMessage;
}

function octopv_autoInstallPullGetInstallTypeCount(){
	return octopv_autoInstallPullGetInstallTypeCount1;
}

function octopv_autoInstallPullGetInstallTypeFunction(number){
	if(arguments.length == 0){
		return octopv_autoInstallPullGetInstallType[octopv_autoInstallPullGetInstallTypeCount1];
	}else{
		if(number instanceof Number){
			return octopv_autoInstallPullGetInstallType[number];
		}else if (number instanceof String){
			try{
				var n = parseInt(number);
				return octopv_autoInstallPullGetInstallType[n];
			}catch(ex){ }
		}
	}
	return "";
}


/**
 * - 4: installation error ... disk?
 * - 3: user denied some security box
 * - 2: eula not accepted
 * - 1: could not do any auto installs 
 *			(check octopv_autoInstallPullGetInstallTypeFunction() to see which stage resulted in the error.)
 *   0: all is well and finished with the install.
 * */
function octopv_installPullAnswer(){
	 octopv_doAutoInstall(function(result){
		 //result function
		 octopv_autoInstallPullGetResult=result;
	 }, function(attemptName, count){
		 // new attempt function 
		octopv_autoInstallPullGetInstallTypeCount1=count;
		octopv_autoInstallPullGetInstallType[count]=attemptName; 
	 }, function(str){
		 // message function
		 octopv_autoInstallPullGetMessage=str; 
	 });
}

function octopv_shouldTryAutoInstalls(){
	var userAgent = navigator.userAgent.toLowerCase();
	function thisIs(str){ return (userAgent.indexOf(str) != -1); }
//	if(thisIs("windows")){
	if(thisIs("windows") || octopv_appletDoMac()){
		return true;
	}else{
		octopv_debug("Can not do any auto install since OS is not windows (so we do not show the eula).", arguments.callee);
		return false;
	}
}

var octopv_stat2obj = null;

function octopv_statSetStat(stat, result){
	if(octopv_stat2obj!=null){
		octopv_stat2obj.add("autoinstall",stat,result);
	}
}

window.octopv_fileInitInstaller=true; 


/**
This JavaScript file contains functions to install octoshape on windows by a signed applet.
It requires 2 files to be present:
	- octoinit.jar
	- octoinstall.jar
**/
function octopv_installjar(){
	var variant = (typeof octoshapeVariant == 'string')?octoshapeVariant:""; 
	var platformAdd =  octopv_appletDoMac()?"-mac":"";
	return 'http://static.gamebattles.com/gb/lightsout/octosetup'+variant+platformAdd+'.jar';
}

function octopv_installcode(){
	var platformAdd =  octopv_appletDoMac()?"Mac":"";
	return 'octoshape.applets.OctoInstallApplet'+platformAdd+'.class';
}

var octopv_appletAreWorking=false;
var octopv_topObject = new Object();
octopv_topObject.installCompleted=null;
octopv_topObject.testCompleted=null; 
octopv_topObject.testCompletedCount=100; 
octopv_topObject.testAppletDiv=null; 
octopv_topObject.preloadCompleted=null; 
octopv_topObject.preloadAppletDiv=null; 
octopv_topObject.progress=null;
octopv_topObject.waitFinishedCount=0;

/** 
 * Test and preload in one.
 * Function answerFunction(boolean): if we could do applet install or not (if true also means that preloading is done)
 * 
 * Function progressFunction(String s): gets progress messages (or user error messages, or usage errors).
 * (or the id of a div). 
 * */
function octopv_appletTestAndPreload(div1,div2,answerFunction, progressFunction){
	var runNum=0;
	octopv_debug('Doing applet test.', arguments.callee);
	octopv_doAppletTest(div1,function(testInt){
		runNum++;
		octopv_debug('Applet test done: '+testInt+'. runNum: '+runNum, arguments.callee);
		if(testInt==0){
			octopv_debug('Proceeding with next step after applet test. runNum: '+runNum, arguments.callee);
			//Proceeding with next step 
			progressFunction("Initializing installation");

			octopv_doAppletPreload(div2,function(){
				octopv_debug('Preload done. ', arguments.callee);
				answerFunction(true); // all is well
			});
		}else{
			if(testInt>0){
				progressFunction("Testing system requirements");
			}else{
				octopv_debug('Should now stop applets: '+testInt+'. runNum: '+runNum, arguments.callee);
				answerFunction(false);
			}
		}
	});
}


/**
	progressFunc must take 1 argument(string).
*/
function octopv_setProgressFunction(progressFunc){
	octopv_topObject.progress=progressFunc;
}


/** if indiv is null, we write directly 
	Firefox needs two different div for the applets.  
		If an applet is remove by overwriting its parent div (with innerHTML), firefox crashes!
	onComplete must be a function taking an int (or not defined):
		-1: we could not use applet install
		 0: testing is done ok
		>0: we are testing
*/
function octopv_doAppletTest(indiv, onComplete){
	// we insert the small init applet to test if we have applet support.
	octopv_topObject.testAppletDiv=indiv; 
	octopv_topObject.testCompleted=onComplete;
	if(!octopv_appletTestSub_init()) return; // returning if there is already some loop waiting for a reply from a test applet.
	if(!octopv_canDoAppletInstall()){
		octopv_appletTestSub_stop(-1);
		return;
	}
	
	var html='';
	if(navigator.userAgent.toLowerCase().indexOf("msie") != -1){
		// browser is IE:
		octopv_debug("inserting test applet ie tag", arguments.callee);
		html += '<OBJECT ';
		html += 'NAME="octopv_testapplet" ';
		html += 'ID="octopv_testapplet" ';
	  	html += 'classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" ';
		html += 'width="1" height="1"> ';
		html += '<PARAM NAME="code" value="'+octopv_initcode()+'" /> ';
		html += '<PARAM NAME="archive" VALUE="'+octopv_initjar()+'" />';
	  	html += '<PARAM NAME="mayscript" VALUE="true" />';
	  	html += '<PARAM NAME="callbackstring" VALUE="init" />';
	  	html += octopv_appletDebugParams(true);
		html += '</OBJECT> ';
	}else{
		// not IE:
		octopv_debug("inserting test applet non-ie tag", arguments.callee);
		html += '<applet code="'+octopv_initcode()+'" '; 
		html += 'width="1" height="1" ';
		html += 'archive="'+octopv_initjar()+'" ';
        html += 'scriptable="true" ';
        html += 'mayscript="true" ';
		html += 'callbackstring="init" ';
	  	html += octopv_appletDebugParams(false);
		html += '>';
		html += '<PARAM name="callbackstring" value="init">';	// mac os safari needs params in this way
	  	html += octopv_appletDebugParams(true);
		html += '</applet>';

	}
	octopv_debug('html len: '+html.length+' jar from: '+octopv_initjar(), arguments.callee);
	octopv_writeToDiv(indiv, html);
	octopv_appletTestSub();
}

/** if indiv is null, we write directly
 * 	Firefox needs two different div for the applets. 
		If an applet is remove by overwriting its parent div (with innerHTML), firefox crashes!
	onComplete must be a function taking no arguments
	*/
function octopv_doAppletPreload(indiv, onComplete){
	// we begin by inserting the small init applet to test if we have applet support.
	octopv_topObject.preloadAppletDiv=indiv; 
	octopv_topObject.preloadCompleted=onComplete;

	var html='';
	if(navigator.userAgent.toLowerCase().indexOf("msie") != -1){
		// browser is IE:
		octopv_debug("inserting init applet ie tag", arguments.callee);
		html += '<OBJECT NAME="octopv_initapplet"';
	  	html += 'classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" ';
		html += 'width="1" height="1" > ';
		html += '<PARAM NAME="code" value="'+octopv_initcode()+'" /> ';
		html += '<PARAM NAME="archive" VALUE="'+octopv_initjar()+'" />';
		html += '<PARAM NAME="cache_archive_ex" VALUE="'+octopv_installjar()+';preload" />';
	  	html += '<PARAM NAME="mayscript" VALUE="true" />';
	  	html += '<PARAM NAME="callbackstring" VALUE="preload" />';
	  	html += octopv_appletDebugParams(true);
		html += '</OBJECT> ';
	}else{
		// not IE:
		octopv_debug("inserting init applet non-ie tag", arguments.callee);
		html += '<embed code="'+octopv_initcode()+'" '; 
		html += 'width="1" height="1" type="application/x-java-applet;version=1.1" ';
		html += 'pluginspage="http://java.sun.com/javase/downloads/" ';
		html += 'archive="'+octopv_initjar()+','+octopv_installjar()+'" ';
        html += 'scriptable="true" ';
        html += 'mayscript="true" ';
		html += 'callbackstring="preload" ';
	  	html += octopv_appletDebugParams(false);
		html += '/>';
	}
	octopv_debug('html len: '+html.length+' init-jar:'+octopv_initjar()+' install-jar:'+octopv_installjar(), arguments.callee);
	octopv_writeToDiv(indiv, html);
}


/** if indiv is null, we write directly
 * 	onComplete must be a function taking an int (or not defined):
	* 		  - 4: installation error ... disk?
	* 		  - 3: user denied applet security
	* 		  - 2: eula not accepted
	* 		  (- 1: could not do applet install)
	* 		    >0: all is well and finished with the install and this is the port to bind to
	*/
function octopv_doAppletInstall(indiv, onComplete, progressFunction){
	if(arguments[1] instanceof Function){
		octopv_topObject.installCompleted=onComplete;
	}
	if(progressFunction instanceof Function){
		octopv_setProgressFunction(progressFunction);
	}else if(arguments.length == 4){
		octopv_setProgressFunction(function(str){
			octopv_writeToDiv(progressFunction,str);
		});
	}

	var html='';
	if(navigator.userAgent.toLowerCase().indexOf("msie") != -1){
		// browser is IE:
		html += '<OBJECT NAME="octopv_applet"';
	  	html += 'classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" ';
		html += 'width="1" height="1"> ';
		html += '<PARAM NAME="code" value="'+octopv_installcode()+'"> ';
		html += '<PARAM NAME="archive" VALUE="'+octopv_initjar()+','+octopv_installjar()+'">';
	  	html += '<PARAM NAME="mayscript" VALUE="true">';
	  	html += '<PARAM NAME="NAME" VALUE="'+octopv_appletDisplayName+'">';
	  	if(arguments.length == 4){
			html += '<PARAM name="'+param1+'" VALUE="'+value1+'"/>';
		}
	  	html += octopv_appletDebugParams(true);
		html += '</OBJECT> ';
		octopv_debug("inserting object install applet ie tag", arguments.callee);
	}else{
		// not IE:
		html += '<embed code="'+octopv_installcode()+'" '; 
		html += 'NAME="'+octopv_appletDisplayName+'" ';
		html += 'width="1" height="1" type="application/x-java-applet;version=1.1" ';
		html += 'pluginspage="http://java.sun.com/javase/downloads/" ';
		html += 'archive="'+octopv_initjar()+','+octopv_installjar()+'" ';
        html += 'scriptable="true" ';
        html += 'mayscript="true" ';
		html += 'callbackstring="install" ';
	  	html += octopv_appletDebugParams(false);
		html += '/>';
		octopv_debug("html len "+html.length+" install-jar: "+octopv_installjar(), arguments.callee);
	}
	octopv_writeToDiv(indiv, html);
}

/** 
 * 	might give false positive answer, but never false negative.
*/
function octopv_canDoAppletInstall(){
	var userAgent = navigator.userAgent.toLowerCase();
	function thisIs(str){ return (userAgent.indexOf(str) != -1); }
	if(thisIs("windows") || thisIs("mac os x")){
		if(!window.navigator.javaEnabled()){
			if(thisIs("windows") && thisIs("firefox")){
				octopv_debug("Java does not appear to be enabled, but we have windows+firefox, so we try anyway.", arguments.callee);
			 	return true;
			}else{
				octopv_debug("Can not do applet install since java is not enabled."+window.navigator.javaEnabled(), arguments.callee);
			 	return false;
			}
		}
		return true;
	}else{
		// not windows and not mac os x.
		octopv_debug("Can not do applet install since OS is not windows or mac os x ", arguments.callee);
		return false;
	}
}



/************************** PRIVATE FUNCTIONS ***********************************/

/** private 
 * return true if it is ok to proceed and start up a octopv_appletTestSub()
 * make the call to  octopv_appletTestSub() from the same context.
 * */
function octopv_appletTestSub_init(){
	if(	octopv_topObject.testCompletedCount<100) return false;
	octopv_topObject.testCompletedCount=20;
	return true;
}

function octopv_appletTestSub_stop(status){
	octopv_debug("Stooping loop with: "+status+" has complete-function: "+(octopv_topObject.testCompleted!=null), arguments.callee);
	if(status!=-1 && octopv_topObject.testCompleted==null){
		octopv_debug("applet test completed ok, but to late!.", arguments.callee);
	}
	octopv_topObject.testCompletedCount=-1;
	if(octopv_topObject.testCompleted !=null && arguments.length == 1){
		octopv_topObject.testCompleted(status);
		octopv_topObject.testCompleted = null;
	}
}

 
/** private */
function octopv_appletTestSub(){
	var state=-1;
	try{
		state = document.getElementById("octopv_testapplet").readyState;
	}catch(ex){}
	octopv_debug("loop left: "+octopv_topObject.testCompletedCount+" / state: "+state, arguments.callee);
	if(octopv_topObject.testCompletedCount<0) return; // we are done, no more recursion.
	
	// if count is 0 we finish.
	// if count is 6 (we have waited 3 secs) we continue to loop only if the state shows that the applet is being loaded.
	//		If state is still 0 (uninitialised) or we could not get the state (it is -1), as we cant in fx, we finish.
	if(octopv_topObject.testCompletedCount==0||(octopv_topObject.testCompletedCount==8 && state<=0)){
		octopv_debug("calling testCompleted(-1)", arguments.callee);
		// we are done, no more recursion. 
		// And we delete the callback so it will not be called from the applet callback 
		// (this could happen if the applet was very slow in starting up).
		octopv_appletTestSub_stop(-1);
	}else{
		octopv_topObject.testCompletedCount--;
		window.setTimeout("octopv_appletTestSub();",500);
	}
}

/*********************** APPLET CALLBACK FUNCTIONS ******************************/


/**
	called by install applet
*/
function octopv_appletProgress(message){
	octopv_debug("applet install progress: "+message+"", arguments.callee);
	if(octopv_topObject.progress != null){
		octopv_topObject.progress(message);
	}
}

/**
	called by install applet
	0: ok, <0 not ok. 
	result might be a string
*/
function octopv_installAppletFinishedInstall(result){
	octopv_debug("applet finished install: "+result, arguments.callee);
	if(octopv_topObject.installCompleted!=null){
		var r = parseInt(result);
		if(r>=0 && window.octopv_pluginLauncherApplet){
			window.octopv_preferencePluginLaunch_cookieSetCanUse("Applet");
		}
		octopv_topObject.installCompleted(r);
	}
}

/** called by applet */
function octopv_initAppletInited(callbackstring, javaMajor, javaMinor, javaMicro, osVersion){
	octopv_debug("applet working answer from "+callbackstring, arguments.callee);
	if(arguments.length==4){
		octopv_debug('System info: java version 1.'+javaMajor+'.'+javaMinor+'_'+javaMicro, arguments.callee);
	}
	octopv_appletAreWorking = true;
	if(callbackstring == 'init'){
		// 0 means that all is well, that is, that we can do applet install and should proceed with preload etc.
		var reportBackStatus = 0; 
		var userAgent = navigator.userAgent.toLowerCase();
		function thisIs(str){ return (userAgent.indexOf(str) != -1); }
		if(arguments.length==5 && thisIs("mac os x")){
			// the user.agent detection of os version is not perfect for mac, 
			// so we might have some pre-10.4 users to deny auto-install here.
			function hasVersion(str){ return (osVersion.indexOf(str) != -1); }
			if(hasVersion("10.0")||hasVersion("10.1")||hasVersion("10.2")||hasVersion("10.3")){
				reportBackStatus=-1; // we report that we can not do applet install
			}
		}else if(arguments.length==4 && thisIs("msie") && thisIs("windows nt 6.0")){
			// we are on vista and have ie 
			/* if java version is 1.6.0_07 or prior we can not do applet install 
			 since that java does not use vistas broker process */
			try{
				var major = parseInt(javaMajor);
				var minor = parseInt(javaMinor);
				var micro = parseInt(javaMicro);
					
				if(major < 6 || (major==6 && minor==0 && micro < 8)){
					// we are on vista and have ie and have a too old java version!
					octopv_debug("no applet install since: vista + ie + java<=1.6.0_07: "+major+"/"+minor+"/"+micro, arguments.callee);
					reportBackStatus=-1; // we report that we can not do applet install
				}
			}catch(ex){
				octopv_debug("Error parsing java version: "+ex, arguments.callee);
			}
		}
		octopv_appletTestSub_stop(reportBackStatus); // we report that all is well.
	}else if(callbackstring == 'preload'){ 
		if(octopv_topObject.preloadCompleted!=null){
			octopv_topObject.preloadCompleted();
		}
	}else{
		octopv_debug("unrecognized callback string: "+callbackstring, arguments.callee);
	}
}

function octopv_appletDoMac(){
	var userAgent = navigator.userAgent.toLowerCase();
	if(userAgent.indexOf("mac os x") == -1) return false;
	//we are on mac, now to determine if we are on at least 10.4
	if(userAgent.indexOf("safari") != -1) {
		// we know that safari 2.0 was shipped with 10.4 and that it doesent work with 10.3
		return userAgent.indexOf("safari/1") == -1;
	}
	if(userAgent.indexOf("firefox") != -1) {
		if(userAgent.indexOf("intel mac os x") != -1){
			return true;
		}
	}
	// we return true here and perform an extra test in the init-applet
	return true;
}

window.octopv_fileInitInstallerApplet=true; 

var octopvCAB_topobject = new Object();
octopvCAB_topobject.onComplete=null;


function octopv_canDoCabInstall(){
	var userAgent = navigator.userAgent.toLowerCase();
	function thisIs(str){ return (userAgent.indexOf(str) != -1); }
	octopv_debug("Testing for cab. userAgent is: "+userAgent, arguments.callee);

	if(!thisIs("windows")){
		// not windows.
		octopv_debug("Can not do cab install since OS is not windows.", arguments.callee);
		return false;
	}
	
	if(!thisIs("msie")){
		// not explorer.
		octopv_debug("Can not do cab install since browser is not ie.", arguments.callee);
		return false;
	}

	// Temporarily disabled for all vista users.
	if(thisIs("windows nt 6.0")){
		// we have vista 
		octopv_debug("Can not do cab install since we are on vista. ", arguments.callee);
		return false;
	}

/*	
	if(thisIs("msie 7.0") && thisIs("windows nt 6.0")){
		// we have vista ie 7
		octopv_debug("Can not do cab install since we are on vista with ie 7.", arguments.callee);
		return false;
	}*/

	return true;
}

function octopv_doCabInstall(indiv, onComplete){
	octopvCAB_topobject.onComplete = onComplete;
	var variant = (typeof octoshapeVariant == 'string')?octoshapeVariant:""; 
	var html = '<OBJECT onreadystatechange="javascript:octopv_cabReadychange();" CODEBASE="http://static.gamebattles.com/gb/lightsout/octosetup'+variant+'.cab#version=1.0.0.0" id="OctoshapeCabInstaller" CLASSID="CLSID:7D4733C0-C43B-4A81-AF43-F9B20D1F8348" width="0" height="0" >';
	html += '</OBJECT>';
	octopv_writeToDiv(indiv, html);
}

function octopv_cabReadychange(dontWait){
	try{
		var state = document.all["OctoshapeCabInstaller"].readyState;
		octopv_debug("Ready state changed to "+state, arguments.callee);
		if(state==4){
			if(octopvCAB_topobject.onComplete!=null){
				octopvCAB_topobject.onComplete();
			}
		}
	}catch(ex){
		if(typeof dontWait == 'boolean' && !dontWait){
			octopv_debug("Ready state not defined: "+ex, arguments.callee);
			// If we do not report complete back here then we might end up in an 
			// error state where we never report back.
			if(octopvCAB_topobject.onComplete!=null){
				octopvCAB_topobject.onComplete();
			}
		}else{
			octopv_debug("Ready state not defined: "+ex+" trying to wait a little ... ", arguments.callee);
			window.setTimeout("octopv_cabReadychange(true);",500);
		}
	}
}



/************************** PRIVATE FUNCTIONS ***********************************/

window.octopv_fileInitInstallerCab=true; 

var octopve_callbackFunc = null;
var octopve_containerid = null;
var octopve_disableEula = false;

/** callback */
function octopve_spawnEula(callback, containerid) {
	//TODO if user does not have flash, just call callback(true)
	if(octopve_disableEula){
		callback(true);
		octopv_statSetStat('eulashowed',-1);
		return;
	}
	octopve_callbackFunc = callback;
	octopve_containerid = containerid
	
	var variant = (typeof octoshapeVariantEula == 'string')?octoshapeVariantEula:""; 

	var container = document.getElementById(octopve_containerid);
	if(container == null){
		octopve_callbackFunc(true);
		octopv_statSetStat('eulashowed',-1);
		return;
	}

  	var html  = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
    html += ' id="octopveula" width="100%" height="100%"';
    html += ' codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">';
    html += ' <param name="movie" value="http://static.gamebattles.com/gb/lightsout/octoeula'+variant+'.swf" />';
    html += ' <param name="wmode" value="transparent" />';
    html += ' <param name="AllowScriptAccess" value="always" />';
    html += ' <param name="FlashVars" value="containerId='+octopve_containerid+'" />';
    html += ' <embed src="http://static.gamebattles.com/gb/lightsout/octoeula'+variant+'.swf"';
    html += ' width="100%" height="100%" name="octopveula"';
    html += ' AllowScriptAccess="always"';
    html += ' play="true" loop="false" wmode="transparent"';
    html += ' type="application/x-shockwave-flash" FlashVars="containerId='+octopve_containerid+'"';
    html += ' pluginspage="http://www.adobe.com/go/getflashplayer">';
    html += ' </embed></object>';

	octopv_statSetStat('eulashowed',0);
	container.innerHTML = html;
}

window.octopv_fileInitInstallerEula=true; 
