/* Created by jankoatwarpspeed.com */

(function($) {
    $.fn.formToWizard = function(options) {
        options = $.extend({  
            submitButton: "buttonID", 
            validationEnabled : true
        }, options); 
        
        var element = this;

        var steps = $(element).find("fieldset");
        var count = steps.size();
        
        var submmitButtonName = "#" + options.submitButton;
        $(submmitButtonName).hide();

        // 2
        $(element).before("<ul id='steps'></ul>");

        steps.each(function(i) {
            $(this).wrap("<div id='step" + i + "'></div>");
            $(this).append("<p id='step" + i + "commands'></p>");

            // 2
            var name = $(this).find("legend").html();
            $("#steps").append("<li id='stepDesc" + i + "'>Step " + (i + 1) + "<span>" + name + "</span></li>");

            //for clicking on the steps on top
            //$("#steps").append("<li id='stepDesc" + i + "' Class='Stepsname'>Step " + (i + 1) + "<span>" + name + "</span></li>");

            //add Class to <li> for CSS

            /**$("#stepDesc" + i).bind("click", function(e) {
                          for (var f=0;f<=count;f++){
                  $("#step" + f).hide();
                  }
                  $("#step" + i).show();
                  if (i + 1 == count)
                              $(submmitButtonName).show();
                  selectStep(i);
                      }); 
            */
            if (i == 0) {
                createNextButton(i);
                selectStep(i);
            }
            else if (i == count - 1) {
                $("#step" + i).hide();
                createPrevButton(i);
            }
            else {
                $("#step" + i).hide();
                createPrevButton(i);
                createNextButton(i);
            }
        });

        function createPrevButton(i) {
            var stepName = "step" + i;
            $("#" + stepName + "commands").append("<a href='#' id='" + stepName + "Prev' class='prev'>< Back</a>");

            $("#" + stepName + "Prev").bind("click", function(e) {
                $("#" + stepName).hide();
                $("#step" + (i - 1)).show();
                $(submmitButtonName).hide();
                selectStep(i - 1);
            });
        }

        function createNextButton(i) {
            var stepName = "step" + i;
            $("#" + stepName + "commands").append("<a href='#' id='" + stepName + "Next' class='next'>Next ></a>");

            $("#" + stepName + "Next").bind("click", function(e) {
            	if (options.validationEnabled) {
            	    var stepIsValid = true;
            	    //for all input form fields
            	    //$("#" + stepName + " :input:not(:submit :checkbox, :radio, :button)").each( function(index) {
            	    $("#" + stepName + " input:not(:submit :checkbox, :radio, :button), #" + stepName + " select").each( function(index) {	
            	    	var xy = element.validate().element($(this));
            	    	if(xy != false) xy = true;
            	    	//alert($(this).attr("type")+" "+$(this).attr("name")+" "+xy);
            	        stepIsValid = xy && stepIsValid;
            	        //alert(stepIsValid);
            	    });

            	    if (!stepIsValid) {
            	         return false;
            	    };
            	}; 
                $("#" + stepName).hide();
                $("#step" + (i + 1)).show();
                if (i + 2 == count)
                    $(submmitButtonName).show();
                selectStep(i + 1);
            });
        }

        function selectStep(i) {
            $("#steps li").removeClass("current");
            $("#stepDesc" + i).addClass("current");
        }

    }
})(jQuery); 
