imgchf.distance = function (a, b) {
   return Math.sqrt(Math.pow(b.x - a.x, 2) + Math.pow(b.y-a.y, 2));  //   float
};

dojo.declare("imgchf.shape", null, {
  initializer: function(id) {
//    dojo.debug("imgchf.shape being created w/name ");// + name);
    this.id = id;
    this.name = id;
    this.mShape = null;
    this.mHandles = null;
    this.mEditHandles = null;
    this.f = null;
    this.s = null;
  },
  	valueForPrototype: 2,
	toString: function () {
		return this.name;
	},
	getGfxShape: function () {
		return this.mShape;
	},
	getBoundingBox: function () {
	},
	getTransform: function () {
		return this.mShape.getTransform();
	},
	setTransform: function (tf) {
		return this.mShape.setTransform(tf);
	},
	getEventSource: function() {
		return this.mShape.getEventSource();
	},
  createHandles: function() {
		return true;
  },
	createPointEdit: function() {
		return false;
	},
	removeHandle: function() {
	},
	removeEditHandles: function() {
		return false;
	},
	moveEditPoint: function() {
		return false;
	},
	applyEffect: function(m) {
		this.mShape.applyTransform(m);
  		this.update();
  	},
	applyRotate: function(m) {
		this.mShape.applyLeftTransform(m);
  		this.update();
  	},
	removeShape: function() {
		surface.remove(this.mShape);
		this.removeHandle();
	},
	translateShape: function() {
	},
	setFill: function(f) {
		this.mShape.setFill(f);
	},
	setStroke: function(s) {
		this.mShape.setStroke(s);
	},
	update: function() {
		if (this.mHandles) {
			this.mHandles.update();
		}
	},
	toSVG: function() {
		return "";
	},
	hidePoints: function() {
	},
	showPoints: function() {
	},
	moveToFront: function() {
	},
	setCursor: function(cursor) {
		if (dojo.render.html.ie) {
			dojo.html.setStyle(this.mShape.getEventSource(), "cursor", cursor);
		} else {
		    this.mShape.getEventSource().setAttribute('cursor', cursor);
		}
	},
	setId : function () {
		this.mShape.getEventSource().setAttribute('shapeid', this.id);
	}
});

dojo.declare("imgchf.circle", imgchf.shape, {
  initializer: function(id) {
//    dojo.debug("imgchf.circle being created with id " + id);
    this.mShape = surface.createCircle({cx: 0, cy: 0, r: 1})
			.setFill([255, 251, 200, 0.001])
//			.setFill([255, 251, 200, 1.0])
			;
// fffbc8
	this.cX = 0;
	this.cY = 0;
	this.r = 1;  
	this.mShape.getEventSource().setAttribute('shapeid', id);
//    this.setId();
//    superclass.this.id =	"shape_" + (gShapeCounter++);
  	},
  	setRadius: function(r) {
  		this.r = r;
  		this.setPosition(this.cX, this.cY);
  	},
  	setPosition: function(x0, y0) {
// 		this.mShape.setTransform();
  		this.mShape.setTransform([dojo.gfx.matrix.translate(x0, y0), dojo.gfx.matrix.scale(this.r, this.r)]);
  		this.cX = x0;
  		this.cY = y0;
  	},
  getPosition: function() {
// 		this.mShape.setTransform();
  	return {x: this.cX, y: this.cY};
  },
	getBoundingBox: function () {
  	return this.mShape.getBoundingBox();
	}
  	
});

dojo.declare("imgchf.ellipse", imgchf.shape, {
  initializer: function(id, centerx, centery, rxinit, ryinit) {
    dojo.debug("imgchf.ellipse being created with id " + id);
    this.mShape = surface.createEllipse({cx: centerx, cy: centery, rx: rxinit, ry: ryinit})
			.setFill([0, 255, 255, 1.0])
			.setStroke({color: [0, 255, 255, 1.0], width: 1})
			;
		this.cx = centerx;
		this.cy = centery;
		this.rx = rxinit;
		this.ry = ryinit;  
		this.mShape.getEventSource().setAttribute('shapeid', id);
//    this.setId();
  	},
  	setExtents: function(rx, ry) {
  		this.rx = rx;
  		this.ry = ry;
  		surface.remove(this.mShape);
	    this.mShape = surface.createEllipse({cx: this.cx, cy: this.cy, rx: this.rx, ry: this.ry})
			.setFill([0, 255, 255, 1.0])
			.setStroke({color: [0, 255, 255, 1.0], width: 1})
			;
		this.mShape.getEventSource().setAttribute('shapeid', id);
		this.setCursor("move");
  	},

	toSVG: function() {
		var svgString = "";
			m = this.getTransform();

			svgMatrix = dojo.gfx.matrix.multiply(dojo.gfx.matrix.scale(1.0/450.0), m);			
			
//			svgString = "<ellipse fill=\"black\" ";
			svgString = "<g transform=\"rotate(-30)\"> <ellipse ";
			outr = dojo.gfx.matrix.multiplyPoint(svgMatrix, {x:this.rx, y:this.ry});
			outc = dojo.gfx.matrix.multiplyPoint(svgMatrix, {x:this.cx, y:this.cy});

			svgString += " rx=\"" + outr.x.toFixed(5) + "\"";
			svgString += " ry=\"" + outr.y.toFixed(5) + "\"";
			svgString += " cx=\"" + outc.x.toFixed(5) + "\"";
			svgString += " cy=\"" + outc.y.toFixed(5) + "\"";

			svgString += " \/> </g>";
		return svgString;
	},
	getBoundingBox: function () {
  	return this.mShape.getBoundingBox();
	}
	
});

dojo.declare("imgchf.ellipse.create", imgchf.ellipse, {
  initializer: function(id, centerx, centery, rx, ry) {
	imgchf.ellipse.create.superclass.initializer(id, centerx, centery, 10, 5);
    dojo.debug("imgchf.ellipse.create being created with id " + id);
  	}
});


dojo.declare("imgchf.rect", imgchf.shape, {
  initializer: function(id) { //, x0, y0, w, h) {
  	
	this.mShape = surface.createRect({ x: 0, y: 0, width: 1, height: 1 }).setFill([0, 0, 0, 1.0]);
	this.mShape.getEventSource().setAttribute('shapeid', id);
	this.mX = this.mY = 0;
//    superclass.this.id =	"shape_" + (gShapeCounter++);
  },
  setExtents: function(w, h) {
  	this.mW = w;
  	this.mH = h;
  	this.setPosition(this.mX, this.mY);
  },
  setPosition: function(x0, y0) {
// 		this.mShape.setTransform();
  	this.mShape.setTransform([dojo.gfx.matrix.translate(x0, y0), dojo.gfx.matrix.scale(this.mW, this.mH)]);
  	this.mX = x0;
  	this.mY = y0;
  },
	getBoundingBox: function () {
  	return this.mShape.getBoundingBox();
	}
});

