/***************************************
  SIMPLE POLYGON SHAPE

=============================
Modified 8/21 by Hawkeye

Changed makePointString function to eliminate trailing whitespace problem.
=============================
///////////////////////////////
// Modified 9-05 by colm.
//
//Modified to use paths instead 
// of the built in SVG points object
///////////////////////////////

***************************************/
Polygon.prototype = new Shape();
Polygon.prototype.constructor = Polygon;
Polygon.superclass = Shape.prototype;
function Polygon( draw ){
  if (arguments.length > 0) {
    this.init( draw , arguments );
  }
}
// initialize polygon with 2 points.
Polygon.prototype.init = function ( draw ) {
  var pointArray = arguments[1];
  var x,y;
  Polygon.superclass.init.call( this, null );
  for (var i=1; i<pointArray.length-1; i+=2){
    x = pointArray[i];
    y = pointArray[i+1];
    this.handles[ (i-1)/2 ] = new Handle( x, y, this );
  }

  if (draw) {
    this.make();
  }
}

// Make a string of all the points in the polygon
// this is necessary for updating a poly with new info.
Polygon.prototype.makePointString = function() {
  if (!this.handles.length) {
    return("");
  }
  var pointstring = "M " + this.handles[0].point.x + " " + this.handles[0].point.y;
  for (var i=1;i<this.handles.length;i++){
    pointstring += ",L " + this.handles[i].point.x + " " + this.handles[i].point.y;
  }
	pointstring +=", Z ";
  return pointstring;
}


// Create svg node and stuff.
Polygon.prototype.make = function(){

  this.svgNode = svgDoc.createElement("path");
  this.svgNode.addEventListener( "click", this, false );
  this.svgNode.setAttribute( "wrapper", this );
  
  // this.svgNode.setAttribute( "points", this.makePointString() );

  this.returnStroke = strokeColor;
  style = this.svgNode.getStyle();
  style.setProperty("fill", fillColor);
  style.setProperty("stroke", strokeColor);
  style.setProperty("stroke-width", strokeWidth);
  drawingGroup.appendChild(this.svgNode);
	
	pathstring = this.makePointString();
 	this.svgNode.setAttribute("d", pathstring);
	this.svgNode.setAttribute("id", "polygon");
}


// allows "move" functions to work, including line dragging
Polygon.prototype.update = function( x, y ) {
  if (arguments.length > 0){
    this.handles[ this.handles.length - 1 ].moveto( x, y );
  }
  this.svgNode.setAttribute( "d", this.makePointString() );
}
// add an additional vertex to the polygon 
Polygon.prototype.addPoint = function(x,y){
  var handle = new Handle( x, y, this );
  this.handles.push( handle );
  this.update();
}
// finish off the polygon by closing it.
Polygon.prototype.endPoly = function(){
  this.handles[ this.handles.length -1 ].moveto( this.handles[0].point.x, this.handles[0].point.y );
  this.update();
}
Polygon.prototype.destructor = function(){
  this.svgNode.parentNode.removeChild( this.svgNode );
  delete this.svgNode;
  for (var i=0;i<this.handles.length;i++){
    delete this.handles[i];
  }
}
Polygon.prototype.debug = function(){
  alert("This polygon exists");
}

