var points_exist = false;

var	id;

function getShape(event) {
	id = event.target.getAttribute('shapeid');
	var shapeobject = id ? gShapes[id] : null;
	dojo.debug("shape object id:" + id);
	return shapeobject;
}

function handleMouseDown(event) {
//	dojo.debug('handle mouse down event');
	toolSet.mouseDown(event);
}
function handleMouseMove(event) {
	toolSet.mouseMove(event);
}
function handleMouseUp(event) {
	toolSet.mouseUp(event);
}

function handleMouseDblClick(event) {
	toolSet.mouseDblClick(event);
}

dojo.declare("imgchf.toolSet", null, {
  initializer: function() {
  	this.gfxToolSet = [];
  },
  stockTools: function(){
		this.gfxToolSet.polyLine = new imgchf.tool.polyLine();
		this.gfxToolSet.editTool = new imgchf.tool.editTool();
		this.gfxToolSet.freehand = new imgchf.tool.freehand();
		this.gfxToolSet.lineTool = new imgchf.tool.lineTool();
		this.gfxToolSet.ellipse = new imgchf.tool.ellipse();
		this.activeTool = this.gfxToolSet.freehand;
  },
  mouseDown: function(event){
		this.activeTool.mouseDown(event);
   },
  mouseMove: function(event){
		this.activeTool.mouseMove(event);
  },
  mouseUp: function(event){
		this.activeTool.mouseUp(event);
  },
  mouseDblClick: function(event){
		this.activeTool.mouseDblClick(event);
  },
  setTool: function(tool) {
  	dojo.debug('toolSet set: ' + tool);
  	this.activeTool = this.gfxToolSet[tool];
  }
});


dojo.declare("imgchf.tool", null, {
  initializer: function() {
  },
  mouseDown: function (event){
		dojo.event.browser.stopEvent(event);
  },
  mouseMove: function (event){
		dojo.event.browser.stopEvent(event);
  },
  mouseUp: function (event){
		dojo.event.browser.stopEvent(event);
  },
  mouseDblClick: function (event){
		dojo.event.browser.stopEvent(event);
  },
  toString: function() {
  	return "I'm a tool";
  }
});

dojo.declare("imgchf.tool.lineTool", imgchf.tool, {
  initializer: function() {
  	this.current_shape_window = {
			x1: 0,
			y1: 0,
			x2: surface_size.width,
			y2: surface_size.height
  	};
  },
  mouseDown: function (event){
		origin = {
			x: event.clientX - theEditor.container_position.x,
			y: event.clientY - theEditor.container_position.y
		};
		id = "shape_" + (gShapeCounter++);
		current_shape = gShapes[id] = new imgchf.rectangle(id, origin.x, origin.y, 1, theEditor.width);
		dojo.event.browser.stopEvent(event);
  },
  mouseMove: function (event){
		if(!current_shape) { 
			return;
		}
//		var x = event.clientX - theEditor.container_position.x;
//		var y = event.clientY - theEditor.container_position.y;
		var x = Math.min(Math.max(event.clientX - theEditor.container_position.x, this.current_shape_window.x1), this.current_shape_window.x2);
		var y = Math.min(Math.max(event.clientY - theEditor.container_position.y, this.current_shape_window.y1), this.current_shape_window.y2);
		
		current_shape.mousePoint({x: event.clientX - theEditor.container_position.x, y: event.clientY - theEditor.container_position.y});
		
		last_position = {x: x, y: y};
		dojo.event.browser.stopEvent(event);
  },
  mouseUp: function (event){
		if(!current_shape) { 
			return;
		}

		var x = event.clientX - theEditor.container_position.x;
		var y = event.clientY - theEditor.container_position.y;
		current_shape.endShape({x: event.clientX - theEditor.container_position.x, y: event.clientY - theEditor.container_position.y});
		current_shape = null;
		dojo.event.browser.stopEvent(event);
  },
  startOver: function () {
  	if (current_shape != null) {
			current_shape.closeShape();
			current_shape.removeShape();
			current_shape = null;
		}
		points_exist = false;
  },
  startOver: function () {
  	if (current_shape != null) {
		current_shape.closeShape();
		current_shape.removeShape();
		current_shape = null;
	}
	points_exist = false;
  },
  closeShapeArbitrary: function () {
  	numPoints = 0;
  	if (current_shape != null) {
  		dojo.debug("close shape in line tool");
			numPoints = current_shape.closeShapeArbitrary();
			if (numPoints < 3) 
				this.startOver();
			current_shape = null;
			points_exist = false;
		}
		return numPoints;
  },
  bar: 15
});

