/**********************************************************************************

 ActiveGS Deployment Script 
 v2.01 2011-11-21 * Debug IE9 support
 v2.00 2011-09-24 + Force update if minimum required version not satisfied
 v1.90 2011-06-21 + Added support for Firefox 5; restored support for Firefox 4
 v1.80 2011-06-21 + Added installation fallback hook 

***********************************************************************************/

var activeGS = {

// public (can be overriden by the calling page)
publicconsts : { 
					"width" : "704", 
					"height" : "462",
					"installPath" : "http://activegs.freetoolsassociation.com",
					"ctrlid" : "activeGSCtrl",
					"parameters" : {}, 
					"minimumRequiredVersion" : "",
					"installFallback" : 0
					},

// private
privateconsts :
				{
					"latestVersion" : "3.5.762",
					"latestVersionDate" : "2012/01/22 14:14:21"
				},
				
instanceParam : {},

getObjectTag: function(_update)
{
	var activegsclass;
	var codebase;
	if (BrowserDetect.browser=="Internet Explorer")
	{
			activegsclass="classid=\"clsid:052DF14F-6F28-44A0-9130-294FDA6176EB\"";
			codebase = "ActiveGS.cab#version=3,5,762,0";
	}
	else
	{
			activegsclass="type=\"application/x-activegs\"";
			
			if (BrowserDetect.browser=="Firefox" && BrowserDetect.version <"4" )
				codebase = "ActiveGS.xpi";
			else
				codebase = "ActiveGS4.xpi";
	}
	
	
	var activegsh1 = " style=\"font-size:14px;font-weight:bold;\" ";
	var activegsmain = " style=\"font-size:18px;font-weight:bold;\" ";
	var pa;
	str = "";
	if (!_update)
	{
	str  += "<object id=\""+this.instanceParam.ctrlid+"\" "+activegsclass+" width=\""+this.instanceParam.width+"\" height=\""+this.instanceParam.height+"\" codebase=\""+this.instanceParam.installPath+"/"+codebase+"\">";		
	for(pa in this.instanceParam.parameters)
		str +="<param name=\""+pa+"\" value=\""+this.instanceParam.parameters[pa]+"\" />";
	}
	
	str += "<div style=\"width:"+this.instanceParam.width+"px;height:"+this.instanceParam.height+"px;background-color:#2222FF;color:white;font-family:verdana;font-size:12px;display:table-cell;vertical-align:middle;text-align:center\">";
		
	if (!_update)
	{
		str += "<p>The <span "+activegsmain+">ActiveGS</span> plugin <br />";
		str += "is required to run apple II/IIGS software within your browser</p>";
	}
	else
	{
		str += "<p>The <span "+activegsmain+">ActiveGS</span> plugin <font color=\"red\"><b>NEEDS TO BE UPDATED</b></font><br />";
		str += " to the latest version to run apple II/IIGS software within this page ";
		str += "<i>(installed version is "+_update+")</i></p>";

	}
	
	
	str += "<img src=\""+this.instanceParam.installPath+"/ActiveGS.png\" width=\"78\" height=\"78\" /><br />";
	str += "<i>Latest version is "+this.privateconsts.latestVersion+"</i>";
	if (_update)
	str += "<p></p><p "+activegsh1+" > Installation Procedure for "+BrowserDetect.browser+" "+BrowserDetect.version+" ("+BrowserDetect.OS+")</p>";

	if (BrowserDetect.browser == "Chrome")
	{
		str += "<div style=\"text-align:left;margin:30px\">";
		str += "<p >Step 1 - Click <a href=\""+this.instanceParam.installPath+"/ActiveGS.crx\"><span style=\"color:white;font-size:20px\"><u>HERE</u></span></a> to install ActiveGS as a Chrome Extension</p>";
		str += "<p>Step 2 - Just  <a href=\"javascript:window.location.reload(true)\"><span style=\"color:white;font-size:20px\">refresh</span></a> your browser when your're done with the installation</p>";
	}
	else	
	if (BrowserDetect.browser == "Internet Explorer")
	{
	   str += "<div style=\"text-align:center;margin:30px\">";
	   str += "<ul>To saferly run ActiveGS within IE, you need to modify your security settings:";
	   str += "<div style=\"text-align:left;margin:30px\">";
	   str += "<li>Select 'Trusted Sites' in Tools/Internet Option/Security Tab, and click on 'Sites'</li>";
		str += "<li>Add http://activegs.freetoolsassociation.com in the 'Add this website to the zone', uncheck https and click 'Add'</li>";     
	   str +="<li>Click 'Close' to go back to the Security Tab,  then click on 'Custom Level' (with 'Trusted Sites' selected - should be the case) to access the 'Security Settings - Trusted Zone'</li>";
		str += "<li>There, check the 'Prompt' button in front of 'Download Unsigned ActiveX Controls'</li>";
		str += "</div>";	   
	   str +="Once finished, just  <a href=\"javascript:window.location.reload(true)\"><span style=\"color:white;font-size:20px\">refresh</span></a> your browser,<br />click on the yellow bar to 'install' then 'accept' the plugin : here you go!";
	   
   	}
	else
	if ( (BrowserDetect.browser == "Firefox") /*&& (BrowserDetect.version <"4")*/ )
	{
			str += "<div style=\"text-align:left;margin:30px\">";
	    str += "<p >Step 1 - Click <a href=\""+this.instanceParam.installPath+"/"+codebase+"\"><span style=\"color:white;font-size:20px\"><u>HERE</u></span></a> to launch the installation of the emulator plugin.</p>";
			str += "<p>Step 2 - Click on the [Autorize] button in the yellow bar that should have appeared at the top of your browser window</p>";
      str += "<div style=\"text-align:left;line-height:28px;vertical-align:middle;width:90%;height:28px; background-color:#FFFFC0; border-color:#808080;border-width:1px;border-color:black;border-style:solid;color:black\"";
			str += " onclick=\"alert('This is just a sample bar. Click on the bar at the top of your browser window!')\" >";
      str += "Do you want to autorise...</div>";
      str += "<p>Step 3 - Click on the [Install Now] button to complete the installation of the emulator.</p>";
      str += "</div>";
	   // str += "<p><i>Note that the emulator is digitally signed for your safety!</i></p>";
  }    
	else
	if ( BrowserDetect.OS == "Mac" )
	{
			str += "<p>There's no installer yet for "+BrowserDetect.browser+": you need to manually install the plugin!</p>";
			str += "<p>Download <a href=\""+this.instanceParam.installPath+"/ActiveGS.plugin.zip\">ActiveGS.plugin.zip</a> and unzip it into /Library/Internet Plug-Ins</p>";
			str += "<p>Warning : only compatible with MacIntel configurations!</p>";
	}
	else
	if ( BrowserDetect.OS == "Windows")
	{
			str += "<p>There's no installer yet for "+BrowserDetect.browser+" "+BrowserDetect.version+" : you need to manually install the plugin!</p>";
			str += "<p>Download <a href=\""+this.instanceParam.installPath+"/npActiveGS.zip\"><font color=\"white\"><u>npActiveGS.zip</u></font></a> and unzip it into your browser plugin directory <br />";
			if (BrowserDetect.browser == "Opera")
				str += "(ex: c:/program files/opera/program/plugins)";
			else
			if (BrowserDetect.browser == "Chrome")
				str += "(ex: c:/Users/<i>username</i>/AppData/Local/Google/Chrome/Application/plugins <br /> you might need to create the plugins/ directory if it does not exist yet)";
			else
				if (BrowserDetect.browser == "Minefield")				
			  	str += "(ex: c:/program files/Minefield/plugins)";
			else
			  	str += "(ex: c:/program files/Mozilla Firefox/plugins)";

			str += "</p>";
	}
	else
	{
		if (typeof this.instanceParam.installFallback=="function")
		{
			var fallbackstr = this.instanceParam.installFallback(this.instanceParam.ctrlid,this.instanceParam.width,this.instanceParam.height,BrowserDetect.OS,BrowserDetect.browser,BrowserDetect.version);
			if (fallbackstr)
			{
				document.write(fallbackstr);
				return ;
			}
		}
		
		str += "Unfortunately, it is not supported yet :=(";
	};
	
	str += "<p>&nbsp;</p><p><i>Please check <a href=\"http://activegs.freetoolsassociation.com\"><font color=\"white\"><u>http://activegs.freetoolsassociation.com</u></font></a> if you need further assistance</i></p>";
	str += "</div>";
	
	if (!_update)
		str += "</object>";

	return str;
},

install : function(_parameters)
{
	
	// récupère les paramètres
	
	this.instanceParam = this.publicconsts ;
	var param;
	for(param in _parameters)
		this.instanceParam[param] = _parameters[param];
	
		// détecte le browser
	BrowserDetect.init();
		
	var sysstr = "_browserName:"+BrowserDetect.browser+";";
	 		sysstr += "_browserVer:"+BrowserDetect.version+";";
	var pc;
	for(pc in this.privateconsts)
			sysstr += "_" +pc+":"+ this.privateconsts[pc]+";";
	 			 	
	if (typeof(this.instanceParam.parameters.EmulatorParam)!='undefined')
	{
		this.instanceParam.parameters.EmulatorParam += ";";
		this.instanceParam.parameters.EmulatorParam += sysstr;
	}
	else
		this.instanceParam.parameters.EmulatorParam = sysstr ;	
	
	var str="<div id=\"container_"+this.instanceParam.ctrlid+"\">"; 
		str += this.getObjectTag(false);
	str += "</div>";	
	document.write(str);	
	
	// check for obsoleted versions
	var obsoleted=0;
	var h = document.getElementById(this.instanceParam.ctrlid);
	if (h && typeof h.Slot51 != 'undefined')
	{
		if (typeof h.Command =='undefined')
				obsoleted=1;
		else
		{
			// vérifie la version
			var ver = this.command("system");
			if (typeof ver.currentBuild == 'undefined')
				obsoleted = 1;
			else 
			if (ver.currentBuild < this.instanceParam.minimumRequiredVersion)
				obsoleted = 1;
		}
	}
	
	if (obsoleted)
	{
		//alert("The installed version of ActiveGS is obsolete ;-(\nYou would need to manually upgrade it to benefit from the new feature set.\nPlease check http://activegs.freetoolsassociation.com for more information");
		//document.write(this.getObjectTag(true));
		var h = document.getElementById("container_"+this.instanceParam.ctrlid);
		h.innerHTML = this.getObjectTag(ver.currentBuild);
		}


 },
 
 command : function (_command)
 {
 	var h = document.getElementById(this.instanceParam.ctrlid);
	var ret ;
 	if (!h)
 		ret = "({ \"error\" : \"ActiveGS control not found in the page\" })";
 	else
 	{
 		if (typeof h.Slot51 != 'undefined')
 		{
 			if (typeof h.Command != 'undefined')
 				ret = h.Command(_command);
	 		else
	 			ret = "({ \"error\" : \"ActiveGS control obsolete : need to be upgraded!\"})";
 		}
 		else
 				ret =  "({ \"error\" : \"ActiveGS control not loaded yet!\"})";			
 	}
 	return eval(ret);
},

setEmulatorParam : function (_param)
{
	var h = document.getElementById(this.instanceParam.ctrlid);
	if (!h) return ;
	h.EmulatorParam = _param;
}

}

/*** 
 Browser detection from : http://www.quirksmode.org/js/detect.html
*/

 
 var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		// added minefield
			{
			string: navigator.userAgent,
			subString: "Minefield",
			identity: "Minefield"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Internet Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};


