/*
 * jQuery Textfade Plugin 1.0
 * Free to use under the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
*/

(function($){

	$.fn.textfade = function(options) {
		
		//Defaults to extend options
        var defaults = {  
            animation: 'fade', 		// fade, horizontal-slide, vertical-slide, horizontal-push
            animationSpeed: 400, 				// how fast animtions are
            afterStepChange: function(){} 		// empty function 
     	};  
        
        //Extend options
        var options = $.extend(defaults, options); 
        
        return this.each(function() {
        	
        	//Global Variables
            var activeTestimonial = 0,
            	numberTestimonials = 0,
            	testimonials;       	       	
        	
        	//Collect all blockquotes
	        testimonials = $(this).find('blockquote');
        	
        	setup();
                       	
            function setup(){	

	            testimonials.each(function() {
	            	var _testimonial = $(this);   	
	            	_testimonial.hide();//Hide all steps
					numberTestimonials++;
					
	            });	

	            //Show First Testimonial
	            testimonials.eq(activeTestimonial).show(); 

            }
            
            window.setInterval(swapTestimonial, 5000);
            
            //Move steps   	
           function swapTestimonial() {
           		//remember previous activeStep
                var prevActiveTestimonial = activeTestimonial;
                
                function reset() {
                    testimonials
                    	.eq(prevActiveTestimonial)
                    	.css({"opacity" : 100}); 
                }
                
                activeTestimonial++;
                
                if(activeTestimonial == numberTestimonials) {
                    activeTestimonial = 0;
                }                    
                             	
                if(options.animation == "fade") {
                	testimonials.eq(prevActiveTestimonial).animate({
                		opacity: '0'
                	},options.animationSpeed, function(){
                		testimonials.eq(prevActiveTestimonial).hide();
                		testimonials.eq(activeTestimonial).fadeIn(); 
                		reset();
                	});   
                }

            }//textfade
                 	
        });//each call
    }//textfade plugin call
})(jQuery);