dojo.declare("imgchf.tool.polyLine", imgchf.tool, {
  initializer: function() {
  },
  mouseDown: function (event){
  	var endThisShape = false;
  	
	last_position = {
		x: event.clientX - theEditor.container_position.x,
		y: event.clientY - theEditor.container_position.y
	};
//    dojo.debug('MouseDown polyLine event');
	if (points_exist) {
		endThisShape = current_shape.addPoint({x: last_position.x, y: last_position.y}, true);
		
		if (endThisShape) {
			current_shape = null;
			points_exist = false;
		}	
//		}
	}
	else {
		id = "shape_" + (gShapeCounter++);
	  if (theEditor.pathKeyPressed) {
				gShapes[id] = new imgchf.path(id);
		} else {
			gShapes[id] = new imgchf.polyline(id);	
		}
		gShapes[id].addPoint({x: last_position.x, y: last_position.y}, true);
		current_shape = gShapes[id];
		points_exist = true;	
	  	theEditor.removeClickText();
	}

	dojo.event.browser.stopEvent(event);
  },
  mouseMove: function (event){
	if(!current_shape) { 
		return;
	}
	var x = event.clientX - theEditor.container_position.x;
	var y = event.clientY - theEditor.container_position.y;
	current_shape.mousePoint({x: event.clientX - theEditor.container_position.x, y: event.clientY - theEditor.container_position.y});
	
	last_position = {x: x, y: y};
	dojo.event.browser.stopEvent(event);
  },
  mouseUp: function (event){
	dojo.event.browser.stopEvent(event);
  },
  startOver: function () {
  	if (current_shape != null) {
		current_shape.closeShape();
		current_shape.removeShape();
		current_shape = null;
	}
	points_exist = false;
  },
  closeShapeArbitrary: function () {
  	numPoints = 0;
  	if (current_shape != null) {
  		dojo.debug("close shape in polyline tool");
			numPoints = current_shape.closeShapeArbitrary();
			if (numPoints < 3) 
				this.startOver();
			current_shape = null;
			points_exist = false;
		}
		return numPoints;
  },
  bar: 3
});

dojo.declare("imgchf.tool.freehand", imgchf.tool, {
  initializer: function() {
  },
  mouseDown: function (event){
  	var endThisShape = false;
  	
		last_position = {
			x: event.clientX - theEditor.container_position.x,
			y: event.clientY - theEditor.container_position.y
		};
	//    dojo.debug('MouseDown polyLine event');
		if (points_exist) {
			current_shape.freehandPoint({x: last_position.x, y: last_position.y});
		}
		else {
			id = "shape_" + (gShapeCounter++);
			gShapes[id] = new imgchf.polyline(id);	
			gShapes[id].freehandPoint({x: last_position.x, y: last_position.y}, false);
			current_shape = gShapes[id];
			points_exist = true;	
	  	theEditor.removeClickText();
		}
	
		dojo.event.browser.stopEvent(event);
  },
  mouseMove: function (event){
		if(!current_shape) { 
			return;
		}
		var x = event.clientX - theEditor.container_position.x;
		var y = event.clientY - theEditor.container_position.y;
		current_shape.freehandPoint({x: x, y: y}, false);
		
		last_position = {x: x, y: y};
		dojo.event.browser.stopEvent(event);
  },
  mouseUp: function (event){
		numPoints = this.closeShapeArbitrary();
		dojo.event.browser.stopEvent(event);
  },
  startOver: function () {
  	if (current_shape != null) {
		current_shape.closeShape();
		current_shape.removeShape();
		current_shape = null;
	}
	points_exist = false;
  },
  closeShapeArbitrary: function () {
  	numPoints = 0;
  	if (current_shape != null) {
  		dojo.debug("close shape in freehand tool");
  		current_shape.simplifyPolyline(2.0);
			numPoints = current_shape.closeShapeArbitrary();
			if (numPoints < 3) 
				this.startOver();
			current_shape = null;
			points_exist = false;
		}
		return numPoints;
  },
  bar: 3
});

dojo.declare("imgchf.tool.ellipse", imgchf.tool, {
  initializer: function() {
  	
  },
  mouseDown: function (event){
    dojo.debug('MouseDown polyLine event');
		origin = {
			x: event.clientX - theEditor.container_position.x,
			y: event.clientY - theEditor.container_position.y
		};
		id = "shape_" + (gShapeCounter++);
		current_shape = gShapes[id] = new imgchf.ellipse(id, origin.x, origin.y, 10, 5);
		dojo.event.browser.stopEvent(event);
  },
  mouseMove: function (event){
  	if (!current_shape) {
  		return;
		}
	  		
		var x = event.clientX - theEditor.container_position.x;
		var y = event.clientY - theEditor.container_position.y;
		var dx = x - origin.x;
		var dy = y - origin.y;
	
  	current_shape.setExtents(dx, dy);
		dojo.event.browser.stopEvent(event);
  },
  mouseUp: function (event){
		current_shape = null;
		dojo.event.browser.stopEvent(event);
  }
});

