function Tween(target,configs,duration,timer){this.init(target,configs,duration,timer);}
Tween.START =  "onStart";Tween.STOP =  "onStop";Tween.MOTION =  "onMotion";Tween.FINISH =  "onFinish";Tween.CONTINUE =  "onContinue";Tween.YOYO =  "onYoyo";Tween.prototype.target;Tween.prototype.current;Tween.prototype.configs;Tween.prototype.duration;Tween.prototype.time;Tween.prototype.timer;Tween.prototype.eStart;Tween.prototype.eContinue;Tween.prototype.eMotion;Tween.prototype.eStop;Tween.prototype.eFinish;Tween.prototype.eYoyo;Tween.prototype.observable;Tween.prototype.dispatcher;
Tween.prototype.init = function(target,configs,duration,timer){if(target == undefined)trace("Tween no TARGET");if(configs == undefined)trace("Tween no CONFIGS");this.observable = new Observable();this.dispatcher = new EventDispatcher();this.target = target;this.configs = configs;this.duration = duration;this.duration = (duration == undefined) ? 400 : duration;this.timer = (timer == undefined) ? new Timer(10) : timer;this.current = {};this.eStart = new Event(this,Tween.START);this.eContinue = new Event(this,Tween.CONTINUE);this.eMotion = new Event(this,Tween.MOTION);this.eStop = new Event(this,Tween.STOP);this.eFinish = new Event(this,Tween.FINISH);this.eYoyo = new Event(this,Tween.YOYO);this.initProperties();}
Tween.prototype.initProperties = function()
{
	var r = this.configs;
	var l = r.length;
	for (var i=0;i<l;i++)
	{
		var c = r[i];
		if(c.ease == undefined)
			c.ease = function(t, b, c, d){return c * ((t = t/d - 1) * t * t * t * t + 1) + b;};
		if(this.target == document)
		{
			if(c.from != undefined) 
				if(c.property == "x")
				{
					scrollTo(c.from,window.pageYOffset);
				}	
				else if(c.property == "y")
					scrollTo(window.pageXOffset,c.from);
			}
			else
			{start
				if(c.from == undefined)
					c.from = this.target[c.property];
				else if(c.property == "alpha")
					setAlpha(this.target,c.from);
				else if(c.addPX)
					this.target[c.property] = c.from + 'px';
				else if(c.addPERCENT)
					this.target[c.property] = c.from + '%';
				else this.target[c.property] = c.from;
			}
		this.current[c.property] = c.from;
	}
}

Tween.prototype.setFromsTos = function(newFroms,newTos){var r = this.configs;var l = r.length;for (var i= 0; i < l; i++){var c = r[i];c.from = newFroms[i];c.to = newTos[i];}}
Tween.prototype.setTos = function(newTos){var r = this.configs;var l = r.length;for (var i = 0; i < l; i++){r[i].to = newTos[i];}}

Tween.prototype.setFroms = function(newFroms)
{
	var r = this.configs;
	var l = r.length;
	for (var i = 0; i < l; i++)
	{
		var c = r[i];
		var n =  newFroms[i];
		c.from = n;
		this.target[c.property] = n;
	}
}

Tween.prototype.continueTo = function(to)
{
	this.timer.addListener(Timer.RUN,this,"onEnterFrame");
	this.time = new Date().getTime();
	this.motion();
	this.dispatchEvent(this.eContinue);
}

Tween.prototype.start = function()
{
	this.timer.addListener(Timer.RUN,this,"onEnterFrame");
	this.time = new Date().getTime();
	this.motion();
	this.dispatchEvent(this.eStart);
}

Tween.prototype.stop = function(){this.timer.removeListener(Timer.RUN,this);this.dispatchEvent(this.eStop);}
Tween.prototype.yoyo = function(){var r = this.configs;var l = r.length;for (var i = 0; i < l; i++){var c = r[i];var t = c.to;c.to = c.from;c.from = t;}this.time = new Date().getTime();this.timer.addListener(this);this.dispatchEvent(this.eYoyo);}
Tween.prototype.getTarget = function(){return this.target;}
Tween.prototype.getCurrent = function(){return this.current;}

Tween.prototype.onEnterFrame = function()
{
	if(this.isFinished()) 
		this.finish();
	else 
		this.motion();
}

Tween.prototype.isFinished = function()
{
	return ((new Date().getTime() - this.time) >= this.duration);
}

Tween.prototype.finish = function()
{
	this.timer.removeListener(Timer.RUN,this);
	
	var r = this.configs;
	var l = r.length;
	for (var i = 0; i < l; i++)
	{
		var c = r[i];
		this.target[c.property] = c.to;
	}
	
	this.dispatchEvent(this.eFinish);

	this.notifyObservers();
}

Tween.prototype.motion = function()
{
	var r = this.configs;
	var l = r.length;
	for (var i = 0; i < l; i++)
	{
		var c = r[i];
		var value = Math.floor(c.ease(new Date().getTime() - this.time,c.from,c.to - c.from,this.duration));
		if(this.target == document)
		{
			if(c.property == "x")
				scrollTo(value,window.pageYOffset);
			else if(c.property == "y")
				scrollTo(window.pageXOffset,value);
		}
		else
		{
			if(c.addPX)
				this.target[c.property] = value + 'px';
			else if(c.addPERCENT)
				this.target[c.property] = value + '%';
			else if(c.property == "alpha")
				setAlpha(this.target,value);
			else this.target[c.property] = value;
		}
		this.current[c.property] = value;
	}
	this.dispatchEvent(this.eMotion);
}
Tween.prototype.execute = function(){this.start();}
Tween.prototype.cancel = function(){this.stop();}
Tween.prototype.addObserver = function(o){return this.observable.addObserver(o);}
Tween.prototype.removeObserver = function(o){return this.observable.removeObserver(o);}
Tween.prototype.notifyObservers = function(){this.observable.notifyObservers();}
Tween.prototype.addListener = function(eT,o,m){return this.dispatcher.addListener(eT,o,m);}
Tween.prototype.getListeners = function(eT){return this.dispatcher.getListeners(eT);}
Tween.prototype.removeListener = function(eT,o){return this.dispatcher.removeListener(eT,o);}
Tween.prototype.dispatchEvent = function(e){this.dispatcher.dispatchEvent(e);}
Tween.prototype.toString = function(){return "TWEEN";}

