/* Created by jankoatwarpspeed.com */
var prevButtonImage;
var nextButtonImage;
var newCust = true;
(function($) {
    $.fn.formToWizard = function(options) {
        options = $.extend({
            submitButton: "",
            stepText: "Step",
            stepForwardText: "Nästa >",
            stepBackwardText: "< Föregående",
            prevButtonImage: "images/knapp_tillbaka.png",
            nextButtonImage: "images/knapp_vidare_small.png"
        }, options);
        
        var element = this;
        prevButtonImage = options.prevButtonImage;
        nextButtonImage = options.nextButtonImage;

        var steps = $(element).find("fieldset");
        var count = steps.size();
        var submmitButtonName = "#" + options.submitButton;
        $(submmitButtonName).hide();

        steps.each(function(i) {
            $(this).wrap("<div id='step" + i + "'></div>");
            $(this).append("<p id='step" + i + "commands'></p>");

            var name = $(this).find("legend").html();

            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'><img src='"+prevButtonImage+"' /></a>");

            $("#" + stepName + "Prev").bind("click", function(e) {
                $("#" + stepName).hide();
                if(!newCust && stepName=='step2')
                	$("#step" + (i - 2)).show();
                else
                	$("#step" + (i - 1)).show();
                $(submmitButtonName).hide();
                
                if(!newCust && stepName=='step2')
			selectStep(i - 2);
		else
                	selectStep(i - 1);
            });
        }

        function createNextButton(i) {            
            var stepName = "step" + i;
            $("#" + stepName + "commands").append("<a href='#' id='" + stepName + "Next' class='next'><img src='"+nextButtonImage+"' /></a>");

            $("#" + stepName + "Next").bind("click", function(e) {
                if(!processStep(this))
                return;
                $("#" + stepName).hide();
                
                if(!newCust && stepName=='step0')
                	$("#step" + (i + 2)).show();
                else
                	$("#step" + (i + 1)).show();
                
                if (i + 2 == count)
                    $(submmitButtonName).show();
                    
                if(!newCust && stepName=='step0')
                	selectStep(i + 2);
                else
                	selectStep(i + 1);
            });
        }

        function selectStep(i) {
            $("#steps li").removeClass("current");
            $("#stepDesc" + i).addClass("current");
            $('#info-box div').hide();
            $('#s'+i).show();
        }

    }
})(jQuery); 
