var undefined;
if (!Function.prototype.apply) {
	Function.prototype.apply = function bu_fix_apply(o,a) {
		var r;
		if (!o) o = {};
		o.___apply=this;
		switch((a && a.length) || 0) {
		case 0: r = o.___apply(); break;
		case 1: r = o.___apply(a[0]); break;
		case 2: r = o.___apply(a[0],a[1]); break;
		case 3: r = o.___apply(a[0],a[1],a[2]); break;
		case 4: r = o.___apply(a[0],a[1],a[2],a[3]); break;
		case 5: r = o.___apply(a[0],a[1],a[2],a[3],a[4]); break;
		case 6: r = o.___apply(a[0],a[1],a[2],a[3],a[4],a[5]); break;
		default:
			for (var i=0, s=""; i<a.length;i++) {
				if(i!=0) s += ",";
				s += "a[" + i +"]";
			}
			r = eval("o.___apply(" + s + ")");
		}
		o.__apply = null;
		return r;
	};
}
if (!Array.prototype.push) {
	Array.prototype.push = function bu_fix_push() {
		for (var i = 0; i < arguments.length; i++) {this[this.length] = arguments[i];}
		return this.length;
	};
}

if (!Array.prototype.pop) {
	Array.prototype.pop = function bu_fix_pop() {
		if (this.length == 0) return undefined;
		return this[this.length--];
	};
}
if (!Array.prototype.shift) {
	Array.prototype.shift = function bu_fix_shift() {
		this.reverse();
		var lastv = this.pop();
		this.reverse();
		return lastv;
	};
}
if (!Array.prototype.splice) {
	Array.prototype.splice = function bu_fix_splice(start, deleteCount) {
		var len = parseInt(this.length);
		start = start ? parseInt(start) : 0;
		start = (start < 0) ? Math.max(start+len,0) : Math.min(len,start);
		deleteCount = deleteCount ? parseInt(deleteCount) : 0;
		deleteCount = Math.min(Math.max(parseInt(deleteCount),0), len);
		var deleted = this.slice(start, start+deleteCount);
		var insertCount = Math.max(arguments.length - 2,0);
		var new_len = this.length + insertCount - deleteCount;
		var start_slide = start + insertCount;
		var nslide = len - start_slide;
		for(var i=new_len - 1;i>=start_slide;--i) {this[i] = this[i - nslide];}
		for(i=start;i<start+insertCount;++i) {this[i] = arguments[i-start+2];}
		return deleted;
	};
}
if (!Array.prototype.unshift) {
	Array.prototype.unshift = function bu_fix_unshift() {
		var a = [0,0];
		for(var i=0;i<arguments.length;i++) {a.push(arguments[i]);}
		var ret = this.splice.apply(a);
		return this.length;
	};
}
if (!Number.prototype.toFixed) {
	function bu_nz(n) {return n <= 0 ? '' : '0000000000000000000000000'.substring(25 - n)}
	Number.prototype.toFixed = function(fracDigits) {
		var f = this;
		if (typeof fracDigits == 'undefined') fracDigits = 0;
		if (fracDigits < 0) throw Error("negative fracDigits " + fracDigits);
		var n = Math.round(Math.abs(f) * Math.pow(10, fracDigits));
		var s;
		if (isNaN(n) || n == 2147483647) {
			s = String(f);
			var dec = s.indexOf('.');
			if (dec == -1) return fracDigits > 0 ? s + '.' + bu_nz(fracDigits) : s;
			var res = s.substring(0,dec+1);
			var fraction = s.substring(dec+1);
			if (fraction.length >= fracDigits) return res + fraction.substring(0,fracDigits);
			res = res + fraction + bu_nz(fracDigits - fraction.length);
			return res;
		}
		s = String(n);
		if (fracDigits > 0) {
		if (s.length > fracDigits)
			s = s.substring(0, s.length - fracDigits) + '.' + s.substring(s.length - fracDigits);
		else {
			s = '0.' + bu_nz(fracDigits - s.length) + s;
		}
	};
	if (f < 0) s = '-' + s;
		return s;
	}
}
if (!Number.prototype.toPrecision) {
	Number.prototype.toPrecision = function(prec) {
		var f = this;
		if (typeof prec == 'undefined') return String(f);
		if (prec < 0) throw Error("negative precision " + prec);
		if (Number.prototype.$$toPrecision$$ ) {
			var nat = Number.prototype.$$toPrecision$$.call(this, prec);
			if (/e/i.test(nat)) return nat;
		}
		var exp = Math.floor(Math.log(Math.abs(f))/Math.LN10 + 0.000001);
		if (exp >= prec) {
			s = this.toExponential(prec - 1);
		}
		else {
			var n = Math.round(Math.abs(f) * Math.pow(10, prec - exp - 1));
			s = String(n);
			var nshift = prec - exp - 1;
			if (nshift == 0) {
			}
			else if (nshift < s.length) {
				s = s.substring(0, s.length - nshift) + '.' + s.substring(s.length - nshift);
			}
			else {
				s = '0.' + bu_nz(nshift - s.length) + s;
			}
		}
		return s;
	};
}
if (!Number.prototype.toExponential) {
	Number.prototype.toExponential = function(fracDigits) {
		var f = this;
		var exp = Math.floor(Math.log(Math.abs(f))/Math.LN10 + 0.000001);
		var n;
		if (typeof fracDigits == 'undefined') {
			n = Math.abs(f) * Math.pow(10, 0 - exp - 1);
		}
		else {
			n = Math.round(Math.abs(f) * Math.pow(10, fracDigits - exp));
		}
		var s = String(n).replace(/(\d)/, "$1.");
		s += (exp >= 0 ? 'e+' : 'e') + exp;
		if (f < 0) s = '-' + s;
		return s;
	};
}
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);
	}
}

