/* Created by jankoatwarpspeed.com */

(function($) {
    $.fn.formToWizard = function(options) {
        options = $.extend({  
            submitButton: '',
			validationEnabled : true  // Kyla added 1/11/10 for validation
        }, 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>");

            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'>&lt; Back</a>");
			$("#" + stepName + "commands").append("<button type='button' name='button' value='&lt; Back' class='btn prev' id='" + stepName + "Prev' ><span><span>&lt; Back</span></span></button>");
			

            $("#" + stepName + "Prev").bind("click", function(e) {
                $("#" + stepName).hide();
                $("#step" + (i - 1)).show();
                $(submmitButtonName).hide();
				$("div.buttons").hide();
                selectStep(i - 1);
            });
        }

        function createNextButton(i) {
            var stepName = "step" + i;
            //$("#" + stepName + "commands").append("<a href='#' id='" + stepName + "Next' class='next'>Next &gt;</a>");

			$("#" + stepName + "commands").append("<button type='button' name='button' value='Next &gt;' class='btn primary next' id='" + stepName + "Next' ><span><span>Next &gt;</span></span></button>");

            $("#" + stepName + "Next").bind("click", function(e) {
				if (options.validationEnabled) { // Kyla added 1/11/10 for validation
					//console.log("validate:\n\n");
					var stepIsValid = true;
					//console.log("Starting Value: " + stepIsValid); 						
					$("#" + stepName + " :input").each( function(index) {					
						//console.log("Validation Results: "+element.validate().element($(this))+" stepIsValid: "+stepIsValid);
						stepIsValid = element.validate().element($(this)) && stepIsValid;
					});
					if (!stepIsValid) {
						return false;
					}
				} 										  
				//console.log("created Next Button\n\n");															  
                $("#" + stepName).hide();
                $("#step" + (i + 1)).show();
                if (i + 2 == count)
                    $(submmitButtonName).show();
					$("div.buttons").show();
                selectStep(i + 1);
            });
        }

        function selectStep(i) {
            $("#steps li").removeClass("current");
            $("#stepDesc" + i).addClass("current");
        }

    }
})(jQuery); 