///////////////////////////////
// Modified 9-05 by colm.
//
//copyconstructor changed to deal with paths 
// 
///////////////////////////////
/***************************************
 * Polygon Doohickey
 **************************************/

PolygonTool.prototype = new DrawingTool(); 
PolygonTool.prototype.constructor = PolygonTool;
PolygonTool.superclass = DrawingTool.prototype;
function PolygonTool(){
  this.drawer = ' <input type="button" value="終了" onclick="currentTool.finish()"><br> '

}

PolygonTool.prototype.initTool = function(){ 

  useCanvas();
  eventCatcher.addEventListener( "mouseup", this, false );
  this.numpoints = 0;
  this.curx = null;
  this.cury = null;
  this.actionState="click0";  

  window.document.getElementById("drawer2").innerHTML = this.drawer;
  window.document.getElementById("drawers").style.visibility = "visible";
}

PolygonTool.prototype.mouseup = function(evt){
  evt = (evt) ? evt : ((window.event) ? window.event : "");
  switch (this.actionState){
  case "click0":
    this.startPoly(evt);
    break;
  case "click1":
    this.setVertex(evt);
    break;
  default:
    alert("Unknown line state:"+this.actionState+":");
    break;
  }
}

PolygonTool.prototype.startPoly = function(evt) {
  evt = (evt) ? evt : ((window.event) ? window.event : "");
  var x=getX(evt);
  var y=getY(evt);
  this.myShape = new Polygon( true, x, y, x, y);
  eventCatcher.addEventListener("mousemove", this, false);
  this.actionState = "click1"
}

PolygonTool.prototype.mousemove = function( evt ){
  evt = (evt) ? evt : ((window.event) ? window.event : "");
  var x = getX(evt);
  var y = getY(evt);
  this.myShape.update( x, y );
}

PolygonTool.prototype.setVertex = function( evt ){
  evt = (evt) ? evt : ((window.event) ? window.event : "");
  var x = getX(evt);
  var y = getY(evt);
  if (this.curx == x && this.cury == y){
    this.finish();
   } 
  else {
    this.curx = x;
    this.cury = y;
    this.myShape.addPoint( x, y );
  }
}

PolygonTool.prototype.finish = function(){
  this.myShape.endPoly();
  eventCatcher.removeEventListener("mousemove", this, false);
  this.actionState="click0";
}

PolygonTool.prototype.deactivate = function(){
  if (this.actionState == "click1"){
    this.myShape.destructor();
    delete this.myShape;
    eventCatcher.removeEventListener("mousemove", this, false);
  }
  eventCatcher.removeEventListener("mouseup", this, false);
  window.document.getElementById("drawer2").innerHTML = "";
  window.document.getElementById("drawers").style.visibility = "hidden";
}


PolygonTool.prototype.parsePath = function( path ){
  //	debug ("parsing ......");
	var myPoly = new Array();
 	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));
		myPoly.push (Number(temp[temp.length-2]));
		myPoly.push (Number(temp[temp.length-1]));
	}
	return myPoly;
}


PolygonTool.prototype.copyconstructor = function( svgNode ) {
  var path = svgNode.getAttribute("d");
	var myPoly = this.parsePath(path);
  // pointstring = pointstring.replace(/\s/g,",");
 	this.myPolygon = eval("new Polygon( false, " + myPoly + " )");
  this.myPolygon.copyconstructor( svgNode );
  return this.myPolygon;
}

var polygonTool = new PolygonTool();
