/**
 * jQuery shuffle plugin
 * @requires jQuery v1.1 or later
 * @requires jQuery.moreSelectors.js  // only if auto is set to true
 *	@url http://www.softwareunity.com/sandbox/JQueryMoreSelectors/
 * 
 * Examples TBD 
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 * 
 * Version: 0.01.1 (Alpha)
 *
 * add (July 30, 2007) return this; to not break chainability
 * removed (July 30, 2007) try/catch not needed
 */

(function($) {
	/**
	 * jqShuffle() provides a mechanism for take a grouping of elements to shuffle through
	 * 
	 * jqShuffle takes is an object the will alter some of the functionality of the shuffle,
	 * the following attributs are supported:
	 * 
	 *  direction: Identifies what direction(s) the animation should go in (TODO)
	 *  
	 *  killPosition: By default, the position of the main element will be set to relative,
	 *  	if you a styling that element already with a position of absolute, you should set
	 *  	the killPosition to true
	 *  
	 *  speed: A string representing the predefined speeds "slow", "normal", and "fast" or 
	 *  	in milliseconds, the default is "slow"
	 *  
	 *  auto: By default, the user needs to click on the element for it to animate, by setting
	 *  	this to true, it will self animate
	 *  
	 *  duration: Only accessed if auto is set to true.  This sets the duration between animation.
	 */
	$.fn.jqShuffle = function(o){
			this.each(function(i){
				var s = {
					direction: 0, // 0 == both w/ top go left 1 == both w/ top go right 2 == top go left 3 == top go right
					speed: 'slow',
					killPosition: false,
					auto: false,
					duration : 3000
				};
				
				if(o)
					$.extend(s, o);
				var c = $(this);
				var el = c.children();
				var elArray = Array();
				var topDir, nextDir;

				if (el.length > 1) {
					if(!s.killPosition)
						c.css('position', 'relative');

					for ( var i = 0; i < el.length; i++ ) {
						
						elArray.push(el[i]);
						$(el[i]).css('z-index', String(el.length-i)).css({'position':'absolute',"left":0,"top":0})
						.click(function(){
							var $this = $(this);
							var w = $this.width();
							switch(s.direction){
								case 0:
									topDir = -w;
									nextDir = (w/3);
									break;
								case 1:
									topDir = w;
									nextDir = -(w/3);
									break;
								case 2:
									topDir = -w;
									break;
								case 3:
									topDir = w;
									break;
								default:
									topDir = -w;
									nextDir = (w/3);
									break;
							}
							
							params = {
								'left'	: topDir,
								'top'	: 15
							};
							
							
							$this.animate(params,
							s.speed,
							function(){
								var cur = elArray.shift();
								elArray.push(cur);
								for ( var i = 0; i <elArray.length; i++ ) {
									$(elArray[i]).css('z-index', String(elArray.length-i));
								};
								
								$this.animate({
									'left':0,
									'top':0
								},s.speed);
							});
							
							if(s.direction == 0 || s.direction == 1){
								var $next = $this.next();
								if($next.size() == 0){
									$next = $(el[0]);
								}
								
								
								params = {
									'left'	: nextDir,
									'top'	: 15
								};
								
								$next.animate(params,
								s.speed,
								function(){
									$next.animate({
										'left':0,
										'top':0
									},s.speed);
								});
								
									
							}
							

							return false;
						});
					};
					
					if(s.auto){
						setInterval(function(){
							$(el).filter(":max(zIndex)").trigger('click');
						},
						s.duration);
							
							
					}
				}
			});
		return this;
	}
})(jQuery);