// JavaScript Document
(function($) 
{
	// plugin definition
	$.fn.EffectChain = function(options) 
	{
		// build main options before element iteration
		var opts = $.extend({}, $.fn.EffectChain.defaults, options);

		//parameters
		var $duration = opts.duration * 400;
		var $effect = opts.effect;
		var $onComplete = opts.onComplete;
		var $onStep = opts.onStep;
		var $delay = opts.delay * 800;
		var $order = String(opts.order).toLowerCase();
		var $animateParams = opts.animateParams;
		var $fadeToOpacity = opts.fadeToOpacity;
		//variables
		var $aItems = new Array();
		var $counter = 0;
		var $nItems = this.length;
		var $nInterval = 0;

	
		this.each(function() {
			var _this = $(this);			
			$aItems.push(_this);
		});//this.each

		switch ($order)
		{
			case "reverse":
				$aItems.reverse();
			break;
			
			case "random":
				$aItems = ArrayShuffler($aItems);
			break;
		}

		startNextStep();

		function startNextStep()
		{
			if ($delay) {clearInterval($nInterval);}
			
			switch ($effect)
			{
			case "animate":
				if (!$animateParams) {return false;}//if no parameters specified exit
				$aItems[$counter][$effect]($animateParams,$duration,validateNextStep);	
			break;
			
			case "fadeTo":
				$aItems[$counter][$effect]($duration,$fadeToOpacity,validateNextStep);	
			break;
			
			default:
				$aItems[$counter][$effect]($duration,validateNextStep);			
			break;
			}
			
		};
		
		function validateNextStep()
		{
			if ($onStep){ $onStep();}
			$counter++;
			if($counter < $nItems)
			{
				if ($delay)
				{
					$nInterval = setInterval(startNextStep,$delay);
				}
				else
				{
					startNextStep();
				}
			}
			else //final call here, end
			{
				if($onComplete)	{$onComplete();}
				$aItems = null;
			}
		};
		
		function ArrayShuffler(v){
			for(var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x);
			return v;
		};
		
		return this;
		// plugin defaults
	}
	$.fn.EffectChain.defaults = {duration : 1,order:null,effect:"hide",animateParams:null,fadeToOpacity:0.1,delay:0,onStep:null,onComplete:null};
})(jQuery);
