
// public constants
JustaAppletObject.prototype.CANCELED = "canceled";
JustaAppletObject.prototype.INIT_FAILED = "init_failed";
JustaAppletObject.prototype.INTERNAL_ERROR = "internal_error";
JustaAppletObject.prototype.NORMAL_EXIT = "normal_exit";
JustaAppletObject.prototype.ABNORMAL_EXIT = "abnormal_exit";

// public fields
JustaAppletObject.prototype.ID = null;
JustaAppletObject.prototype.browser = new Browser();

// private fields
JustaAppletObject.prototype.javaArchive = "";
JustaAppletObject.prototype.javaCode = "";
JustaAppletObject.prototype.paramButtonValue = "";
JustaAppletObject.prototype.params = new Array();
JustaAppletObject.prototype.paramJSAppletObjectValue = "";
JustaAppletObject.prototype.width = "120";
JustaAppletObject.prototype.height = "25";
JustaAppletObject.prototype.special = "";

// private constants
JustaAppletObject.prototype.OBJECT_CLASSID
 = "clsid:CAFEEFAC-0015-0000-0006-ABCDEFFEDCBA";
// = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93";
JustaAppletObject.prototype.OBJECT_CODEBASE = 
 "http://justa.ssc.lt/JRE/"
 + "jre-1_5_0_06-windows-i586-p-iftw.exe#Version=1,5,0,6";
JustaAppletObject.prototype.OBJECT_TYPE
 = "application/x-java-applet;version=1.5.0.6";
JustaAppletObject.prototype.EMBED_TYPE
 = "application/x-java-applet;version=1.5";
//	http://plugins.netscape.com/plug-in_finder.adp?mimetype=application/x-java-vm
JustaAppletObject.prototype.EMBED_PLUGINSPAGE
 = "https://justa.ssc.lt/web/resources/plugins/index.php";
JustaAppletObject.prototype.JAVA_CODEBASE
 = "https://justa.ssc.lt/";
JustaAppletObject.prototype.PARAM_BUTTON_NAME = "main_button_title";
JustaAppletObject.prototype.PARAM_JS_APPLET_OBJECT_NAME = "js_applet_object";

// Protected functions
JustaAppletObject.prototype.init = function ( jArchive, jCode ) {
	this.javaArchive = jArchive;
	this.javaCode = jCode;
	this.ID = Math.floor( Math.random()* Math.pow( 10, 16 ) ).toString();
}

JustaAppletObject.prototype.setCodeBase = function ( base ) {
	this.JAVA_CODEBASE = base;
}

// Public functions
/**
 * @desc Return correct HTML applet code corresponding
 *		to user browser type and version.
 **/
JustaAppletObject.prototype.toHTML = function ( ) {	

	var appletHTMLRepresentation = "";
	type = ( arguments.length > 0 ) ? arguments[0] : '';

	 if( this.browser.winNT_40 || type == 'applet' ) {
		appletHTMLRepresentation = "<APPLET"
		 + " CODEBASE=\""+ this.JAVA_CODEBASE +"\""
		 + " archive=\""+ this.javaArchive +"\""
		 + " scriptable"
		 + " code=\""+ this.javaCode +"\""
		 + " ID=\""+ this.ID +"\""
		 + " style=\"width:"+ this.width +"; height:" + this.height +"\""
		 + " "+ this.special + ">"
		 
		 + "<PARAM NAME=\""+ this.PARAM_BUTTON_NAME
		  +"\" VALUE=\""+ this.paramButtonValue +"\">"
		 + "<PARAM NAME=\""+ this.PARAM_JS_APPLET_OBJECT_NAME
		  +"\" VALUE=\""+ this.paramJSAppletObjectValue +"\">";
		
		var c = this.params.length;
		for ( var i=0; i<c; i++ ) {
			appletHTMLRepresentation += "<PARAM NAME=\""
			 + this.params[i][0] +"\" VALUE=\""+ this.params[i][1] +"\">";
		}
		
		appletHTMLRepresentation += "</applet>";

	} else if ( this.browser.mz || this.browser.op
	 || this.browser.ns8 || type == 'embed' ) {
		appletHTMLRepresentation = "<EMBED"
		 + " TYPE=\""+ this.EMBED_TYPE +"\""
		 + " WIDTH=\""+ this.width +"\""
		 + " HEIGHT=\""+ this.height +"\""
		 + " ID=\""+ this.ID +"\""
		 + " "+ this.special
		 + " CODE=\""+ this.javaCode +"\""
		 + " CODEBASE=\""+ this.JAVA_CODEBASE +"\""
		 + " ARCHIVE=\""+ this.javaArchive +"\""
		 + " "+ this.PARAM_BUTTON_NAME +"=\""+ this.paramButtonValue +"\""
		 + " "+ this.PARAM_JS_APPLET_OBJECT_NAME +"=\""
		  + this.paramJSAppletObjectValue +"\"";
		
		var c = this.params.length;
		for ( var i=0; i<c; i++ ) {
			appletHTMLRepresentation += " "
			 +this.params[i][0] +"=\""+ this.params[i][1] +"\"";
		}
		
		appletHTMLRepresentation += " MAYSCRIPT=\"true\""
		 + " PLUGINSPAGE=\""+ this.EMBED_PLUGINSPAGE +"\""
		 + " />";

	} else if ( this.browser.ie || type == 'object' ) {
		appletHTMLRepresentation = "<OBJECT"
		 + " CLASSID=\""+ this.OBJECT_CLASSID +"\""
		 + " CODEBASE=\""+ this.OBJECT_CODEBASE +"\""
		 + " ID=\""+ this.ID +"\""
		 + " style=\"width:"+ this.width +"; height:" + this.height +"\""
		 + " "+ this.special + ">"
		 + "<PARAM NAME=\"type\" VALUE=\""+ this.OBJECT_TYPE +"\">"
		 + "<PARAM NAME=\"codebase\" VALUE=\""+ this.JAVA_CODEBASE +"\">"
		 + "<PARAM NAME=\"archive\" VALUE=\""+ this.javaArchive +"\">"
		 + "<PARAM NAME=\"code\" VALUE=\""+ this.javaCode +"\">"
		 + "<PARAM NAME=\"scriptable\" VALUE=\"true\">"
		 + "<PARAM NAME=\""+ this.PARAM_BUTTON_NAME
		  +"\" VALUE=\""+ this.paramButtonValue +"\">"
		 + "<PARAM NAME=\""+ this.PARAM_JS_APPLET_OBJECT_NAME
		  +"\" VALUE=\""+ this.paramJSAppletObjectValue +"\">"
		 		 		
		var c = this.params.length;
		for ( var i=0; i<c; i++ ) {
			appletHTMLRepresentation += "<PARAM NAME=\""
			 + this.params[i][0] +"\" VALUE=\""+ this.params[i][1] +"\">";
		}
		
		appletHTMLRepresentation += "</OBJECT>";

	} else {
		appletHTMLRepresentation = "<hr />Currently we has check how"
		+ " this code work only under IE 5.5+, Opera 7.54 and"
		+ " Mozilla 1.7+<br /> So, try to use one of them.<hr />";
	}
	return appletHTMLRepresentation;
}

