var Brush = Class.create();
Brush.prototype = {
	initialize: function(canvas, options) {
	  this.options = Object.extend({
      max_steps: 12,
		  rotation_function: function(thing, step) {
		    return(Math.PI*2*Math.PI/step);
		  },
		  bg_opacity: 0.05
	  }, options || {})
    this.canvas = canvas;
	this.ctx = canvas.getContext("2d");
    this.maxX = canvas.offsetWidth;
    this.maxY = canvas.offsetHeight;
    this.offsetX = this.canvas.offsetLeft;
    this.offsetY =  this.canvas.offsetTop;        
    this.canvas.onmousedown = function(e) {
      this.drawing = true;
    }.bind(this);
    this.canvas.onmouseup = function(e) {
      this.drawing = false;
    }.bind(this);
		this.canvas.onmousemove = function(e) { 
		  this.set_Brush_target(Event.pointerX(e) - this.offsetX, 
		                          Event.pointerY(e)  - this.offsetY) ;
	  }.bind(this);
	  this.set_Brush_target(0,0);
  },
	
	draw: function() {
	this.offsetX = this.canvas.offsetLeft;
	this.offsetY =  this.canvas.offsetTop;	
    this.ctx.fillStyle = 'rgba(0,150,255,.1)';
	this.ctx.strokeStyle = 'yellow';
    this.ctx.fillRect(0,0,this.maxX/2,this.maxY);
    this.ctx.lineCap = "round";
	this.ctx.lineWidth = 14;    

    this.foo(this.maxX/2, this.maxY/2, 1);
	},
	
	//**************************** brents
	/*
	foo: function(posx, posy, steps) {
	  this.ctx.rotate(this.options.rotation_function(this, steps));
	  this.line(posx, 0, posx, this.maxY);
	  this.line(0, posy, this.maxX, posy);
    posx = this.find_pos(posx, this.target.x, this.maxX, steps);
    posy = this.find_pos(posy, this.target.y, this.maxY, steps);
	  steps += 1;
	  if(steps == this.options.max_steps) return;
		this.foo(posx, posy, steps);
	},
	*/
	
	
	/*
	foo: function(posx, posy, steps) {
	if(this.drawing){		
	  this.ctx.rotate(this.options.rotation_function(this, steps)); // this is the lineee
	  this.line(posx, 0, posx, this.maxY);
	  this.line(0, posy, this.maxX, posy);
    posx = this.find_pos(posx, this.target.x, this.maxX, steps);
    posy = this.find_pos(posy, this.target.y, this.maxY, steps);
	  steps += 1;
	  if(steps == this.options.max_steps) return;
		this.foo(posx, posy, steps);
	}	
	},*/

	
	//**************************** myfoo
	
	foo: function(posx, posy, steps) {
	if(this.drawing){
			
	//this.ctx.rotate(this.options.rotation_function(this, steps));
	
	this.line(this.old_target.x, this.old_target.y, this.target.x, this.target.y );
	this.ctx.fillStyle = 'black';
 
	this.ctx.beginPath();
	this.ctx.arc(this.target.x, this.target.y,4,0,Math.PI*2,true);
    //this.ctx.arc(posx, posy,4,0,Math.PI*2,true);
    this.ctx.fill();
	//this.line(posx-2, posy-2, posx+2, posy+2);
	//this.line(posx-2, posy+2, posx+2, posy-2);
	//this.line(0, posy, this.maxX, posy);
    posx = this.find_pos(posx, this.target.x, this.maxX, steps);
    posy = this.find_pos(posy, this.target.y, this.maxY, steps);
	steps += 1;
	if(steps == this.options.max_steps) return;
	this.foo(posx, posy, steps);
    }
	},
	
	
	
	
	
	near: function(p1, p2, amt) {
	  return(Math.abs(p1 - p2) <= amt);
	},
	
	line: function(fromx, fromy, tox,toy) {
    this.ctx.beginPath();
    this.ctx.moveTo(fromx, fromy);
    this.ctx.lineTo(tox,toy);
    this.ctx.stroke();
	},
	
	find_pos: function(current, target, size, step_no) {
    var inc = size / Math.pow(2, step_no);
    if(current == target) return current;
	  else if(target > current) {
	    return (current + inc);
	  }else if(target < current) {
		  return (current - inc);
		}
	},
	
  set_Brush_target: function(x, y) {
    this.old_target = this.target;
    this.target = {x:x, y:y}
  }
	
}

