///////////////////////////////
// Modified 9-05 by colm.
//
//copyconstructor changed to deal with paths 
// instead of the built in SVG rect object
///////////////////////////////
/***************************************
  RECTANGLE TOOL
***************************************/
RectangleTool.prototype = new DrawingTool(); 
RectangleTool.prototype.constructor = RectangleTool;
RectangleTool.superclass = DrawingTool.prototype;
function RectangleTool(){}
RectangleTool.prototype.initTool = function(){ 
  if (debugLevel >= 20) {
    debug("initing RectangleTool");
  }
  useCanvas();
  eventCatcher.addEventListener( "mouseup", this, false )
  this.actionState="click0";
  window.document.getElementById("drawers").style.visibility = "visible";
}
RectangleTool.prototype.mouseup = function(evt){
  evt = (evt) ? evt : ((window.event) ? window.event : "");
  if (debugLevel >= 20)
    debug("mouseupped:"+this.actionState+":");
  switch (this.actionState){
  case "click0":
    this.startRectangle(evt);
    break;
  case "click1":
    this.finishRectangle(evt);
    break;
  default:
    alert("Unknown state:"+this.actionState+":");
    break;
  }
}
RectangleTool.prototype.startRectangle = function(evt) {
  if (debugLevel >= 20)
    debug("startRectangle");
  evt = (evt) ? evt : ((window.event) ? window.event : "");
  this.x1 = getX(evt);
  this.y1 = getY(evt);

	this.x3 = this.x1;
	this.y3 = this.y1;

  this.x2 = this.x3;
  this.y2 = this.y1;
	
	this.x4 = this.x1;
	this.y4 = this.y3;
	
  eventCatcher.addEventListener("mousemove", this, false);
  this.actionState = "click1"
  this.myRectangle = new Rectangle(this.x1, this.y1, this.x2, this.y2, this.x3, this.y3, this.x4, this.y4, true );
}

RectangleTool.prototype.mousemove = function(evt) {
  //  debug("drag rectangle");
  evt = (evt) ? evt : ((window.event) ? window.event : "");
  this.x2=getX(evt);
  this.y2=getY(evt);

  this.myRectangle.update( this.x1, this.y1, this.x2, this.y2);
}


RectangleTool.prototype.finishRectangle = function(evt) {
  if (debugLevel >= 20)
    debug("finishRectangle");
  evt = (evt) ? evt : ((window.event) ? window.event : "");
  eventCatcher.removeEventListener("mousemove", this, false);
  this.actionState="click0";
}
RectangleTool.prototype.deactivate = function() {
  if ( this.actionState == "click1"){
    this.myRectangle.destructor();
    delete this.myRectangle;
    eventCatcher.removeEventListener("mousemove", this, false);
  }
  eventCatcher.removeEventListener( "mouseup", this, false);
  window.document.getElementById("drawers").style.visibility = "hidden";
}
RectangleTool.prototype.parsePath = function( path ){
  //	debug ("parsing ......");
 	var str_arr = new Array();
 	var l_index = 0;
	for (var i =0; i < path.length; i++){
		if(path.charAt(i) == ","){
			str_arr.push(path.substring(l_index,  i));
			l_index = i;	
		} 	 	
	}
	for (var k = 0; k < str_arr.length; k ++){
		var temp = new Array();
		l_index = 0;
		for (var i = 0; i < str_arr[k].length; i++){
		
			if(str_arr[k].charAt(i) == " "){
				temp.push(str_arr[k].substring(l_index,  i));
				l_index = i;	
			}
		} 
		temp.push(str_arr[k].substring(l_index,str_arr[k].length));
		if(k == 0){
			this.x1 = Number(temp[temp.length-2]);
			this.y1 = Number(temp[temp.length-1]);
		}
		else if (k == 1){
			this.x2 = Number(temp[temp.length-2]);
			this.y2 = Number(temp[temp.length-1]);
		}
		else if (k == 2){
			this.x3 = Number(temp[temp.length-2]);
			this.y3 = Number(temp[temp.length-1]);
		}
		else if (k == 3){
			this.x4 = Number(temp[temp.length-2]);
			this.y4 = Number(temp[temp.length-1]);
		}
	}
}

RectangleTool.prototype.copyconstructor = function( svgNode ){
	var path = svgNode.getAttribute("d");
	this.parsePath(path); 
  this.myRectangle = new Rectangle(this.x1, this.y1, this.x2, this.y2, this.x3, this.y3, this.x4, this.y4, false);
  this.myRectangle.copyconstructor( svgNode );
 	return this.myRectangle;
}

var rectTool = new RectangleTool;
