/**
 * JavaObject v1.5: Java VM detection and embed - http://blog.deconcept.com/javaobject/
 *
 * JavaObject is (c) 2007 Kaj Kandler and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 */
if(typeof deconcept == "undefined") var deconcept = new Object();
if(typeof deconcept.util == "undefined") deconcept.util = new Object();
if(typeof deconcept.JavaObjectUtil == "undefined") deconcept.JavaObjectUtil = new Object();
deconcept.JavaObject = function(applet, id, w, h, ver, redirectUrl, detectKey) {
	if (!document.getElementById) { return; }
	this.DETECT_KEY = detectKey ? detectKey : 'detectjava';
	this.skipDetect = true; //deconcept.util.getRequestParameter(this.DETECT_KEY);
	this.params = new Object();
	this.variables = new Object();
	this.attributes = new Array();
	if(applet) { this.setAttribute('applet', applet); }
	if(id) { this.setAttribute('id', id); }
	if(w) { this.setAttribute('width', w); }
	if(h) { this.setAttribute('height', h); }
	if(ver) { this.setAttribute('version', new deconcept.JavaVersion(ver.toString().split("."))); }
	this.installedVer = deconcept.JavaObjectUtil.getJavaVersion();
	this.setAttribute('redirectUrl', '');
	if(redirectUrl) { this.setAttribute('redirectUrl', redirectUrl); }
}
deconcept.JavaObject.prototype = {
	setAttribute: function(name, value){
		this.attributes[name] = value;
	},
	getAttribute: function(name){
		return this.attributes[name];
	},
	addParam: function(name, value){
		this.params[name] = value;
	},
	getParams: function(){
		return this.params;
	},
	addVariable: function(name, value){
		this.variables[name] = value;
	},
	getJavaHTML: function() {
		var appletNode = "";
		if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) { // netscape plugin architecture
			appletNode = '<embed type="application/x-java-applet;version=1.4"'+
							+' code="'+ this.getAttribute('applet')+'"'
							+' width="'+ this.getAttribute('width')+'"'
							+' height="'+ this.getAttribute('height')+'"'
							+' style="'+ this.getAttribute('style') +'"'
							+' id="'+ this.getAttribute('id') 
							+' name="'+ this.getAttribute('id') +'"\n ';
			var params = this.getParams();
			for(var key in params) {
				appletNode += [key]+'="'+params[key]+'"\n ';
			}
			appletNode += '/>\n';
/* for debugging
			appletNode += '<!-- IE version - '
			appletNode += '<object id="'+ this.getAttribute('id') +'"'
							+' classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"'
							+' width="'+ this.getAttribute('width')+'"'
							+' height="'+ this.getAttribute('height')+'"'
							+' codebase="http://java.sun.com/products/plugin/autodl/jinstall-1_4-windows-i586.cab#Version=1,4,0,0"';
			appletNode += '<param name="code" value="'+ this.getAttribute('applet') +'" />\n';
			var params = this.getParams();
			for(var key in params) {
				appletNode += '<param name="'+ key +'" value="'+ params[key] +'" />\n';
			}
			//var pairs = this.getVariablePairs().join("&");
			//if(pairs.length > 0) {
			//	appletNode += '<param name="flashvars" value="'+ pairs +'" />';
			//}
			appletNode += '</object> -->';
*/
		} else { // PC IE
			appletNode = '<object id="'+ this.getAttribute('id') +'"'
							+' classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"'
							+' width="'+ this.getAttribute('width')+'"'
							+' height="'+ this.getAttribute('height')+'"'
							+' codebase="http://java.sun.com/products/plugin/autodl/jinstall-1_4-windows-i586.cab#Version=1,4,0,0"';
			appletNode += '<param name="code" value="'+ this.getAttribute('applet') +'.class" />';
			var params = this.getParams();
			for(var key in params) {
				appletNode += '<param name="'+ key +'" value="'+ params[key] +'" />';
			}
			appletNode += "</object>";
		}
		return appletNode;
	},
	write: function(elementId){
		if(this.skipDetect || this.getAttribute('doExpressInstall') || this.installedVer.versionIsValid(this.getAttribute('version'))){
			var n = (typeof elementId == 'string') ? document.getElementById(elementId) : elementId;
			n.innerHTML = this.getJavaHTML();
			return true;
		}else{
			if(this.getAttribute('redirectUrl') != "") {
				document.location.replace(this.getAttribute('redirectUrl'));
			}
		}
		return false;
	}
}

/* ---- detection functions ---- */
deconcept.JavaObjectUtil.getJavaVersion = function(){
	var JavaVersion = new deconcept.JavaVersion([0,0,0]);
	if(navigator.plugins && navigator.mimeTypes.length){
		var x = navigator.plugins["Java"];
		if(x && x.description) {
			JavaVersion = new deconcept.JavaVersion(x.description.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split("."));
		}
	}else if (navigator.userAgent && navigator.userAgent.indexOf("Windows CE") >= 0){ // if Windows CE
		var axo = 1;
		var counter = 3;
		while(axo) {
			try {
				counter++;
				axo = new ActiveXObject("Java.JVM."+ counter);
//				document.write("player v: "+ counter);
				JavaVersion = new deconcept.JavaVersion([counter,0,0]);
			} catch (e) {
				axo = null;
			}
		}
	} else { // Win IE (non mobile)
		// do minor version lookup in IE, but avoid fp6 crashing issues
		// see http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
		try{
			var axo = new ActiveXObject("Java.JVM.1");
		}catch(e){
			try {
				var axo = new ActiveXObject("Java.JVM.1");
				JavaVersion = new deconcept.JavaVersion([1,0,0]);
				axo.AllowScriptAccess = "always"; // error if player version < 6.0.47 (thanks to Michael Williams @ Adobe for this code)
			} catch(e) {
				if (JavaVersion.major == 6) {
					return JavaVersion;
				}
			}
			try {
				axo = new ActiveXObject("Java.JVM");
			} catch(e) {}
		}
		if (axo != null) {
			JavaVersion = new deconcept.JavaVersion(axo.GetVariable("$version").split(" ")[1].split(","));
		}
	}
	return JavaVersion;
}
deconcept.JavaVersion = function(arrVersion){
	this.major = arrVersion[0] != null ? parseInt(arrVersion[0]) : 0;
	this.minor = arrVersion[1] != null ? parseInt(arrVersion[1]) : 0;
	this.rev = arrVersion[2] != null ? parseInt(arrVersion[2]) : 0;
}
deconcept.JavaVersion.prototype.versionIsValid = function(fv){
	if(this.major < fv.major) return false;
	if(this.major > fv.major) return true;
	if(this.minor < fv.minor) return false;
	if(this.minor > fv.minor) return true;
	if(this.rev < fv.rev) return false;
	return true;
}
/* ---- get value of query string param ---- */
deconcept.util = {
	getRequestParameter: function(param) {
		var q = document.location.search || document.location.hash;
		if (param == null) { return q; }
		if(q) {
			var pairs = q.substring(1).split("&");
			for (var i=0; i < pairs.length; i++) {
				if (pairs[i].substring(0, pairs[i].indexOf("=")) == param) {
					return pairs[i].substring((pairs[i].indexOf("=")+1));
				}
			}
		}
		return "";
	}
}
/* add document.getElementById if needed (mobile IE < 5) */
if (!document.getElementById && document.all) { 
	document.getElementById = function(id) { 
	return document.all[id]; }
}
