///////////////////////////////
// Modified 9-05 by colm.
//
//Copyconstructor altered to deal with paths
// 
///////////////////////////////

/***************************************
  LINE TOOL
***************************************/
LineTool.prototype = new DrawingTool(); 
LineTool.prototype.constructor = LineTool;
LineTool.superclass = DrawingTool.prototype;
function LineTool(){}
LineTool.prototype.initTool = function(){ 
  if (debugLevel >= 20) {
    debug("initing LineTool");
  }	
  useCanvas();
  eventCatcher.addEventListener( "mouseup", this, false );
  this.actionState="click0";
  window.document.getElementById("drawers").style.visibility = "visible";

}
LineTool.prototype.mouseup = function(evt){
  evt = (evt) ? evt : ((window.event) ? window.event : "");
  if (debugLevel >= 20)
    debug("mouseupped:"+this.actionState+":");
  switch (this.actionState){
  case "click0":
    this.startLine(evt);
    break;
  case "click1":
    this.endLine(evt);
    break;
  default:
    alert("Unknown line state:"+this.actionState+":");
    break;
  }
}
LineTool.prototype.startLine = function(evt) {
  evt = (evt) ? evt : ((window.event) ? window.event : "");
  this.x1=getX(evt);
  this.y1=getY(evt);;
  this.x2 = this.x1;
  this.y2 = this.y1;
  this.myLine = new Line(this.x1, this.y1, this.x2, this.y2, 0,  true);
  eventCatcher.addEventListener("mousemove", this, false);
  this.actionState = "click1"
}
LineTool.prototype.mousemove = function( evt ){
  evt = (evt) ? evt : ((window.event) ? window.event : "");
  this.x2=getX(evt);
  this.y2=getY(evt);
  this.myLine.update(this.x1, this.y1, this.x2, this.y2);
}
LineTool.prototype.endLine = function(evt) {
  if (debugLevel >= 20)
    debug("endLine");
  eventCatcher.removeEventListener("mousemove", this, false);
  this.actionState="click0";
}
LineTool.prototype.deactivate = function(){
  if (this.actionState == "click1"){
    this.myLine.destructor();
    delete this.myLine;
    eventCatcher.removeEventListener("mousemove", this, false);
  }
  eventCatcher.removeEventListener("mouseup", this, false);
  window.document.getElementById("drawers").style.visibility = "hidden";

}


LineTool.prototype.parsePath = function( path ) {
	//debug ("parsing line path.");
	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]);
		}
	}
}


// the copyconstructor function takes an svgNode and constructs a wrapper around it.
// this is useful when opening an existing svg document.
LineTool.prototype.copyconstructor = function( svgNode ){
  path = svgNode.getAttribute("d");
 //	debug ("line path : " + path );
	this.parsePath(path);
//	debug (" ");
//	debug ("x1 y1 : " + this.x1 + " " + this.y1);
//	debug ("x2 y2 : " + this.x2 + " " + this.y2);
	
		 
 	this.myLine = new Line(this.x1, this.y1, this.x2, this.y2, 0, false);
  this.myLine.copyconstructor( svgNode );
  return this.myLine;
}


var lineTool = new LineTool();

