/***************************************************************\
| |\  /|                                                We Put  |
| | >< Hypercosm       hc_sketchup_interface.js         3d      |
| |/  \|                                                To Work |
|***************************************************************|
|                                                               |
|        This file defines a Javascript API for user            |
|        interfaces used to control SketchUp applets.           |
|                                                               |
|        This defines an "abstract" interface.  Specific        |
|        types of SketchUp applet interfaces are created        |
|        by extending this class.                               |
|                                                               |
|***************************************************************|
|                Copyright (c) 2007 Hypercosm, LLC.             |
\***************************************************************/


//
// "class" constructor
//


function HCSketchUpInterface(element, appletSrc, appletResources, commandLine) {
  if (!element)
    return this;

  // set optional parameter defaults
  //
  if (appletResources == undefined)
    appletResources = null;
  if (commandLine == undefined)
    commandLine = "";

  // add wait command to allow the user interface to parse
  // the query string before starting to display the applet
  //
  if (commandLine == "")
    commandLine = "-wait_for_messages";
  else
    commandLine = "-wait_for_messages; " + commandLine;

  // set attributes
  //
  this.element = element;
  this.appletSrc = appletSrc;
  this.appletResources = appletResources;
  this.commandLine = commandLine;
  this.windowBaseLocation = getBaseLocation(getWindowLocation());

  // store reference to user interface
  //
  HCSketchUpInterface.interfaceArray[HCSketchUpInterface.interfaceCount] = this;
  HCSketchUpInterface.interfaceIndexArray[element] = HCSketchUpInterface.interfaceCount;
  HCSketchUpInterface.interfaceCount++;
  
  // set callback for after window loads
  //
  var appletInterface = this;
  addEvent(window, "load", function() {
									
    // correct PNG images for IE5.5 and IE6
    //
    if (isDefined("correctPNG"))
      correctPNG();
	  
    // load user interface elements
    //
    appletInterface.onLoad();
	
    // create new applet after interface is loaded
    //
    appletInterface.applet = new HCSketchUpApplet("applet", appletInterface.appletSrc,
      appletInterface.appletResources, appletInterface.commandLine, appletInterface);
  });
 
  return this;
}    // HCSketchUpInterface


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


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


HCSketchUpInterface.prototype.onLoad = function() {  
  
  // call superclass constructor
  //
  container.call(this, this.element);

  // finish loading
  //
  this.loaded = false;
}    // onLoad


HCSketchUpInterface.prototype.onActivate = function() {  
  if (this.activated)
    return;

  this.activated = true;
  
  // activate toolbars
  //
  this.componentGroup.call("onActivate");
  
  // initialize camera attributes
  //
  this.initialCamera = this.applet.camera.duplicate();
  
  // initialize user interface settings
  //
  this.initialize();
}    // onActivate


HCSketchUpInterface.prototype.initialize = function() {  
  
  // activate toolbars
  //
  this.componentGroup.call("initialize");

  // set the state of the applet using the query string portion of the URL
  //
  var queryString = getQueryString();
  if (queryString) {
    var applet = getQueryVariable(queryString, "applet");
    if ((applet == undefined) || (parseInt(applet) == this.applet.index))
      this.parseQueryString(getQueryString());
  }
  
  this.initialized = true;
}    // onInitialize

  
//
// query string methods
//


HCSketchUpInterface.prototype.parseQueryString = function(queryString) {
  if (!queryString)
    return;
	
  // allow each user interface component to parse the query string
  //
  for (var counter = 0; counter < this.componentGroup.numComponents; counter++)
    if (this.componentGroup.componentArray[counter].parseQueryString)
	  this.componentGroup.componentArray[counter].parseQueryString(queryString);
  
  // parse camera query string
  //
  var camera = new HCSketchUpCamera();
  camera.parseQueryString(queryString);
  this.applet.setCamera(camera);
}    // parseQueryString


HCSketchUpInterface.prototype.getQueryString = function() {
  var queryString = null;

  if (HCApplet.appletCount > 1)
    queryString = addQueryString(queryString, "applet=" + this.applet.index);
	
  // allow each user interface component to add to the query string
  //
  for (var counter = 0; counter < this.componentGroup.numComponents; counter++)
    if (this.componentGroup.componentArray[counter].getQueryString)
      queryString = addQueryString(queryString, this.componentGroup.componentArray[counter].getQueryString());
	  
  // add camera information to query string
  //
  if (this.applet.view == undefined)
    if (!this.applet.camera.equals(this.initialCamera))
      queryString = addQueryString(queryString, this.applet.camera.getQueryString());
	
  return queryString;
}    // getQueryString


HCSketchUpInterface.prototype.setQueryString = function(queryString) {
  if (queryString)
    setWindowLocation(this.windowBaseLocation + "?" + queryString);
  else
    setWindowLocation(this.windowBaseLocation);
}    // setQueryString


//
// "class" or "static" methods
//


HCSketchUpInterface.getInterfaceById = function(id) {
  return this.getInterfaceByIndex(HCSketchUpInterface.interfaceIndexArray[id]);
}	// getInterfaceById


HCSketchUpInterface.getInterfaceByIndex = function(index) {
  return HCSketchUpInterface.interfaceArray[index];
}	// getInterfaceByIndex


//
// "class" or "static" member variables
//


HCSketchUpInterface.interfaceCount = 0;
HCSketchUpInterface.interfaceArray = new Array();
HCSketchUpInterface.interfaceIndexArray = new Array();

