///////////////////////////////
// Modified 9-05 by colm.
//
//Modified to use paths instead 
// of the built in SVG rect object
///////////////////////////////

/***************************************
  SIMPLE RECTANGLE SHAPE
***************************************/
Rectangle.prototype = new Shape( );
Rectangle.constructor = Rectangle;
Rectangle.superclass = Shape.prototype;
function Rectangle(x1,y1,x2,y2,x3,y3,x4,y4,draw){
  if (arguments.length > 0) {
    this.init(x1,y1,x2,y2,x3,y3,x4,y4,draw);
  }
}
Rectangle.prototype.init = function (x1,y1,x2,y2,x3,y3,x4,y4,draw) {
  Rectangle.superclass.init.call( this, null );
  this.handles[0] = new Handle( x1,y1, this);
  this.handles[1] = new Handle( x2,y2, this );
  this.handles[2] = new Handle( x3,y3, this);
  this.handles[3] = new Handle( x4,y4, this );
  if (draw) {
    this.make();
  }
}

Rectangle.prototype.make = function(){
	this.svgNode = svgDoc.createElement("path");
  this.svgNode.addEventListener( "click", this, false );
  style = this.svgNode.getStyle();
  style.setProperty("fill", fillColor);
  style.setProperty("stroke", strokeColor);
  style.setProperty("stroke-width", strokeWidth);
	drawingGroup.appendChild(this.svgNode);
	var pathstring = "M " + this.handles[0].point.x + " " + this.handles[0].point.y + ", L " 
												+ this.handles[1].point.x + " " + this.handles[1].point.y + ", L "
												+ this.handles[2].point.x + " " + this.handles[2].point.y + ", L " 
												+ this.handles[3].point.x + " " + this.handles[3].point.y + ", Z ";
 	this.svgNode.setAttribute("d", pathstring);
	this.svgNode.setAttribute("id", "rect");
}

Rectangle.prototype.update = function( x1, y1, x2, y2){
  if (x1 && y1) {
    this.handles[0].moveto( x1, y1 );
		this.handles[1].moveto( this.handles[2].point.x, this.handles[0].point.y );
		this.handles[3].moveto( this.handles[0].point.x, this.handles[2].point.y ); 
	}
  if (x2 && y2) {
    this.handles[2].moveto( x2, y2 );
		this.handles[1].moveto( this.handles[2].point.x, this.handles[0].point.y );
		this.handles[3].moveto( this.handles[0].point.x, this.handles[2].point.y ); 
	}
	var pathstring = "M " + this.handles[0].point.x + " " + this.handles[0].point.y + ", L " 
												+ this.handles[1].point.x + " " + this.handles[1].point.y + ", L "
												+ this.handles[2].point.x + " " + this.handles[2].point.y + ", L " 
												+ this.handles[3].point.x + " " + this.handles[3].point.y + ", Z ";
	this.svgNode.setAttribute("d", pathstring);
}

Rectangle.prototype.temp = function(){
  if (this.svgNode){
    this.svgNode.setAttribute( "style", "fill:none;stroke-width:1; stroke:black; stroke-dasharray:5,5");
		this.svgNode.removeEventListener("click", this, false );
  }
}

Rectangle.prototype.destructor = function() {
  this.svgNode.parentNode.removeChild( this.svgNode );
  delete this.svgNode;
	for(var k = 0; k < this.handles.length; k ++)
  	delete this.handles[k];
}

Rectangle.prototype.debug = function(){
  alert("This rectangle exists");
}
