/* Created by jankoatwarpspeed.com */

(function($) {
    $.fn.formToWizard = function(options) {
        options = $.extend({  
            submitButton: "" 
        }, options); 
        
        var element = this;
		
        var steps = $(element).find("fieldset");
        var count = steps.size();
        var submmitButtonName = options.submitButton;
        $(submmitButtonName).hide();

        // 2
        $(element).prepend("<ul id='steps'></ul>");

        steps.each(function(i) {
            $(this).wrap("<div id='step" + i + "' class='step'></div>");
            $(this).append("<p id='step" + i + "commands' class='step-controls'></p>");

            // 2
            var name = $(this).find("legend").html();
            $("#steps").append("<li id='stepDesc" + i + "'><a href='#' class='step' rel='step"+i+"'><span>Step " + (i + 1) + " <strong>" + name + "</strong></span></a></li>");
			$('#stepDesc' + i).css('opacity','.5');

            if (i == 0) {
                createNextButton(i);
                selectStep(i);
            }
            else if (i == count - 1) {
                $("#step" + i).hide('slow');
                createPrevButton(i);
            }
            else {
                $("#step" + i).hide('slow');
                createPrevButton(i);
                createNextButton(i);
            }

			$('ul#steps a').click(function(e){
				e.preventDefault();
				var target = $(this).attr('rel');
				$("div.step:not(#"+target+")").fadeOut('fast');
				$("#" + target).fadeIn('slow');

	            $('#steps li').css('opacity','.5').removeClass("current");
		        $(this).parent().addClass("current").css('opacity','1').fadeIn('slow');

				if ($('#'+target).is('#step'+(count-1))) {
					$(submmitButtonName).show();
				} else {
					$(submmitButtonName).hide();
				}
			});
        });

        function createPrevButton(i) {
            var stepName = "step" + i;
            $("#" + stepName + "commands").append("<a href='#' id='" + stepName + "Prev' class='prev submit large blue'>&larr; Back</a>");

            $("#" + stepName + "Prev").bind("click", function(e) {
				e.preventDefault();
                $("#" + stepName).fadeOut('fast');
                $("#step" + (i - 1)).fadeIn('slow');
                $(submmitButtonName).hide();
                selectStep(i - 1);
            });
        }

        function createNextButton(i) {
            var stepName = "step" + i;
            $("#" + stepName + "commands").append("<a href='#' id='" + stepName + "Next' class='next submit large blue'>Next &rarr;</a>");

            $("#" + stepName + "Next, #steps a").bind("click", function(e) {
				e.preventDefault();
                $("#" + stepName).fadeOut('fast');
                $("#step" + (i + 1)).fadeIn('slow');
                if (i + 2 == count)
                    $(submmitButtonName).show();
                selectStep(i + 1);
            });
        }

        function selectStep(i) {
            $("#steps li").css('opacity','.5').removeClass("current");
            $("#stepDesc" + i).addClass("current").css('opacity','1').fadeIn('slow');
        }
    }
})(jQuery); 
