$(document).ready(function() {

    // Mascaras
    
    $("#telefone_consulta,#telefone_duvida,#telefone").mask("(99) 9999-9999");
    $("#data").mask("99/99/9999");

    // Google maps
    $('#gmapa').css({
        left:($(window).width()/2 - $('#gmapa').outerWidth() / 2),
        top: - $('#gmapa').outerHeight()
    });

    $('.abre_mapa').click(function(e) {
        e.preventDefault();
        loadMaps();
        $('#gmapa').animate({
            top:($(window).height()/2 - $('#gmapa').outerHeight() / 2)
        }, 800);
    });

    $('#termo').focus(function() {
        stringBusca = $(this).val();
        if (stringBusca == "Digite um termo...") {
            $(this).val("");
        };
    });
    $('#termo').blur(function() {
        stringBusca = $(this).val();
        if (stringBusca == "") {
            $(this).val("Digite um termo...");
        };
    });

    $('#bt_busca').click(function(e) {
        e.preventDefault();

        if(document.form_busca.termo.value=="Digite um termo...")
            alert('Digite um termo para buscar!');
        else
          document.form_busca.submit();
    });

    $('#close1').click(function(e) {
        e.preventDefault();
        $('#gmapa').animate({
            top: - $('#gmapa').outerHeight()
        }, 800);
    });


    // Banners
    $('#lista_banners').jcarousel({
        auto: 12,
        wrap: 'circular',
        scroll: 1
    });

    $('#ok_news').click(function(e){

        e.preventDefault();

        var email = $('#email_news').val();
        
        if(document.form_news.email_news.value == ''){
            alert('Coloque o email no campo!');
            document.form_news.email_news.focus();
        }else{

        $.ajax({

            url: baseUrl+'/index/news',
            dataType: 'html',
            type: 'post',
            data: ( {email: email} ),

            success: function(data) {

              if(data==1){
                alert('Dados enviados com sucesso.');
                $("#form_news")[0].reset();
              }
              else if(data==2){
                alert('Houve uma falha, tente novamente.');
              }else if(data==3){
                alert('Seu email já esta cadastrado.');
              }else if(data==4){
                alert('E-mail no formato incorreto.');
              }
            },

            error: function(xhr, er) {
                alert('Error ' + xhr.status + ' - ' + xhr.statusText);
            }
        });

        }
    });



    // Formularios Atendimento

    $('#form_consulta').validate({

        submitHandler: function(form) {
           
            $.ajax({
                url: baseUrl+'/atendimento/consulta',
                dataType: 'html',
                type: 'post',
                data: $('#form_consulta').serialize(),

                success: function(data) {
                    if(data==1){
                        alert('Dados enviados com sucesso.');
                        $("#form_consulta")[0].reset();
                    }else{
                        alert('Houve um erro,tente novamente.');
                    }
                },
                error: function(xhr, er) {
                    alert('Error ' + xhr.status + ' - ' + xhr.statusText);
                }
            });

       
        },


        rules: {
            nome: {
                required : true,
                maxlength: 50
            },
            email: {
                required : true,
                email: true,
                maxlength: 50
            },
            telefone: {
                required : true,
                maxlength: 50
            },
            data: {
                required : true,
                maxlength: 50
            },
            mensagem: {
                required : true,
                maxlength: 1000
            }
        }
    });
    //////////////////////////


    $('#form_duvida').validate({

        submitHandler: function(form) {

            $.ajax({
                url: baseUrl+'/atendimento/duvida',
                dataType: 'html',
                type: 'post',
                data: $('#form_duvida').serialize(),

                success: function(data) {
                    if(data==1){
                        alert('Dados enviados com sucesso.');
                        $("#form_duvida")[0].reset();
                    }else{
                        alert('Houve um erro,tente novamente.');
                    }
                },
                error: function(xhr, er) {
                    alert('Error ' + xhr.status + ' - ' + xhr.statusText);
                }
            });


        },


        rules: {
            nome: {
                required : true,
                maxlength: 50
            },
            email: {
                required : true,
                email: true,
                maxlength: 50
            },
            telefone: {
                required : true,
                maxlength: 50
            },
            mensagem: {
                required : true,
                maxlength: 1000
            }
        }
    });

    $('#bt_enviar_pesquisa').click(function(e){
        e.preventDefault();

        $.ajax({
            url: baseUrl+'/atendimento/pesquisa',
            dataType: 'html',
            type: 'post',
            data: $('#form_pesquisa').serialize(),

            success: function(data) {
                if(data==1){
                    alert('Dados enviados com sucesso.');
                    $("#form_pesquisa")[0].reset();
                }else{
                    alert('Houve um erro,tente novamente.');
                }
            },
            error: function(xhr, er) {
                alert('Error ' + xhr.status + ' - ' + xhr.statusText);
            }
        });
    });


    $('#saiba_cirurgia').click(function(e){
        e.preventDefault();

        $('#dialog_3d').dialog({
            modal: true,
            resizable : false,
            width: 735,
            height: 650,
            title: 'Cirurgia 3D'
        });
    });

    $('#saiba_mais').click(function(e){
        e.preventDefault();

        $('#dialog_saibamais').dialog({
            modal: true,
            resizable : false,
            width: 735,
            height: 650,
            title: 'Saiba Mais'
        });
    });

    $('#saiba_duvida').click(function(e){
        e.preventDefault();

        $('#dialog_duvida').dialog({
            modal: true,
            resizable : false,
            width: 850,
            height:320,
            title: 'Tire sua dúvida'
        });
    });

    $('#saiba_consulta').click(function(e){
        e.preventDefault();

        $('#dialog_consulta').dialog({
            modal: true,
            resizable : false,
            width: 850,
            height: 360,
            title: 'Agende sua consulta'
        });
    });

});


