/** Copyright (c) 1997 - 2006 Thinkmap, Inc. All Rights Reserved. 
 * 
 * This JavaScript library is useful for automatically embedding a Thinkmap Applet
 * into an HTML page.  It essentially creates a universal JavaScript API for
 * specifying the parameters to an applet, regardless of your browser 
 * configuration.
 * 
 * Basic Usage:
 * -----------------------------------------------------------------------------
 * To use it, first include this script in your HTML file as follows:
 *   <script type="text/javascript" src="applet.js"></script>
 *
 * Then, in a subsequent <script> tag, you can create an applet as follows:
 * <script type="text/javascript" >
 *    // construct a new Applet, which takes one parameter, the Applet's class name
 *    var applet = new ThinkmapApplet("thinkmap.context.ThinkmapApplet"); 
 *	  applet.codebase="lib" // set the applet codebase
 *	  applet.archive="thinkmapclient.jar";	// set the applet's archive attribute	
 *    applet.width=applet.height="100%";	// set the applet's height and width
 *
 *    // applet parameters are set using the "params" object
 *	  applet.params['config.url']="client_gui.xml"
 *	  applet.params['datasource.url']="server.tas";
 *	  applet.params['initial.search']="Breakfast Club";
 *
 *    // give the applet an id, so it can be found using the document.getElementById method
 *    applet.id = "myapplet";
 * </script>
 *
 * Next, to write the appropriate tag to your HTML file, insert the following where
 * you would typically put your <applet>,<embed>,or <object> tags:
 *
 * 	   <script type="text/javascript">applet.write()</script>	 
 *  
 **/
function ThinkmapApplet(code) {
	this.IEdownloadURL = "http://java.sun.com/update/1.5.0/jinstall-1_5_0_08-windows-i586.cab";
	this.pluginsPage = "http://java.sun.com/j2se/1.5.0/download.html";
	this.minVersion="1.3";	
	this.code = (arguments.length==0) ? "thinkmap.context.ThinkmapApplet" : code;
	this.mayscript = true;	
	this.width='100%';
	this.height='100%';	
		
	ThinkmapApplet.appletCount = (ThinkmapApplet.appletCount) ? ThinkmapApplet.appletCount+1 : 1;	
	this.id="__applet"+ThinkmapApplet.appletCount;

	this.codebase=null;
	this.name=null;
	this.params={};
	
	this.archive = null;
	var archives = [];
	
	this.mode = null;	
	
	this.write = function() {
		document.write(this);
	}
	
	this.toString = function() { 
		if (!this.mode) this.detect();
		switch (this.mode) {
			case 'applet': {
				return this.toApplet();
				break;
			}
			case 'object': {
				return this.toObject();
				break;
			}
			case 'embed': {
				return this.toEmbed();
				break;
			}
			default:
				throw new Error('unknown mode:'+this.mode);
		}
	 };
		
	this.addArchive = function( archive ) {
		archives[archives.length]=archive;
	}	
	
	this.attach = function( id ) {	
		var d = document.getElementById(id);
		if (!d) throw new Error('Element with id:'+id+' not found');		
		d.innerHTML=this.toString();
	}	
				
	this.toObject = function() {
		var ret = '<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" ';
		if (this.width && this.height) ret+=' width="'+this.width+'" height="'+this.height+'" ';		
		
		if (this.id)   			ret+=' id="'+this.id+'" ';
		if (this.name) 			ret+=' name="'+this.name+'" ';		
		if (this.IEdownloadURL) ret+=' codebase="'+this.IEdownloadURL+'" ';
		ret+='>';
		
		if (this.codebase) ret+='\n\t<param name="codebase" value="'+this.codebase+'">';
		
		ret+='\n\t<param name="code" value="'+this.code+'">';
		ret+='\n\t<param name="type" value="application/x-java-applet;jpi-version='+this.minVersion+'">'
		
		if (archives.length>0 || this.archive){			
			ret+='\n\t<param name="archive" value="'+this.getArchives()+'">';
		}
		
		if (this.mayscript) {
		  ret+= '\n\t<param name="MAYSCRIPT" value="true">';
		  ret+= '\n\t<param name="scriptable" value="true">';		  
		}		
			
		for (var p in this.params) {
			ret+='\n\t<param name="'+p+'" value="'+this.params[p]+'">';
		}
		ret+='\n</object>';
		return ret;
	}	
	this.toEmbed = function() {
		var ret ='\n\t<embed type="application/x-java-applet" code="'+this.code+'" ';
		if (this.codebase) ret+=' codebase="'+this.codebase+'" ';	
		if (this.width && this.height) ret+=' width="'+this.width+'" height="'+this.height+'" ';
		if (this.id) ret+=' id="'+this.id+'" ';
		if (this.name) ret+=' name="'+this.name+'" ';		
		
		if (archives.length>0 || this.archive){			
			ret+=' archive="'+this.getArchives()+'" ';
		}		
		for (var p in this.params) {
			ret+=' '+p+'="'+this.params[p]+'" ';
		}
		if (this.mayscript) {
			ret+=' MAYSCRIPT="true" ';
			ret+=' scriptable="true" ';
		}
		ret +='></embed>';
		return ret;
	}
	this.toApplet = function() {
		var ret ='<applet  code="'+this.code+'" ';
		if (this.codebase) ret+=' codebase="'+this.codebase+'" ';	
		if (this.width && this.height) ret+=' width="'+this.width+'" height="'+this.height+'" ';
		if (this.id) ret+=' id="'+this.id+'" ';
		if (this.name) ret+=' name="'+this.name+'" ';		
		if (this.mayscript) ret+=' MAYSCRIPT="true" ';
		if (archives.length>0 || this.archive){				
			ret+=' archive="'+this.getArchives()+'" ';
		}		
		ret+=">";
		for (var p in this.params) {
			ret+='\n\t<param name="'+p+'" value="'+this.params[p]+'">';
		}
		ret +='\n</applet>';
		return ret;
	}
	
	this.getArchives = function() {
		var ret = (this.archive) ? this.archive : "";		
		if (archives.length>0) {
			if (ret.length > 0) ret +="," 		
			ret+=archives.join(',');
		}		
		return ret;
	}
	
	this.detect = function() {				
		this.mode = 'applet';			
		try {
			if (window.ActiveXObject) { 				
				try {
					new ActiveXObject("JavaPlugin"); 
					this.mode = 'object';
				} catch (e) {					
				}
			} 
		} catch (e) {}
	}
}

