//////////////////////////////////////////////////////////////////////////
//  ACL - Aplet Compatibility Layer
//  Purpose : Platworm independent Aplet Deployment
//  Author : Aleksander Łoś 
//  Date:25. lutego 2003, 10:55:04
//  Copyright: not decided yet, some kind of OpenSource license       
//////////////////////////////////////////////////////////////////////////







//////////////////////////////////////////////////////////////////////////
// Constants
//////////////////////////////////////////////////////////////////////////

// toggle debugging
var DEBUG=false;

//////////////////////////////////////////////////////////////////////////
//                          Applet Object
// Purpose: Holds information about applet 
//          Code, Archiwe, Codebase, Parameters, JVM version requaiered
//////////////////////////////////////////////////////////////////////////


// creates new applet
function   Applet( 
		   			  code, // Applet class eg "mypackage/MyApplet"
					  width, // dimensions
					  height, 					   
					  codebase, // this is not OBJECT tag codebase, this sis aplet 
					  			// base from where additional resources are downloaded
					  archive, // aptional archive eg "MyJar.jar"								
					  params // parameters of  applet (object) 
					)
{
  this.code = code;
  if ( archive != null && archive != "" )
  	 this.archive = archive;
  if ( codebase != null && codebase != "" )
  	 this.codebase = codebase;
  else this.codebase=".";
  this.width=width;
  this.height=height;
  this.params = params;
  if ( !this.params )
   this.params={}; 
  return this; 
}



////////////////////////////////////////////////////////////////////////
// AppletDeployer - main class
// 
//  HowTo - create applet ( a ) create d=new AppletDeployer(a)
//          call d.deploy(); taraaaa !!! aplet is ready, everybady happy.  
//



function AppletDeployer( applet ){
  /////////////////////////////////////////////////////////////////////
  // Remember to always add new renderers ( for new browser version )
  // at the end of this list, so it always find the best way  
  this.renderers=
   [
      new APPLET_Renderer()
	 ,new MSIE_Renderer()
	 ,new MRJ_Renderer()
	 ,new MOZILLA_Renderer()
   ];
       
  this.applet = applet;
  // main function
  this.deploy = function(){
  
   // if there is no method selected in url 
   // ( eg. http://myserver/mypage?2 ) selects 2'nd renderer in renderers
   // array   
    if ( !location.search ){   
     var rendered = false;
	 var its = new Its();
     // starting from end of the array
     for ( var i=this.renderers.length-1;i>=0; i-- )
	    if ( this.renderers[i].fits(its) ){
		   writeText(this.renderers[i].render( this.applet ));
		   rendered = true;
		   break;
		}
	 // display shrot message that applet couldn't been rendered
	 if ( !rendered ) 
	  document.write("<font color=\"red\">Applet rendering failed</FONT>");
     
	 document.write( "<P><b><font color=\"#666666\" face=\"Verdana\" size=\"1\">If the game does not appear, try one of these links below:</fonts></b></P>\n");
	 document.write("<UL>\n");
     for ( var i=this.renderers.length-1;i>=0; i-- )
	 	document.write("<LI><A HREF=\""+location+"?"+i +"\">"+this.renderers[i].decription()+"</A>" ); 	  	   
     document.write("</UL>\n");	      
  }// if ( !location.query ...
  else{ 
    var indx=parseInt(location.search.substr(1));
	if ( indx+1 )
      writeText( this.renderers[indx].render( this.applet ) ); 
  }
 }// end of this.deploy()
}


////////////////////////////////////////////////////////////////////////
// Advanced Browser Sniffer based on  Apple sample code
// Purpose:Properties of this object will be passed to test applet
// 		   and from test applet to Prolog engine as facts.
//         Prolog engine will discover wchich method of applet 
//         embedding to use and pass the name of this method 
//         back to html page ( in fat url )
// 

