/**
 * @author sole / http://soledadpenades.com/
 * @author mr.doob / http://mrdoob.com/
 */

var MANUAL_TWEEN = MANUAL_TWEEN || {};

MANUAL_TWEEN.Manager = function () {

	var tweens = [];

	this.add = function ( tween ) {

		tweens.push( tween );
	};

	this.remove = function ( tween ) {

		for ( var i = 0, l = tweens.length; i < l; i++ ) {

			if ( tween == tweens[ i ] ) {

				tweens.splice( i, 1 );
				return;

			}
		}

	};

	this.update = function( time ) {

		for ( var i = 0, l = tweens.length; i < l; i++ ) {

			tweens[ i ].update( time )

		}

	};

};

MANUAL_TWEEN.Tween = function ( object, property ) {

	var _object = object,
	_valuesStart = {},
	_valuesChange = {},
	_valuesEnd = {},
	_duration = 1000,
	_startTime = 0,
	_easingFunction = TWEEN.Easing.Linear.EaseNone,
	_onUpdateFunction = null,
	_onCompleteFunction = null;

	this.to = function( duration, properties ) {

		_duration = duration * 1000;

		for ( var property in properties ) {

			if ( _object[ property ] === null ) {

				continue;

			}

			_valuesStart[ property ] = _object[ property ];
			_valuesChange[ property ] = properties[ property ] - _object[ property ];
			_valuesEnd[ property ] = properties[ property ];

		}

		return this;

	};

	this.delay = function ( amount ) {

		_startTime += amount * 1000;
		return this;

	};

	this.easing = function ( easing ) {

		_easingFunction = easing;
		return this;

	};

	this.update = function ( time ) {

		var property, elapsed;

		if ( time < _startTime ) {

			for ( property in _valuesStart ) {

				_object[ property ] = _valuesStart[ property ];

			}

			return;

		}

		elapsed = time - _startTime;

		if ( elapsed >= _duration ) {

			for ( property in _valuesEnd ) {

				_object[ property ] = _valuesEnd[ property ];

			}

			return;

		}

		for ( property in _valuesChange ) {

			_object[ property ] = _easingFunction( elapsed, _valuesStart[ property ], _valuesChange[ property ], _duration );

		}

	};
};
