/***************************************************************\
| |\  /|                                                We Put  |
| | >< Hypercosm         hc_sketchup_applet.js          3d      |
| |/  \|                                                To Work |
|***************************************************************|
|                                                               |
|        This file defines the Javascript interface to a        |
|        SketchUp Hypercosm applet.                             |
|                                                               |
|***************************************************************|
|                Copyright (c) 2007 Hypercosm, LLC.             |
\***************************************************************/


//
// "class" constructor
//


function HCSketchUpApplet(element, appletSrc, resources, commandLine) {
  this.element = element;
  this.appletSrc = appletSrc;
  this.resources = resources;
  this.commandLine = commandLine;
  this.activated = false;
  this.loaded = false;
  
  // set default zoom durations
  //
  this.zoomViewDuration = 0;
  this.zoomWindowDuration = 1;
  this.zoomPageDuration = 2;

  // 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;
}	// HCSketchUpApplet


// inherit prototype from "superclass"
//
HCSketchUpApplet.prototype = new HCApplet();


//
// "class" methods
//


HCSketchUpApplet.prototype.onLoad = function(script) {
  this.loaded = true;
  
  // run initialization script
  //
  if (script != null) {
    eval(script);
  }
}	// onLoad


//
// display mode changing methods
//


HCSketchUpApplet.prototype.setDisplayMode = function(displayMode) {
  if (!this.loaded)
    return;
  if (displayMode == "wireframe")
    this.sendMessage("-display_mode wireframe");
  else if (displayMode == "hidden_line")
    this.sendMessage("-display_mode hidden_line");
  else if (displayMode == "monochrome")
    this.sendMessage("-display_mode monochrome");
  else if (displayMode == "shaded")
    this.sendMessage("-display_mode shaded");
  else if (displayMode == "shaded_with_textures")
    this.sendMessage("-display_mode shaded_with_textures");
}    // setDisplayMode

 
HCSketchUpApplet.prototype.setXRayMode = function(xRayMode) {
  if (!this.loaded)
    return;
  if (xRayMode == true) 
    this.sendMessage("-x_ray true");
  else if (xRayMode == false)
    this.sendMessage("-x_ray false");
}    // setXRayMode


HCSketchUpApplet.prototype.setShowEdges = function(showEdges) {
  if (!this.loaded)
    return;
  if (showEdges == true) 
    this.sendMessage("-edges true");
  else if (showEdges == false)
    this.sendMessage("-edges false"); 
}    // setShowEdges
  
  
HCSketchUpApplet.prototype.setShowProfileEdges = function (showProfileEdges) {
  if (!this.loaded)
    return;
  if (showProfileEdges == true) 
    this.sendMessage("-profile_edges true");
  else if (showProfileEdges == false)
    this.sendMessage("-profile_edges false"); 
}    // setShowProfileEdges


//
// view changing methods
//


HCSketchUpApplet.prototype.setView = function(view) {
  if (!this.loaded)
    return;
  if (view == "iso")
    this.sendMessage("-view iso " + this.zoomViewDuration);
  else if (view == "top")
    this.sendMessage("-view top " + this.zoomViewDuration);
  else if (view == "front")
    this.sendMessage("-view front " + this.zoomViewDuration);
  else if (view == "right")
    this.sendMessage("-view right " + this.zoomViewDuration);
  else if (view == "back")
    this.sendMessage("-view back " + this.zoomViewDuration);
  else if (view == "left")
    this.sendMessage("-view left " + this.zoomViewDuration);
  else if (view == "previous")
    this.sendMessage("-view previous " + this.zoomViewDuration);
  else if (view == "auto")
    this.sendMessage("-view auto " + this.zoomWindowDuration);
}    // setView

 
HCSketchUpApplet.prototype.setPageIndex = function(pageIndex) {
  if (!this.loaded)
    return;
  this.sendMessage("-page " + pageIndex + " " + this.zoomPageDuration);
}    // setPage


HCSketchUpApplet.prototype.stopZoom = function() {
  if (!this.loaded)
    return;
  this.sendMessage("-stop");
}    // stopZoom


HCSketchUpApplet.prototype.setEye = function(eye) {
  if (!this.loaded)
    return;
  this.sendMessage("-eye " + eye.x + " " + eye.y + " " + eye.z);
}    // setEye


HCSketchUpApplet.prototype.setLookAt = function(lookAt) {
  if (!this.loaded)
    return;
  this.sendMessage("-lookat " + lookAt.x + " " + lookAt.y + " " + lookAt.z);
}    // setLookAt


