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; 

