/*
 *  jquery.rotator
 *  Version: 0.1
 *  Modified by FDumitrescu:
 *  - fixed image flicker (happened from time to time and is reported as a bug on the plugin site on jQuery.com)
 *  - stopped the rotation when the mouse is over one of the items inside the rotator
 */
 
(function($){ $.fn.rotator = function(options){

    var defaults = {
		ms: 2000,
		n: 1,
		autoHeight: false
	};
  
    var options = $.extend(defaults, options);
	
	return this.each(function(index) {
		
		var $this = $(this);
		var inHover = false;
		
		var initialHeight = 0;
		$this.children().filter(":lt(" + options.n + ")").each(function(index, item) {
		    initialHeight += $(item).outerHeight();
		});
		
		$this.height(initialHeight);
		$this.hover(function() { inHover = true; }, function() { inHover = false; });
		
		setInterval(function(){
		    if(inHover)
				return;

            var childHeight = $this.children().filter(":first-child").outerHeight();
		    var animParams = {scrollTop: (childHeight) + "px"};
			var autoHeight = 0;
			$this.children().filter(":lt(" + (options.n + 1) + ")").each(function(index, item) {
			    if (index > 0) autoHeight += $(item).outerHeight();
		    });
			if(options.autoHeight)animParams = $.extend({height:(autoHeight) + "px"}, animParams);
		
			
		    $this.animate(animParams, 500, function(){
			    $this.append($this.children().filter(":first-child"));
				$this.scrollTop(0);
				$this.css("overflow","hidden"); //Chrome hack
		    });
		}, options.ms);
	    

	});
  
}})(jQuery);
