var Curve = {
	engine	: null,
	x1		: 0,
	y1		: 0,
	x2		: 0,
	y2		: 0,
	c1		: 0,
	c2		: 0,
	c3		: 0,
	c4		: 0,
	alpha	: 1,  
	removing: false,

	initialize: function(engine) {
		this.engine = engine;

		var fStart = Math.round(Math.random()*3);
    	var fStop  = Math.round(Math.random()*3);
 
		while(fStop == fStart) {
			fStop = Math.round(Math.random()*3);
		}

		//Start
		switch (fStart) {
			case 0:
		    	this.x1 = Math.random()*this.engine.width;
		    	this.y1 = 0;
		    	break;
			case 1:
		    	this.x1 = this.engine.width;
		    	this.y1 = Math.random()*this.engine.height;
		    	break;
			case 2:
		    	this.x1 = Math.random()*this.engine.width;
		    	this.y1 = this.engine.height;
		    	break;
			case 3:
		    	this.x1 = 0;
		    	this.y1 = Math.random()*this.engine.height;
		    	break;
		}

	    //Stop
    	switch (fStop) {
      		case 0:
        		this.x2 = Math.random()*this.engine.width;
        		this.y2 = 0;
        		break;
      		case 1:
      	  		this.x2 = this.engine.width;
        		this.y2 = Math.random()*this.engine.height;
        		break;
      		case 2:
        		this.x2 = Math.random()*this.engine.width;
        		this.y2 = this.engine.height;
        		break;
      		case 3:
        		this.x2 = 0;
        		this.y2 = Math.random()*this.engine.height;
        		break;
    	}
     
    	this.c1 = this.c3 = Math.random(this.engine.width/4, this.engine.width/4 + this.engine.width/2);
    	this.c2 = this.c4 = Math.random(this.engine.height/4, this.engine.height/4 + this.engine.height/2);
  	},

	bezierPoint: function(a, b, c, d, t) {
    	var t1 = 1.0 - t;
    	return a*t1*t1*t1 + 3*b*t*t1*t1 + 3*c*t*t*t1 + d*t*t*t;
	},
	
  	getPoint: function(p) {
    	return {"x": this.bezierPoint(this.x1, this.c1, this.c3, this.x2, p), "y": this.bezierPoint(this.y1, this.c2, this.c4, this.y2, p)};
	},

	draw: function() {
		this.bezier(this.x1, this.y1, this.c1, this.c2, this.c3, this.c4, this.x2, this.y2);    
    
		if (this.removing) {
			this.alpha -= 0.05;
      		if (this.alpha < 0) this.engine.curves.shift();
    	}
  	},
  
	drawTo: function(t) {    
    	var p = 0.01;

		this.engine.ctx.beginPath();
	    this.engine.ctx.strokeStyle = "rgba(93, 200, 234,"+this.alpha+")";
		
		var p1 = this.getPoint(0);

    	//this.engine.ctx.moveTo(p1.x1, p1.y);

	    while(p < t) {
			var p2 = this.getPoint(p-0.001);
	    	
	    	this.engine.ctx.lineTo(p2.x, p2.y);

      		p += 0.005;
    	}
    	this.engine.ctx.stroke();  	
  	},
  
  	bezier: function(x1, y1, c1, c2, c3, c4, x2, y2) {
	    this.engine.ctx.strokeStyle = "rgba(93, 200, 234,"+this.alpha+")";
  		this.engine.ctx.moveTo(x1,y1);
    	this.engine.ctx.bezierCurveTo(c1, c2, c3, c4, x2, y2);	
    	this.engine.ctx.stroke();  	
  	},
  	
  	remove: function() {
    	this.removing = true;
  	}
}
Curve = Class.create(Curve);
