/***************************************************************\
| |\  /|                                                We Put  |
| | >< Hypercosm           user_interface.js            3d      |
| |/  \|                                                To Work |
|***************************************************************|
|                                                               |
|        This file defines the top level user interface         |
|        for the COLBERT treadmill trainer.                     |
|                                                               |
|***************************************************************|
|                Copyright (c) 2007 Hypercosm, LLC.             |
\***************************************************************/


//
// "class" constructor
//


function userInterface(element, commandLine) {
  if (!element)
    return this;

  // call superclass method
  //
  container.call(this, element); 

  // set attributes
  //
  this.element = element;
  this.commandLine = commandLine;
  this.applet = undefined;
  this.step = undefined;
  this.substep = undefined;
  
  // create reset camera button
  //
  this.resetCameraButton = getElementById("resetCameraButton", this.element);
  var self = this;
  this.resetCameraButton.onclick = function() {self.resetCamera()};
 
  return this;
}    // userInterface


// inherit prototype from "superclass"
//
userInterface.prototype = new container();


//
// "object" or "instance" methods
//


userInterface.prototype.getAppletName = function(step) {
  var appletName = "step";
  
  // add step number to applet base name
  //
  if (step < 10)
    appletName += "0" + step;
  else
    appletName += step;
  
  // add file extension to applet name
  //
  return appletName + ".hcvm";
}	// getAppletName


userInterface.prototype.setStep = function(step, substep) {
  if (step == undefined || substep == undefined)
    return;
	
  // load new applet and set to step
  //
  if (this.step != step || !this.applet) {
    var commandLine = "step " + substep;
	
	// get previous node
	//
	this.previousNode = document.getElementById("applet");
	
    // create new applet instance
    //
    this.applet = new HCScriptableApplet("applet", "applets/" + this.getAppletName(step), [], commandLine);
    
    // update current step and substep
    //
    this.step = step;
    this.substep = substep;
  } else {

    // set state of current applet to selected step
    //
    if (substep != undefined)  {
      if (this.applet)
        this.applet.sendMessage("step " + substep);
	  
      // update current substep
      //
      this.substep = substep;
    }
  }
}	// setStep


userInterface.prototype.clearApplet = function() {
	
  // replace applet
  //
  if (this.previousNode && this.applet) {
    var element = document.getElementById(this.applet.id); 
    element.parentNode.replaceChild(this.previousNode, element);
	this.applet = null;
  }
}	// clearApplet


userInterface.prototype.resetCamera = function() {
  if (this.step != undefined && this.substep != undefined)
    this.setStep(this.step, this.substep);
}	// resetCamera
