
/* 
 * __-- Do ActioNs --__
 *
 *
 * Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
 * 
 * Made by Tommy Thierry, tommy.thierry@gmail.com, Montreal, QC
 * Website: http://www.tom-art.ca/
 *
 *
 * VERSION: 1.0 LAST UPDATE: 16.12.2011
 */
(function($) {	

	var methods = {
		init : function( options ) 
		{		
			var defaults = {
				action_name : 'launch',
				actions : 	new Array,
				timeout : 	500,
				repeat 	:	false,
				sleep_before_repeat : 0
			};
			var opts = $.extend(defaults, options); 
			var actions = opts.actions;
			var timeout = opts.timeout;
			var repeat = opts.repeat;
			var sleep_before_repeat = opts.sleep_before_repeat;
			var action_name = opts.action_name;
			
			this.do_actions('do_action', 0 ,actions, timeout, repeat, sleep_before_repeat, action_name);
		},
		destroy : function () 
		{		
			return this.each(function()
			{			
				var $this = $(this),
				data = $this.data('do_actions');
			
				// Namespacing FTW
				$(window).unbind('.do_actions');
				data.do_actions.remove();
				$this.removeData('do_actions');			
			})		
		},
		do_action : function (x ,actions, timeout, repeat, sleep_before_repeat, action_name)
		{
			eval(actions[x]);
			
			var nb_actions  = actions.length;
			$('body').animate({ opacity: 1 }, timeout, function() 
			{					
				x++;
				if(x == nb_actions)
				{
					if(repeat)
					{
						$('body').animate({ opacity: 1 }, sleep_before_repeat, function() 
						{
							$(action_name).do_actions('do_action', 0 ,actions, timeout, repeat, sleep_before_repeat);
						});
					}
				}
				else
				{
					$(action_name).do_actions('do_action', x ,actions, timeout, repeat, sleep_before_repeat);	
				}
			});			
		}
	};

	$.fn.do_actions = function( method ) 
	{	
		if ( methods[method] ) 
		{
	  		return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} 
		else if ( typeof method === 'object' || ! method ) 
		{
	 		return methods.init.apply( this, arguments );
		} 
		else 
		{
	 		$.error( 'Method ' +  method + ' does not exist on jQuery.do_actions' );
		}
	};
	
})(jQuery);
