/*
 *  jquery.rotator
 *  Version: 0.1
 *  Modified to support AJAX loading and defaults for 2 alternating items (mcc, 20100421)
 */
 
(function($){ $.fn.rotator = function(options){

    var defaults = {
		ms: 5000,
		n: 2,
		autoHeight: false,
		ajaxurl: ""
	};
  
    var options = $.extend(defaults, options);
	
	return this.each(function(index) {
		
		var $this = $(this);
		
		var initialHeight = 0;
		$this.children().filter(":lt("+options.n+")").each(function(index,item){
		    initialHeight += $(item).height();
		});

		$this.height(initialHeight);

		$this.children().filter(":eq(1)").load(options.ajaxurl, "");			
		
		setInterval(function(){
		    
			var childHeight = $this.children().filter(":first-child").height();
		    var animParams = {scrollTop: (childHeight) + "px"};
			var autoHeight = 0;
		    $this.children().filter(":lt("+(options.n+1)+")").each(function(index,item){
		        if(index>0)autoHeight += $(item).height();
		    });
			if(options.autoHeight)animParams = $.extend({height:(autoHeight) + "px"}, animParams);
		
			
		    $this.animate(animParams, 500, function(){
		        $this.children().filter(":first-child").css('display', 'none');
		        $this.scrollTop(0);
		        $this.children().filter(":first-child").css('display', 'block');         
                $this.children().filter(":first-child").load(options.ajaxurl,"");		        
			    $this.append($this.children().filter(":first-child"));			    			    
				$this.css("overflow","hidden"); //Chrome hack
		    });

			
	    }, options.ms);
		

	});

  
}})(jQuery);

