/**
 * jQuery shuffle plugin
 * @requires jQuery v1.1 or later
 * 
 * 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.9
 *
 * 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
	 *  
	 *  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',
					auto: false,
					duration : 3000,
					easing		: 'linear'	//	shuffle easing :: suggestion from Mike Alsup
				};
				
				if(o)
					$.extend(s, o);
				var $parent = $(this);
				var el = $parent.children();
				var elArray = Array();
				var topDir, nextDir,$topLevel,$interval;

				if (el.length > 2) {
					if ($parent.css('position') == 'static') $parent.css('position', 'relative');
					$topLevel = $(el[0]);

					for ( var i = 0; i < el.length; i++ ) {
						$parent.css('height', $(el[i]).height());
						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,s.easing,
							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,s.easing);
							});
							
							var $next = $this.next();
							if($next.size() == 0){
								$next = $(el[0]);
							}
							$topLevel = $next;
							
							if(s.direction == 0 || s.direction == 1){
								var $next = $this.next();
								if($next.size() == 0){
									$next = $(el[0]);
								}
								
								$next.animate({
									'left'	: nextDir,
									'top'	: 15
								},
								s.speed,s.easing,
								function(){
									$next.animate({
										'left':0,
										'top':0
									},s.speed,s.easing);
								});
								

							}
							

							return false;
						});
					};
					
					$.fn.jqShuffle.auto(s,$topLevel);
				}
			});
		return this;
	}
	
	$.fn.jqShuffle.auto = function(s,$topLevel){
		if(s.auto){
			$.fn.jqShuffle.interval = setInterval(function(){$topLevel.trigger('click');},
			s.duration);
		}
	};
	$.fn.jqShuffle.interval = null;
})(jQuery);
$(document).ready(function(){
	$(".imageBox").jqShuffle();
	$(".imageBox2").jqShuffle({direction:1});
	$(".imageBox3").jqShuffle({direction:2});
	$(".imageBox4").jqShuffle({direction:3});
});