$(function(){
    
    /**
     * MURAL DE RECADOS
     */
    $('#nome').focus( function(){
        if ( $(this).val() == 'Seu nome completo' )
            $(this).val('');
    }).blur( function(){
        if ( $(this).val() == '' ) {
            $(this).val('Seu nome completo');
            $(this).css('border', '1px solid #fff');
        } else if ( $(this).val().length < 4)  {
            $(this).css('border', '1px solid #f00');
        } else {
            $(this).css('border', '1px solid #fff');
        }
    })

    $('#cidade').focus( function(){
        if ( $(this).val() == 'Sua cidade / estado' )
            $(this).val('');
    }).blur( function(){
        if ( $(this).val() == '' ) {
            $(this).val('Sua cidade / estado');
            $(this).css('border', '1px solid #fff');
        } else if ( $(this).val().length < 5)  {
            $(this).css('border', '1px solid #f00');
        } else {
            $(this).css('border', '1px solid #fff');
        }
    })

    $('#msg').focus( function(){
        if ( $(this).val() == 'Deixe aqui o seu recado ou pedido musical!' )
            $(this).val('');
    }).blur( function(){
        if ( $(this).val() == '' ) {
            $(this).val('Deixe aqui o seu recado ou pedido musical!');
            $(this).css('border', '1px solid #fff');
        } else if ( $(this).val().length < 8)  {
            $(this).css('border', '1px solid #f00');
        } else {
            $(this).css('border', '1px solid #fff');
        }
    })

    $('#btnEnviar').click( function(){

        var ok     = true;
        var nome   = $.trim( $('#nome').val() );
        var cidade = $.trim( $('#cidade').val() );
        var msg    = $.trim( $('#msg').val() );

        nome   = ( nome == 'Seu nome completo' ) ? '' : nome;
        cidade = ( cidade == 'Sua cidade / estado' ) ? '' : cidade;
        msg    = ( msg == 'Deixe aqui o seu recado ou pedido musical!' ) ? '' : msg;

        if (( nome == '' ) || ( nome.length < 4 )) {
            //$('#erro').text("Preencha o campo 'NOME' corretamente (mínimo 4 letras).").show().fadeOut(1000);
            alert('Preencha o campo "NOME" corretamente (mínimo 4 letras).\nTente novamente.\n\nExemplo: Pedro Paulo');
            $('#nome').css('border', '1px solid #f00');
            $('#nome').focus();
            ok = false;
            return;
        } else {
            $('#nome').css('border', '1px solid #fff');
        }

        if (( cidade == '' ) || ( cidade.length < 5 )) {
            alert('Preencha o campo "CIDADE / UF" corretamente (mínimo 5 letras).\nTente novamente.\n\nExemplo: Mamborê/PR');
            $('#cidade').css('border', '1px solid #f00');
            $('#cidade').focus();
            ok = false;
            return;
        } else {
            $('#cidade').css('border', '1px solid #fff');
        }

        if (( msg == '' ) || ( msg.length < 10 )) {
            alert('Preencha o campo "MENSAGEM" corretamente (mínimo 10 letras).\nTente novamente.');
            $('#msg').css('border', '1px solid #f00');
            $('#msg').focus();
            ok = false;
            return;
        } else {
            $('#msg').css('border', '1px solid #fff');
        }

        if ( ok ) {

            $.post("inc/mural_ajax.php", {
                nome: nome,
                cidade: cidade,
                msg : msg
            },
            function( data ) {
                
                /*
                $.get('inc/recados_ajax.php', function(data) {
                    $('#recados').html( data );
                });
                */
                
                
                $.blockUI({
                    message: data,
                    fadeIn: 500,
                    css: {
                    border                 : 'none',
                    padding                : '15px',
                    backgroundColor        : '#000',
                    '-webkit-border-radius': '10px',
                    '-moz-border-radius'   : '10px',
                    opacity: .6,
                    color: '#fff'
                } });

                setTimeout($.unblockUI, 3500);
                setTimeout(refreshPage , 4000);
                
                $('#nome').val('Seu nome completo');
                $('#cidade').val('Sua cidade / estado');
                $('#msg').val('Deixe aqui o seu recado ou pedido musical!');
                //http://www.malsup.com/jquery/block/#demos
            });
        }
    })

})

function refreshPage () {
    history.go(0);
}
