/***************************************************************\
| |\  /|                                                We Put  |
| | >< Hypercosm            hc_applet.js                3d      |
| |/  \|                                                To Work |
|***************************************************************|
|                                                               |
|        This file defines a Javascript function to add a       |
|        Hypercosm applet to a web page.                        |
|                                                               |
|        Although applets can be added to a web page without    |
|        using Javascript, this method is preferred because     |
|        following Microsoft's loss to the Eolas patent,        |
|        inline ActiveX objects now have to be clicked on to    |
|        be activated.  This javascript allows the objects      |
|        to become active as soon as the web page is loaded.    |
|                                                               |
|***************************************************************|
|                Copyright (c) 2007 Hypercosm, LLC.             |
\***************************************************************/


//
// "class" constructor
//


function HCApplet(element, appletSrc, resources, commandLine) {
  this.element = element;
  this.appletSrc = appletSrc;
  this.resources = resources;
  this.commandLine = commandLine;
  this.plugIn = null;
  this.activated = false;
  this.messages = "";
  this.bufferMessages = false;

  // This function binds a method to a particular
  // object for executing at a later time
  //
  function getClosure(objectName, methodName) {
    function execute() {
      objectName[methodName]();
    }
    return execute;
  }	// getClosure
  
  // activate ActiveX control
  //
  if (this.element != null)
    window.setTimeout(getClosure(this, "activate"), 500);
  
  return this;
}	// HCApplet


//
// "class" methods
//


HCApplet.prototype.activate = function() {
  this.element.innerHTML = this.getHTML();
  this.plugIn = document[this.element.id];
  this.activated = true;
  this.messageBuffer = "";
  this.bufferMessages = false;
}	// activate


//
// message passing methods
//


HCApplet.prototype.sendMessage = function(message) {
  if (this.verbose)
    setStatus(message);
  if (this.activated) {
    if (this.bufferMessages) {
	  if (this.messageBuffer != "")
	    this.messageBuffer = this.messageBuffer + ";";
	  this.messageBuffer = this.messageBuffer + message;
	} else
	  this.plugIn.SendMessage(message);
  }
}	// sendMessage


// 
// message buffering methods
//


HCApplet.prototype.beginMessages = function(message) {
  this.bufferMessages = true;
  this.messageBuffer = "";
}	// beginMessages


HCApplet.prototype.endMessages = function() {
  this.plugIn.SendMessage(this.messageBuffer);
  this.bufferMessages = false;
  this.messageBuffer = "";
}	// endMessages


//
// activation methods
//


HCApplet.prototype.getHTML = function() {
  
  // write object begin tag
  //
  var HTML = "<object id='" + this.element.id + "' width='" + this.element.style.width + "' height='" + this.element.style.height + "'\n";
  
  // write ActiveX insertion code for IE
  //  
  HTML += "classid='CLSID:34B8892A-9303-4893-9E12-1CEE6C3BF95D'>\n";
  HTML += "<param name='AppletSrc' value='" + this.appletSrc + "'>\n";

  // write out IE resources
  //
  HTML += "<param name='Resources' value=" + '"';
  for (var counter = 0; counter < this.resources.length; counter++) {
    HTML += "'" + this.resources[counter] + "';\n";
  }
  HTML += '">\n';
  
  // write out IE command line
  //
  if (this.commandLine != null)
    HTML += "<param name='CommandLine' value=\"" + this.commandLine + "\">\n";
  
  // write open embed tag code for Firefox, Netscape etc.
  //
  HTML += "<embed\n";
  HTML += "name='" + this.element.id + "'\n";
  HTML += "type='application/x-hypercosm'\n";
  HTML += "width='" + this.element.style.width + "' height='" + this.element.style.height + "'\n";
  HTML += "appletsrc='" + this.appletSrc + "'\n";
  
  // write out Firefox resources
  //
  HTML += "resources=" + '"';
  for (var counter = 0; counter < this.resources.length; counter++) {
    HTML += "'" + this.resources[counter] + "';\n";
  }
  HTML += '"\n';
  
  // write out Firefox command line
  //
  if (this.commandLine != null)
    HTML += "commandline=\"" + this.commandLine + "\"\n";
  
  // write close of embed tag
  //
  HTML += ">\n";
  
  // write object end tag
  //
  HTML += "</object>\n";
  
  return HTML;
}    // getHTML