///////////////////////////////
// Modified by Colm 9-06 
// now uses paths instead of built in svg ellipse
//////////////////////////////////


/***************************************
  SIMPLE ELLIPSE SHAPE
***************************************/
Ellipse.prototype = new Shape;
Ellipse.constructor = Ellipse;
Ellipse.superclass = Shape.prototype;

function Ellipse( pt1, pt2, rx, ry, circle_flag, axis_rot, draw ){
  if (arguments.length > 0) {
    this.init(pt1, pt2, rx, ry, circle_flag, axis_rot, draw);
  }
}

Ellipse.prototype.init = function(pt1, pt2, radx, rady, circle_flag, axis_rot, draw){  
	Ellipse.superclass.init.call( this, null );

	this.circle_flag = circle_flag;
	this.axis_rot = axis_rot;
	this.center = new Point2D((pt1.x + pt2.x)/2,(pt1.y + pt2.y)/2);
  this.rx = radx; 
  this.ry = rady; 
	
	if(this.circle_flag == 1){
		var maxrad = (this.rx > this.ry) ? this.rx : this.ry;
		this.rx = maxrad;
		this.ry = maxrad;
	}

	this.handles[0] = new Handle(pt1.x, pt1.y, this);
	this.handles[1] = new Handle(pt2.x, pt2.y, this);
		
	this.handles[2] = new Handle((this.ry * Math.cos((this.axis_rot + 90) * (Math.PI / 180))),
															 (this.ry * Math.sin((this.axis_rot + 90) * (Math.PI / 180))));
	this.handles[2].point.x += this.center.x; 
	this.handles[2].point.y += this.center.y;
	
	this.sweep = 0;
	this.large_arc = 0;
	if (draw) {
    this.make();
  }
}

Ellipse.prototype.constructPathString = function(){
	var pathstring = "M";
	pathstring += this.handles[0].point.x + ", " + this.handles[0].point.y + "A" + this.rx + ", " + 
								this.ry + " " + this.axis_rot + " " +this.large_arc + ", " + 
							  this.sweep + " " + this.handles[1].point.x + " " + this.handles[1].point.y; 

	pathstring += " M" + this.handles[1].point.x + ", " + this.handles[1].point.y + "A" + this.rx + ", " + 
								this.ry + " " + this.axis_rot + " " +this.large_arc + ", " + 
							  this.sweep + " " + this.handles[0].point.x + " " + this.handles[0].point.y; 
	return pathstring;
}

Ellipse.prototype.make = function() {
  this.svgNode = svgDoc.createElement("path");
	this.svgNode.setAttribute("id", "ellipse");

  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 = this.constructPathString();	
	this.svgNode.setAttribute("d", pathstring);
}


Ellipse.prototype.calcRadii = function() {
	this.ry = Math.sqrt(((this.handles[2].point.x - this.center.x) * (this.handles[2].point.x - this.center.x)) + 
	 					 ((this.handles[2].point.y - this.center.y) * (this.handles[2].point.y - this.center.y))) 

	this.rx = Math.sqrt(((this.handles[1].point.x - this.center.x) * (this.handles[1].point.x - this.center.x)) + 
	 					 ((this.handles[1].point.y - this.center.y) * (this.handles[1].point.y - this.center.y))) 
}


Ellipse.prototype.update = function( x, y ){
  if ( x && y ){
	  this.rx = Math.abs(this.center.x - x);
  	this.ry = Math.abs(this.center.y - y);

		if(this.circle_flag == 1){
			var maxrad = (this.rx > this.ry) ? this.rx : this.ry;
			this.rx = maxrad;
			this.ry = maxrad;
		}
		this.handles[0].moveto(this.center.x - this.rx, this.center.y, this);
  	this.handles[1].moveto(this.center.x + this.rx, this.center.y, this);
		this.handles[2].moveto(this.center.x, this.center.y - this.ry);
  }
	else {	
		this.center.x = (this.handles[0].point.x + this.handles[1].point.x) / 2;
		this.center.y = (this.handles[0].point.y + this.handles[1].point.y) / 2;
		this.calcRadii();
		if(this.circle_flag == 1){
			var maxrad = (this.rx < this.ry) ? this.rx : this.ry;
			this.rx = maxrad;
			this.ry = maxrad;
		}
	}
	var pathstring = this.constructPathString();	
	// debug ("pathstring : " + pathstring);
	this.svgNode.setAttribute("d", pathstring);
}

Ellipse.prototype.rotatePlease = function ( from, angle ){
	this.axis_rot += angle;
	// debug ("axis rot : " + this.axis_rot);
	for (var i=0;i<this.handles.length;i++){
  	this.handles[i].rotatePlease( from,angle); 
  }
	this.update();
}


Ellipse.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];
  delete this.rx;
  delete this.ry;
  delete this.large_arc;
	delete this.axis_rot;
	delete this.sweep;
}
