/* Created by jankoatwarpspeed.com */ 

(function($) { 
$.fn.formToWizard = function(options) { 
options = $.extend({ 
submitButton: '',
selected: 0 
}, options); 

var element = this; 
var selected = options.selected;
var steps = $(element).find("fieldset"); 
var actual = selected; 
var count = steps.size(); 
var submmitButtonName = "#" + options.submitButton; 
$(submmitButtonName).hide(); 

// 2 
$(element).before("<ul id='wizard-steps'></ul>"); 

steps.each(function(i) { 
    $(this).wrap("<div id='wizard-step" + i + "'></div>");
    $(this).append("<p id='wizard-step" + i + "commands'></p>");

// 2 
var name = $(this).find("legend").html(); 
            $("#wizard-steps").append("<li id='wizard-stepDesc" + i + "'>Step " + (i + 1) + "<span>" + name + "</span></li>");

$("#goto" + i).bind("click", function(e) { 
$("#step" + actual).hide(); 
$("#step" + (i)).show(); 
$(submmitButtonName).hide(); 
selectStep(i); 
actual = i; 
}); 

if (i == 0) { 
$("#wizard-step" + i).hide(); 
createNextButton(i); 
} 
else if (i == count - 1) { 
$("#wizard-step" + i).hide(); 
createPrevButton(i); 
} 
else { 
$("#wizard-step" + i).hide(); 
createPrevButton(i); 
createNextButton(i); 
}

}); 

$("#wizard-step" + actual).hide(); 
$("#wizard-step" + (selected)).show();
selectStep(selected);

function createPrevButton(i) {
    var stepName = "wizard-step" + i;
    $("#" + stepName + "commands").append("<a href='#' id='" + stepName + "Prev' class='wizard-prev'>< Back</a>");

    $("#" + stepName + "Prev").bind("click", function(e) {
        $("#" + stepName).hide();
        $("#wizard-step" + (i - 1)).show();
        $(submmitButtonName).hide();
        selectStep(i - 1);
    });
}

function createNextButton(i) {
    var stepName = "wizard-step" + i;
    $("#" + stepName + "commands").append("<a href='#' id='" + stepName + "Next' class='wizard-next'>Next ></a>");

    $("#" + stepName + "Next").bind("click", function(e) {
        $("#" + stepName).hide();
        $("#wizard-step" + (i + 1)).show();
        if (i + 2 == count)
            $(submmitButtonName).show();
        selectStep(i + 1);
    });
}

function selectStep(i) { 
$("#wizard-steps li").removeClass("wizard-current"); 
$("#wizard-stepDesc" + i).addClass("wizard-current"); 
} 

} 
})(jQuery);