/***********************************
  File: TatamiTool.js
  Author: Hawkeye
  Create Date: 8/22/2002

  This tool creates tatami mats.
  It is a three state process.  First a box may be drawn representing the floorspace.  That is a two state process.
    Finally the layout direction of the tatami is given by dragging the mouse to align the tatami North, South, East or West.

Word.
*************************************/

TatamiTool.prototype = new ApplianceTool();
TatamiTool.prototype.constructor = TatamiTool;
TatamiTool.superclass = ApplianceTool.prototype;

function TatamiTool(){}

TatamiTool.prototype.initTool = function(){
  useCanvas();
  eventCatcher.addEventListener( "mouseup", this, false );
  this.actionState = "click0";
}

TatamiTool.prototype.mouseup = function( evt ){
  evt = (evt) ? evt : ((window.event) ? window.event : "");

  switch (this.actionState){
  case "click0":
    this.startBox(evt);
    break;
  case "click1":
    this.endBox(evt);
    break;
  case "click2":
    this.finish(evt);
    break;
  default:
    alert("Unknown line state:"+this.actionState+":");
    break;
  } 
}
TatamiTool.prototype.mousemove = function(evt){
  evt = (evt) ? evt : ((window.event) ? window.event : "");
  switch (this.actionState){
  case "click0":
    alert("mousemove on Tatami click0.  \n\n WTF??!?!");
    break;
  case "click1":
    this.dragBox(evt);
    break;
  case "click2":
    this.orientLayout(evt);
    break;
  default:
    alert("Unknown line state:"+this.actionState+":");
    break;
  } 
}
TatamiTool.prototype.startBox = function(evt){
  this.x0 = getX(evt);
  this.y0 = getY(evt);

  this.tempBox = new Rectangle(this.x0,this.y0,this.x0,this.y0,this.x0,this.y0,this.x0,this.y0,true);
  this.tempBox.temp();
  this.actionState = "click1";
  eventCatcher.addEventListener( "mousemove", this, false );
}
TatamiTool.prototype.dragBox = function(evt){
  var x = getX(evt);
  var y = getY(evt);
  this.tempBox.update( x, y );
}
TatamiTool.prototype.endBox = function(evt){
  var x = getX(evt);
  var y = getY(evt);
  this.tempBox.update( x, y );
  // the following is used to determine orientation in step 3 
  this.cx = ( x + this.x0 ) / 2;
  this.cy = ( y + this.y0 ) / 2;
  this.k1 = this.cx - this.cy;
  this.k2 = this.cx + this.cy;
  // debug(" x0, y0 " + this.x0 +"," + this.y0);
  // debug(" cx, cy, k1, k2 " + this.cx +"," + this.cy+"," + this.k1+"," +this.k2 +",");
  this.myTatami = new Tatami( this.tempBox, "N", true);
  this.actionState = "click2";
}
TatamiTool.prototype.orientLayout = function( evt ){
  var o;
  var x = getX(evt);
  var y = getY(evt);
  var line1 = (x > y + this.k1);
  var line2 = (x < this.k2 - y);
  var  orientation = "X";
  //  debug("line1,line2:"+line1+line2);
  if ( line1 && line2){
    orientation = "N";
  } else if (line1 && !line2) {
    orientation = "E";
  } else if (!line1 && line2) {
    orientation = "W";
  } else if (!line1 && !line2) {
    orientation = "S";
  }
  this.myTatami.updateLines(orientation);
}
TatamiTool.prototype.finish = function( evt ){
  this.actionState = "click0";
  eventCatcher.removeEventListener( "mousemove", this, false );
  
}
TatamiTool.prototype.deactivate = function(){
  if (this.actionState == "click1"){
    this.tempBox.destructor()
    delete this.tempBox;
    eventCatcher.removeEventListener("mousemove", this, false);
  }
  if (this.actionState == "click2"){
    this.myTatami.destructor();
    delete this.x0;
    delete this.y0;
    delete this.cx;
    delete this.cy;
    delete this.k1;
    delete this.k2;
    eventCatcher.removeEventListener("mousemove", this, false);
  }
  delete this.actionState;
  eventCatcher.removeEventListener("mouseup", this, false);
}
var tatamiTool = new TatamiTool();
