/**
 * jQuery plugin for shuffling images
 *
 * @author Bob Shiels
 */
(function($) {
	var UNDEFINED;
	var DATA_TABLE = "shuffle";
	
	var extra = {
		defaults: {
			speed	: 1500,
			interval : 7000  // ms duration for each slide
		},
		queue: [],
		get: function(selector) {
			var shuffle = $(selector).eq(0).data(DATA_TABLE);
			if (!shuffle) {
				throw new Error("Object not associated with 1st parameter");
			}
			return shuffle;
		}
	};
	$.extend({shuffle: extra});
	
	$.fn.shuffle = function(opts) {
		var op = $.extend({}, $.shuffle.defaults, opts);
		
		return this.each(function() {
			// Use 'html' instead of 'this' -- because it doesn't get shadowed by a different 'this' with nested functions
			var html = this;
			
			/**
			 * Shuffle object
			 * @param p - the parent Node (or null if root)
			 */
			function Shuffle(p) {
				// Use 'shuffle' instead of 'this' -- because it doesn't get shadowed by a different 'this' with nested functions
				var shuffle = this;
				
				// Associate this Shuffle object with the HTML container object
				$(p).data(DATA_TABLE, shuffle);
				
				var currentNode = null;
				function getNextNode() {
					var nextNode = currentNode;
					do {
						if (nextNode) {
							nextNode = nextNode.nextSibling;
						}
						if (!nextNode) {
							nextNode = html.firstChild;
						}
					} while (nextNode && nextNode != currentNode && nextNode.nodeType != 1);
					return nextNode;
				}
				
				function cycle() {
					var nextNode = getNextNode();

					$(currentNode).css("top", 0).css("left", 0);
					$(nextNode).css("top", 0).css("left", 0);
					
					$(nextNode).css("position", "relative");
					$(currentNode).css("position", "relative");
					var currentPos = { top: currentNode.offsetTop, left: currentNode.offsetLeft, width: currentNode.offsetWidth };

					$(nextNode).show();
					$(currentNode).css("position", "absolute");
					$(currentNode).css("top", currentPos.top).css("left", currentPos.left);
					
					$(currentNode).css("zIndex", 3);
					$(nextNode).css("zIndex", 2);
					
					$(currentNode).animate({left: currentPos.left - currentPos.width }, op.speed / 2, function() {

						$(currentNode).css("zIndex", 1);
						$(nextNode).css("zIndex", 2);
						
						$(currentNode).animate({left: currentPos.left}, op.speed / 2, function() {
							var $c = $(currentNode);
							$c.fadeOut(250, function() {
								$c.css("zIndex", 2);
								$c.css("top", 0).css("left", 0);
								$c.css("position", "relative");
							});
							$(html).append(currentNode);
							currentNode = nextNode;
						});
					});
				}
				
				/**
				 * Starts the shuffling
				 */
				var iv;
				shuffle.start = function() {
					iv = setInterval(cycle, op.interval);
				};
				
				/**
				 * Stops the shuffling
				 */
				shuffle.stop = function() {
					clearInterval(iv);
				};
				
				function initialize() {
					var first = true;
					$(html).children().each(function() {
						if (first) {
							currentNode = this;
							first = false;
							$(this).show().css("z-index", 2);
						} else {
							$(this).hide().css("z-index", 2);
						}
					});
				}
				initialize();
			}
			
			// Create root node
			var shuffle = new Shuffle(html);
			if ($.shuffle.queue) {
				$.shuffle.queue.push(function() {
					shuffle.start();
				});
			} else {
				shuffle.start();
			}
		});
	};
}) (jQuery);

$(window).load(function() {
	var q = jQuery.shuffle.queue;
	if (q) {
		var f = q.shift();
		while (f) {
			f();
			f = q.shift();
		}
	}
	jQuery.shuffle.queue = null;
});