dojo.declare("imgchf.rectangle", imgchf.shape, {
  initializer: function(id, x0, y0, w, h) {
  	imgchf.rectangle.superclass.initializer(id);
  	dojo.debug("rect " + x0 + " ," + y0 + " ," + w  + " ," + h);	
    this.mPointList = [];
    this.mPointList.push({x: x0, y: y0});

		this.mShape = surface.createRect({ x: x0, y: y0, width: 1, height: 1 });
		//.setFill([0, 0, 0, 1.0]);
		this.mShape.getEventSource().setAttribute('shapeid', id);
		this.mX = x0;
		this.mY = y0;
		this.mH = theEditor.width;
		this.mW = 1;
		this.mdegrees = 0;
		this.bVisibleEdit = true;
//		this.id = id;
		
  },
  mousePoint: function(point) {
//	  dojo.debug("rect mousePoint " + point.x + " ," + point.y);	

   	var ptLength = imgchf.distance(this.mPointList[0], point);
		this.mH = theEditor.width;
		this.mW = ptLength;
    	
   	dy = point.y - this.mPointList[0].y;
   	dx = point.x - this.mPointList[0].x;
  	degrees = Math.atan2(dy,dx);
		this.mdegrees = degrees;
//  rootmatrix = aHandle.mRootShape.getTransform();
//  rotatePt = dojo.gfx.matrix.multiplyPoint(rootmatrix, aHandle.p);
  	
//  dojo.debug("anchor rotatePt xTo:" + aHandle.p.x + " yTo:" + aHandle.p.y);
//    dojo.debug("lineTo degrees:" + degrees);
		m = [dojo.gfx.matrix.rotateAt(-degrees, this.mPointList[0]),dojo.gfx.matrix.translate(0, -7)];
//		mdebug(m);
		if (this.mShape)
			surface.remove(this.mShape);
		this.mShape = surface.createRect({ x: this.mPointList[0].x, y: this.mPointList[0].y, width: ptLength, height: theEditor.width }).setFill([62, 62, 255, 0.9]);
		this.mShape.applyLeftTransform(m);
		this.mShape.getEventSource().setAttribute('shapeid', this.id);
	
	},
  setExtents: function(w, h) {
  	this.mW = w;
  	this.mH = h;
		surface.remove(this.mShape);
		this.mShape = surface.createRect({ x: this.mX, y: this.mY, width: this.mW, height: this.mH }).setFill([62, 62, 255, 0.9]);
  	this.setPosition(this.mX, this.mY);
  },
  endShape: function(point) {
    this.mPointList.push(point);
		this.mousePoint(point);  	
    this.setId();
  },
 	move: function(dx, dy) {
	  dojo.debug("rect move " + dx + " ," + dy);	
		this.mShape.applyLeftTransform(dojo.gfx.matrix.translate(dx, dy));
    this.mPointList[0].x += dx;
    this.mPointList[0].y += dy;
    this.mPointList[1].x += dx;
    this.mPointList[1].y += dy;
 	  this.mX = this.mPointList[0].x; 	
	  this.mY = this.mPointList[0].y; 	
    
    this.mEditHandles[0].setPosition(this.mPointList[0].x, this.mPointList[0].y);
    this.mEditHandles[1].setPosition(this.mPointList[1].x, this.mPointList[1].y);
 	},
  setPosition: function(x0, y0) {
// 		this.mShape.setTransform();
 // 	this.mShape.setTransform(dojo.gfx.matrix.translate(x0, y0));
//  	this.mX = x0;
//  	this.mY = y0;
  },
//        this.mEditHandles[i] = new imgchf.handle.pointSelect(new_id, this, this.mPointList[i], TrackPoint, DeletePoint, imgchf.pointfcn.pt, i);
  createPointEdit: function() {
  	if (!this.bHandles) {
			this.mEditHandles = [];
//			editM = this.mShape.getTransform();
      for (var i = 0; i < this.mPointList.length; ++i) {
    		var new_id = "shape_" + (gShapeCounter++);
        this.mEditHandles[i] = new imgchf.handle.pointSelect(new_id, this, this.mPointList[i], TrackPoint, DeletePoint, imgchf.pointfcn.pt, i);
//        editPt = dojo.gfx.matrix.multiplyPoint(editM, this.mPointList[i]);
        		
        this.mEditHandles[i].setPosition(this.mPointList[i].x, this.mPointList[i].y);
        gShapes[new_id] = this.mEditHandles[i];
     	}
     	this.bHandles = true;
//        	dojo.debug(this.toSVG());
   	}
		this.showPoints();
  	return true;
  },
	movePoint: function(x1, y1, i) {
  		this.mPointList[i] = {x: x1, y: y1};
//  		dojo.debug("moved point is " + i);
	   	ptLength = imgchf.distance(this.mPointList[0], this.mPointList[1]);
//	  dojo.debug("rect ptLength is " + ptLength);	
	   
	  this.mX = this.mPointList[0].x; 	
	  this.mY = this.mPointList[0].y; 	
		this.mH = theEditor.width;
		this.mW = ptLength;
    	
   	dy = this.mPointList[1].y - this.mPointList[0].y;
   	dx = this.mPointList[1].x - this.mPointList[0].x;
  	degrees = Math.atan2(dy,dx);
  	
//		m = dojo.gfx.matrix.rotateAt(-degrees, this.mPointList[0]);
		m = [dojo.gfx.matrix.rotateAt(-degrees, this.mPointList[0]),dojo.gfx.matrix.translate(0, -7)];

		if (this.mShape)
			surface.remove(this.mShape);
		this.mShape = surface.createRect({ x: this.mPointList[0].x, y: this.mPointList[0].y, width: ptLength, height: theEditor.width }).setFill([62, 62, 255, 0.9]);
		this.mShape.applyLeftTransform(m);
    this.setId();

    this.mEditHandles[i].setPosition(x1, y1);
	},
	endMovePoint: function() {
	},
	update: function() {
		if (this.bVisibleEdit) {
			if (this.mEditHandles) {
   	    for (i = 0; i < this.mEditHandles.length; ++i) {
// 		    	this.mEditHandles[i].update(this.getTransform(), this.mPointList[i]);
 	    	}
				
			}
		}
	},
	hidePoints: function() {
		if (this.bVisibleEdit) {
			if (this.mEditHandles) {
  	    for (i = 0; i < this.mEditHandles.length; ++i) {
   	    	if (dojo.render.html.ie) {
						dojo.html.setStyle(this.mEditHandles[i].mShape.getEventSource(), 'visibility', 'hidden');
					} else {
				    	this.mEditHandles[i].mShape.getEventSource().setAttribute('visibility', 'hidden');
			    }
 	    	}
				
			}
			this.bVisibleEdit = false;
		}
	},
	showPoints: function() {
		this.bVisibleEdit = true;
		if (this.mEditHandles) {
 	    for (i = 0; i < this.mEditHandles.length; ++i) {
 	    	if (dojo.render.html.ie) {
					dojo.html.setStyle(this.mEditHandles[i].mShape.getEventSource(), 'visibility', 'visible');
 	    	} else {
			    this.mEditHandles[i].mShape.getEventSource().setAttribute('visibility', 'visible');
				}
    	}
			
		}
	},
	toSVG: function() {
		var svgString = "";
//			m = this.getTransform();
	   	ptLength = imgchf.distance(this.mPointList[0], this.mPointList[1]);
//	  dojo.debug("rect ptLength is " + ptLength);	
	   
	  this.mX = this.mPointList[0].x; 	
	  this.mY = this.mPointList[0].y; 	
		this.mH = theEditor.width;
		this.mW = ptLength;
    	
   	dy = this.mPointList[1].y - this.mPointList[0].y;
   	dx = this.mPointList[1].x - this.mPointList[0].x;
  	degrees = Math.atan2(dy,dx);
  	
//		m = dojo.gfx.matrix.rotateAt(-degrees, this.mPointList[0]);
		m = [dojo.gfx.matrix.rotateAt(-degrees, this.mPointList[0]),dojo.gfx.matrix.translate(0, -7)];
			svgsmall = dojo.gfx.matrix.scale(1.0/450.0, 1.0/450.0)
			svgMatrix = dojo.gfx.matrix.multiply(dojo.gfx.matrix.scale(1.0/450.0), m);			
			writematrix(svgMatrix);

//			svgunity = dojo.gfx.matrix.scale(1.0, 1.0)
			
			svgString = "<g id=\"imgchfrect\" transform=\"matrix(" + writematrix(svgMatrix) + ")\"> <rect ";

			svgString += " x=\"" + this.mX + "\"";
			svgString += " y=\"" + this.mY + "\"";
			svgString += " width=\"" + this.mW + "\"";
			svgString += " height=\"" + this.mH + "\"";
			svgString += " \/> </g>";

//			svgString += " \/>";
		return svgString;
	},
	load: function(svgRectGroup) {
		var extractedPoint;
						
//						document.body.innerHTML += html.firstChild.nodeValue;
						
            dojo.debug("group transform " + svgRectGroup.getAttribute("transform"));
//            dojo.debug("gnode " + rect0[0].getAttribute("x"));
		var rect0 = svgRectGroup.getElementsByTagName("rect")[0];
		
		dojo.debug("rect received:" + rect0);
		dojo.debug("rect x:" + rect0.getAttribute("x") + " y:" + rect0.getAttribute("y") + " width:"  + rect0.getAttribute("width") + " height:"  + rect0.getAttribute("height") );
		this.mX = parseFloat(rect0.getAttribute("x"));
		this.mY = parseFloat(rect0.getAttribute("y"));
		this.mW = parseFloat(rect0.getAttribute("width"));
		this.mH = parseFloat(rect0.getAttribute("height"));
    this.mShape = surface.createRect({ x: this.mX, y: this.mY, width: this.mW, height: this.mH }).setFill([62, 62, 255, 0.9]);
    
    var matrix = null;
    if(svgRectGroup){
      matrix = svgRectGroup.getAttribute("transform");
      if(matrix.match(/^matrix\(.+\)$/)){
        var t = matrix.slice(7, -1).split(",");
        matrix = dojo.gfx.matrix.normalize({
          xx: parseFloat(t[0]), xy: parseFloat(t[2]), 
          yx: parseFloat(t[1]), yy: parseFloat(t[3]), 
          dx: parseFloat(t[4]), dy: parseFloat(t[5])
        });
      }
    }
		svgMatrix = dojo.gfx.matrix.multiply(dojo.gfx.matrix.scale(450.0), matrix);			
    
//    m = dojo.gfx.Shape.attachTransform(svgRectGroup);
    mdebug(svgMatrix);            
		this.mShape.applyLeftTransform(svgMatrix);

		pt0 = {x: this.mX, y: this.mY };
		pt1 = {x: this.mX + this.mW, y: this.mY};
		debugPt(pt0);
		debugPt(pt1);
		tm = [svgMatrix,dojo.gfx.matrix.translate(0, 7)];
    this.mPointList[0] = dojo.gfx.matrix.multiplyPoint(tm, pt0);
    this.mPointList[1] = dojo.gfx.matrix.multiplyPoint(tm, pt1);
		
		debugPt(this.mPointList[0]);
		debugPt(this.mPointList[1]);
		
    this.setId();
/*
		if (this.createPointEdit()) {
			if (theEditor.editedshape != this) {
				if (theEditor.editedshape) {
//					theEditor.editedshape.removeEditHandles();
					theEditor.editedshape.hidePoints();
					theEditor.editedshape.removeHandle();
				}
				this.createHandles();
				theEditor.editedshape = this;
			}
		}
*/
  	theEditor.removeClickText();
		theEditor.editedshape = this;
    toolSet.setTool("editTool");
    bg.select("move");
	    
	    
    dojo.debug("loading of this rectangle is done");
//	    current_shape = this;
	    
	},
	removeShape: function() {
		this.removeEditHandles();
		surface.remove(this.mShape);
	},
 	removeEditHandles: function() {
 		if (this.bHandles) {
   		for (var i = 0; i < this.mEditHandles.length; ++i) {
				this.mEditHandles[i].removeShape();
   		}
   		this.mEditHandles = null;
 		}
 	},
	getBoundingBox: function () {
  	return this.mShape.getBoundingBox();
	}
});

