/*
* Visual Eingine (compact version)
* @Author: Alexander Gavazov
* @Site: www.studio.bg
*/


VE = function(start, end, easingFunction, duration, parameters)
{
	this.init(start, end, easingFunction, duration, parameters);
}

// Events
VE.prototype.onChange = function(){};
VE.prototype.onStart = function(){};
VE.prototype.onStop = function(){};
VE.prototype.onFinish = function(){};
VE.prototype.onResume = function(){};

// Other vars
VE.startTime = 0;
VE.time = 0;
VE.stopMove = false;
VE.position = 0;
VE.easingFunction = null;
VE.frameRate = 25;


VE.prototype.init = function(start, end, easingFunction, duration)
{
	// Move parameters
	this.positionStart = start;
	this.positionEnd = end;
	this.duration = duration ? duration : 1.2;
	this.easingFunction = (easingFunction) ? easingFunction : function(t, b, c, d) { return c * t / d + b };

	// Set frame rate
	this.setFrameRate();
}

VE.prototype.setFrameRate = function(frameRate)
{
	this.frameRate = (frameRate) ? 25 : 0; // Set frame rate in class function (or will Opera buged)
}

VE.prototype.start = function()
{
	this.stopMove = false;
	this.startTime = new Date().getTime();
	this.onStart(this.position, this);
	this.move();
}

VE.prototype.setPosition = function()
{
	this.onChange(this.position, this);
}

VE.prototype.move = function()
{
	this.time = (new Date().getTime() - this.startTime) / 1000

	if(this.time > this.duration)
	{
		this.time = this.duration;
	}

	var end = this.positionEnd - this.positionStart;

	this.position = Math.round(this.easingFunction(this.time, this.positionStart, end, this.duration));
	this.setPosition();

	if(this.stopMove)
	{
		return;
	}
	else if(this.time >= this.duration)
	{
		this.position = this.positionEnd;
		this.setPosition();
		this.onFinish(this.position, this);
		return;
	}

	setTimeout(this.move.bind(this), this.frameRate);
}

VE.prototype.stop = function()
{
	this.onStop(this.position, this);
	this.stopMove = true;
}

VE.prototype.resume = function()
{
	this.onResume(this.position, this);
	this.stopMove = false;
	this.startTime = new Date().getTime() - (this.time * 1000);
	this.move();
}