(function($) {
	
	$.fn.fadeTicker = function(options) {

		// set default options
		var defaults = {
			speed : 2000,
			pause : 7000,
			transition : 'fade'
		},

		// Take the options that the user selects, and merge them with defaults.
		options = $.extend(defaults, options);
		
		// Needed to fix a tiny bug. If the pause is less than speed, it'll cause a flickr.
		// This will check for that, and if it is smaller, it increases it to just about the options.speed.
		if(options.pause <= options.speed) options.pause = options.speed + options.pause;
	
		// for each item in the wrapped set
		return this.each(function() {
		
			// cache "this."
			var $this = $(this);
			
			// Set the width to a really high number. Adjusting the "left" css values, so need to set positioning.
			$this.css({
				'position' : 'relative',
				'padding' : 0
			});

			// If the user chose the "fade" transition, instead pile all of the images on top of each other.
			if(options.transition === 'fade') {
				$this.children().css({
					'width' : $this.children().width(),
					'position' : 'absolute',
					'left' : 0,
					'opacity' : 0
				});
				
				// reorder elements to fix z-index issue.
				
/*				for(var i = $this.children().length, y = 0; i > 0; i--, y++) { 		
					$this.children().eq(y).css('zIndex', i + 99999);
				}	
*/
				// Call the fade function. 
				fade();
			}
			
			function fade() {
				
				$this.children().eq(0).css({'opacity' : 1});
				
				var totalChildren = $this.children().length;
				var i = 0
				
				setInterval(function() {
					$this.children().eq(i).animate({'opacity' : 0}, options.speed, function() {});

					if (i === totalChildren - 1) i = 0;
					else i++;
					
					$this.children().eq(i).animate({'opacity' : 1}, options.speed, function() {});
					console.log(i);
				}, options.pause);
			} // end fade			

		}); // end each		
	
	} // End plugin. Go eat cake.
	
})(jQuery);
