/***********************************************
  File: TatamiObject.js
  Creator: Hawkeye
  Create Date: 8/22/2002

  This class creates a picture of a tatami mat out of lines and boxes.  Yay!

************************************************/
Tatami.prototype = new ObjectGroup( );
Tatami.constructor = Tatami;
Tatami.superclass = ObjectGroup.prototype;

function Tatami(box, orient, draw){
  if (arguments.length > 0){
    this.init( box, orient, draw );
  }
}

Tatami.prototype.init = function( box, orient, draw ){
  Tatami.superclass.init.call( this );
  this.orientation = orient;
  this.box = box;
  this.addObject( box );
  var style = this.box.svgNode.getStyle();
  style.setProperty("stroke-dasharray", "1,0");
  // debug("HERE THERE YAY!");

  if ( draw ) {
    this.make();
  }
}
Tatami.prototype.make = function(){
  // debug("Tatami.make");
  this.L1 = new Line( 0,0,0,0, false, true);
  this.L1.setShapeStyle("stroke-width","1");
  this.L1.setShapeStyle("stroke","black");
  this.L2 = new Line( 0,0,0,0, false, true);
  this.L2.setShapeStyle("stroke-width","1");
  this.L2.setShapeStyle("stroke","black");
  this.L3 = new Line( 0,0,0,0, false, true);
  this.L3.setShapeStyle("stroke-width","1");
  this.L3.setShapeStyle("stroke","black");
  this.L4 = new Line( 0,0,0,0, false, true);
  this.L4.setShapeStyle("stroke-width","1");
  this.L4.setShapeStyle("stroke","black");
  this.L5 = new Line( 0,0,0,0, false, true);
  this.L5.setShapeStyle("stroke-width","1");
  this.L5.setShapeStyle("stroke","black");
  this.updateLines();
  this.addObject( this.L1 );
  this.addObject( this.L2 );
  this.addObject( this.L3 );
  this.addObject( this.L4 );
  this.addObject( this.L5 );
}

/*
  FIVE LINES
  
*/
Tatami.prototype.updateLines = function( orient ){ 
  if (arguments.length > 0){
    //    debug("orient:" + orient);
    this.orientation = orient;
  }
  var a = new Point2D();
  var W =0;
  var H =0;
  var x1, y1, x2, y2;
  var bx = Number(this.box.handles[0].point.x);
  var by = Number(this.box.handles[0].point.y);
  var bw = Number(this.box.handles[2].point.x-bx);
  var bh = Number(this.box.handles[2].point.y-by);

  switch( this.orientation ){
  case "N":
    a.x = bx;
    a.y = by;
    W   = bw;
    H   = bh;
    break;
  case "E":
    W = bw;
    H = -1 * bh;
    a.x = bx;
    a.y = by - H;
    break;
  case "S":
    W = -1 * bw;
    H = -1 * bh;
    a.x = bx - W;
    a.y = by - H;
    break;
  case "W":
    W = -1 * bw;
    H = bh;
    a.x = bx - W;
    a.y = by;
    break;
  default:
    alert("UNKNOWN ORIENTATION!");
    break;
  }
  if ( this.orientation == "N" || this.orientation == "S" ){
    x1 = a.x + W/4;
    y1 = a.y;
    x2 = a.x + W/4;
    y2 = a.y + 2*H/3;
    this.L1.update(x1, y1, x2, y2);
    
    x1 = a.x + 3*W/4
    y1 = a.y
    x2 = a.x + 3*W/4
    y2 = a.y + 2*H/3
    this.L2.update(x1, y1, x2, y2);
    
    x1 = a.x + W/4
    y1 = a.y + H/3
    x2 = a.x + 3*W/4
    y2 = a.y + H/3
    this.L3.update(x1, y1, x2, y2);
    
    x1 = a.x
    y1 = a.y + 2*H/3
    x2 = a.x + W
    y2 = a.y + 2*H/3
    this.L4.update(x1, y1, x2, y2);

    x1 = a.x + W/2
    y1 = a.y + 2*H/3
    x2 = a.x + W/2
    y2 = a.y + H
    this.L5.update(x1, y1, x2, y2);
  } else {
    y1 = a.y + H/4;
    x1 = a.x;
    y2 = a.y + H/4;
    x2 = a.x + 2*W/3;
    this.L1.update(x1, y1, x2, y2);
    
    y1 = a.y + 3*H/4
    x1 = a.x
    y2 = a.y + 3*H/4
    x2 = a.x + 2*W/3
    this.L2.update(x1, y1, x2, y2);
    
    y1 = a.y + H/4
    x1 = a.x + W/3
    y2 = a.y + 3*H/4
    x2 = a.x + W/3
    this.L3.update(x1, y1, x2, y2);
    
    y1 = a.y
    x1 = a.x + 2*W/3
    y2 = a.y + H
    x2 = a.x + 2*W/3
    this.L4.update(x1, y1, x2, y2);

    y1 = a.y + H/2
    x1 = a.x + 2*W/3
    y2 = a.y + H/2
    x2 = a.x + W
    this.L5.update(x1, y1, x2, y2);
  }
}
Tatami.prototype.destructor = function( ){
  if ( this.myBox ){
    this.myBox.destructor();
    this.L1.destructor();
    this.L2.destructor();
    this.L3.destructor();
    this.L4.destructor();
    this.L5.destructor();
  }
  Tatami.superclass.destructor.call(this);
}