Tween = function(obj, prop, func, begin, finish, duration, suffixe, cb){
	this.init(obj, prop, func, begin, finish, duration, suffixe, cb)
}
var t = Tween.prototype;

t.obj = new Object();
t.prop='';
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._cb = false;
t._duration = 0;
t._time = 0;
t._pos = 0;
t._position = 0;
t._startTime = 0;
t._finish = 0;
t.name = '';
t.suffixe = '';
t._listeners = new Array();	
t.setTime = function(t){
	this.prevTime = this._time;
	if (t > this.getDuration()) {
		if (this.looping) {
			this.rewind (t - this._duration);
			this.update();
			this.broadcastMessage('onMotionLooped',{target:this,type:'onMotionLooped'});
		} else {
			this._time = this._duration;
			this.update();
			this.stop();
			this.broadcastMessage('onMotionFinished',{target:this,type:'onMotionFinished'});
			try {if (this._cb) this._cb();}catch(e){}
		}
	} else if (t < 0) {
		this.rewind();
		this.update();
	} else {
		this._time = t;
		this.update();
	}
}
t.getTime = function(){
	return this._time;
}
t.setDuration = function(d){
	this._duration = (d == null || d <= 0) ? 100000 : d;
}
t.getDuration = function(){
	return this._duration;
}
t.setPosition = function(p){
	this.prevPos = this._pos;
	var a = this.suffixe != '' ? this.suffixe : '';
	if (typeof this.obj == "function") this.obj(this.prop, Math.round(p) + a);
	else this.obj[this.prop] = Math.round(p) + a;
	this._pos = p;
	this.broadcastMessage('onMotionChanged',{target:this,type:'onMotionChanged'});
}
t.getPosition = function(t){
	if (t == undefined) t = this._time;
	return this.func(t, this.begin, this.change, this._duration);
};
t.setFinish = function(f){
	this.change = f - this.begin;
};
t.geFinish = function(){
	return this.begin + this.change;
};
t.init = function(obj, prop, func, begin, finish, duration, suffixe, cb){
	if (!arguments.length) return;
	this._listeners = new Array();
	this.addListener(this);
	if(suffixe) this.suffixe = suffixe;
	this.obj = obj;
	this.prop = prop;
	this.begin = begin;
	this._pos = begin;
	this.setDuration(duration);
	if (func!=null && func!='') {
		this.func = func;
	}
	this.setFinish(finish);
	this._cb = cb;
}
t.start = function(){
	this.rewind();
	this.startEnterFrame();
	this.broadcastMessage('onMotionStarted',{target:this,type:'onMotionStarted'});
	//alert('in');
}
t.rewind = function(t){
	this.stop();
	this._time = (t == undefined) ? 0 : t;
	this.fixTime();
	this.update();
}
t.fforward = function(){
	this._time = this._duration;
	this.fixTime();
	this.update();
}
t.update = function(){
	this.setPosition(this.getPosition(this._time));
	}
t.startEnterFrame = function(){
	this.stopEnterFrame();
	this.isPlaying = true;
	this.onEnterFrame();
}
t.onEnterFrame = function(){
	if(this.isPlaying) {
		this.nextFrame();
		setTimeout(Delegate.create(this, this.onEnterFrame), 0);
	}
}
t.nextFrame = function(){
	this.setTime((this.getTimer() - this._startTime) / 1000);
	}
