/**********************************************************************************

 ActiveGS Deployment Script 
 v1.40 November 30th 2009 
 
***********************************************************************************/

var activeGS = {

// public (can be overriden by the calling page)
publicconsts : { 
					"width" : "704", 
					"height" : "462",
					"installPath" : "http://activegs.freetoolsassociation.com",
					"ctrlid" : "activeGSCtrl",
					"parameters" : {} 
					},

// private
privateconsts :
				{
					"latestVersion" : "2.4.26",
					"latestVersionDate" : "2009/12/14 21:45:49"
				},
				
instanceparam : {},

install : function(_parameters)
{
	
	// récupère les paramètres
	
	this.instanceParam = this.publicconsts ;
	var i;
	for(i in _parameters)
		this.publicconsts[i] = _parameters[i];
	
		// détecte le browser
	BrowserDetect.init();
		
	
	var sysstr = "_browserName:"+BrowserDetect.browser+";";
	 		sysstr += "_browserVer:"+BrowserDetect.version+";";
	for(i in this.privateconsts)
			sysstr += "_" +i+":"+ this.privateconsts[i]+";";
	 			 	
	if (typeof(this.instanceParam.parameters.EmulatorParam)!='undefined')
	{
		this.instanceParam.parameters.EmulatorParam += ";";
		this.instanceParam.parameters.EmulatorParam += sysstr;
	}
	else
		this.instanceParam.parameters.EmulatorParam = sysstr ;	
	
	
	
	// détecte le browser
	BrowserDetect.init();
	
	var activegsclass;
	var codebase;
	if (BrowserDetect.browser=="Internet Explorer")
	{
			activegsclass="classid=\"clsid:052DF14F-6F28-44A0-9130-294FDA6176EB\"";
			codebase = "ActiveGS.cab#version=2,4,26,0";
	}
	else
	{
			activegsclass="type=\"application/x-activegs\"";
			codebase = "ActiveGS.xpi";
	}
	
	var str="";
	var activegsh1 = " style=\"font-size:14px;font-weight:bold;\" ";
	var activegsmain = " style=\"font-size:18px;font-weight:bold;\" ";
		
	str  += "<object id=\""+this.instanceParam.ctrlid+"\" "+activegsclass+" width=\""+this.instanceParam.width+"\" height=\""+this.instanceParam.height+"\" codebase=\""+this.instanceParam.installPath+"/"+codebase+"\">";		
	for(i in this.instanceParam.parameters)
		str +="<param name=\""+i+"\" value=\""+this.instanceParam.parameters[i]+"\" />";
	
	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\">";
		
	str += "<p>The <span "+activegsmain+">ActiveGS</span> plugin <br />";
	str += "is required to safely run apple II/IIGS software within your browser</p>";
	str += "<img src=\""+this.instanceParam.installPath+"/ActiveGS.png\" width=\"78\" height=\"78\" /><br />";
	str += "<i>Latest version is "+this.privateconsts.latestVersion+"</i>";


	str += "<p></p><p "+activegsh1+" > Installation Procedure for "+BrowserDetect.browser+" "+BrowserDetect.version+" ("+BrowserDetect.OS+")</p>";
			
	if (BrowserDetect.browser == "Internet Explorer")
	{
			str += "<li style=\"text-align:left\">Step1: ";
			str += "A yellow bar (like the one below) should have appeared at the top of your browser window : just click on it to authorize and complete the installation of the plugin.</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 += "&nbsp;This site requires the module ActiveGS.Cab published by Second Sight Software...</div>";
			str+="</li>";
			str+="<li>Step2: ";
			str +="Once completed, just refresh this page to activate the emulator</p>";
			str +="</li>";
	}
	else
	if (BrowserDetect.browser == "Firefox")
	{
			str += "<p>If the automatic installation process from Firefox did not work,<br />click <a href=\""+this.instanceParam.installPath+"/"+codebase+"\"><font color=\"white\"><u>HERE</u></font></a> to manually download the plugin.</p>";
			str += "<p>Once downloaded, press on the [Install Now] button that will be enabled (after 3seconds !)</br>in the next window to complete the installation process.</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+": 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/&lt;username>/AppData/Local/Google/Chrome/Application/plugins <br /> you might need to create the plugins/ directory if it does not exist yet)";
			str += "</p>";
	}
	else
	{
			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>";
	str += "</object>";

	document.write(str);
		
	// check for obsoleted versions
	var h = document.getElementById(this.instanceParam.ctrlid);
	if (h && typeof h.Slot51 != 'undefined')
	{
		if (typeof h.Command =='undefined')
				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");
	}
 },
 
 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"
		},
		{
			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"
		}
	]

};