dojo.declare("imgchf.polyline", imgchf.shape, {
  initializer: function(id) {
    dojo.debug("imgchf.polyline being created w/pointlist ");
    this.count = 0;
    this.mPointList = [];
    this.mCreatePointList = [];
    this.bHandles = false;
    this.nNewPt = null;
    this.newI = -1;
    this.moveI = -1;
    this.bVisibleEdit = true;
//    this.endBall = null;
//    superclass.this.id =	"shape_" + (gShapeCounter++);
 	},
 	addPoint: function(point, closeit) {
 		var abdist;
 		var shapeEnded = false;
  	var pt0;
  		
  	pt0 = point;	
		if (closeit) {	    
	    if (this.count == 0) {
				this.endBall = surface.createCircle({cx: point.x, cy: point.y, r: 7})
				.setFill([0, 0, 255, 1.0])
				.setStroke({color: [0, 255, 255, 1.0], width: 1});
	    } else if (theEditor.constrainKeyPressed) {
				dx = point.x - this.mPointList[this.count - 1].x ;
				dy = point.y - this.mPointList[this.count - 1].y ;
	
				if (dx < 0) dx = -dx;
				if (dy < 0) dy = -dy;
				
				if (dx > dy)
			  	pt0 = {x: point.x, y: this.mPointList[this.count - 1].y};
			  else
			  	pt0 = {x: this.mPointList[this.count - 1].x, y: point.y};
			} 
	  }  
	  
	  if (this.count > 0)
	   if (this.mPointList[this.count - 1].x == pt0.x && this.mPointList[this.count - 1].y == pt0.y) {
	   		dojo.debug("dupe point");
	  		return shapeEnded;
	  	}

 		this.mPointList.push(pt0);
 		
 		if (this.mShape) {
 			surface.remove(this.mShape);
 		}
 		this.count = this.count + 1;

		if (closeit) {
	    if (this.count > 1) {
	    	abdist = imgchf.distance(this.mPointList[0], this.mPointList[this.count - 1]);
		    	
	    	if (abdist < 8) {
		  		this.closeShape();
		  		shapeEnded = true;
	    	}
	    }
	  }  
	    
    if (shapeEnded) {
  		this.mShape = surface.createPolyline(this.mPointList).setStroke({color: "black", width: 2}).setFill([62, 62, 255, 0.9]);;
    	this.setCursor("move");
	    this.createPointEdit();
	    this.createHandles();
		    
	    if (theEditor.editedshape) {
				theEditor.editedshape.hidePoints();
				theEditor.editedshape.removeHandle();
			}
			theEditor.editedshape = this;
		  toolSet.setTool("editTool");
		  bg.select("move");
		    
    } else {
//	    this.mShape = surface.createPolyline(this.mPointList).setStroke({color: "black", width: 2});
	    this.mShape = surface.createPolyline(this.mPointList).setStroke({color: "black", width: 2});
		}
	    
    this.setId();
    return shapeEnded;
  	},
 	freehandPoint: function(point) {
 		var abdist;
 		var shapeEnded = false;
  	var pt0;
  		
  	pt0 = point;	
	  
	  if (this.count > 0)
	   if (this.mPointList[this.count - 1].x == pt0.x && this.mPointList[this.count - 1].y == pt0.y) {
	   		dojo.debug("dupe point");
	  		return shapeEnded;
	  	}

 		this.mPointList.push(pt0);
	    
//    if (false) {
    if (this.testline != null && dojo.render.html.ie) {
    	var raw = this.testline.getNode();
//   		dojo.debug("raw path " + raw.path.v);
			len = raw.path.v.length;
			if (this.count > 1)
				newPt = "," + pt0.x.toFixed() + "," + pt0.y.toFixed() + " e";
			else
				newPt = pt0.x.toFixed() + "," + pt0.y.toFixed() + " e";
			raw.path.v = raw.path.v.substr(0, len - 2) + newPt;
//   		dojo.debug("substring is raw path " + raw.path.v);

    } else {
	 		if (this.testline != null) {
 				surface.remove(this.testline);
 			}

	  	this.testline = surface.createPolyline(this.mPointList).setStroke({color: "black", width: 2});
	  }
 		this.count = this.count + 1;
	    
    return shapeEnded;
  	},
  closeShape: function() {
  	if (this.count > 0) {
 			this.mPointList[this.count - 1] = this.mPointList[0];
 		}
 		if (this.endBall) {
	  		surface.remove(this.endBall);
	  		this.endBall = null;
	  	}
  		if (this.testline) {
  			surface.remove(this.testline);
		    this.testline = null;
  		}
  		
  		return this.count;
   	},
  	closeShapeArbitrary: function() {
  		if (this.mShape) {
  			surface.remove(this.mShape);
  		}
  		if (this.count > 0) {
  		if (this.endBall) {
	  		surface.remove(this.endBall);
	  		this.endBall = null;
	  	}
  		if (this.testline) {
  			surface.remove(this.testline);
		    this.testline = null;
  		}
  		
 			this.mPointList[this.count] = this.mPointList[0];
 			this.count++;
	  		this.mShape = surface.createPolyline(this.mPointList).setStroke({color: "black", width: 2}).setFill([62, 62, 255, 0.9]);;
	    	this.setCursor("move");
		    this.createPointEdit();
		    this.createHandles();
		    
		    if (theEditor.editedshape) {
				theEditor.editedshape.hidePoints();
				theEditor.editedshape.removeHandle();
			}
			theEditor.editedshape = this;
			toolSet.setTool("editTool");
			bg.select("move");
			this.setId();

 		}
 		if (this.endBall) {
	  	surface.remove(this.endBall);
	  	this.endBall = null;
	  }
  	if (this.testline) {
  		surface.remove(this.testline);
		  this.testline = null;
		}
  		
  	return this.count;
  },
  mousePoint: function(point) {
  	tempPoly = [];
  	tempPoly[0] = this.mPointList[this.count - 1];

		if (theEditor.constrainKeyPressed) {
			dx = point.x - this.mPointList[this.count - 1].x ;
			dy = point.y - this.mPointList[this.count - 1].y ;

			if (dx < 0) dx = -dx;
			if (dy < 0) dy = -dy;
			
			if (dx > dy)
		  	tempPoly[1] = {x: point.x, y: this.mPointList[this.count - 1].y};
		  else
		  	tempPoly[1] = {x: this.mPointList[this.count - 1].x, y: point.y};
		} else {
	  	tempPoly[1] = point;
	  }
		
    abdist = imgchf.distance(this.mPointList[0], tempPoly[1]);
	    	
    if (abdist < 8) {
		 	this.endBall.setFill([255, 0, 0, 1.0]);
    }
    else {
		 	this.endBall.setFill([0, 0, 255, 1.0]);
    }
    	
    if (this.testline != null && dojo.render.html.ie) {
    	
    	var raw = this.testline.getNode();
			raw.path.v = "m" + tempPoly[0].x.toFixed() + " " + tempPoly[0].y.toFixed() +
			"l" + tempPoly[1].x.toFixed() + " " + tempPoly[1].y.toFixed() + "e";
    } else {
	 		if (this.testline != null) {
 				surface.remove(this.testline);
 			}

	  	this.testline = surface.createPolyline(tempPoly).setStroke({color: "FC2A00", width: 2});
	  }
  },
  createHandles: function() {
  	if (!this.mHandles) {
  		this.mHandles = new imgchf.handles(this);
  	}
		return true;
  },
	removeHandle: function() {
		if (this.mHandles) {
			this.mHandles.remove();
		}
		this.mHandles = null;
	},
  createPointEdit: function() {
  	if (!this.bHandles) {
			this.mEditHandles = [];
			this.mCreateHandles = [];
			this.mCreatePointList = [];
			editM = this.mShape.getTransform();
      for (var i = 0; i < this.mPointList.length - 1; ++i) {
    		var new_id = "shape_" + (gShapeCounter++);
        this.mEditHandles[i] = new imgchf.handle.pointSelect(new_id, this, this.mPointList[i], TrackPoint, DeletePoint, imgchf.pointfcn.pt, i);
        editPt = dojo.gfx.matrix.multiplyPoint(editM, this.mPointList[i]);
        		
        this.mEditHandles[i].setPosition(editPt.x, editPt.y);
        gShapes[new_id] = this.mEditHandles[i];
     	}
 	    for (var i = 0; i < this.mPointList.length - 1; ++i) {
     		var new_id = "shape_" + (gShapeCounter++);
     		var midPt;
     		this.mCreateHandles[i] = new imgchf.handle.pointSelect(new_id, this, this.mPointList[i], CreateTrackPoint, NoDeletePoint, imgchf.pointfcn.pt, i);
     		this.mCreateHandles[i].setRadius(0);
		    this.mCreateHandles[i].setFill([255,255,255,0.001]);
		    this.mCreateHandles[i].setStroke();
        		
   			midPt = { x: (this.mPointList[i].x + this.mPointList[i + 1].x) / 2, 
       					  y: (this.mPointList[i].y + this.mPointList[i + 1].y) / 2 };

     		this.mCreatePointList[i] = midPt;
     		midPt = dojo.gfx.matrix.multiplyPoint(editM, midPt);
     		this.mCreateHandles[i].setPosition(midPt.x, midPt.y);
     	}
        	
 	    for ( i = 0; i < this.mPointList.length - 1; ++i) {
 	    	pt0 = dojo.gfx.matrix.multiplyPoint(editM, this.mPointList[i]);
 	    	pt1 = dojo.gfx.matrix.multiplyPoint(editM, this.mPointList[i + 1]);
 	    	pt2 = this.mCreateHandles[i].getPosition();
 	    	midPt = dojo.gfx.matrix.multiplyPoint(editM, pt2);
    	    	
 	    	if (this.createPtTolerance(pt0, pt1, midPt)) {
// 	    		dojo.debug("create handlers attached");
					dojo.event.connect(this.mCreateHandles[i].mShape.getEventSource(), 'onmouseover', this.mCreateHandles[i], 'setVisible');
					dojo.event.connect(this.mCreateHandles[i].mShape.getEventSource(), 'onmouseout', this.mCreateHandles[i], 'setInvisible');
					this.mCreateHandles[i].setRadius(6);
					gShapes[this.mCreateHandles[i].id] = this.mCreateHandles[i];
							
				}
 	    }
//			this.showPoints();
     	this.bHandles = true;
//        	dojo.debug(this.toSVG());
   	}
		this.showPoints();
   	return true;
 	},
 	createPtTolerance: function (p0, p1, pm) {
   	abdist0 = imgchf.distance(p0, pm);
   	abdist1 = imgchf.distance(p1, pm);
//   	dojo.debug("abdist0 " + abdist0 + " abdist1 " + abdist1);
   	if (abdist0 > 10 && abdist1 > 10) {
			return true;
   	}
   	return false;
 	},
 	removeEditHandles: function() {
 		if (this.bHandles) {
   		for (var i = 0; i < this.mEditHandles.length; ++i) {
				this.mEditHandles[i].removeShape();
   		}
   		for (i = 0; i < this.mCreateHandles.length; ++i) {
				dojo.event.disconnect(this.mCreateHandles[i].mShape.getEventSource(), 'onmouseover', this.mCreateHandles[i], 'setVisible');
				dojo.event.disconnect(this.mCreateHandles[i].mShape.getEventSource(), 'onmouseout', this.mCreateHandles[i], 'setInvisible');
				this.mCreateHandles[i].removeShape();
   		}
   		this.mEditHandles = null;
   		this.mCreateHandles = null;
//	   		this.bHandles = false;
 		}
 	},
	movePoint: function(x1, y1, i) {
	  	this.moveI = i;
  		this.mPointList[i] = {x: x1, y: y1};
  		
  		if (i === 0) {
	  		this.mPointList[this.mPointList.length - 1] = this.mPointList[i]; 
 			}
 			
	 		var pt0;
 			var pt2;
	 		var i0, i2;
 			if (i === 0) {
 				i0 = this.count - 2;
 				i2 = 1; 
	 		} else if (i == this.count - 2) {
 				i0 =  this.count - 3;
 				i2 = 0;
	 		} else {
 				i0 = i - 1;
 				i2 = i + 1;
	 		}
 		
 			pt0 = this.mPointList[i0];
 			pt2 = this.mPointList[i2];

  		tempPoly = [];
  		tempPoly[0] = pt0;
  		tempPoly[1] = {x: x1, y: y1};
  		tempPoly[2] = pt2;

    	if (this.testline != null && dojo.render.html.ie) {
    		var raw = this.testline.getNode();

    		var attr = [];
				attr.push("m");
				attr.push(pt0.x.toFixed());
				attr.push(pt0.y.toFixed());
				attr.push("l");
				attr.push(tempPoly[1].x.toFixed());
				attr.push(tempPoly[1].y.toFixed());
				attr.push("l");
				attr.push(pt2.x.toFixed());
				attr.push(pt2.y.toFixed());
				attr.push("e");
				raw.path.v = attr.join(" ");

    	} else {
		 		if (this.testline != null) {
 					surface.remove(this.testline);
 				}

		  	this.testline = surface.createPolyline(tempPoly).setStroke({color: "FC2A00", width: 2});
		  }
 		
			this.setCursor("move");
	    this.mEditHandles[i].update(this.getTransform(), this.mPointList[i]);
  			
    	this.mEditHandles[i0].getGfxShape().moveToFront();
    	this.mEditHandles[i].getGfxShape().moveToFront();
    	this.mEditHandles[i2].getGfxShape().moveToFront();

 	},
 	moveCreatePoint: function(x1, y1, i) {
		mat1 = this.getTransform();

		this.mNewPt = {x: x1, y: y1};
 		this.newI = i;
 		 		
 		var pt0;
 		var pt2;
 		var i0, i2;
		i0 = i;
		i2 = i + 1;

		if (i2 == (this.count - 1)) {
			i2 = 0;
		} 		
 		pt0 = this.mPointList[i0];
 		pt2 = this.mPointList[i2];
 		tempPoly = [];
 		tempPoly[0] = pt0;
 		tempPoly[1] = {x: x1, y: y1};
 		tempPoly[2] = pt2;

   	if (this.testline != null && dojo.render.html.ie) {
  		var raw = this.testline.getNode();

   		var attr = [];
			attr.push("m");
			attr.push(pt0.x.toFixed());
			attr.push(pt0.y.toFixed());
			attr.push("l");
			attr.push(tempPoly[1].x.toFixed());
			attr.push(tempPoly[1].y.toFixed());
			attr.push("l");
			attr.push(pt2.x.toFixed());
			attr.push(pt2.y.toFixed());
			attr.push("e");
			raw.path.v = attr.join(" ");

    	} else {
	 		if (this.testline != null) {
 				surface.remove(this.testline);
 			}

	  		this.testline = surface.createPolyline(tempPoly).setStroke({color: "FC2A00", width: 2});
	  	}
 		
    	this.mCreateHandles[i].update(this.getTransform(), this.mNewPt);
  		
    	this.mCreateHandles[i].getGfxShape().moveToFront();
    	this.mEditHandles[i].getGfxShape().moveToFront();
    	this.mEditHandles[i2].getGfxShape().moveToFront();

   	},
   	endMovePoint: function() {
	 		if (this.testline != null) {
 				surface.remove(this.testline);
 				this.testline = null;
			}
		
			var saveTransform = this.mShape.getTransform();

			if (this.newI >= 0) {
				// new bbox
				this.calcBBox(this.mNewPt);
				this.mPointList.splice(this.newI + 1,0,this.mNewPt);
	   	  this.count++;
			// set the position of the handle correctly
       	var new_id0 = "shape_" + (gShapeCounter++);
       	var newIndex = this.newI + 1;
       	var newEditHandle = new imgchf.handle.pointSelect(new_id0, this, this.mPointList[newIndex], TrackPoint, DeletePoint, imgchf.pointfcn.pt, newIndex);
       	editPt = this.mPointList[newIndex];
       	newEditHandle.setPosition(editPt.x, editPt.y);
       	gShapes[new_id0] = newEditHandle;
       		
       		// update handles about their new twins
        for (aa = this.newI + 1; aa < this.mEditHandles.length; aa++) {
        	this.mEditHandles[aa].setIndex(aa + 1);
        } 	
				// splice in the new handle
				this.mEditHandles.splice(this.newI + 1,0,newEditHandle);

				// move old create point and splice in new one
				i0 = this.newI;	
				i1 = this.newI + 1;
				i2 = this.newI + 2;


   			midPt = { x: (this.mPointList[i0].x + this.mPointList[i1].x) / 2, 
  							  y: (this.mPointList[i0].y + this.mPointList[i1].y) / 2 };

     		this.mCreatePointList[i0] = midPt;
     		this.mCreateHandles[i0].setPosition(midPt.x, midPt.y);

				// see if old creatept is active or not
     		pt0 = this.mPointList[i0];
    	  pt1 = this.mPointList[i1];
    	    	
 	    	if (!this.createPtTolerance(pt0, pt1, midPt)) {
 	    		dojo.debug("create handlers detached");
					dojo.event.disconnect(this.mCreateHandles[i0].mShape.getEventSource(), 'onmouseover', this.mCreateHandles[i0], 'setVisible');
					dojo.event.disconnect(this.mCreateHandles[i0].mShape.getEventSource(), 'onmouseout', this.mCreateHandles[i0], 'setInvisible');
					this.mCreateHandles[i0].setRadius(0);
					delete gShapes[this.mCreateHandles[i0].id];
				}
       	this.mCreateHandles[i0].setInvisible();
        	
       	// new create pt
       	new_id0 = "shape_" + (gShapeCounter++);
     		newCreateHandle = new imgchf.handle.pointSelect(new_id0, this, this.mPointList[i1], CreateTrackPoint, NoDeletePoint, imgchf.pointfcn.pt, i1);
     		newCreateHandle.setRadius(0);
		    newCreateHandle.setFill([255,255,255,0.001]);
		    newCreateHandle.setStroke();

   			midPt = { x: (this.mPointList[i1].x + this.mPointList[i2].x) / 2, 
  					  y: (this.mPointList[i1].y + this.mPointList[i2].y) / 2 };

				this.mCreatePointList.splice(this.newI + 1,0, midPt);
     		newCreateHandle.setPosition(midPt.x, midPt.y);
       	newCreateHandle.setInvisible();

//     		this.mCreateHandles[i0].setPosition(midPt.x, midPt.y);
     		
				// see if new creatept is active or not
     		pt0 = this.mPointList[i1];
    	  pt1 = this.mPointList[i2];
    	    	
 	    	if (this.createPtTolerance(pt0, pt1, midPt)) {
					newCreateHandle.setRadius(6);
	     		gShapes[newCreateHandle.id] = newCreateHandle;
					dojo.event.connect(newCreateHandle.mShape.getEventSource(), 'onmouseover', newCreateHandle, 'setVisible');
					dojo.event.connect(newCreateHandle.mShape.getEventSource(), 'onmouseout', newCreateHandle, 'setInvisible');
				}
				
				this.mCreateHandles.splice(i1,0,newCreateHandle);

       	// update handles about their new twins
        for (aa = this.newI + 1; aa < this.mCreateHandles.length; aa++) {
        	this.mCreateHandles[aa].setIndex(aa);
        } 	

	   	   this.newI = -1;
			}	else if (this.moveI >= 0) {
				// move old create points
				i1 = this.moveI;
				i2 = this.moveI + 1;
				i0 = this.moveI - 1;	
				this.calcBBox(this.mPointList[this.moveI]);

				if (i1 == 0) {
					i0 = this.mPointList.length - 2;
				} 		
			
   			midPt = { x: (this.mPointList[i0].x + this.mPointList[i1].x) / 2, 
   							  y: (this.mPointList[i0].y + this.mPointList[i1].y) / 2 };

     		this.mCreatePointList[i0] = midPt;
     		this.mCreateHandles[i0].setPosition(midPt.x, midPt.y);
     		
				// see if if this creatept is active or not
     		pt0 = this.mPointList[i0];
    	  pt1 = this.mPointList[i1];
    	    	
 	    	if (this.createPtTolerance(pt0, pt1, midPt)) {
// 	    		dojo.debug("create handlers attached");
					dojo.event.connect(this.mCreateHandles[i0].mShape.getEventSource(), 'onmouseover', this.mCreateHandles[i0], 'setVisible');
					dojo.event.connect(this.mCreateHandles[i0].mShape.getEventSource(), 'onmouseout', this.mCreateHandles[i0], 'setInvisible');
					this.mCreateHandles[i0].setRadius(6);
					
					gShapes[this.mCreateHandles[i0].id] = this.mCreateHandles[i0];
				} 
				else {
// 	    		dojo.debug("create handlers detached");
					dojo.event.disconnect(this.mCreateHandles[i0].mShape.getEventSource(), 'onmouseover', this.mCreateHandles[i0], 'setVisible');
					dojo.event.disconnect(this.mCreateHandles[i0].mShape.getEventSource(), 'onmouseout', this.mCreateHandles[i0], 'setInvisible');
					this.mCreateHandles[i0].setRadius(0);
					delete gShapes[this.mCreateHandles[i0].id];
				}

   			midPt = { x: (this.mPointList[i1].x + this.mPointList[i2].x) / 2, 
		   					  y: (this.mPointList[i1].y + this.mPointList[i2].y) / 2 };

     		this.mCreatePointList[i1] = midPt;
     		this.mCreateHandles[i1].setPosition(midPt.x, midPt.y);

				// see if if this creatept is active or not
     		pt0 = this.mPointList[i1];
    	  pt1 = this.mPointList[i2];
    	    	
 	    	if (this.createPtTolerance(pt0, pt1, midPt)) {
// 	    		dojo.debug("create handlers attached");
					dojo.event.connect(this.mCreateHandles[i1].mShape.getEventSource(), 'onmouseover', this.mCreateHandles[i1], 'setVisible');
					dojo.event.connect(this.mCreateHandles[i1].mShape.getEventSource(), 'onmouseout', this.mCreateHandles[i1], 'setInvisible');
					gShapes[this.mCreateHandles[i1].id] = this.mCreateHandles[i1];
				} else {
// 	    		dojo.debug("create handlers detached");
					dojo.event.disconnect(this.mCreateHandles[i1].mShape.getEventSource(), 'onmouseover', this.mCreateHandles[i1], 'setVisible');
					dojo.event.disconnect(this.mCreateHandles[i1].mShape.getEventSource(), 'onmouseout', this.mCreateHandles[i1], 'setInvisible');
					delete gShapes[this.mCreateHandles[i1].id];
				}
       		
     		this.moveI = -1;
			}

			// multiply points by the present matrix and set as the present points
			for (i = 0; i < this.mPointList.length; i++) {
   			this.mPointList[i] = dojo.gfx.matrix.multiplyPoint(saveTransform, this.mPointList[i]);
   		}
			if (this.mCreateHandles) {
	 	    for (i = 0; i < this.mCreateHandles.length; ++i) {
   				this.mCreatePointList[i] = dojo.gfx.matrix.multiplyPoint(saveTransform, this.mCreatePointList[i]);
  	    	this.mCreateHandles[i].update(saveTransform, this.mCreatePointList[i]);
      	}
				
			}
			// set transform to identity now that points have been transformed
	    this.mShape.setTransform();
		
    	if (dojo.render.html.ie) {
    		var raw = this.mShape.getNode();
//    		dojo.debug("raw path " + raw.path.v);
    		
    		var attr2 = [];
				attr2.push("m");
				attr2.push(this.mPointList[0].x.toFixed());
				attr2.push(this.mPointList[0].y.toFixed());
				var q;
			
				for (q = 1; q < this.mPointList.length; q++) {
					attr2.push("l");
					attr2.push(this.mPointList[q].x.toFixed());
					attr2.push(this.mPointList[q].y.toFixed());
				}

				attr2.push("e");
				raw.path.v = attr2.join(" ");
				
				this.calcCompleteBBox();
				this.update();
				this.removeHandle();
//			this.removeEditHandles();
//			this.bHandles = false;
//			this.createPointEdit();
				this.createHandles();
			
//			if (this.mHandles) {
//				this.mHandles.update();
//			}
    	} else {
    		var raw = this.mShape.getNode();
    	
//    			var p = this.shape.points;
    			var p = this.mPointList;
				var attr3 = [];

				for(var i = 0; i < p.length; ++i){
					attr3.push(p[i].x.toFixed(8));
					attr3.push(p[i].y.toFixed(8));
				}
				raw.setAttribute("points", attr3.join(" "));

//		raw.setAttribute("d", "M100 100 200 100 200 200Q200 250 125 175T100 100z"); 
//		.createPath("M100 100 200 100 200 200Q200 250 125 175T100 100z")

				this.calcCompleteBBox();
				this.update();
				this.removeHandle();
//			this.removeEditHandles();
//			this.bHandles = false;
//			this.createPointEdit();
				this.createHandles();

    	}
	    this.setCursor("move");
 	},
 	move: function(dx, dy) {
 		this.mShape.applyTransform({dx: dx, dy: dy});
 	
 	},
	removeShape: function() {
		this.removeEditHandles();
		this.bHandles = false;
		this.removeHandle();
		surface.remove(this.mShape);
	},
	toSVG: function() {
		var svgString = "";

		if (this.mPointList.length > 2){
			m = this.getTransform();

			// transform bbox and get translation, scaling matrices to 
			// put svgoutput in unit square coords
			
			bbox = this.getBoundingBox();
			
			p = [];
			t = [];
			
			p[0] = { x:bbox.x,              y:bbox.y };
			p[1] = { x:bbox.x + bbox.width, y:bbox.y };
			p[2] = { x:bbox.x,              y:bbox.y + bbox.height};
			p[3] = { x:bbox.x + bbox.width, y:bbox.y + bbox.height};

			t[0] = dojo.gfx.matrix.multiplyPoint(m, p[0]);
			t[1] = dojo.gfx.matrix.multiplyPoint(m, p[1]);
			t[2] = dojo.gfx.matrix.multiplyPoint(m, p[2]);
			t[3] = dojo.gfx.matrix.multiplyPoint(m, p[3]);
			
			mint = { x:1000, y:1000 };
			maxt = { x:-1000, y:-1000};
			for ( i = 0; i < 4; i++) {
				if ( t[i].x < mint.x ) {
					mint.x = t[i].x;
				}
				if ( t[i].y < mint.y ) {
					mint.y = t[i].y;
				}
				if ( t[i].x > maxt.x ) {
					maxt.x = t[i].x;
				}
				if ( t[i].y > maxt.y ) {
					maxt.y = t[i].y;
				}
			}

			scalex = 1.0 / (maxt.x - mint.x);
			scaley = 1.0 / (maxt.y - mint.y);
			scalefactor = scalex > scaley ? scaley : scalex;
			svgMatrix = dojo.gfx.matrix.multiply(dojo.gfx.matrix.scale(1.0/450.0), m);			
			svgString = "<polygon points=\"";
			for (i = 0; i < this.mPointList.length - 1; i++) {
				outpoint = dojo.gfx.matrix.multiplyPoint(svgMatrix, this.mPointList[i]);
				
				pointString = outpoint.x.toFixed(5) + "," + outpoint.y.toFixed(5) + " ";
				svgString += pointString;
			}			
			svgString += "\" \/>";
		}
		return svgString;
	},
	load: function(svgPoly) {
		var extractedPoint;
		
		if (dojo.render.html.ie) {
			var pointArrayString = svgPoly.getAttribute("points");
			var pointArray = pointArrayString.split(" ");
			for (i = 0; i < pointArray.length; i++) {
				if ( pointArray[i].length < 1 ) {
					continue;
				}
					
				var pointString = pointArray[i].split(",");
				extractedPoint = {x: parseFloat(pointString[0]), y: parseFloat(pointString[1])}; 
	 			this.mPointList[this.count] = {x: extractedPoint.x * 450.0, y: extractedPoint.y * 450.0};
	 			this.count++;
			}
			this.mPointList[this.count] = this.mPointList[0];
 			this.count++;
//			dojo.debug("pointArray in gfx_shape is:" + pointArray);
				
		} else {
			for (i = 0; i < svgPoly.points.numberOfItems; i++) {
				extractedPoint = svgPoly.points.getItem(i);
	 			this.mPointList[this.count] = {x: extractedPoint.x * 450.0, y: extractedPoint.y * 450.0};
	 			this.count++;
			}
			this.mPointList[this.count] = this.mPointList[0];
 			this.count++;
		}

  		if (this.mShape) {
  			surface.remove(this.mShape);
  		}
	    this.mShape = surface.createPolyline(this.mPointList).setFill([62, 62, 255, 0.9]);
	    this.setId();

		this.mShape.moveToFront();
		if (this.createPointEdit()) {
			if (theEditor.editedshape != this) {
				if (theEditor.editedshape) {
					theEditor.editedshape.hidePoints();
					theEditor.editedshape.removeHandle();
				}
				this.createHandles();
				theEditor.editedshape = this;
			}
		}
  	theEditor.removeClickText();
		theEditor.editedshape = this;
    toolSet.setTool("editTool");
    bg.select("move");


    dojo.debug("loading of this polygon is done");
	},
	update: function() {
		if (this.mHandles) {
			this.mHandles.update();
		}
		if (this.bVisibleEdit) {
			if (this.mEditHandles) {
   	    for (i = 0; i < this.mEditHandles.length; ++i) {
 		    	this.mEditHandles[i].update(this.getTransform(), this.mPointList[i]);
 	    	}
				
			}
			if (this.mCreateHandles) {
   	    for (i = 0; i < this.mCreateHandles.length; ++i) {
 		    	this.mCreateHandles[i].update(this.getTransform(), this.mCreatePointList[i]);
 	    	}
	
			}
		}
	},
	hidePoints: function() {
		if (this.bVisibleEdit) {
			if (this.mEditHandles) {
  	    for (i = 0; i < this.mEditHandles.length; ++i) {
   	    	if (dojo.render.html.ie) {
						dojo.html.setStyle(this.mEditHandles[i].mShape.getEventSource(), 'visibility', 'hidden');
					} else {
				    	this.mEditHandles[i].mShape.getEventSource().setAttribute('visibility', 'hidden');
			    }
 	    	}
				
			}
			if (this.mCreateHandles) {
   	    for (i = 0; i < this.mCreateHandles.length; ++i) {
   	    	if (dojo.render.html.ie) {
						dojo.html.setStyle(this.mCreateHandles[i].mShape.getEventSource(), 'visibility', 'hidden');
	 	    	} else {
			    	this.mCreateHandles[i].mShape.getEventSource().setAttribute('visibility', 'hidden');
			    }
	    	}
				
			}
			this.bVisibleEdit = false;
		}
	},
	showPoints: function() {
		this.bVisibleEdit = true;
		if (this.mEditHandles) {
 	    for (i = 0; i < this.mEditHandles.length; ++i) {
 	    	if (dojo.render.html.ie) {
					dojo.html.setStyle(this.mEditHandles[i].mShape.getEventSource(), 'visibility', 'visible');
 	    	} else {
			    this.mEditHandles[i].mShape.getEventSource().setAttribute('visibility', 'visible');
				}
    	}
			
		}
		if (this.mCreateHandles) {
 	    for (i = 0; i < this.mCreateHandles.length; ++i) {
 	    	if (dojo.render.html.ie) {
					dojo.html.setStyle(this.mCreateHandles[i].mShape.getEventSource(), 'visibility', 'visible');
 	    	} else {
			    this.mCreateHandles[i].mShape.getEventSource().setAttribute('visibility', 'visible');
				}
    	}
			
		}
	},
	moveToFront: function() {
		this.mShape.moveToFront();
    for (i = 0; i < this.mEditHandles.length; ++i) {
    	this.mEditHandles[i].getGfxShape().moveToFront();
    }
    for (i = 0; i < this.mCreateHandles.length; ++i) {
    	this.mCreateHandles[i].getGfxShape().moveToFront();
    }
	},
	calcBBox : function(p) {
		var bboxorig = this.getBoundingBox();
			
		var bbox = {
			l:		bboxorig.x, 
			r:		bboxorig.x + bboxorig.width, 
			t:		bboxorig.y, 
			b:		bboxorig.y + bboxorig.height
		};
		if(bbox.l > p.x) bbox.l = p.x;
		if(bbox.r < p.x) bbox.r = p.x;
		if(bbox.t > p.y) bbox.t = p.y;
		if(bbox.b < p.y) bbox.b = p.y;
			
		this.mShape.bbox = {
			x:		bbox.l, 
			y:		bbox.t, 
			width:	bbox.r - bbox.l, 
			height:	bbox.b - bbox.t
		};
	},
	calcCompleteBBox: function() {
		var p = this.mPointList;
		var l = p.length;
		var t = p[0];
		bbox = {l: t.x, t: t.y, r: t.x, b: t.y};
		for(var i = 1; i < l; ++i){
			t = p[i];
			if(bbox.l > t.x) bbox.l = t.x;
			if(bbox.r < t.x) bbox.r = t.x;
			if(bbox.t > t.y) bbox.t = t.y;
			if(bbox.b < t.y) bbox.b = t.y;
		}
		this.mShape.bbox = {
			x:		bbox.l, 
			y:		bbox.t, 
			width:	bbox.r - bbox.l, 
			height:	bbox.b - bbox.t
		};
	},
	delPoint: function(aHandle) {
		var aIndex = aHandle.getIndex();
		
		if (this.mPointList.length > 4) {
			if (aIndex > 0) {
				dojo.debug("length before splice " + this.mPointList.length);
				this.mPointList.splice(aIndex,1);
				dojo.debug("length after splice " + this.mPointList.length);
				
				this.removeHandle();
				this.removeEditHandles();
				this.bHandles = false;
				this.createPointEdit();
				this.createHandles();
				this.update();
			} else {
				this.mPointList.splice(this.mPointList.length - 1,1);
				this.mPointList[0] = this.mPointList[this.mPointList.length - 1];
			}
			this.count--;
			this.moveI = -1;	
			this.calcCompleteBBox();
			this.removeHandle();
			this.removeEditHandles();
			this.bHandles = false;
			this.createPointEdit();
			this.createHandles();
			this.update();
		}
	},
	getBoundingBox: function () {
  	return this.mShape.getBoundingBox();
	},
	tossClosePoints: function() {
		if (this.mPointList.length == 0)
			return;
			
		var i, k, pv;
		var result = [];
		
		pv = 0;
		k = 0;
		result[0] = this.mPointList[0]; 
		for ( i = 2; i < this.mPointList.length; i++ ) {
			currentVector = {x: this.mPointList[i].x - this.mPointList[pv].x, y: this.mPointList[i].y - this.mPointList[pv].y};
			dotProduct = currentVector.x * currentVector.x + currentVector.y * currentVector.y;              
			if (dotProduct < 3.5) 
				continue;
			result[k++] = this.mPointList[i];
			pv = i;
		}	
		if (pv < this.mPointList.length)
			result[k++] = this.mPointList[this.mPointList.length - 1];
			
		this.mPointList = result;					 
	},
	simplifyPolyline: function(tolerance) {
		// first toss out points too close to each other
		
		this.tossClosePoints();
    // set default tolerance, if not defined
    if ( tolerance == null ) tolerance == 0.25;

    // make sure parameters are OK
    // NOTE: we can still have faulty input if the contents of the array are
    // not all Point2D objects.  I may want to add that at a future date
//    if ( points.constructor !== Array )
//        throw new Error("SimplifyPolyline: points must be an array");
    if ( isNaN(tolerance) )
        throw new Error("simplifyPolyline: tolerance must be a number");
    if ( tolerance < 0 )
        throw new Error("simplifyPolyline: tolerance must be a positive number");

    // begin processing
    var length = this.mPointList.length;
    var result = [];

    // process all points
    switch (length) {
        case 0:
            // do nothing
            break;
        case 1:
            result.push( this.mPointList[0] );
            break;
        case 2:
            result.push( this.mPointList[0] );
            result.push( this.mPointList[1] );
            break;
        default:
            // NOTE: There are a number of places in this algorithm where we
            // need to add a point to the result array and perform some other
            // maintenance.  As opposed to repeating that code in each
            // location, we create a private helper function to perform these
            // tasks for us.
            var acceptPoint = function() {
                lastAcceptedPoint = previousPoint;
                result.push(lastAcceptedPoint);
                insiders = [];
            }

            // init variables, so we can...
            var previousPoint = this.mPointList[0];
            var lastAcceptedPoint;
            var insiders;

            // ...add the first point to the result array.  This also inits
            // lastAcceptedPoint and insiders
            acceptPoint();

            // pretend like we've already visited the second point in the
            // original point array.  We can do this since the first two points
            // form a straight line which cannot be outside of the tolerance
            var currentPoint = this.mPointList[1];

            // we also need the vector between those two points
            var currentVector = {x: currentPoint.x - previousPoint.x, y: currentPoint.y - previousPoint.y};
//            var currentVector = Vector2D.fromPoints(previousPoint, currentPoint);
            var previousVector;

            // process the rest of the original point array
            for ( var i = 2; i < length; i++ ) {
                previousPoint  = currentPoint;
                currentPoint   = this.mPointList[i];
                previousVector = currentVector;
		            currentVector = {x: currentPoint.x - previousPoint.x, y: currentPoint.y - previousPoint.y};
//                currentVector  = Vector2D.fromPoints(previousPoint, currentPoint);
  							//    return this.x*that.x + this.y*that.y;
  							
  							dotProduct = previousVector.x * currentVector.x + previousVector.y * currentVector.y;              
//                if ( previousVector.dot(currentVector) <= 0 ) {
                if ( dotProduct <= 0 ) {
                    // NOTE: the value of the dot product of two vectors is a
                    // scalar multiple of the cosine of the angle between the
                    // vectors.  We want to identify vectors having angles
                    // between -90 degrees and 90 degrees, inclusive, to
                    // preserve sharp angles in the original point data.  Since
                    // the cosine of any angle in that range is negative (and
                    // zero at -90 and 90), we only need to test the sign of
                    // the dot product to recognize if we have an acute angle.

                    // found a sharp angle, so add it to the result
                    acceptPoint();
                } else {
                    // find the vector between the last accepted point and the
                    // current point we are testing
				            var candidate = {x: currentPoint.x - lastAcceptedPoint.x, y: currentPoint.y - lastAcceptedPoint.y};
//                    var candidate = Vector2D.fromPoints(lastAcceptedPoint, currentPoint);

                    // add the vector between the last accepted point and the
                    // previous point we tested.  Pushing this vector onto the
                    // insiders array and serves as a cache so we don't have to
                    // keep creating the same vectors each time we come to the
                    // following loop.
                    insiders.push(
				            	{x: previousPoint.x - lastAcceptedPoint.x, y: previousPoint.y - lastAcceptedPoint.y}
//                        Vector2D.fromPoints(lastAcceptedPoint, previousPoint)
                    );

                    // determine if the candidate vector is outside of the
                    // specified tolerance
                    for ( var j = 0; j < insiders.length; j++ ) {
                        // find the shortest vector from the previous candidate
                        // and the current candiate
//                        Vector2D.prototype.project = function(that) {
//											    var percent = this.dot(that) / that.dot(that);

//											    return that.multiply(percent);
//												};
                        
                        
//													Vector2D.prototype.perpendicular = function(that) {
//                            return this.subtract(this.project(that));
                        percent = (insiders[j].x * candidate.x + insiders[j].y * candidate.y) / (candidate.x * candidate.x + candidate.y * candidate.y);
                        projcandidate = {x: candidate.x * percent, y: candidate.y * percent};
                        var perp = {x: insiders[j].x - projcandidate.x, y: insiders[j].y - projcandidate.y};
                        perplength = Math.sqrt(perp.x*perp.x + perp.y*perp.y);
//                        var perp = insiders[j].perpendicular(candidate);

                        // if the length of that vector exceeds our tolerance,
                        // then this point is outside of our tolerance.  Store
                        // the previous point (since it was not outside of out
                        // tolerance) and reset things to start processing from
                        // there
//                        if ( perp.length() > tolerance ) {
                        if ( perplength > tolerance ) {
                            acceptPoint();
                            break;
                        }
                    }
                }
            }
            
            // add the final point from the original point array to our result
            // since it is impossible for the previous code to do so
            result.push( this.mPointList[this.mPointList.length-1] );
    }
		this.count = result.length;
		this.mPointList = result;
//    return result;
},
	
  testline: null,
	endBall: null
});

