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

(function($){
	$.fn.whizzer = function(options) {
		
		//Defaults to extend options
        var defaults = {  
            animation: 'horizontal-push', 		// fade, horizontal-slide, vertical-slide, horizontal-push
            animationSpeed: 200, 				// how fast animtions are
            afterStepChange: function(){} 		// empty function 
     	};  
        
        //Extend options
        var options = $.extend(defaults, options); 
        
        return this.each(function() {
        
        	//Global Variables
            var activeStep = 0,
            	numberSteps = 0,
            	whizzerWidth,
            	steps,
            	validate = true;
            
            //Initialize
            var whizzer = $(this).addClass('whizzer'),
            	whizzerSteps = whizzer.prepend('<ul class="whizzer-steps" />'),
            	whizzerNav = whizzer.append('<a href="javascript:void" class="submit-btn right" id="next"><span>Next Step</span></a>'),
            	whizzerNav = whizzer.append('<a href="javascript:void" class="cancel-btn left" id="prev"><span>Previous Step</span></a>');        	       	
        	
        	//Collect all fieldsets
	        steps = $(this).find('fieldset');
        	
        	setup();
                       	
            function setup(){			 
	            steps.each(function() {
	            	var _slide = $(this);
	            	_slide.hide();//Hide all steps
					numberSteps++;
					$('.whizzer-steps').append('<li><span class="step-number">'+numberSteps+'</span><span class="step-title">'+_slide.children('legend').text()+'</span></li>');
	            });	
	            
	            //Show First Step
	            steps.eq(activeStep).show(); 
            	
            	//Show First Step Nav
	            $('.whizzer-steps').children('li').eq(activeStep).addClass('active');  
	            
	            $('#prev').hide(); 	

            }
           
           //Next Button 
           $('#next').live('click', function(event){           		
           		submitForm();
           		
           		if(validate){
           			shift('next');
           		}
           		      		
           		event.preventDefault();
           });
           
           //Submit form to check validation
           function submitForm()
           {
           		$('#signupForm').submit();
           		
           		if(steps.eq(activeStep).find('input:text').hasClass('error')){
					validate = false;
           		}else{
           			validate = true;      			
           		}
           		
           }
           
           //Previous Button 
           $('#prev').live('click', function(event){
           		shift('prev');
           		event.preventDefault();
           });
           
           //Step Nav Execution
        	function setActiveStep() { 
	        	$('.whizzer-steps').children('li').removeClass('active').eq(activeStep).addClass('active');
	        	if(activeStep == numberSteps-1) {
                    $('#next').hide();
                }else {
                    $('#next').show();
                }
                
                if(activeStep == 0) {
                    $('#prev').hide();
                }else {
                    $('#prev').show();
                }
        	}
           
           //Move steps   	
           function shift(direction) {
           		
        	    //remember previous activeStep
                var prevActiveStep = activeStep,
                	stepDirection = direction; 	
                
                function reset() {
                    steps
                    	.eq(prevActiveStep)
                    	.css({"opacity" : 100}); 
                }
                        		
                if(direction == 'next') {
                    activeStep++;
                    if(activeStep == numberSteps) {
                        activeStep = 0;
                    }                    
                }else if(direction == "prev") {
                    activeStep--;
                    if(activeStep < 0) {
                        activeStep = numberSteps-1;
                    }
                }
                                       	
                if(options.animation == "horizontal-push") {
                    if(stepDirection == "next") {
                    	steps.eq(prevActiveStep).animate({
                    		opacity: '0'
                    	},options.animationSpeed, function(){
                    		steps.eq(prevActiveStep).hide();
                    		steps.eq(activeStep).fadeIn(); 
                    		reset();
                    	});   
                    }
                    if(stepDirection == "prev") {
                        steps.eq(prevActiveStep).animate({
                    		opacity: '0'
                    	},options.animationSpeed, function(){
                    		steps.eq(prevActiveStep).hide();
                    		steps.eq(activeStep).fadeIn(options.animationSpeed);
                    		reset(); 
                    	});  
                    }
                }
                    
               if(activeStep == numberSteps) {
                	$('#next').fadeOut(); 	
                } 
                
                if(activeStep >= 1) {
                	$('#prev').fadeIn(); 	
                } 
               setActiveStep();	

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

