// 12.02.2009 11:02
function PointOnCubicBezier(cp, t)
{
	var result = {};
	cx = 3.0 * (cp[1].x - cp[0].x);
	bx = 3.0 * (cp[2].x - cp[1].x) - cx;
	ax = cp[3].x - cp[0].x - cx - bx;
		
	cy = 3.0 * (cp[1].y - cp[0].y);
	by = 3.0 * (cp[2].y - cp[1].y) - cy;
	ay = cp[3].y - cp[0].y - cy - by;
		
	tSquared = t * t;
	tCubed = tSquared * t;
		
	result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
	result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
	
	return result;
}

function FlyItem()
{
	this.sX = this.sY = this.eX = this.eY = 0;
	this.fT = 500;
	this.sT = 0;
	this.o = null;
	this.tP = [];
	this.cF = new Function();
}

FlyItem.prototype =
{
	setSP: function(x,y){this.sX=x;this.sY=y;},
	setEP: function(x,y){this.eX=x;this.eY=y;},
	setTime: function(t){this.fT=t;},
	setObj: function(o){this.o=o},
	setCB: function(f){this.cF=f;},
	doFly: function()
	{
		if(!this.o) return;
		this.o.style.left = parseInt(this.sX) + 'px';
		this.o.style.top = parseInt(this.sY) + 'px';

		dx = (this.eX - this.sX);
		dy = (this.eY - this.sY);

		this.tP = [
			{x:this.sX,y:this.sY},
			{x:this.sX,y:this.sY+dy*0.5-Math.abs(dx*0.1)},
			{x:this.eX-dx*0.3,y:this.eY-Math.abs(dx*0.05)},
			{x:this.eX,y:this.eY}
		];
	
		var d = new Date();
		this.sT = d.getTime();
		var func = this._flystep;
		var obj = this;
		setTimeout(function(){func(obj);}, 1);
	},
	_flystep:function(obj)
	{
		if(obj==undefined)return;
		var d = new Date();
		t = d.getTime();
		var elapsed = t - obj.sT;
		if(elapsed > obj.fT) elapsed = obj.fT;
		var a = elapsed / obj.fT;
		var res = PointOnCubicBezier(obj.tP, a);
		obj.o.style.left = parseInt(res.x) + 'px';
		obj.o.style.top = parseInt(res.y) + 'px';
		obj.cF(obj,a);
		if(elapsed < obj.fT) setTimeout(function(){obj._flystep(obj);}, 1);
	}
}

function CBTimer()
{
	this.fT = 500;
	this.sT = 0;
	this.o = null;
	this.cF = new Function();
}
CBTimer.prototype =
{
	setTime: function(t){this.fT=t;},
	setObj: function(o){this.o=o},
	setCB: function(f){this.cF=f;},
	doTimer: function()
	{
		if(!this.o) return;
		var d = new Date();
		this.sT = d.getTime();
		func = this._step;
		obj = this;
		setTimeout(function(){func(obj);}, 1);
	},
	_step:function(obj)
	{
		var d = new Date();
		t = d.getTime();
		var elapsed = t - obj.sT;
		if(elapsed > obj.fT) elapsed = obj.fT;
		var a = elapsed / obj.fT;

		obj.cF(obj.o,a);
		if(elapsed < obj.fT) setTimeout(function(){obj._step(obj);}, 1);
	}
}
