/* 
	j123 -- JavaScript common library
	mail: sebastian.hanula@gmail.com
	www:  http://www.hanula.com/lab/j123

	j123 dhtml effects library
*/

j123.effects = {
	sinusoidal : function(t) { return ((-Math.cos(t * Math.PI)/2) + 0.5) },
	lineral : function(t) { return t},
	cubic:function(t) { return Math.pow(t,3);},
	circle:function(t) {return Math.sqrt(t);}
};




j123.effects.Base = j123.Class.create({
	start: function(params) {
		this.params = {
			duration : 500,
			transition: j123.effects.sinusoidal		
		}
		
		j123.extend(this.params, params);

		var i; 
		for(i in params) {
			if (typeof(params[i]) == 'function') this[i]=params[i];
			else this.params[i] = params[i];
		}
	
		this.startTime = 0;
	
		this.startTime = (new Date).getTime();
		this.timer = setInterval(this.step.bind(this), 10);

		this.onStart();
	},
	
	onStart : function () {},
	onStop : function(){},
	update: function(){pos},
	
	step : function() {
		var now = (new Date()).getTime();
	
		if(now >= this.startTime + this.params.duration) {
			clearInterval(this.timer);
			this.update(1);
			this.onStop();
		} else {
			var pos = (now - this.startTime) / (this.params.duration);
	//		this.percentDone =
			//$('debug').innerHTML = pos;
			this.update( this.params.transition(pos));	
		}
	
	}
});


j123.effects.FadeIn = j123.effects.Base.extend({
	initialize: function(el,params) {
		this.SUPER();
		this.el = el;
		this.start(params);
	},
	update : function(pos) {
		if(j123.browser.isIE) {
			this.el.style.filter = 'alpha(opacity=' + pos*100 + ')';
			
		} else this.el.style.opacity = pos;
	}
});

j123.effects.FadeOut = j123.effects.Base.extend({
	initialize: function(el,params) {
		this.SUPER();
		this.el = el;
		this.start(params);
	},
	update : function(pos) {
		this.el.style.opacity = 1-pos;
	}
});


j123.effects.FlyTo = j123.effects.Base.extend({
	initialize:function(el,from,to,params) {
		this.SUPER();
		if(from.length < 2) {
			from=[j123.HTML.findPosX(el),j123.HTML.findPosY(el)];
		}
		el.style.position='absolute';
		el.style.left = from[0]+'px';
		el.style.top = from[1]+'px';
		this.el = el;
		this.from = from;
		this.to = to;
		this.start(params);
	},
	
	update: function(pos) {
		var x;
		
		this.el.style.left = this.from[0] + (this.to[0]-this.from[0])*pos + 'px';
		this.el.style.top = this.from[1] + (this.to[1]-this.from[1])*pos + 'px';
		
		
	}
																 
});

//j123.registerOnInit(j123.drag.initalize);