/***************************************
  Wouldn't you just guess that we'd be rewriting these a third time?
  This time svg wrappers are implemented with an additional wrapper around thier 
	key descriptors (points).  
	This is so that functions may be applied to shapes without digging through 
	shape properties (get this and get that).

  Again, a tip of the hat to Kevlindev for the shape handling routines.

=========================================

Modified 8/19 by Hawkeye

Added Shape.prototype.copyconstructor() to the Shape class.

=========================================

=========================================

Modified 8/27 by Colm

Added rotatePlease function and neuterRotate function
move function also changed slightly to use SVG transform 
instead of manual.
All in shape class.

=========================================
=========================================

Modified 8/30 by Colm

scalePlease frunctions in both handle and shape
classes altered to allow scaling to work properly
after a move (translate) transform has been applied to
the shape. 
scaling still doen't work properly after a rotate
transform has been applied.

=========================================

=========================================

Modified 9/05 by Colm

all manipulation functions have now been
changed back to using manual transformations
instead of matrices. so RotatePlease(), ScalePlease()
and Move() have all been changed.
=========================================



****************************************/

/***************************************
  SHAPE SUPERCLASS
ALLOWS HANDLING OF MOUSE EVENTS
***************************************/
Shape.prototype = new EventHandler();
Shape.prototype.constructor = Shape;
Shape.superclass = EventHandler.prototype;
function Shape(svgNode){                   // svgNode may be null
  if (arguments.length > 0) {
    this.init(svgNode);
  }
}
Shape.prototype.init = function( svgNode, temp_flag ){
  this.handles = new Array();
  this.svgNode = svgNode;
  this.selected = 0;
  this.returnStroke = null;  
	this.temp_flag = temp_flag;
	
}

Shape.prototype.click = function( evt) {
	if(this.temp_flag != "on"){
  	evt = (evt) ? evt : ((window.event) ? window.event : "");
		selectedNode = (evt.target) ? evt.target : evt.srcElement;
		if ( selectedNode != this.svgNode ) {
 			alert("how the hell did I get here?");
		}
		grabObject( evt, this );	
	}
}
Shape.prototype.setShapeStyle = function( prop, val ) {
  if (this.svgNode){
    this.svgNode.getStyle().setProperty( prop, val );
    return 1;
  }
  return 0;
}

Shape.prototype.move = function( delta ) {
  for (var i=0; i<this.handles.length; i++){
    this.handles[i].moveby( delta );
  }
  this.update();
}

Shape.prototype.initTransform = function( from ){
  for (var i=0;i<this.handles.length;i++){
    this.handles[i].initTransform( from );
  }
}

Shape.prototype.scalePlease = function( from, percentx, percenty ){
  for (var i=0;i<this.handles.length;i++){
    this.handles[i].scalePlease( from, percentx, percenty );
  }
  this.update();
}

Shape.prototype.rotatePlease = function( from, angle ){
	this.centre = new Point2D();
	this.centre.x = (this.handles[0].point.x +  this.handles[1].point.x) / 2;
	this.centre.y = (this.handles[0].point.y +  this.handles[1].point.y) / 2;
	
	for (var i=0;i<this.handles.length;i++){
   this.handles[i].rotatePlease( from,angle,this.centre );
  }
 	this.update();
}
Shape.prototype.copyconstructor = function ( svgNode ){
  this.svgNode = svgNode;
  this.svgNode.addEventListener( "click", this, false );
  this.returnStroke = this.svgNode.getStyle().getPropertyValue("stroke");
  return 1;
}
Shape.prototype.mouseup = function(){
  alert("Pure virtual function 'mouseup' must be defined");
}
Shape.prototype.update = function(){
  alert("Pure virtual function 'update' must be defined");
}
Shape.prototype.destructor = function(){
  alert("Pure virtual function 'destructor' must be defined");
}


/***************************************
  HANDLE SUPERCLASS
  ALLOWS MANIPULATION (MOVING, SIZING ETC.) OF SHAPES
***************************************/
Handle.prototype = new EventHandler();
Handle.prototype.constructor = Handle;
Handle.superclass = EventHandler.prototype;

function Handle( x, y, owner ){
  if (arguments.length > 0) {
    this.init( x, y, owner );
  }
}

Handle.prototype.init = function( x,y,owner ){
  Handle.superclass.init.call( this );
  this.point = new Point2D( x, y );
  this.owner = owner;
}

Handle.prototype.rotatePlease = function( from, angle, centre ){
	var tempx = this.point.x - from.x;
	var tempy = this.point.y - from.y;
	var rad = angle * (3.14 / 180);
	var x = tempx * Math.cos(rad) - tempy * Math.sin(rad); 
	var y = tempy * Math.cos(rad) + tempx * Math.sin(rad); 
	x +=from.x;
	y +=from.y;
	this.point.setXY(x, y);
}

Handle.prototype.initTransform = function( from ) {
  this.transformOffset = this.point.subtract( from );
}

Handle.prototype.scalePlease = function( from, percentx, percenty ){
  delta = this.transformOffset.multiplyxy( percentx, percenty );
  this.point = from.add( delta ); 
}

Handle.prototype.translate = function(delta) {
  this.point.addEquals(delta);
};
Handle.prototype.moveto = function( x, y ){
  this.point.setXY(x,y);
}
Handle.prototype.moveby = function( delta ){
  this.point.addEquals( delta );
}