/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////

dojo.declare("imgchf.path", imgchf.shape, {
  initializer: function(id) {
    dojo.debug("imgchf.path being created w/pointlist ");
    this.count = 0;
    this.mPointList = [];
    this.mCreatePointList = [];
    this.bHandles = false;
    this.nNewPt = null;
    this.newI = -1;
    this.moveI = -1;
    this.bVisibleEdit = true;
//    this.endBall = null;
//    superclass.this.id =	"shape_" + (gShapeCounter++);
 	},
 	addPoint: function(point, closeit) {
 		var abdist;
 		var shapeEnded = false;
  		
    if (this.count == 0) {
			this.endBall = surface.createCircle({cx: point.x, cy: point.y, r: 7})
			.setFill([0, 0, 255, 1.0])
			.setStroke({color: [0, 255, 255, 1.0], width: 1});
			// first point
	 		this.mPointList[this.count] = {info: "M", x: point.x, y: point.y};
    } else {
    	if ((this.count % 2) == 0) {
		 		this.mPointList[this.count] = {info: "L", x: point.x, y: point.y};
//		 	} else if (this.count == 5) {
//		 		this.mPointList[this.count] = {info: "T", x: point.x, y: point.y};
		 	} else {
		 		this.mPointList[this.count] = {info: "Q", x: point.x, y: point.y};
		 	}
    }
	    
 		if (this.mShape) {
 			surface.remove(this.mShape);
 		}
 		this.count = this.count + 1;

    if (this.count > 1) {
    	abdist = imgchf.distance(this.mPointList[0], this.mPointList[this.count - 1]);
	    	
    	if (abdist < 8) {
	  		this.closeShape();
	  		shapeEnded = true;
    	}
    }
	    
    if (shapeEnded) {
//  		this.mShape = surface.createPolyline(this.mPointList).setStroke({color: "black", width: 2}).setFill([62, 62, 255, 0.9]);
			var p1 = "M100 100 200 100 200 200Q200 250 125 175T100 100z";
			var foo = p1.match(dojo.gfx.pathRegExp);
			dojo.debug("foo is " + foo);
			
			var pathattr = [];
			
			pathattr.push("M");
			pathattr.push(this.mPointList[0].x.toFixed());
			pathattr.push(this.mPointList[0].y.toFixed());
			var q;
			
			for (q = 1; q < this.mPointList.length; q++) {
//    		dojo.debug("x, y " + this.mPointList[q].x + "," + this.mPointList[q].y);
//				if (this.mPointList[q].info != "L") {
					pathattr.push(this.mPointList[q].info);
//				}
				pathattr.push(this.mPointList[q].x.toFixed());
				pathattr.push(this.mPointList[q].y.toFixed());
			}

			pathattr.push("z");
			pathstring = pathattr.join(" ");

			this.mShape = surface.createPath(pathstring);

//  		this.mShape = surface.createPath(this.mPointList)
  		this.mShape.setStroke({color: "black", width: 2}).setFill([62, 62, 255, 0.9]);
    	this.setCursor("move");
	    this.createPointEdit();
	    this.createHandles();
		    
	    if (theEditor.editedshape) {
				theEditor.editedshape.hidePoints();
				theEditor.editedshape.removeHandle();
			}
			theEditor.editedshape = this;
		  toolSet.setTool("editTool");
		  bg.select("move");
		    
    } else {
//	    this.mShape = surface.createPolyline(this.mPointList).setStroke({color: "black", width: 2});
			dojo.debug("shape not ended");
	    this.mShape = surface.createPolyline(this.mPointList).setStroke({color: "black", width: 2});
		}
	    
    this.setId();
    return shapeEnded;
  	},
  closeShape: function() {
//  	if (this.count > 0) {
// 			this.mPointList[this.count - 1] = this.mPointList[0];
// 		}
 		if (this.endBall) {
	  		surface.remove(this.endBall);
	  		this.endBall = null;
	  	}
  		if (this.testline) {
  			surface.remove(this.testline);
		    this.testline = null;
  		}
  		
  		return this.count;
   	},
  	closeShapeArbitrary: function() {
  		if (this.mShape) {
  			surface.remove(this.mShape);
  		}
  		if (this.count > 0) {
 			this.mPointList[this.count] = this.mPointList[0];
 			this.count++;
	  		this.mShape = surface.createPolyline(this.mPointList).setStroke({color: "black", width: 2}).setFill([62, 62, 255, 0.9]);;
	    	this.setCursor("move");
		    this.createPointEdit();
		    this.createHandles();
		    
		    if (theEditor.editedshape) {
				theEditor.editedshape.hidePoints();
				theEditor.editedshape.removeHandle();
			}
			theEditor.editedshape = this;
			toolSet.setTool("editTool");
			bg.select("move");
			this.setId();

 		}
 		if (this.endBall) {
	  	surface.remove(this.endBall);
	  	this.endBall = null;
	  }
  	if (this.testline) {
  		surface.remove(this.testline);
		  this.testline = null;
		}
  		
  	return this.count;
  },
  mousePoint: function(point) {
  	tempPoly = [];
  	tempPoly[0] = this.mPointList[this.count - 1];
  	tempPoly[1] = point;

    abdist = imgchf.distance(this.mPointList[0], tempPoly[1]);
	    	
    if (abdist < 8) {
		 	this.endBall.setFill([255, 0, 0, 1.0]);
    }
    else {
		 	this.endBall.setFill([0, 0, 255, 1.0]);
    }
    	
    if (this.testline != null && dojo.render.html.ie) {
//    	        dojo.byId("statusfield").innerHTML = "foo" + tempPoly[1].x + "," + tempPoly[1].y;
    	
    	var raw = this.testline.getNode();
			raw.path.v = "m" + tempPoly[0].x.toFixed() + " " + tempPoly[0].y.toFixed() +
			"l" + tempPoly[1].x.toFixed() + " " + tempPoly[1].y.toFixed() + "e";
    } else {
	 		if (this.testline != null) {
 				surface.remove(this.testline);
 			}

	  	this.testline = surface.createPolyline(tempPoly).setStroke({color: "FC2A00", width: 2});
	  }
  },
  createPointEdit: function() {
  	if (!this.bHandles) {
			this.mEditHandles = [];
			this.mCreateHandles = [];
			this.mCreatePointList = [];
			editM = this.mShape.getTransform();
      for (var i = 0; i < this.mPointList.length - 1; ++i) {
    		var new_id = "shape_" + (gShapeCounter++);
        this.mEditHandles[i] = new imgchf.handle.pointSelect(new_id, this, this.mPointList[i], TrackPoint, DeletePoint, imgchf.pointfcn.pt, i);
        editPt = dojo.gfx.matrix.multiplyPoint(editM, this.mPointList[i]);
        		
        this.mEditHandles[i].setPosition(editPt.x, editPt.y);
        gShapes[new_id] = this.mEditHandles[i];
     	}
 	    for (var i = 0; i < this.mPointList.length - 1; ++i) {
     		var new_id = "shape_" + (gShapeCounter++);
     		var midPt;
     		this.mCreateHandles[i] = new imgchf.handle.pointSelect(new_id, this, this.mPointList[i], CreateTrackPoint, NoDeletePoint, imgchf.pointfcn.pt, i);
     		this.mCreateHandles[i].setRadius(0);
		    this.mCreateHandles[i].setFill([255,255,255,0.001]);
		    this.mCreateHandles[i].setStroke();
        		
   			midPt = { x: (this.mPointList[i].x + this.mPointList[i + 1].x) / 2, 
       					  y: (this.mPointList[i].y + this.mPointList[i + 1].y) / 2 };

     		this.mCreatePointList[i] = midPt;
     		midPt = dojo.gfx.matrix.multiplyPoint(editM, midPt);
     		this.mCreateHandles[i].setPosition(midPt.x, midPt.y);
     	}
        	
 	    for ( i = 0; i < this.mPointList.length - 1; ++i) {
 	    	pt0 = dojo.gfx.matrix.multiplyPoint(editM, this.mPointList[i]);
 	    	pt1 = dojo.gfx.matrix.multiplyPoint(editM, this.mPointList[i + 1]);
 	    	pt2 = this.mCreateHandles[i].getPosition();
 	    	midPt = dojo.gfx.matrix.multiplyPoint(editM, pt2);
    	    	
 	    	if (this.createPtTolerance(pt0, pt1, midPt)) {
// 	    		dojo.debug("create handlers attached");
					dojo.event.connect(this.mCreateHandles[i].mShape.getEventSource(), 'onmouseover', this.mCreateHandles[i], 'setVisible');
					dojo.event.connect(this.mCreateHandles[i].mShape.getEventSource(), 'onmouseout', this.mCreateHandles[i], 'setInvisible');
					this.mCreateHandles[i].setRadius(6);
					gShapes[this.mCreateHandles[i].id] = this.mCreateHandles[i];
							
				}
 	    }
//			this.showPoints();
     	this.bHandles = true;
//        	dojo.debug(this.toSVG());
   	}
		this.showPoints();
   	return true;
 	},
 	createPtTolerance: function (p0, p1, pm) {
   	abdist0 = imgchf.distance(p0, pm);
   	abdist1 = imgchf.distance(p1, pm);
//   	dojo.debug("abdist0 " + abdist0 + " abdist1 " + abdist1);
   	if (abdist0 > 10 && abdist1 > 10) {
			return true;
   	}
   	return false;
 	},
 	removeEditHandles: function() {
 		if (this.bHandles) {
   		for (var i = 0; i < this.mEditHandles.length; ++i) {
				this.mEditHandles[i].removeShape();
   		}
   		for (i = 0; i < this.mCreateHandles.length; ++i) {
				dojo.event.disconnect(this.mCreateHandles[i].mShape.getEventSource(), 'onmouseover', this.mCreateHandles[i], 'setVisible');
				dojo.event.disconnect(this.mCreateHandles[i].mShape.getEventSource(), 'onmouseout', this.mCreateHandles[i], 'setInvisible');
				this.mCreateHandles[i].removeShape();
   		}
   		this.mEditHandles = null;
   		this.mCreateHandles = null;
//	   		this.bHandles = false;
 		}
 	},
	movePoint: function(x1, y1, i) {
	  	this.moveI = i;
  		this.mPointList[i].x = x1;
  		this.mPointList[i].y = y1;

  		
  		if (i === 0) {
	  		this.mPointList[this.mPointList.length - 1] = this.mPointList[i]; 
 			}
 			
	 		var pt0;
 			var pt2;
	 		var i0, i2;
 			if (i === 0) {
 				i0 = this.count - 2;
 				i2 = 1; 
	 		} else if (i == this.count - 2) {
 				i0 =  this.count - 3;
 				i2 = 0;
	 		} else {
 				i0 = i - 1;
 				i2 = i + 1;
	 		}
 		
 			pt0 = this.mPointList[i0];
 			pt2 = this.mPointList[i2];

  		tempPoly = [];
  		tempPoly[0] = pt0;
  		tempPoly[1] = {x: x1, y: y1};
  		tempPoly[2] = pt2;

    	if (this.testline != null && dojo.render.html.ie) {
//    	        dojo.byId("statusfield").innerHTML = "foo" + tempPoly[1].x + "," + tempPoly[1].y;
    	
    		var raw = this.testline.getNode();
    		
//  	 		dojo.byId("statusfield").innerHTML = "foo " + foo[0] + "," + foo[1] + "," + foo[2] + "," + foo[3];

    		var attr = [];
				attr.push("m");
				attr.push(pt0.x.toFixed());
				attr.push(pt0.y.toFixed());
				attr.push("l");
				attr.push(tempPoly[1].x.toFixed());
				attr.push(tempPoly[1].y.toFixed());
				attr.push("l");
				attr.push(pt2.x.toFixed());
				attr.push(pt2.y.toFixed());
				attr.push("e");
				raw.path.v = attr.join(" ");

    	} else {
		 		if (this.testline != null) {
 					surface.remove(this.testline);
 				}

		  	this.testline = surface.createPolyline(tempPoly).setStroke({color: "FC2A00", width: 2});
		  }
 		
			this.setCursor("move");
	    this.mEditHandles[i].update(this.getTransform(), this.mPointList[i]);
  			
    	this.mEditHandles[i0].getGfxShape().moveToFront();
    	this.mEditHandles[i].getGfxShape().moveToFront();
    	this.mEditHandles[i2].getGfxShape().moveToFront();

 	},
 	moveCreatePoint: function(x1, y1, i) {
		mat1 = this.getTransform();

		this.mNewPt = {x: x1, y: y1};
 		this.newI = i;
 		 		
 		var pt0;
 		var pt2;
 		var i0, i2;
		i0 = i;
		i2 = i + 1;

		if (i2 == (this.count - 1)) {
			i2 = 0;
		} 		
 		pt0 = this.mPointList[i0];
 		pt2 = this.mPointList[i2];
 		tempPoly = [];
 		tempPoly[0] = pt0;
 		tempPoly[1] = {x: x1, y: y1};
 		tempPoly[2] = pt2;

   	if (this.testline != null && dojo.render.html.ie) {
//    	        dojo.byId("statusfield").innerHTML = "foo" + tempPoly[1].x + "," + tempPoly[1].y;
    	
  		var raw = this.testline.getNode();
    		
//  	 		dojo.byId("statusfield").innerHTML = "foo " + foo[0] + "," + foo[1] + "," + foo[2] + "," + foo[3];

   		var attr = [];
			attr.push("m");
			attr.push(pt0.x.toFixed());
			attr.push(pt0.y.toFixed());
			attr.push("l");
			attr.push(tempPoly[1].x.toFixed());
			attr.push(tempPoly[1].y.toFixed());
			attr.push("l");
			attr.push(pt2.x.toFixed());
			attr.push(pt2.y.toFixed());
			attr.push("e");
			raw.path.v = attr.join(" ");

    	} else {
	 		if (this.testline != null) {
 				surface.remove(this.testline);
 			}

	  		this.testline = surface.createPolyline(tempPoly).setStroke({color: "FC2A00", width: 2});
	  	}
 		
    	this.mCreateHandles[i].update(this.getTransform(), this.mNewPt);
  		
    	this.mCreateHandles[i].getGfxShape().moveToFront();
    	this.mEditHandles[i].getGfxShape().moveToFront();
    	this.mEditHandles[i2].getGfxShape().moveToFront();

   	},
   	endMovePoint: function() {
   		dojo.debug("endMovePoint");
	 		if (this.testline != null) {
 				surface.remove(this.testline);
 				this.testline = null;
			}
		
			var saveTransform = this.mShape.getTransform();

			if (this.newI >= 0) {
				// new bbox
				this.calcBBox(this.mNewPt);
				this.mPointList.splice(this.newI + 1,0,this.mNewPt);
	   	  this.count++;
			// set the position of the handle correctly
       	var new_id0 = "shape_" + (gShapeCounter++);
       	var newIndex = this.newI + 1;
       	var newEditHandle = new imgchf.handle.pointSelect(new_id0, this, this.mPointList[newIndex], TrackPoint, DeletePoint, imgchf.pointfcn.pt, newIndex);
       	editPt = this.mPointList[newIndex];
       	newEditHandle.setPosition(editPt.x, editPt.y);
       	gShapes[new_id0] = newEditHandle;
       		
       		// update handles about their new twins
        for (aa = this.newI + 1; aa < this.mEditHandles.length; aa++) {
        	this.mEditHandles[aa].setIndex(aa + 1);
        } 	
				// splice in the new handle
				this.mEditHandles.splice(this.newI + 1,0,newEditHandle);

				// move old create point and splice in new one
				i0 = this.newI;	
				i1 = this.newI + 1;
				i2 = this.newI + 2;


   			midPt = { x: (this.mPointList[i0].x + this.mPointList[i1].x) / 2, 
  							  y: (this.mPointList[i0].y + this.mPointList[i1].y) / 2 };

     		this.mCreatePointList[i0] = midPt;
     		this.mCreateHandles[i0].setPosition(midPt.x, midPt.y);

				// see if old creatept is active or not
     		pt0 = this.mPointList[i0];
    	  pt1 = this.mPointList[i1];
    	    	
 	    	if (!this.createPtTolerance(pt0, pt1, midPt)) {
 	    		dojo.debug("create handlers detached");
					dojo.event.disconnect(this.mCreateHandles[i0].mShape.getEventSource(), 'onmouseover', this.mCreateHandles[i0], 'setVisible');
					dojo.event.disconnect(this.mCreateHandles[i0].mShape.getEventSource(), 'onmouseout', this.mCreateHandles[i0], 'setInvisible');
					this.mCreateHandles[i0].setRadius(0);
					delete gShapes[this.mCreateHandles[i0].id];
				}
       	this.mCreateHandles[i0].setInvisible();
        	
       	// new create pt
       	new_id0 = "shape_" + (gShapeCounter++);
     		newCreateHandle = new imgchf.handle.pointSelect(new_id0, this, this.mPointList[i1], CreateTrackPoint, NoDeletePoint, imgchf.pointfcn.pt, i1);
     		newCreateHandle.setRadius(0);
		    newCreateHandle.setFill([255,255,255,0.001]);
		    newCreateHandle.setStroke();

   			midPt = { x: (this.mPointList[i1].x + this.mPointList[i2].x) / 2, 
  					  y: (this.mPointList[i1].y + this.mPointList[i2].y) / 2 };

				this.mCreatePointList.splice(this.newI + 1,0, midPt);
     		newCreateHandle.setPosition(midPt.x, midPt.y);
       	newCreateHandle.setInvisible();

//     		this.mCreateHandles[i0].setPosition(midPt.x, midPt.y);
     		
				// see if new creatept is active or not
     		pt0 = this.mPointList[i1];
    	  pt1 = this.mPointList[i2];
    	    	
 	    	if (this.createPtTolerance(pt0, pt1, midPt)) {
					newCreateHandle.setRadius(6);
	     		gShapes[newCreateHandle.id] = newCreateHandle;
					dojo.event.connect(newCreateHandle.mShape.getEventSource(), 'onmouseover', newCreateHandle, 'setVisible');
					dojo.event.connect(newCreateHandle.mShape.getEventSource(), 'onmouseout', newCreateHandle, 'setInvisible');
				}
				
				this.mCreateHandles.splice(i1,0,newCreateHandle);

       	// update handles about their new twins
        for (aa = this.newI + 1; aa < this.mCreateHandles.length; aa++) {
        	this.mCreateHandles[aa].setIndex(aa);
        } 	

	   	   this.newI = -1;
			}	else if (this.moveI >= 0) {
				// move old create points
				i1 = this.moveI;
				i2 = this.moveI + 1;
				i0 = this.moveI - 1;	
				this.calcBBox(this.mPointList[this.moveI]);

				if (i1 == 0) {
					i0 = this.mPointList.length - 2;
				} 		
			
   			midPt = { x: (this.mPointList[i0].x + this.mPointList[i1].x) / 2, 
   							  y: (this.mPointList[i0].y + this.mPointList[i1].y) / 2 };

     		this.mCreatePointList[i0] = midPt;
     		this.mCreateHandles[i0].setPosition(midPt.x, midPt.y);
     		
				// see if if this creatept is active or not
     		pt0 = this.mPointList[i0];
    	  pt1 = this.mPointList[i1];
    	    	
 	    	if (this.createPtTolerance(pt0, pt1, midPt)) {
// 	    		dojo.debug("create handlers attached");
					dojo.event.connect(this.mCreateHandles[i0].mShape.getEventSource(), 'onmouseover', this.mCreateHandles[i0], 'setVisible');
					dojo.event.connect(this.mCreateHandles[i0].mShape.getEventSource(), 'onmouseout', this.mCreateHandles[i0], 'setInvisible');
					this.mCreateHandles[i0].setRadius(6);
					
					gShapes[this.mCreateHandles[i0].id] = this.mCreateHandles[i0];
				} 
				else {
// 	    		dojo.debug("create handlers detached");
					dojo.event.disconnect(this.mCreateHandles[i0].mShape.getEventSource(), 'onmouseover', this.mCreateHandles[i0], 'setVisible');
					dojo.event.disconnect(this.mCreateHandles[i0].mShape.getEventSource(), 'onmouseout', this.mCreateHandles[i0], 'setInvisible');
					this.mCreateHandles[i0].setRadius(0);
					delete gShapes[this.mCreateHandles[i0].id];
				}

   			midPt = { x: (this.mPointList[i1].x + this.mPointList[i2].x) / 2, 
		   					  y: (this.mPointList[i1].y + this.mPointList[i2].y) / 2 };

     		this.mCreatePointList[i1] = midPt;
     		this.mCreateHandles[i1].setPosition(midPt.x, midPt.y);

				// see if if this creatept is active or not
     		pt0 = this.mPointList[i1];
    	  pt1 = this.mPointList[i2];
    	    	
 	    	if (this.createPtTolerance(pt0, pt1, midPt)) {
// 	    		dojo.debug("create handlers attached");
					dojo.event.connect(this.mCreateHandles[i1].mShape.getEventSource(), 'onmouseover', this.mCreateHandles[i1], 'setVisible');
					dojo.event.connect(this.mCreateHandles[i1].mShape.getEventSource(), 'onmouseout', this.mCreateHandles[i1], 'setInvisible');
					gShapes[this.mCreateHandles[i1].id] = this.mCreateHandles[i1];
				} else {
// 	    		dojo.debug("create handlers detached");
					dojo.event.disconnect(this.mCreateHandles[i1].mShape.getEventSource(), 'onmouseover', this.mCreateHandles[i1], 'setVisible');
					dojo.event.disconnect(this.mCreateHandles[i1].mShape.getEventSource(), 'onmouseout', this.mCreateHandles[i1], 'setInvisible');
					delete gShapes[this.mCreateHandles[i1].id];
				}
       		
     		this.moveI = -1;
			}

			// multiply points by the present matrix and set as the present points
			for (i = 0; i < this.mPointList.length; i++) {
				temppt = { x: this.mPointList[i].x, y: this.mPointList[i].y};
//				dojo.debug("temppt " + temppt.x + "," + temppt.y);
				
   			temppt = dojo.gfx.matrix.multiplyPoint(saveTransform, temppt);
//				dojo.debug("temppt transform " + temppt.x + "," + temppt.y);
   			
   			this.mPointList[i].x = temppt.x;
   			this.mPointList[i].y = temppt.y;
   		}
			if (this.mCreateHandles) {
	 	    for (i = 0; i < this.mCreateHandles.length; ++i) {
   				this.mCreatePointList[i] = dojo.gfx.matrix.multiplyPoint(saveTransform, this.mCreatePointList[i]);
  	    	this.mCreateHandles[i].update(saveTransform, this.mCreatePointList[i]);
      	}
				
			}
			// set transform to identity now that points have been transformed
	    this.mShape.setTransform();
		
//    	if (dojo.render.html.ie) {
    	if (false) {
//    	dojo.byId("statusfield").innerHTML = "foo" + tempPoly[1].x + "," + tempPoly[1].y;
    	
    		var raw = this.mShape.getNode();
    		dojo.debug("raw path " + raw.path.v);
    		
//  	 	dojo.byId("statusfield").innerHTML = "foo " + foo[0] + "," + foo[1] + "," + foo[2] + "," + foo[3];
    		var attr2 = [];
				attr2.push("m");
				attr2.push(this.mPointList[0].x.toFixed());
				attr2.push(this.mPointList[0].y.toFixed());
				var q;
			
				for (q = 1; q < this.mPointList.length; q++) {
//    		dojo.debug("x, y " + this.mPointList[q].x + "," + this.mPointList[q].y);
					attr2.push("l");
					attr2.push(this.mPointList[q].x.toFixed());
					attr2.push(this.mPointList[q].y.toFixed());
				}

				attr2.push("e");
				raw.path.v = attr2.join(" ");
				
				this.calcCompleteBBox();
				this.update();
				this.removeHandle();
//			this.removeEditHandles();
//			this.bHandles = false;
//			this.createPointEdit();
				this.createHandles();
			
//			if (this.mHandles) {
//				this.mHandles.update();
//			}
    	} else {
				surface.remove(this.mShape);
			
				var pathattr = [];
			
				pathattr.push("M");
				pathattr.push(this.mPointList[0].x.toFixed(8));
				pathattr.push(this.mPointList[0].y.toFixed(8));
				var q;
			
				for (q = 1; q < this.mPointList.length; q++) {
    		dojo.debug("x, y " + this.mPointList[q].x + "," + this.mPointList[q].y);
					if (this.mPointList[q].info != "L") {
						pathattr.push(this.mPointList[q].info);
					}
					pathattr.push(this.mPointList[q].x.toFixed(8));
					pathattr.push(this.mPointList[q].y.toFixed(8));
				}

				pathattr.push("z");
				pathstring = pathattr.join(" ");
    		dojo.debug("pathattr is " + pathstring);

				this.mShape = surface.createPath(pathstring);

//  		this.mShape = surface.createPath(this.mPointList)
	  		this.mShape.setStroke({color: "black", width: 2}).setFill([62, 62, 255, 0.9]);;
		    this.setId();


/*    	
    		var raw = this.mShape.getNode();
    	
//    			var p = this.shape.points;
    			var p = this.mPointList;
				var attr3 = [];

				for(var i = 0; i < p.length; ++i){
					attr3.push(p[i].x.toFixed(8));
					attr3.push(p[i].y.toFixed(8));
				}
				raw.setAttribute("points", attr3.join(" "));
*/
//		raw.setAttribute("d", "M100 100 200 100 200 200Q200 250 125 175T100 100z"); 
//		.createPath("M100 100 200 100 200 200Q200 250 125 175T100 100z")

				this.calcCompleteBBox();
				this.update();
				this.removeHandle();
//			this.removeEditHandles();
//			this.bHandles = false;
//			this.createPointEdit();
				this.createHandles();

/*    	
				surface.remove(this.mShape);

				
		    this.mShape = surface.createPolyline(this.mPointList).setStroke({color: "black", width:2}).setFill([62, 62, 255, 0.9]);

		    this.setId();
				this.removeHandle();
//			this.removeEditHandles();
//			this.bHandles = false;
//			this.createPointEdit();
				this.createHandles();
				this.update();
   		  for (i = 0; i < this.mEditHandles.length; ++i) {
   	    	this.mEditHandles[i].getGfxShape().moveToFront();
	   	  }
   		  for (i = 0; i < this.mCreateHandles.length; ++i) {
   	    	this.mCreateHandles[i].getGfxShape().moveToFront();
	   	  }
*/
    	}
	    this.setCursor("move");
 	},
	removeShape: function() {
		this.removeEditHandles();
		this.bHandles = false;
		this.removeHandle();
		surface.remove(this.mShape);
	},
	toSVG: function() {
		var svgString = "";
		// example svg polygon
		/* <polygon fill="lime" stroke="blue" stroke-width="10" 
            points="850,75  958,137.5 958,262.5
                    850,325 742,262.6 742,137.5" />
		*/
		if (this.mPointList.length > 2){
			m = this.getTransform();

			// transform bbox and get translation, scaling matrices to 
			// put svgoutput in unit square coords
			
			bbox = this.getBoundingBox();
			
			p = [];
			t = [];
			
			p[0] = { x:bbox.x,              y:bbox.y };
			p[1] = { x:bbox.x + bbox.width, y:bbox.y };
			p[2] = { x:bbox.x,              y:bbox.y + bbox.height};
			p[3] = { x:bbox.x + bbox.width, y:bbox.y + bbox.height};

			t[0] = dojo.gfx.matrix.multiplyPoint(m, p[0]);
			t[1] = dojo.gfx.matrix.multiplyPoint(m, p[1]);
			t[2] = dojo.gfx.matrix.multiplyPoint(m, p[2]);
			t[3] = dojo.gfx.matrix.multiplyPoint(m, p[3]);
			
			mint = { x:1000, y:1000 };
			maxt = { x:-1000, y:-1000};
			for ( i = 0; i < 4; i++) {
				if ( t[i].x < mint.x ) {
					mint.x = t[i].x;
				}
				if ( t[i].y < mint.y ) {
					mint.y = t[i].y;
				}
				if ( t[i].x > maxt.x ) {
					maxt.x = t[i].x;
				}
				if ( t[i].y > maxt.y ) {
					maxt.y = t[i].y;
				}
			}

			scalex = 1.0 / (maxt.x - mint.x);
			scaley = 1.0 / (maxt.y - mint.y);
			scalefactor = scalex > scaley ? scaley : scalex;
//			dojo.debug("toSVG scalex:" + scalex + " scaley:" + scaley);
//			dojo.debug("toSVG mint:" + mint.x + "," + mint.y);
//			dojo.debug("toSVG maxt:" + maxt.x + "," + maxt.y);
//			svgMatrix = dojo.gfx.matrix.multiply(dojo.gfx.matrix.translate(-mint.x,-mint.y), dojo.gfx.matrix.scaleAt(mint, scalex, scaley), m);			
//			svgMatrix = dojo.gfx.matrix.multiply(dojo.gfx.matrix.scale(scalefactor), dojo.gfx.matrix.translate(-mint.x,-mint.y), m);			
//			svgMatrix = dojo.gfx.matrix.multiply(dojo.gfx.matrix.scale(1.0/(theEditor.grid_size)), m);			
			svgMatrix = dojo.gfx.matrix.multiply(dojo.gfx.matrix.scale(1.0/450.0), m);			
			svgString = "<path d=\"";
			
			outpoint = dojo.gfx.matrix.multiplyPoint(svgMatrix, {x: this.mPointList[0].x, y: this.mPointList[0].y});
			var pathattr = [];
			
			pathattr.push("M");
			pathattr.push(outpoint.x.toFixed(5));
			pathattr.push(outpoint.y.toFixed(5));
			var q;

			for (i = 1; i < this.mPointList.length; i++) {
				outpoint = dojo.gfx.matrix.multiplyPoint(svgMatrix, {x: this.mPointList[i].x, y: this.mPointList[i].y});
					if (this.mPointList[i].info != "L")
						pathattr.push(this.mPointList[i].info);
					pathattr.push(outpoint.x.toFixed(5));
					pathattr.push(outpoint.y.toFixed(5));
			}			
			pathattr.push("z");
			pathstring = pathattr.join(" ");
			svgString += pathstring;
			svgString += "\" \/>";
			
			
			var p1 = "M100 100 200 100 200 200Q200 250 125 175T100 100z";
			var p2 = p1.match(dojo.gfx.pathRegExp);
		
			for ( i = 0; i < p2.length; ++i) {
				if (p2[i] == "M") {
					dojo.debug("M" + p2[i+1] + " " + p2[i + 2]);
					i += 2;
				}
				else if (p2[i] == "L") {
					dojo.debug("L" + p2[i+1] + " " + p2[i + 1]);
					i += 2;
				}
				else if (p2[i] == "Q") {
					dojo.debug("Q" + p2[i+1] + " " + p2[i + 2]);
					i += 2;
				}
				else if (p2[i] == "T") {
					dojo.debug("T" + p2[i+1] + " " + p2[i + 2]);
					i += 2;
				}
				else if (p2[i] == "z") {
					dojo.debug("z");
					break;
				} else {
					dojo.debug("implicit L" + p2[i] + " " + p2[i + 1]);
					i += 1;
				}
			
			}
			
		}
		return svgString;
	},
	load: function(svgPath) {
		var extractedPoint;
		
		if (false) {
//		if (dojo.render.html.ie) {
			var pointArrayString = svgPoly.getAttribute("points");
			var pointArray = pointArrayString.split(" ");
//			for (key in pointArray) {
			for (i = 0; i < pointArray.length; i++) {
//				dojo.debug("pointArray[key] is" + "pointArray[" + key +"]" + pointArray[key]);
				if ( pointArray[i].length < 1 ) {
					continue;
				}
					
				var pointString = pointArray[i].split(",");
				extractedPoint = {x: parseFloat(pointString[0]), y: parseFloat(pointString[1])}; 
//				dojo.debug("point is x:" + extractedPoint.x + " y:" + extractedPoint.y);
				
//	 			this.mPointList[this.count] = {x: extractedPoint.x * theEditor.grid_size, y: extractedPoint.y * theEditor.grid_size};
	 			this.mPointList[this.count] = {x: extractedPoint.x * 450.0, y: extractedPoint.y * 450.0};
	 			this.count++;
			}
			this.mPointList[this.count] = this.mPointList[0];
 			this.count++;
//			dojo.debug("pointArray in gfx_shape is:" + pointArray);
				
		} else {
			var pathData = svgPath.getAttribute("d");

		var p2 = pathData.match(dojo.gfx.pathRegExp);
		
		var j = 0;
		for ( i = 0; i < p2.length; ++i) {
			if (p2[i] == "M") {
	 			this.mPointList[j++] = {info: "M", x: p2[i+1] * 450.0, y: p2[i + 2] * 450.0};
				dojo.debug("M" + p2[i+1] + " " + p2[i + 2]);
				i += 2;
			}
			else if (p2[i] == "L") {
	 			this.mPointList[j++] = {info: "L", x: p2[i+1] * 450.0, y: p2[i + 2] * 450.0};
				dojo.debug("L" + p2[i+1] + " " + p2[i + 1]);
				i += 2;
			}
			else if (p2[i] == "Q") {
	 			this.mPointList[j++] = {info: "Q", x: p2[i+1] * 450.0, y: p2[i + 2] * 450.0};
				dojo.debug("Q" + p2[i+1] + " " + p2[i + 2]);
				i += 2;
			}
			else if (p2[i] == "T") {
	 			this.mPointList[j++] = {info: "T", x: p2[i+1] * 450.0, y: p2[i + 2] * 450.0};
				dojo.debug("T" + p2[i+1] + " " + p2[i + 2]);
				i += 2;
			}
			else if (p2[i] == "z") {
				dojo.debug("z");
				break;
			} else {
	 			this.mPointList[j++] = {info: "L", x: p2[i] * 450.0, y: p2[i + 1] * 450.0};
				dojo.debug("implicit L" + p2[i] + " " + p2[i + 1]);
				i += 1;
			}
		}
		
		this.count = j;
		}

  		if (this.mShape) {
  			surface.remove(this.mShape);
  		}
	    

				var pathattr = [];
			
				pathattr.push("M");
				pathattr.push(this.mPointList[0].x.toFixed(8));
				pathattr.push(this.mPointList[0].y.toFixed(8));
				var q;
			
				for (q = 1; q < this.mPointList.length; q++) {
    		dojo.debug("x, y " + this.mPointList[q].x + "," + this.mPointList[q].y);
					if (this.mPointList[q].info != "L") {
						pathattr.push(this.mPointList[q].info);
					}
					pathattr.push(this.mPointList[q].x.toFixed(8));
					pathattr.push(this.mPointList[q].y.toFixed(8));
				}

				pathattr.push("z");
				pathstring = pathattr.join(" ");
	    
//  		this.count = this.count + 1;
	    
//	    this.mShape = surface.createPolyline(this.mPointList).setStroke({color: "blue"}).setFill([128, 128, 128, 0.9]);
	    this.mShape = surface.createPath(pathstring).setFill([62, 62, 255, 0.9]);
	    this.setId();

		if (this.createPointEdit()) {
			if (theEditor.editedshape != this) {
				if (theEditor.editedshape) {
//					theEditor.editedshape.removeEditHandles();
					theEditor.editedshape.hidePoints();
					theEditor.editedshape.removeHandle();
				}
				this.createHandles();
				theEditor.editedshape = this;
			}
		}
	  	theEditor.removeClickText();
		theEditor.editedshape = this;
	    toolSet.setTool("editTool");
	    bg.select("move");
	    
	    
	    dojo.debug("loading of this polygon is done");
//	    current_shape = this;
	    
	},
	update: function() {
		if (this.mHandles) {
			this.mHandles.update();
		}
		if (this.bVisibleEdit) {
			if (this.mEditHandles) {
   	    for (i = 0; i < this.mEditHandles.length; ++i) {
 		    	this.mEditHandles[i].update(this.getTransform(), this.mPointList[i]);
 	    	}
				
			}
			if (this.mCreateHandles) {
   	    for (i = 0; i < this.mCreateHandles.length; ++i) {
 		    	this.mCreateHandles[i].update(this.getTransform(), this.mCreatePointList[i]);
 	    	}
	
			}
		}
	},
	hidePoints: function() {
		if (this.bVisibleEdit) {
			if (this.mEditHandles) {
  	    for (i = 0; i < this.mEditHandles.length; ++i) {
   	    	if (dojo.render.html.ie) {
						dojo.html.setStyle(this.mEditHandles[i].mShape.getEventSource(), 'visibility', 'hidden');
					} else {
				    	this.mEditHandles[i].mShape.getEventSource().setAttribute('visibility', 'hidden');
			    }
 	    	}
				
			}
			if (this.mCreateHandles) {
   	    for (i = 0; i < this.mCreateHandles.length; ++i) {
   	    	if (dojo.render.html.ie) {
						dojo.html.setStyle(this.mCreateHandles[i].mShape.getEventSource(), 'visibility', 'hidden');
	 	    	} else {
			    	this.mCreateHandles[i].mShape.getEventSource().setAttribute('visibility', 'hidden');
			    }
	    	}
				
			}
			this.bVisibleEdit = false;
		}
	},
	showPoints: function() {
		this.bVisibleEdit = true;
		if (this.mEditHandles) {
 	    for (i = 0; i < this.mEditHandles.length; ++i) {
 	    	if (dojo.render.html.ie) {
					dojo.html.setStyle(this.mEditHandles[i].mShape.getEventSource(), 'visibility', 'visible');
 	    	} else {
			    this.mEditHandles[i].mShape.getEventSource().setAttribute('visibility', 'visible');
				}
    	}
			
		}
		if (this.mCreateHandles) {
 	    for (i = 0; i < this.mCreateHandles.length; ++i) {
 	    	if (dojo.render.html.ie) {
					dojo.html.setStyle(this.mCreateHandles[i].mShape.getEventSource(), 'visibility', 'visible');
 	    	} else {
			    this.mCreateHandles[i].mShape.getEventSource().setAttribute('visibility', 'visible');
				}
    	}
			
		}
	},
	moveToFront: function() {
		this.mShape.moveToFront();
    for (i = 0; i < this.mEditHandles.length; ++i) {
    	this.mEditHandles[i].getGfxShape().moveToFront();
    }
    for (i = 0; i < this.mCreateHandles.length; ++i) {
    	this.mCreateHandles[i].getGfxShape().moveToFront();
    }
	},
	calcBBox : function(p) {
		var bboxorig = this.getBoundingBox();
			
		var bbox = {
			l:		bboxorig.x, 
			r:		bboxorig.x + bboxorig.width, 
			t:		bboxorig.y, 
			b:		bboxorig.y + bboxorig.height
		};
		if(bbox.l > p.x) bbox.l = p.x;
		if(bbox.r < p.x) bbox.r = p.x;
		if(bbox.t > p.y) bbox.t = p.y;
		if(bbox.b < p.y) bbox.b = p.y;
			
		this.mBbox = {
			x:		bbox.l, 
			y:		bbox.t, 
			width:	bbox.r - bbox.l, 
			height:	bbox.b - bbox.t
		};
	},
	calcCompleteBBox: function() {
		var p = this.mPointList;
		var l = p.length;
		var t = p[0];
		bbox = {l: t.x, t: t.y, r: t.x, b: t.y};
		for(var i = 1; i < l; ++i){
			t = p[i];
			if(bbox.l > t.x) bbox.l = t.x;
			if(bbox.r < t.x) bbox.r = t.x;
			if(bbox.t > t.y) bbox.t = t.y;
			if(bbox.b < t.y) bbox.b = t.y;
		}
		this.mBbox = {
			x:		bbox.l, 
			y:		bbox.t, 
			width:	bbox.r - bbox.l, 
			height:	bbox.b - bbox.t
		};
	},
	delPoint: function(aHandle) {
		var aIndex = aHandle.getIndex();
		
		if (this.mPointList.length > 4) {
			if (aIndex > 0) {
				dojo.debug("length before splice " + this.mPointList.length);
				this.mPointList.splice(aIndex,1);
				dojo.debug("length after splice " + this.mPointList.length);
				
				this.removeHandle();
				this.removeEditHandles();
				this.bHandles = false;
				this.createPointEdit();
				this.createHandles();
				this.update();
			} else {
				this.mPointList.splice(this.mPointList.length - 1,1);
				this.mPointList[0] = this.mPointList[this.mPointList.length - 1];
			}
			this.count--;
			this.moveI = -1;	
			this.calcCompleteBBox();
			this.removeHandle();
			this.removeEditHandles();
			this.bHandles = false;
			this.createPointEdit();
			this.createHandles();
			this.update();
		}
	},
	getBoundingBox: function () {
/*
		var p1 = "M100 100 200 100 200 200Q200 250 125 175T100 100z";
		var p2 = p1.match(dojo.gfx.pathRegExp);
//		var p = rawNode.path.v.match(dojo.gfx.pathRegExp);
		dojo.debug("in getBounding Box p2 is " + p2);
		var u = [], skip = false;
		for(var i = 0; i < p2.length; ++i){
			var s = p2[i];
			if(s in this._pathSvgToVmlMap) {
				skip = false;
				u.push(this._pathSvgToVmlMap[s]);
			} else if(!skip){
				var n = parseInt(s);
				dojo.debug("number " + n);
				if(isNaN(n)){
					skip = true;
				}else{
					u.push(n);
				}
			}
		}
*/
		var p = this.mPointList;
		var l = p.length;
		var t = p[0];
		bbox = {l: t.x, t: t.y, r: t.x, b: t.y};
		for(var i = 1; i < l; ++i){
			t = p[i];
			if(bbox.l > t.x) bbox.l = t.x;
			if(bbox.r < t.x) bbox.r = t.x;
			if(bbox.t > t.y) bbox.t = t.y;
			if(bbox.b < t.y) bbox.b = t.y;
		}
		this.mBbox = {
			x:		bbox.l, 
			y:		bbox.t, 
			width:	bbox.r - bbox.l, 
			height:	bbox.b - bbox.t
		};
		
		
  	return this.mBbox;
	},
	_pathVmlToSvgMap: {m: "M", l: "L", t: "m", r: "l", c: "C", v: "c", qb: "Q", x: "z", e: ""},
	_pathSvgToVmlMap: {M: "M", L: "L", m: "m", l: "l", C: "C", c: "c", Q: "Q", q: "q", z: "z", T: "T"},
	
	mBbox: null,	
  testline: null,
	endBall: null
});