function Its() {
  var n = navigator;
  // string comparisons are much easier if we lowercase everything now.
  // to make indexOf() tests more compact/readable, we prepend a space 
  // to the userAgent string (to get around '-1' indexOf() comparison)
  var ua = ' ' + n.userAgent.toLowerCase();
  var pl = n.platform.toLowerCase(); // not supported in NS3.0
  var an = n.appName.toLowerCase();

  // browser version
  this.version = n.appVersion;
  
  this.nn = ua.indexOf('mozilla') > 0;

  // 'compatible' versions of mozilla aren't navigator
  if(ua.indexOf('compatible') > 0) {
    this.nn = false;
  }

  this.opera = ua.indexOf('opera') > 0;
  this.webtv = ua.indexOf('webtv') > 0;
  this.ie = ua.indexOf('msie') > 0;
  this.aol = ua.indexOf('aol') > 0;
  this.omniweb = ua.indexOf('omniweb') > 0;
  this.galeon = ua.indexOf('galeon') > 0;
  
  this.major = parseInt( this.version );
  this.minor = parseFloat( this.version );
  
  // platform
  this.mac = ua.indexOf('mac') > 0;
  this.mac68k = (ua.indexOf('68k') > 0 || ua.indexOf('68000') > 0);
  this.macppc = (ua.indexOf('ppc') > 0 || ua.indexOf('powerpc') > 0);

  this.win = ua.indexOf('win') > 0;
  this.win16 = (ua.indexOf('16') > 0 && ua.indexOf('win') > 0);
  this.win31 = this.win16;
  this.win32 = (!this.win16 && this.win );
  this.win95 = (ua.indexOf('95') > 0 && ua.indexOf('win') > 0);
  this.win98 = (ua.indexOf('98') > 0 && ua.indexOf('win') > 0);
  this.winme = (ua.indexOf('win 9x 4.90') > 0 && ua.indexOf('win') > 0);
  this.winnt = (ua.indexOf('nt') > 0 && ua.indexOf('win') > 0);
  this.win2k = (ua.indexOf('nt 5') > 0 && ua.indexOf('win') > 0);

  this.os2 = ua.indexOf('os/2') > 0;

  this.sun = ua.indexOf('sunos') > 0;
  this.irix = ua.indexOf('irix') > 0;
  this.hpux = ua.indexOf('hpux') > 0;
  this.aix = ua.indexOf('aix') > 0;
  this.dec = (ua.indexOf('dec') > 0 || ua.indexOf('alpha') > 0 || ua.indexOf('osf1') > 0 || ua.indexOf('ultrix') > 0);
  this.sco = (ua.indexOf('sco') > 0 || ua.indexOf('unix_sv') > 0);
  this.vms = (ua.indexOf('vax') > 0 || ua.indexOf('openvms') > 0);
  this.linux = ua.indexOf('linux') > 0;
  this.sinix = ua.indexOf('sinix') > 0;
  this.reliant = ua.indexOf('reliantunix') > 0;
  this.freebsd = ua.indexOf('freebsd') > 0;
  this.openbsd = ua.indexOf('openbsd') > 0;
  this.netbsd = ua.indexOf('netbsd') > 0;
  this.bsd = ua.indexOf('bsd') > 0;
  this.unixware = ua.indexOf('unix_system_v') > 0;
  this.mpras = ua.indexOf('ncr') > 0;

  this.unix = ua.indexOf("x11") > 0;

  // workarounds
  // - IE5/Mac reports itself as version 4.0
  if(this.ie && this.mac) {
    if(ua.indexOf("msie 5")) {
      this.major = 5;
      var actual_index = ua.indexOf("msie 5");
      var actual_major = ua.substring(actual_index + 5, actual_index + 8);
      this.minor = parseFloat(actual_major);
    }
  }
  
  // any java vm is on 
  this.java = navigator.javaEnabled();
  
  // if javaplugin is on ????
  // it doesn't work on IE since navigator.plugins is always empty 
  // in IE. But it doesn't matter. On IE/Windows there will be 
  // automatic download, install feature when using javaplugin      
  this.javaplugin=false;
  
  for ( var i=0; i<navigator.plugins.length; i++ )
    if ( navigator.plugins[i].name.toUpperCase().indexOf('JAVA')>-1 ){
	   this.javaplugin=true;	   
	   break;
	}
	   
  return this;
}



/////////////////////////////////////////////////////////////////////////////
// Definitions of Applet Renderers
// Aplet renderer is JavaScript Object with
// following methods:
//                   boolean fit(its)
//                   string    render( applet )
//
// "fits" function shold check if method of applet deployment fits 
// browser, it do this by checking members of Its object
// "render" returns string representing html code to deploy object 





////////////////////////////////////////////////////////////////////////////
// Rendering with <APPLET ></APPLET>
// Its default renderer, remember to use it on the end
function  APPLET_Renderer(){
  this.render = function( applet ){
  
    			var  text = "<!-- default <APPLET>  applet -->\n";
   			  //  text += "<H5>Warning Your Browser seems to be Java incompatible</H5>\n";  
 			    // rendering opening tag
			    text += "<APPLET CODE=\""+applet.code+"\" ";
				if ( applet.archive )
      			   text += "ARCHIVE=\""+applet.archive+"\" ";
  				if ( applet.codebase )
      			   text += "CODEBASE=\""+applet.codebase+"\" ";
  				text += " WIDTH="+applet.width+" HEIGHT="+applet.height ;
  				text += ">\n";  
  				// rendering parameters
				text+=renderParams1( applet.params );
  				// closing tag 
  				text += "</APPLET>\n";
  				return text ; 
			}
  this.fits=function( its ){
    return its.java;
  }	
  this.decription=function(){
    return "Plain APPLET tag";
  }
}



//////////////////////////////////////////////////////////////////////////
// MSIE Object Renderer

