(function($){
	$.fn.extend({ 
			//plugin name - runCycleRun
			runCycleRun: function(options) {
			
			var defaults = {
				selector: '.cycle_item', 
				fadeDuration: 1000,
				pauseTime: 5000,
				randomized: false
			};
			
			var options = $.extend(defaults, options);
			
			
			return this.each(function() {
				var o = options;
				var obj = $(this);
				var items = $(o.selector, obj);
				
				// array to store all the cycle items
				var all_cycle_items = new Array();
				var currentIndex = -1;
				
				// our cycle loop function
				var cycle = function() {
					if (currentIndex != -1) {
						all_cycle_items[currentIndex].css('z-index', 2);
						all_cycle_items[currentIndex].animate({ opacity:0 }, o.fadeDuration);
					}
					currentIndex++;
					if (currentIndex == all_cycle_items.length) {
						currentIndex = 0;
					}
					all_cycle_items[currentIndex].css('z-index', 1);
					all_cycle_items[currentIndex].animate({ opacity:1 }, o.fadeDuration);					
					setTimeout(cycle, o.pauseTime);
				};
				
				// loop through each cycle item
				items.each(function(){
					// hide it
					var cycleItem = $(this);
					//cycleItem.hide();
					cycleItem.css('opacity', 0);
					cycleItem.css('z-index', 0);
					// add it to our array
					all_cycle_items.push(cycleItem);						
				});
				
				// if the randomized option is true, shuffle the array
				if (o.randomized == true) all_cycle_items = $.shuffle(all_cycle_items);
				
				//start the cycle
				cycle();
			});

		}
	});
})(jQuery);

/*
 * jQuery shuffle
 *
 * Copyright (c) 2008 Ca-Phun Ung <caphun at yelotofu dot com>
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://yelotofu.com/labs/jquery/snippets/shuffle/
 *
 * Shuffles an array or the children of a element container.
 * This uses the Fisher-Yates shuffle algorithm <http://jsfromhell.com/array/shuffle [v1.0]>
 */
 
(function($){

	$.fn.shuffle = function() {
		return this.each(function(){
			var items = $(this).children().clone(true);
			return (items.length) ? $(this).html($.shuffle(items)) : this;
		});
	}
	
	$.shuffle = function(arr) {
		for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
		return arr;
	}
	
})(jQuery);
