/**
@author Baglan Dosmagambetov <baglan.dos@gmail.com>
*/

// Based on Beziér Curve Code by Dan Pupius (www.pupius.net)
BezierCurve = function( x1, y1, x2, y2, x3, y3, x4, y4 ) {
	this.x1 = x1;
	this.y1 = y1;
	this.x2 = x2;
	this.y2 = y2;
	this.x3 = x3;
	this.y3 = y3;
	this.x4 = x4;
	this.y4 = y4;
	
	this.f1 = function(t) { return (t*t*t); }
	this.f2 = function(t) { return (3*t*t*(1-t)); } 
	this.f3 = function(t) { return (3*t*(1-t)*(1-t)); }
	this.f4 = function(t) { return ((1-t)*(1-t)*(1-t)); }
	
	/* p from 0 to 1 */
	this.x = function(p) { return this.x1*this.f1(p) + this.x2*this.f2(p) +this.x3*this.f3(p) + this.x4*this.f4(p); }
	this.y = function(p) { return this.y1*this.f1(p) + this.y2*this.f2(p) +this.y3*this.f3(p) + this.y4*this.f4(p); }
}

// Just a modified copy of an Effect.Move from the effect.js file distributed with the script.aculo.us library
Effect.Curve = Class.create(Effect.Base, {
  initialize: function(element) {
    this.element = $(element);
    if (!this.element) throw(Effect._elementDoesNotExistError);
    var options = Object.extend({
      curve:    new BezierCurve(0,0,0,0,0,0,0,0)
    }, arguments[1] || { });
    this.start(options);
  },
  setup: function() {
    this.element.makePositioned();
  },
  update: function(position) {
    this.element.setStyle({
      left: this.options.curve.x(1-position).round() + 'px',
      top:  this.options.curve.y(1-position).round() + 'px'
    });
  }
})
