
(function($) {
    $.fn.formToWizard = function(options) {
        options = $.extend({  
            submitButton: "",
			pagTitle: "",
			verPasos: true
        }, options); 
        
        var element = this;

        var steps = $(element).find("fieldset");
        var count = steps.size();
        var submmitButtonName = "#" + options.submitButton;
        $(submmitButtonName).hide();

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

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

            // 2
            var name = $(this).find("legend").html();
            $("#steps").append("<a href='#step" + i + "'><li id='Descstep" + i + "'>" + options.pagTitle + " " + (i + 1) + "<span>" + name + "</span></li></a>");
			if(options.verPasos==false)  $("#steps").hide();

            $("#Descstep"+i).bind("click", function(e) {
                steps.each(function (a) {
					$("#step" + a).fadeTo('normal',0).hide();
				});
                $("#step" + i).fadeTo('normal',1).show();
                if (i == count - 1) $(submmitButtonName).show();
                else $(submmitButtonName).hide();
                selectStep(i);
            });

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

        function createPrevButton(i) {
            var stepName = "step" + i;
			if($("#prevButton" + i).length){
				$("#prevButton" + i).append("<a href='#step" + (i - 1) + "' id='" + stepName + "Prev'><img src='../images/btnBack.png' /></a>");
			} else {
				$("#" + stepName + "commands").append("<a href='#step" + (i - 1) + "' id='" + stepName + "Prev' class='prev'><img src='../images/poramor/razaideal/btn_prev.gif' /></a>");
			}

            $("#" + stepName + "Prev").bind("click", function(e) {
                $("#" + stepName).hide();
                $("#step" + (i - 1)).fadeTo('normal',1).show();
                $(submmitButtonName).fadeTo('normal',0).hide();
                selectStep(i - 1);
            });
        }

        function createNextButton(i) {
            var stepName = "step" + i;
			if($("#nextButton" + i).length)  {
            	$("#nextButton" + i).append("<a href='#step" + (i + 1) + "' id='" + stepName + "Next'><img src='../images/btnFwd.png' /></a>");
			} else {
				$("#" + stepName + "commands").append("<a href='#step" + (i + 1) + "' id='" + stepName + "Next' class='next'><img src='../images/poramor/razaideal/btn_next.gif' /></a>");
			}

            $("#" + stepName + "Next").bind("click", function(e) {
                $("#" + stepName).fadeTo('normal',0).hide();
                $("#step" + (i + 1)).fadeTo('normal',1).show();
                if (i + 2 == count)
                    $(submmitButtonName).show();
                selectStep(i + 1);
            });
        }

        function selectStep(i) {
            $("#steps li").removeClass("current");
            $("#Descstep" + i).addClass("current");
        }

    }
})(jQuery); 