HCSketchUpApplet.prototype.setFieldOfView = function(fieldOfView) {
  if (!this.loaded)
    return;
  this.sendMessage("-fov " + fieldOfView);
}    // setFieldOfView


//
// scene attribute changing methods
//


HCSketchUpApplet.prototype.setLayerVisibility = function(layer, visibility) {
  if (!this.loaded)
    return;
  this.sendMessage("-layer " + layer + " " + visibility);
}    // setLayerVisibility


HCSketchUpApplet.prototype.setAxes = function(showAxes) {
  if (!this.loaded)
    return;
  this.sendMessage("-axes " + showAxes);
}    // setAxes


HCSketchUpApplet.prototype.setShadows = function(shadows) {
  if (!this.loaded)
    return;
  this.sendMessage("-shadows " + shadows);
}    // setShadows


//
// mouse mode changing methods
//


HCSketchUpApplet.prototype.setMouseMode = function(button, mouseMode, cursor, callback) {
  if (!this.loaded)
    return;
  
  // get index of cursor icon
  //
  if (cursor != null)
    var index = cursor.index + 1;
  else
    var index = 0;

  // camera modes
  //
  if (mouseMode == "orbit") 
    this.sendMessage("-mouse_mode " + button + " orbit " + index);
  else if (mouseMode == "pan")
    this.sendMessage("-mouse_mode " + button + " pan " + index); 
  else if (mouseMode == "zoom")
    this.sendMessage("-mouse_mode " + button + " zoom " + index);
  else if (mouseMode == "zoom_fov")
    this.sendMessage("-mouse_mode " + button + " zoom_fov " + index);
  else if (mouseMode == "zoom_window")
    this.sendMessage("-mouse_mode " + button + " zoom_window " + index + " " + this.zoomWindowDuration); 
  else if (mouseMode == "zoom_window_fov")
    this.sendMessage("-mouse_mode " + button + " zoom_window_fov " + index + " " + this.zoomWindowDuration);
	
  // walkthrough modes
  //
  else if (mouseMode == "walk")
    this.sendMessage("-mouse_mode " + button + " walk " + index); 
  else if (mouseMode == "look_around")
    this.sendMessage("-mouse_mode " + button + " look_around " + index);
  else if (mouseMode == "position_camera")
    this.sendMessage("-mouse_mode " + button + " position_camera " + index);
	
  // construction modes
  //
  else if (mouseMode == "tape_measure")
    this.sendMessage("-mouse_mode " + button + " tape_measure " + index);
	
  // drawing modes
  //
  else if (mouseMode == "draw_line")
    this.sendMessage("-mouse_mode " + button + " draw_line " + index + " " + callback);
  else if (mouseMode == "freehand")
    this.sendMessage("-mouse_mode " + button + " freehand " + index + " " + callback);
  
  // unknown mode
  //
  else
    this.sendMessage("-mouse_mode " + button + " none " + index);
}    // setMouseMode


//
// sectioning control methods
//


HCSketchUpApplet.prototype.setSectionPlanes = function (sectionPlanes) {
  if (!this.loaded)
    return;
  if (sectionPlanes == true) 
    this.sendMessage("-section_planes true");
  else if (sectionPlanes == false)
    this.sendMessage("-section_planes false"); 
}    // setSectionPlanes


HCSketchUpApplet.prototype.setSectionCuts = function(sectionCuts) {
  if (!this.loaded)
    return;
  if (sectionCuts == true) 
    this.sendMessage("-section_cuts true");
  else if (sectionCuts == false)
    this.sendMessage("-section_cuts false"); 
}    // setSectionCuts


//
// sound related methods
//


HCSketchUpApplet.prototype.loadSound = function(soundName) {
  if (!this.loaded)
    return;
  this.sendMessage("-load_sound " + soundName);
}    // loadSound


HCSketchUpApplet.prototype.playSound = function(soundName) {
  if (!this.loaded)
    return;
  this.sendMessage("-play_sound " + soundName);
}    // playSound


//
// methods to get and set applet values
//


HCSketchUpApplet.prototype.setValue = function(valueName, value) {
  if (!this.loaded)
    return;
  if (typeof(value) == "object")
  
    // vector values
    //
    this.sendMessage("-" + valueName + " " + value.x + " " + value.y + " " + value.z);
  else
    // scalar values
    //
    this.sendMessage("-" + valueName + " " + value);
}    // setValue


HCSketchUpApplet.prototype.getValues = function(callback, valueNames) {
  if (!this.loaded)
    return;
  var names = "";
  for (var counter = 0; counter < valueNames.length; counter++) {
    if (counter != 0)
      names += " ";
    names += valueNames[counter];
  }
  this.sendMessage("-get " + callback + " " + names);
}    // getValues


