/////////////////////////
//Modified by Colm 9-06
//copy construtor changed to deal with paths.
// also theres a circle flag global variable introduced
// to tell tool when its building a circle
// as opposed to an ellipse.
//////////////////////////

/***************************************
  ELLIPSE TOOL
***************************************/
EllipseTool.prototype = new DrawingTool();
EllipseTool.prototype.constructor = EllipseTool;
EllipseTool.superclass = DrawingTool.prototype;
function EllipseTool(flag){
	this.pt1 = new Point2D(0,0);
	this.pt2 = new Point2D(0,0);
	this.rx = 0;
	this.ry = 0;
	this.axis_rot = 0;
	this.sweep = 0;
  this.actionState="click0";
	this.circle_flag = flag;				// if flag == 0 its an ellipse - else if 1 its a circle.
}
EllipseTool.prototype.initTool = function(){
  if (debugLevel >= 20) {
    debug("initing EllipseTool");
  }
  this.actionState="click0";
  useCanvas();
  eventCatcher.addEventListener( "mouseup", this, false )
  window.document.getElementById("drawers").style.visibility = "visible";
}
EllipseTool.prototype.mouseup = function(evt){
  evt = (evt) ? evt : ((window.event) ? window.event : "");
  if (debugLevel >= 20)
    debug("handling ellipseTool event:"+evt.type);
  switch (this.actionState){
  case "click0":
    this.startEllipse(evt);
    break;
  case "click1":
    this.finishEllipse(evt);
    break;
  default:
    alert("Unknown state:"+this.actionState+":");
    break;
  }
}
EllipseTool.prototype.startEllipse = function(evt){
  evt = (evt) ? evt : ((window.event) ? window.event : "");
	this.pt1.setXY(getX(evt), getY(evt));
	this.pt2.setXY(getX(evt) + 1, getY(evt)); // if both these pts are the same
	this.rx = 1;															// svg draws an ugly (hawkeyes word) line
	this.ry = 1;															// before the ellipse.
	
	this.sweep = "off";
	this.axis_rot = 0;
	this.myEllipse =  new Ellipse(this.pt1, this.pt2, this.rx, this.ry, this.circle_flag, this.axis_rot, true);
  eventCatcher.addEventListener("mousemove", this, false);
  this.actionState="click1";
}

EllipseTool.prototype.finishEllipse = function(evt){
	eventCatcher.removeEventListener("mousemove", this, false);
  this.actionState="click0";
}

EllipseTool.prototype.mousemove = function(evt){
  evt = (evt) ? evt : ((window.event) ? window.event : "");
  this.x2 = getX(evt);
  this.y2 = getY(evt);
  this.myEllipse.update( this.x2, this.y2 );
}

EllipseTool.prototype.deactivate = function(){
  if (this.actionState == "click1") {
    this.myEllipse.destructor();
    delete this.myEllipse;
    eventCatcher.removeEventListener("mousemove", this, false);
    this.actionState="click0";
  }
  eventCatcher.removeEventListener("mouseup", this, false);
  window.document.getElementById("drawers").style.visibility = "hidden";
}

EllipseTool.prototype.parsePath = function( path ){
 	var str_arr = new Array();
 	var l_index = 0;
	var flag = 0;
	var i = 0;
	while (flag != 2){
		if(path.charAt(i) == " "){
			str_arr.push(path.substring(l_index,  i));
			l_index = i;	
		} 	 	
		else if (path.charAt(i) == "M"){
			flag ++;	
		}
		i++;
	}
	var ind = str_arr[1].indexOf("A");
	this.ay = Number(str_arr[1].substring(0, ind));
	this.rx = Number(str_arr[1].substring(ind + 1,str_arr[1].length -1));
	this.ry = Number(str_arr[2]);
	this.ax = Number(str_arr[0].substring(1, str_arr[0].length -1));	
	this.bx = Number(str_arr[str_arr.length - 2]);
	this.by = Number(str_arr[str_arr.length - 1]);
	this.axis_rot = Number(str_arr[str_arr.length - 5]);
	this.sweep = Number(str_arr[str_arr.length - 3]);
}

EllipseTool.prototype.copyconstructor = function(svgNode){
 	var path = svgNode.getAttribute("d");
	this.parsePath(path);

	this.pt1.setXY(this.ax,this.ay);
	this.pt2.setXY(this.bx,this.by);
	// this.r.setXY(this.rx,this.ry);

 	if (this.rx == this.ry)
		this.circle_flag = 1;
	else 
		this.circle_flag = 0;
	this.myEllipse =  new Ellipse(this.pt1, this.pt2, this.rx, this.ry, this.circle_flag, this.axis_rot, false);
  this.myEllipse.copyconstructor(svgNode);
  return this.myEllipse;
}
var ellipseTool = new EllipseTool(0);
var circleTool = new EllipseTool(1);