dojo.declare("imgchf.tool.editTool", imgchf.tool, {
  initializer: function() {
  	this.shapeobject = null;
  	this.editobject = null;
  	this.current_shape_window = {
			x1: 0,
			y1: 0,
			x2: surface_size.width,
			y2: surface_size.height
  	};
  },
  mouseDown: function (event){
  	this.shapeobject = null;
  	this.editobject = null;
		var shapeobject = getShape(event);
	
		if (!shapeobject) {
			if (theEditor.editedshape) {
				theEditor.editedshape.hidePoints();		
	//			theEditor.editedshape.removeEditHandles();
				theEditor.editedshape.removeHandle();
				theEditor.editedshape.setStroke();
	//		setStroke({color: "black", width:2})
				theEditor.editedshape = null;
			}
			return;
		}

		last_position = {
			x: event.clientX - theEditor.container_position.x,
			y: event.clientY - theEditor.container_position.y
		};
		if (shapeobject.createPointEdit()) {
			this.shapeobject = shapeobject;	
			if (theEditor.editedshape != shapeobject) {
				if (theEditor.editedshape) {
					theEditor.editedshape.hidePoints();		
	//				theEditor.editedshape.removeEditHandles();
					theEditor.editedshape.removeHandle();
					theEditor.editedshape.setStroke();
				}
				this.shapeobject.createHandles();
				theEditor.editedshape = this.shapeobject;
				theEditor.editedshape.setStroke({color: "black", width:2});
				theEditor.editedshape.moveToFront();
			}
			this.calcShapeWindow(this.shapeobject);
		} else {
			this.editobject = shapeobject;
			this.editobject.setStroke({color: "FC2A00", width: 2.0/imgchf.handle.SIZE});
			if (this.shapeobject == null) {
				this.shapeobject = this.editobject.mRootShape;	
			}
			this.calcShapeWindow(this.editobject);
		}
	

		dojo.event.browser.stopEvent(event);
  },
  mouseMove: function (event){
		if(!this.editobject && !this.shapeobject) {
			return;
		}
		var x = Math.min(Math.max(event.clientX - theEditor.container_position.x, this.current_shape_window.x1), this.current_shape_window.x2);
		var y = Math.min(Math.max(event.clientY - theEditor.container_position.y, this.current_shape_window.y1), this.current_shape_window.y2);
	
		if (this.editobject) {
			this.editobject.applyEffect(x, y);
		} else {
			var dx = x - last_position.x;
			var dy = y - last_position.y;
			theEditor.editedshape.hidePoints();		
			this.shapeobject.move(dx, dy);
			this.shapeobject.update();
			
		}
		last_position = {x: x, y: y};	
//		this.calcShapeWindow(this.shapeobject);
		dojo.event.browser.stopEvent(event);
  },
  mouseUp: function (event){
		dojo.event.browser.stopEvent(event);
  	if (this.editobject) {
	  	if (theEditor.deleteKeyPressed) {
	  		this.editobject.delPoint();
	  	} 
		}
		if (theEditor.editedshape != null) {
			theEditor.editedshape.showPoints();		
//		theEditor.editedshape.update();
			theEditor.editedshape.endMovePoint();
		}
	
		if (this.editobject) {	
			this.editobject.setStroke({color: [0, 0, 0], width: 2.0/imgchf.handle.SIZE});
		}
		this.editobject = null;
		this.shapeobject = null;
  },
  calcShapeWindow : function (theObject) {
  	var dx = last_position.x;
	var dy = last_position.y;

	// FIXME - This will need to take into account rotation!!!!!!!
	var bbox = theObject.getBoundingBox();
	var m = theObject.getTransform();
	var p = [];
	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[1] = {x: bbox.x + bbox.width, y: bbox.y + bbox.height};
	
	var t = [];
	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[1] = dojo.gfx.matrix.multiplyPoint(m, p[1]);
	
	
//	dojo.debug('select: bbox coords, bbox.x:' + bbox.x + ' bbox.width:' + bbox.width + ' bbox.y:' + bbox.y + ' bbox.height:' + bbox.height);
		
  	this.current_shape_window = {
			x1: dx - t[0].x + imgchf.handle.OFFSET + imgchf.handle.SIZE,
			y1: dy - t[0].y + imgchf.handle.OFFSET + imgchf.handle.SIZE,
			x2: surface_size.width - (t[1].x + imgchf.handle.OFFSET + imgchf.handle.SIZE + imgchf.rotate.SIZE - dx),
			y2: surface_size.height - (t[1].y + imgchf.handle.OFFSET + imgchf.handle.SIZE - dy)
  	};
/*
	dojo.debug('calling mouseDown function in pathEdit');
	dojo.debug('shape window is x1:' + this.current_shape_window.x1
				+ ' y1:' + this.current_shape_window.y1 
				+ ' x2:' + this.current_shape_window.x2 
				+ ' y2:' + this.current_shape_window.y2 
				);
*/
	}
});

dojo.declare("imgchf.tool.deleteTool", imgchf.tool, {
  initializer: function(id) {
  	
  },
  mouseUp: function (event){
	var shapeobject = getShape(event);
   	dojo.debug("deleteTool: got shapeobject");
	if (shapeobject) {
		dojo.debug("deleteTool: calling shapeobject.removeShape(): " + shapeobject.id);
		shapeobject.removeShape();
	}
  },
  bar: 3
});
  




