//rotator plugin
//The only option that can be specified is delayMilliseconds
/*
 * Dan Nuttle, Adobe Systems
 * expected HTML:
 * (parent element specified in jQuery selector)
 * 		div.center-content
 * 			div.show
 * 				a (one or more)
 *		div.hide
 * 			a (one or more)
 * 		.next
 * 		.prev
*/
(function($){
	$.fn.rotator = function() {
		var timeout;
		var rotate = function($item, dir, now){
			clearTimeout(timeout);
			timeout = setTimeout(function(){
				var $content = $item.find("div.center-content");
				var $show = $content.find("div.show:eq(0)");
				var $hide = $item.find("div.hide:eq(0)");
				//If no hidden elements, no need to keep rotating
				if($hide.find("> *").length==0) return;
				if(dir=="next")
				{
					var r = $show.find("> *:eq(0)");
					var h = $hide.find("> *:eq(0)");
					if(r.length && h.length)
					{
						r = r.remove();
						h = h.remove();
						$show.append(h);
						$hide.append(r);
					}
					rotate($item, "next");
				}else if(dir=="prev"){
					var r = $show.find("> *:eq(0)");
					var h = $hide.find("> *:last(0)");
					if(r.length && h.length)
					{
						r = r.remove();
						h = h.remove();
						$show.append(h);
						$hide.prepend(r);
					}
					rotate($item, "next");
				}
			}, now ? 0 : opts.delayMilliseconds);
		};
		var opts = {delayMilliseconds:10000};
		if(arguments.length && typeof arguments[0] == "object"){
			opts = $.extend(opts, arguments[0]);
		}
		return $(this).each(function(idx, item){
			var $this = $(this);
			$this.find(".next").click(function(evt){
				evt.preventDefault();
				rotate($this,"next",true);
			});
			$this.find(".prev").click(function(evt){
				evt.preventDefault();
				rotate($this,"prev",true);
			});
			rotate($this,"next");
		});
	};
	$.fn.rotator.version = "1.0";
}(jQuery));


