﻿function Delegate() {}
Delegate.create = function (o, f) {
	var a = new Array() ;
	var l = arguments.length ;
	for(var i = 2 ; i < l ; i++){ a[i - 2] = arguments[i] ;}
	return function() {
		var aP = [].concat(arguments, a) ;
		f.apply(o, aP);
	}
}

miniCart = function(obj, begin, finish, duration, pxStr){
    this.init(obj, begin, finish, duration, pxStr)
}

var t = miniCart.prototype;

t.obj = new Object();
t.prop='top';
t.func = function (t, b, c, d) { return c*t/d + b; };
t.begin = 0;
t.change = 0;
t.prevTime = 0;
t.prevPos = 0;
t.looping = false;
t._duration = 0;
t._time = 0;
t._pos = 0;
t._position = 0;
t._startTime = 0;
t._finish = 0;
t.name = '';
t.pxStr = '';
//t._listeners = new Array();	

t.init = function(obj, begin, finish, duration, pxStr){
	if (!arguments.length) return;
	//this._listeners = new Array();
	//this.addListener(this);
	if(pxStr) this.pxStr = pxStr;
	this.obj = obj;
	this.begin = begin;
	this._pos = begin;
	this.setDuration(duration);
	this.func = miniCart.regularEaseOut;
	this.setFinish(finish);
}
t.setDuration = function(d){
	this._duration = (d == null || d <= 0) ? 100000 : d;
}
t.setFinish = function(f){
	this.change = f - this.begin;
}
t.continueTo = function(finish, duration){
	this.begin = this._pos;
	this.setFinish(finish);
	if (this._duration == undefined){
		this.setDuration(duration);
	}
	this.stop();
	//alert(this.prevPos+":"+this._pos);
    this.start();
	//alert("every 3 seconds");
}
t.start = function(){
	this.rewind();
	this.startEnterFrame();
}
t.rewind = function(t){
	this.isPlaying = false;
    this._time = (t == undefined) ? 0 : t;
	this.fixTime();
	this.update();
}
t.fixTime = function(){
	this._startTime = this.getTimer() - this._time * 1000;
}
t.getTimer = function(){
	return new Date().getTime() - this._time;
}
t.update = function(){
	this.setPosition(this.getPosition(this._time));
	//alert(this.getPosition(this._time));
}
t.getPosition = function(t){
	if (t == undefined) t = this._time;
	return this.func(t, this.begin, this.change, this._duration);
}
t.setPosition = function(p){
	this.prevPos = this._pos;
	var a = this.pxStr != '' ? this.pxStr : '';
	this.obj['top'] = Math.round(p) + a;
	this._pos = p;
}
t.startEnterFrame = function(){
	//this.isPlaying = false;
	//this.stopEnterFrame();
	this.isPlaying = true;
	this.onEnterFrame();
}
t.onEnterFrame = function(){
	if(this.isPlaying) {
		this.nextFrame();
		setTimeout(Delegate.create(this, this.onEnterFrame), 0);
        //setTimeout(this.isPlaying, 1000);
		//alert("of course");
	}

}
t.nextFrame = function(){
	this.setTime((this.getTimer() - this._startTime) / 1000);
}
t.setTime = function(t){
	//this.prevTime = this._time;
	if (t > this._duration) {//alert("stop");
		this._time = this._duration;
		this.update();
		this.stop();
	} else if (t < 0) {
		this.rewind();
		this.update();
	} else {
		this._time = t;
		this.update();
	}
}
t.stop = function(){
	this.isPlaying = false;
}







miniCart.regularEaseOut = function(t,b,c,d){
	return -c *(t/=d)*(t-2) + b;
}
