/*
Script Name: 	FX (http://jastegg.it/eggs/fx ) 
version: 		2.0 beta optimized for JAST-A-Slide
version date:	2008-06-21
Plugin for:		JAST ( http://jastegg.it )
--------------------------------
*/
JASTEggIt.extend('fx', {
	info: {
		title: 	'FX 2.0 - Special Effects',
		version:'2.0',
		eggUrl:	'http://jastegg.it/eggs/fx2',
		author:	'Diego La Monica',
		url:	'http://diegolamonica.info'
	},
	effects: [],
	queues:	[],
	queue: function(effects){
		var q = new Object();
		q.effects = effects;
		q.paused = false;
		q.done = false;
		q.current = 0;
		var queueIndex = JASTEggIt.fx.queues.length;
		JASTEggIt.fx.queues[queueIndex] = q;
		JASTEggIt.fx.runQueue(queueIndex);
		return queueIndex;
	},
	appendToQueue: function(index, effect){
		if(JASTEggIt.fx.queues[index]==null){
			return JASTEggIt.fx.queue([effect]);
		}else{
			var q = JASTEggIt.fx.queues[index];
			var e = q.effects;
			e[e.length] = effect;
			if(q.done){
				q.done = false;
				JASTEggIt.fx.runQueue(index);
			} 
		}
	},
	runQueue: function(index){
		var q = JASTEggIt.fx.queues[index];
		var c = q.current;
		var f = q.effects[c];
		if(q.paused){
			if(q.interval == null) q.interval = setInterval('JASTEggIt.fx.runQueue('+index+')', 100);
			return false;
		}
		if(q.interval!=null){
			clearInterval(q.interval);
			q.interval = null;
		} 
		f.fn(f.id,index);
	},
	queueCommandDone: function(index){
		var q = JASTEggIt.fx.queues[index];
		var c = q.current;
		if (c==q.effects.length-1){
			q.done = true;
		} else {
			q.current +=1;
			JASTEggIt.fx.runQueue(index);
		}
	},
	commandDone:function(o){
		o.done = true;
		clearInterval(o.intvl);
		if(o.queueIndex!=null) JASTEggIt.fx.queueCommandDone(o.queueIndex);
	},
	setEffect:function(id, effect, options, speed){
		options.done = false;
		options.paused = false;
		if(JASTEggIt.fx.effects[id] == null) JASTEggIt.fx.effects[id] = new Object();
		var o = JASTEggIt.fx.getEffect(id, effect);
		if(o!=null) clearInterval(o.intvl);
		JASTEggIt.fx.effects[id][effect] = options;
		options.intvl = setInterval('JASTEggIt.fx._' +  effect + '("'+id+'");', speed);
		
	},
	getEffect: function(id, effect){
		if(JASTEggIt.fx.effects[id] == null) return null;
		return JASTEggIt.fx.effects[id][effect];
	},
	effectExists: function(id, effect){
		if(JASTEggIt.fx.effects[id] == null) return false;
		return (JASTEggIt.fx.effects[id][effect] != null);
	},
	_fade: function(id){
		var f = JASTEggIt.fx.effects[id];
		if(f==null) return false;
		var o = f['fade'];
		if(o==null) return false;
		if(o.paused) return false;
		if((o.opacity > o.limit && o.step >0) || (o.opacity<o.limit && o.step<0)  ){
			o.opacity = o.limit;			
		}
		if(o.opacity == null && o.step>0) o.opacity = 0;
		if(o.opacity == null && o.step<0) o.opacity = 100;
		var el = JASTEggIt._id(id);
		if (el != null) {
			var st = el.style;
			if (JASTEggIt.Browser.ie) {
				if (st.height == null || st.height == '') {
					var sz = JASTEggIt.DOM.realSize(el);
					st.height = st.height;
					o.forced = true;
				};
				if (o.opacity >= 100 && o.forced) {
					el.style.height = '';
					el.style.filter = '';
				};
				el.style.filter = 'alpha(opacity=' + o.opacity + ')';
			}
			else {
				el.style.opacity = o.opacity / 100;
				el.style.MozOpacity = o.opacity / 100;
				el.style.KhtmlOpacity = o.opacity / 100;
			};
		}
		if(o.opacity== o.limit){
			JASTEggIt.fx.commandDone(o);
			if(o.limit==100 && JASTEggIt.Browser.ie){
				el.style.filter = '';
			}	
		}else{
			o.opacity += o.step;
		}
	},
	fade: function(id, speed, limit, step, q){
		var options = new Object();
		options.limit = limit;

		options.step = step;
		options.queueIndex = q;
		var f = JASTEggIt.fx.getEffect(id, 'fade');
		if(f!=null) options.opacity = f.opacity;
		JASTEggIt.fx.setEffect(id, 'fade', options, speed);
	},
	fadeIn: function(id, speed, limit, step, q){
		if(step<0) step = step * -1;
		JASTEggIt.fx.fade(id, speed, limit, step, q);
	},
	fadeOut: function(id, speed, limit, step, q){
		if(step>0) step = step * -1;
		JASTEggIt.fx.fade(id, speed, limit, step, q);
	}
});
