	
(function ($) {


    $.fn.slider = function () {

        var banners = $(this).find("ul.images");
        var timer = null;

        var open = function (po) {
            banners.find('li').css({ opacity: 0 }).hide().removeClass('show');
            banners.find('li[id=' + po + ']').css({ opacity: 1.0 }).show().addClass('show');
            var li = banners.find('li[id=' + po + ']');
            timer = setInterval(slideshow, 8000);

        }

        var slideshow = function () {
            var current = (banners.find('li.show') ? banners.find('li.show') : banners.find('li:first'));
            var next = (current.next().length ? current.next() : banners.find('li:first'));
            var id = next.attr("id");
            next.css({ opacity: 0.0 }).show().addClass('show').animate({ opacity: 1.0 }, 2000);
            current.animate({ opacity: 0.0 }, 2000).hide().removeClass('show');
        }

        open(1);
    };


    // *******************************************************************	
    //  Formulários Tipos.... 
    // *******************************************************************

    $.fn.newsletter = function () {

        $(this).formulario("newsletter.aspx/insert");
    }

    // *******************************************************************	
    //  Cria e envia um formulário básico...
    // *******************************************************************

    $.fn.formulario = function (url) {

        var el = $(this);
        var fo = el.find("#formulario");
        var bt = el.find("#submit");
        var msg = el.find("#msg");
        var loading = el.find("#loading");

        // Adiciona submit ao botão...
        bt.click(function () { if (validar()) { send(); } });

        // Processa... envia...
        var send = function () {

            var params = {};
            fo.find("input[checked],select,input[type='text'],input[type='hidden'],input[type='password'],textarea").not("[name^=__]").each(function () {
                if (params[this.name] == undefined)
                    params[this.name] = this.value;
                else
                    params[this.name] = params[this.name] + "," + this.value;
            });
            fo.find('input:not(:checked)').not("[name^=__]").each(function () {
                if (params[this.name] == undefined)
                    params[this.name] = "";
            });

            msg.hide();
            fo.hide();
            loading.show();
            $.ajax({
                type: "POST",
                url: url,
                data: JSON.stringify(params),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    loading.hide();
                    if (response.d.status) {  // exibir mensagem....
                        msg.removeClass("error").show().html(response.d.mensagem);
                        msg.find("a").click(function () { reset(); fo.show(); return false; });
                    } else { // problema de validação...
                        var json = response.d;
                        $.each(json, function (key) {
                            fo.find("input[name='" + key + "'][class^='required']").val(json[key]).addClass("error");
                        });
                        fo.show();
                    }

                },
                error: function (xhr, ajaxOptions, thrownError) {
                    console.log(xhr.statusText);
                    console.log(thrownError);
                    msg.html("Desculpe, uma falha ocorreu. Tente novamente, mas tarde!").show();
                }
            });

        };

        // verifica os campos requeridos...
        var validar = function () {
            var re = Boolean(true);
            fo.find("input[class^='required']").each(function () {
                var input = $(this);
                if ($.trim(this.value) == "" || this.value == input.attr("title")) {
                    input.addClass("error");
                    if (input.attr("title") != undefined)
                        input.val(this.name + " é obrigatório")
                    else
                        msg.html("Erro: favor, preencher os campos obrigatórios, marcados em vermelho").addClass("error").show();
                    re = Boolean(false);

                };
            });
            return re;
        }

        // Pré-preenche campos
        var reset = function () {
            fo.find("input,textarea").not("input[type='hidden']").each(function () { addtxt($(this)); });
            msg.hide();
            loading.hide();
        }
        var addtxt = function (input) {
            var title = input.attr("title");
            var txt = (title == undefined) ? "" : title;
            input.val(txt).addClass("op");
        }

        // Adiciona eventos de erros aos campos requeridos 			
        fo.find("input[class^='required']").each(function () {
            $(this).bind("keypress", function (e) {
                $(this).removeClass('error').removeClass("op");
            }).bind("blur", function () {
                if (this.value == "" && $(this).attr("title") == "") {
                    $(this).addClass("error").val(this.name + " é obrigatório");
                }
            })
        });

        // Adiciona eventos aos campos			
        fo.find("input").each(function () {
            $(this).bind("focus click", function () { if (this.value == $(this).attr("title")) { this.value = ""; } })
                   .bind("blur", function () { if (this.value == "") { addtxt($(this)); } })
                   .bind("keypress", function () { $(this).removeClass("op"); });
        });

        // Preenche campos com valores padrões....
        reset();

    };



})(jQuery);