t.stop = function(){
	this.stopEnterFrame();
	this.broadcastMessage('onMotionStopped',{target:this,type:'onMotionStopped'});
}
t.stopEnterFrame = function(){
	this.isPlaying = false;
}

t.continueTo = function(finish, duration){
	this.begin = this._pos;
	this.setFinish(finish);
	if (this._duration != undefined)
		this.setDuration(duration);
	this.start();
}
t.resume = function(){
	this.fixTime();
	this.startEnterFrame();
	this.broadcastMessage('onMotionResumed',{target:this,type:'onMotionResumed'});
}
t.yoyo = function (){
	this.continueTo(this.begin,this._time);
}

t.addListener = function(o){
	this.removeListener (o);
	return this._listeners.push(o);
}
t.removeListener = function(o){
	var a = this._listeners;	
	var i = a.length;
	while (i--) {
		if (a[i] == o) {
			a.splice (i, 1);
			return true;
		}
	}
	return false;
}
t.broadcastMessage = function(){
	var arr = new Array();
	for(var i = 0; i < arguments.length; i++){
		arr.push(arguments[i])
	}
	var e = arr.shift();
	var a = this._listeners;
	var l = a.length;
	for (var i=0; i<l; i++){
		if(a[i][e])
		a[i][e].apply(a[i], arr);
	}
}
t.fixTime = function(){
	this._startTime = this.getTimer() - this._time * 1000;
}
t.getTimer = function(){
	return new Date().getTime() - this._time;
}
Tween.backEaseIn = function(t,b,c,d,a,p){
	if (s == undefined) var s = 1.70158;
	return c*(t/=d)*t*((s+1)*t - s) + b;
}
Tween.backEaseOut = function(t,b,c,d,a,p){
	if (s == undefined) var s = 1.70158;
	return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
}
Tween.backEaseInOut = function(t,b,c,d,a,p){
	if (s == undefined) var s = 1.70158; 
	if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
	return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
}
Tween.elasticEaseIn = function(t,b,c,d,a,p){
		if (t==0) return b;  
		if ((t/=d)==1) return b+c;  
		if (!p) p=d*.3;
		if (!a || a < Math.abs(c)) {
			a=c; var s=p/4;
		}
		else 
			var s = p/(2*Math.PI) * Math.asin (c/a);
		
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	
}
Tween.elasticEaseOut = function (t,b,c,d,a,p){
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (!a || a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return (a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b);
	}
Tween.elasticEaseInOut = function (t,b,c,d,a,p){
	if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) var p=d*(.3*1.5);
	if (!a || a < Math.abs(c)) {var a=c; var s=p/4; }
	else var s = p/(2*Math.PI) * Math.asin (c/a);
	if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
}

Tween.bounceEaseOut = function(t,b,c,d){
	if ((t/=d) < (1/2.75)) {
		return c*(7.5625*t*t) + b;
	} else if (t < (2/2.75)) {
		return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
	} else if (t < (2.5/2.75)) {
		return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
	} else {
		return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
	}
}
Tween.bounceEaseIn = function(t,b,c,d){
	return c - Tween.bounceEaseOut (d-t, 0, c, d) + b;
	}
Tween.bounceEaseInOut = function(t,b,c,d){
	if (t < d/2) return Tween.bounceEaseIn (t*2, 0, c, d) * .5 + b;
	else return Tween.bounceEaseOut (t*2-d, 0, c, d) * .5 + c*.5 + b;
	}

Tween.strongEaseInOut = function(t,b,c,d){
	return c*(t/=d)*t*t*t*t + b;
	}

Tween.regularEaseIn = function(t,b,c,d){
	return c*(t/=d)*t + b;
	}
Tween.regularEaseOut = function(t,b,c,d){
	return -c *(t/=d)*(t-2) + b;
	}

Tween.regularEaseInOut = function(t,b,c,d){
	if ((t/=d/2) < 1) return c/2*t*t + b;
	return -c/2 * ((--t)*(t-2) - 1) + b;
	}
Tween.strongEaseIn = function(t,b,c,d){
	return c*(t/=d)*t*t*t*t + b;
	}
Tween.strongEaseOut = function(t,b,c,d){
	return c*((t=t/d-1)*t*t*t*t + 1) + b;
	}

Tween.strongEaseInOut = function(t,b,c,d){
	if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
	return c/2*((t-=2)*t*t*t*t + 2) + b;
	}