function  MSIE_Renderer(){


  this.render = function( applet ){
  			 var text = "<!-- MSIE/Win32 browsers applet -->\n";
		  	 text += "<object classid=\"clsid:8AD9C840-044E-11D1-B3E9-00805F499D93\"\n";
		  	 text += "codebase=\"http://java.sun.com/products/plugin/autodl/jinstall-1_4_1-windows-i586.cab#version=1,4,1\"\n";
 		  	 text += " WIDTH="+applet.width+" HEIGHT="+applet.height+">\n";		  

			  ////////////////////////////////////////////////////////////////
			  // rest of applet properties is rendered as ordinary parameters
			  applet.params.code = applet.code;
			  applet.params.codebase = applet.codebase;
			  if ( applet.archive )
				  applet.params.archive = applet.archive;

  			  text += renderParams1( applet.params );
			  text+="<p>You need the Java Plugin."+
				         "Get it from <a href=\"http://java.sun.com/products/plugin/index.html\">here.</a></p>";			  			  
  			  text+="</object>"; 
  			  return text;  			  		
	  }
  this.fits=function( its ){
  		    return its.java && its.ie && its.win32 && its.major >= 4;
	  }	
  this.decription=function(){
    return "Microsoft Internet Explorer (Version 6+)";
  }
}

////////////////////////////////////////////////////////////////////////////////
// Mozilla based browser renderer ( not MRJ on MAC  )

function  MOZILLA_Renderer(){
  this.render = function( applet ){
  			  var text = "<!-- MOZILLA based browsers applet -->\n";	 			  
    		  text +=  "<object\n";
			  text += "\ttype=\"application/x-java-applet;jpi-version=1.4.1_01\"\n";			  
			  text += "\twidth=\""+applet.width+"\"\n";
  			  text += "\theight=\""+applet.height+"\">\n";
			  
			  ////////////////////////////////////////////////////////////////
			  // rest of applet properties is rendered as ordinary parameters
			  applet.params.code = applet.code;
			  applet.params.codebase = applet.codebase;
			  if ( applet.archive )
			  	  applet.params.archive = applet.archive;
			  
			  // rendering params
			  text+=renderParams1( applet.params );
			  text+="<p>You need the Java Plugin."+
				         "Get it from <a href=\"http://java.sun.com/products/plugin/index.html\">here.</a></p>";			  			  
  			  text+="</object>"; 
  			  return text;  
	  }
  this.fits=function( its ){
  		    return  its.nn && !its.mac;
	  }
  this.decription=function(){
    return "Mozilla based browsers (i.e., Mozilla-FireFox, Netscape, Opera, Galeon)";
  }	
}



//////////////////////////////////////////////////////////////////////////////// 
// Renders Applet for MRJ Plugin ( application/x-java-vm )

function  MRJ_Renderer(){
  this.render = function( applet ){
    		  text =  "<EMBED\n";
			  text += "\tTYPE=\"application/x-java-vm\"\n";
  			  text += "\tPLUGINSPAGE=\"http://www.mozilla.org/oji/\"\n";
  			  text += "\tBORDER=\"5\"\n";
			  text += "\tWIDTH=\""+applet.width+"\"\n";
  			  text += "\tHEIGHT=\""+applet.height+"\"\n";
  			  if ( applet.archive )
			     text += "\tARCHIVE=\""+applet.archive+"\"\n";
  			  if ( applet.codebase )
			     text += "\tCODEBASE=\""+applet.codebase+"\"\n";
			  applet.params.code = applet.code; 
  			  // rendering parameters
			  text+=renderParams2(applet.params);
  			  text+=">"; 
  			  return text;  
	  }
  this.fits=function( its ){
  		    return its.java && its.mac && its.nn;
	  }
  this.decription=function(){
    return "MRJ Plugin for the Mac";
  }	
}


 







////////////////////////////////////////////////////////////////////
// Utility funcions
////////////////////////////////////////////////////////////////////


// renders parameters as <PARAM NAME="..." VALUE="..."> tags
function renderParams1( params ){
  var text="";
	for ( var i in params )
 	 text += "\t<PARAM NAME="+i+" VALUE=\""+params[i]+"\">\n" ;
  return text;
}

// renders params as  param_name=param_value
function renderParams2( params ){
  var text="";
	for ( var i in params )
 	 text += "\t"+i+"="+"\""+params[i]+"\""+"\n" ;
  return text;
}



// writeText: Wrapper around document.write
// Purpose  : Allows Debbuging
function writeText ( 
		 		  	aText // text to wrie
				   )
{
 if ( DEBUG )
  document.write("<FORM TARGET=\"_blank\">"+
  "<TEXTAREA NAME=\"RendererDebugText\" COLS=\"60\" ROWS=\"15\" WRAP=\"OFF\" READONLY>" );

 document.write ( aText );
 
 if ( DEBUG )
  document.write(" </TEXTAREA></FORM> " );
 
} 


//////////////////////////////////////////////////////////////////////
// testBrowser : writes browser characteristics 
// Purpose: Allows us Display HTML decribing browser 
//          if something goes wrong. User can feedback us this 
//          description

function testBrowser(){
  var its = new Its();
  var text="";
 for ( var i in its )
   text += "<BR>its."+i+"=<b>"+its[i]+"</b>\n";
  document.write(text);
}
