/***************************************************************\
| |\  /|                                                We Put  |
| | >< Hypercosm         layer_checkboxes.js       		3d      |
| |/  \|                                                To Work |
|***************************************************************|
|                                                               |
|        This file defines the Javascript behaviors of a        |
|        specific type of SketchUp user interface toolbar.      |
|                                                               |
|***************************************************************|
|                Copyright (c) 2007 Hypercosm, LLC.             |
\***************************************************************/


//
// "class" constructor
//


function layerCheckboxes(element, parent) {

  // call superclass constructor
  //
  checkboxes.call(this, element, parent);
  
  return this;
}    // layerCheckboxes


// inherit prototype from "superclass"
//
layerCheckboxes.prototype = new checkboxes();


//
// "class" methods
//


layerCheckboxes.prototype.onActivate = function() {

  // add applet layers
  //
  for (var counter = 0; counter < this.parent.applet.layerNames.length; counter++)
    this.addLayer(this.parent.applet.layerNames[counter], this.parent.applet.layerVisibility[counter]);
  
  // set callback for when applet's layer visibility changes
  //
  var layerCheckboxes = this;
  var callback = function(visibility) { 
    for (var counter = 0; counter < visibility.length; counter++) 
      layerCheckboxes.setLayerVisibility(counter, visibility[counter]);
  }
  this.parent.applet.setCallback("layer_visibility", callback);
}	// onActivate


layerCheckboxes.prototype.initialize = function() {
	
  // store initial layer visibility
  //
  this.initialLayerVisibility = new Array();
  for (counter = 0; counter < this.parent.applet.layerVisibility.length; counter++)
    this.initialLayerVisibility[counter] = this.parent.applet.layerVisibility[counter];
}	// initialize


//
// click event handling methods
//


layerCheckboxes.prototype.onClick = function(index) {
	
  // call superclass method
  //
  checkboxes.prototype.onClick.call(this, index);
  
  // change applet layer visibility
  //
  this.parent.applet.setLayerVisibility(index, this.getLayerVisibility(index)); 
}	// onClick


//
// construction methods
//


layerCheckboxes.prototype.addLayer = function(layerName, visible) {
  this.addCheckbox(layerName, visible);
}	// addLayer


//
// attributes querying methods
//


layerCheckboxes.prototype.getLayerVisibility = function(index) {
  return this.isCheckboxChecked(index);
}	// getLayerVisibility


//
// attributes changing methods
//


layerCheckboxes.prototype.setLayerVisibility = function(index, visibility) {
  this.setCheckbox(index, visibility);
}	// setLayerVisibility


layerCheckboxes.prototype.toggleLayerVisibility = function(index) {
  this.setCheckbox(index, !this.isCheckboxChecked(index));
}	// toggleLayerVisibility


//
// query string methods
//


layerCheckboxes.prototype.parseQueryString = function(queryString) {
  if (!queryString)
    return;
	
  for (var counter = 0; counter < this.parent.applet.layerVisibility.length; counter++) {   
    var visibility = getQueryVariable(queryString, "layer" + counter);
    if (visibility != undefined)
      this.parent.applet.setLayerVisibility(counter, (visibility == "true"));
  }
  
  this.parent.applet.getAttribute("layer_visibility");
}	// parseQueryString


layerCheckboxes.prototype.getQueryString = function() {
  var queryString = null;
  
  for (var counter = 0; counter < this.parent.applet.layerVisibility.length; counter++) {
    if (this.parent.applet.layerVisibility[counter] != this.initialLayerVisibility[counter])
	  queryString = addQueryString(queryString, "layer" + counter + "=" + this.parent.applet.layerVisibility[counter]);
  }
  
  return queryString;
}	// getQueryString
