/* 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).before("<ul id='steps'></ul>");

        steps.each(function (i) {
            $(this).wrap("<div class='c_step' 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'>< Back</a>");

            $("#" + stepName + "Prev").bind("click", function (e) {
                $("#" + stepName).hide();
                getDivContent(i - 1);
                $("#step" + (i - 1)).show();
                $(submmitButtonName).hide();
                selectStep(i - 1);
            });
        }

        function createNextButton(i) {
            var stepName = "step" + i;
            $("#" + stepName + "commands").append("<a href='#' id='" + stepName + "Next' class='next'>Next ></a>");

            if ($("#nome-empresa-entidade-apoio").val() != "") {
                var name = $("#nome-empresa-entidade-apoio").val();
                $("#nome-instituicao-ong").attr('value', name);
            } 
            if ($("#contato-apoio").val() != "") {
                var contato = $("#contato-apoio").val();
                $("#contato-instituicao-ong").attr('value', contato);
            }
             if($("#email-apoio").val() != ""){
                var email = $("#email-apoio").val();
                $("#mail-instituicao-ong").attr('value', email);
            }

            $("#" + stepName + "Next").bind("click", function (e) {

                $("#" + stepName).hide();
                getDivContent(i + 1);

                $("#step" + (i + 1)).show();
                if (i + 2 == count)
                    $(submmitButtonName).show();
                selectStep(i + 1);
            });
        }

        function selectStep(i) {
            $("#steps li").removeClass("current");
            $("#stepDesc" + i).addClass("current");
        }

        function getDivContent(i) {
            var prev = i - 1;
            switch (i) {
                case 3:
                    var id_coverage = 0;
                    $('#step' + prev + ' input:radio').each(function () {

                        if (this.checked) {
                            id_coverage = $(this).attr('value');
                        }
                    });

                    $.ajax({
                        url: "/GetPlanNetworkByCoverage?id_coverage=" + id_coverage,
                        success: function (data) {
                            $(".itens-simulador3").html(data);
                        }
                    });

                    break;
                case 4:
                    var id_network = 0;
                    $('#step' + prev + ' input:radio').each(function () {

                        if (this.checked) {
                            id_network = $(this).attr('value');
                        }

                    });

                    $.ajax({
                        url: "/GetPlanAccommodationByNetwork?id_network=" + id_network,
                        success: function (data) {
                            $(".itens-simulador4").html(data);
                        }
                    });

                    break;
                case 5:
                    var id_accommodation = 0;
                    $('#step' + prev + ' input:radio').each(function () {

                        if (this.checked) {
                            id_accommodation = $(this).attr('value');
                        }

                    });

                    $.ajax({
                        url: "/GetPlanCoParticipationByAccommodation?id_accommodation=" + id_accommodation,
                        success: function (data) {
                            $(".itens-simulador5").html(data);
                        }
                    });

                    break;
                default:

                    break;
            }
        }
    }
})(jQuery);