//
// overlay markup methods
//


HCSketchUpApplet.prototype.addOverlay = function(overlay) {
  if (!this.loaded)
    return;
  
  // add marker overlays
  //
  if (overlay.__proto__ == HCMarker.prototype) {
    var iconIndex = 0;
	
    // get icon index if there is a custom icon
    //
    if (overlay.icon != null)
      iconIndex = overlay.icon.index + 1;
	
    this.sendMessage("-add_marker " + overlay.location.x + " " + overlay.location.y + " " + overlay.location.z
      + " " + iconIndex);
	
  // add line overlays
  //	
  } else if (overlay.__proto__ == HCLine.prototype) {
    var message = "-add_line";
	for (var counter = 0; counter < overlay.vertices.length; counter++)
	  message += " " + overlay.vertices[counter].x + " " + overlay.vertices[counter].y + " " + overlay.vertices[counter].z;
    this.sendMessage(message);
  }
}    // addOverlay


HCSketchUpApplet.prototype.removeOverlay = function(overlay) {
  if (!this.loaded)
    return;	
  this.sendMessage("-remove_overlay " + (overlay.index + 1));
}    // removeOverlay


HCSketchUpApplet.prototype.clearOverlays = function() {
  if (!this.loaded)
    return;	
  this.sendMessage("-clear_overlays");
}    // clearOverlays


//
// icon and cursor methods
//


HCSketchUpApplet.prototype.addIcon = function(icon) {
  if (!this.loaded)
    return;
  var x, y;
  if (icon.iconAnchor != null) {
    x = icon.iconAnchor.x;
    y = icon.iconAnchor.y;
	} else {
    x = 0;
    y = 0;
  }
  this.sendMessage("-add_icon '" + icon.imageName + "' " + icon.hAlignment + " " + icon.vAlignment + " " + x + " " + y);
}	// addIcon


HCSketchUpApplet.prototype.setCursor = function(icon) {
  if (!this.loaded)
    return;
  var index = 0;
  if (icon != null)
    index = icon.index + 1;
  this.sendMessage("-set_cursor " + index);
}


//
// section plane methods
//


HCSketchUpApplet.prototype.addSectionPlane = function(origin, normal) {
  if (!this.loaded)
    return;
  this.sendMessage("-add_section_plane " + origin.x + " " + origin.y + " " + origin.z + " " +
    normal.x + " " + normal.y + " " + normal.z);
}    // addSectionPlane


//
// mouse event call back enabling methods
//


HCSketchUpApplet.prototype.setEventHandler = function(handler) {
  if (!this.loaded)
    return;
  this.sendMessage("-events " + handler);
}    // setEventHandler


//
// lighting methods
//


HCSketchUpApplet.prototype.setLightDirection = function(lightDirection) {
  if (!this.loaded)
    return;
  this.sendMessage("-light_direction " + lightDirection.x.toFixed(3) + " " + 
    lightDirection.y.toFixed(3) + " " + lightDirection.z.toFixed(3));
}	// setLightDirection


HCSketchUpApplet.prototype.setLightCoords = function(lightCoords) {
  if (!this.loaded)
    return;
  if (lightCoords == "world")
    this.sendMessage("-light_coords world");
  else if (lightCoords == "camera")
    this.sendMessage("-light_coords camera");  
}	// setLightCoords


HCSketchUpApplet.prototype.setLightColor = function(lightColor) {
  if (!this.loaded)
    return;
  this.sendMessage("-light_color " + lightColor.x.toFixed(3) + " " + 
    lightColor.y.toFixed(3) + " " + lightColor.z.toFixed(3));
}	// setLightColor


HCSketchUpApplet.prototype.setBrightness = function(brightness) {
  if (!this.loaded)
    return;
  this.sendMessage("-brightness " + brightness.toFixed(3));
}	// setBrightness


HCSketchUpApplet.prototype.setAmbient = function(ambient) {
  if (!this.loaded)
    return;
  this.sendMessage("-ambient " + ambient.x.toFixed(3) + " " + 
    ambient.y.toFixed(3) + " " + ambient.z.toFixed(3));
}	// setAmbient


// 
// coloring methods
//


HCSketchUpApplet.prototype.setFaceFrontColor = function(red, green, blue) {
  if (!this.loaded)
    return;
  this.sendMessage("-face_front_color " + red + " " + green + " " + blue);
}	// setFaceFrontColor


HCSketchUpApplet.prototype.setFaceBackColor = function(red, green, blue) {
  if (!this.loaded)
    return;
  this.sendMessage("-face_back_color " + red + " " + green + " " + blue);
}	// setFaceBackColor