/** Copyright (c) 1997 - 2009 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.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() { 
		return this.toApplet();
	 };
		
	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.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 += "<br><p style='color:white;'>Per la corretta visualizzazione della pagina scaricare la Virtual Machine dal sito " +
		"<a style='color:white;' href='http://www.java.com'>www.java.com</a></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;
	}
}