/**
 * @desc Apply visibility:hidden style to the applet tag.
 **/
JustaAppletObject.prototype.hide = function () {
	try {
		document.getElementById( this.ID ).style.visibility = 'hidden';
	} catch( e ) { alert( e.message ); }
}

/**
 * @desc Apply visibility:visible style to the applet tag.
 **/
JustaAppletObject.prototype.show = function () {
	try {
		document.getElementById( this.ID ).style.visibility = 'visible';
	} catch( e ) { alert( e.message ); }
}

/**
 * @desc Use this function to add applet tag attributes,
 *		like: align, title and so on, not defined by default.
 **/
JustaAppletObject.prototype.setSpecial = function ( spec ) {
	this.special = spec;
}

/**
 * @desc Use it to define applet width and height. 
 * @param int w applet width.
 * @param int h applet height.
 * @see resize();
 **/
JustaAppletObject.prototype.setSize = function ( w, h ) {
	this.width = w;
	this.height = h;
}

/**
 * @desc Use it to resize an applet placed on page. To set size an applet
 *		must be resized to use setSize();
 * @see setSize();
 **/
JustaAppletObject.prototype.resize = function () {
	var applet = document.getElementById( this.ID );
	if( applet != null ) {
		applet.style.width = this.width;
		applet.style.height = this.height;
	}
}

/**
 * @desc Sets text must be placed on main applet button.
 *		Note: Button text must be defined before call toHTML().
 * @param String text applet main button text.
 **/
JustaAppletObject.prototype.setMainButtonText = function ( text ) {
	this.paramButtonValue = text;
}

/**
 * @desc Sets variable name, main communication functions be
 *		accessed through.
 * @param String name communication object holding variable name.
 **/
JustaAppletObject.prototype.setJSAppletObjectName = function ( name ) {
	if( name == "" ) { 
		alert( "Variable, holder of applet Object can't be empty!" );
		return;
	}
	this.paramJSAppletObjectValue = name;
}

/**
 * @desc Becuase not always possible return js function result into Java
 *		use this function instead.
 * @param String result.
 **/
JustaAppletObject.prototype.setCallResult = function ( result ) {
	try {
		document.getElementById( this.ID ).setCallResult( result );
	} catch( e ) {
		alert( e.message );
	}
}

/**
 * @desc Becuase not always possible get Java function from js,
 *		use this function to do that.
 **/
JustaAppletObject.prototype.getCallResult = function () {
	try {
		return document.getElementById( this.ID ).getCallResult();
	} catch( e ) {
		alert( e.message );
	}
	return null;
}

/**
 * @desc 
 *		
 **/
JustaAppletObject.prototype.setParam = function ( n, v ) {
	var c = this.params.length;
	var isAre = false;
	for ( var i=0; i<c; i++ ) {
		if( this.params[i][0] == n ) {
			this.params[i][1] = v;
			isAre = true;
			break;
		}
	}
	if( !isAre ) {
		this.params[ this.params.length ] = new Array( n, v );		
	}
	return null;
}

/**
 * @desc Justa Applets main class. Holds main options to create HTML 
 *		representing code to display applet correctly in different browsers.
 **/
function JustaAppletObject( ) {
	this.ID = Math.floor( Math.random()* Math.pow( 10, 16 ) ).toString();
	this.setSize( "120", "25" );
}
