// JavaScript Document
jQuery.noConflict();
jQuery(function () {
    /* add favoritos de news */
    jQuery(".addFavoritos").click(function() {
        vTitle          = jQuery(this).attr('id');        
        jQuery.ajax({
            type: "GET",
            url: "/ajax_blog.php",
            data: "op=addFavoritos&pTitle="+vTitle,
            success: function(msg){
                alert(msg);
            },
            error: function(obj, msg, exception) {
                alert("<div class='atencao'>Erro Carregando dados!</div>");
            }
        });
    })
    
    /* alerta fazer login para add favoritos */
    jQuery(".alertAddFavoritos").click(function() {
        alert("Faça login ou cadastre-se para poder adicionar em favoritos!");
        window.location.replace("/login.php");
    })

    /* watermark */
    jQuery("#txtCommentNome").watermark("Seu nome");
    jQuery("#txtCommentEmail").watermark("Seu email");
    jQuery("#txtCommentTexto").watermark("Sua mensagem");

    /* limitador chars textarea */
    jQuery('.limited').inputlimitor({
        limit: 255,
        remText: '%n caractere%s restantes',
        limitText: 'Campo limitado a %n caractere%s.'
    });

    jQuery(".fone_mask").mask("(99) 9999-9999");
    jQuery(".data_mask").mask("99/99/9999");

    /* resetar form busca */
    jQuery(".resetForm").click(function() {
        jQuery('form').clearForm();
    })

    /* envio do form */
    jQuery(".frmAjax").submit(function() {
        var enviar_ok = true;
        var form_name = jQuery(this).attr('id');
        var vCamposErro = "Preencha os seguintes campos: \n";

        jQuery("#loading").show();

        /* checar campos */
        jQuery('#'+form_name+' :input[title=requerido] ').each(function(){
            if(jQuery.trim(jQuery("#"+this.id).val()) == ''){
                jQuery("#"+this.id).css({
                    background: "#FF9F9F"
                });
                enviar_ok = false;
                vCamposErro = vCamposErro + " - " + jQuery(this).attr('name') + "\n";
            } else {
                jQuery("#"+this.id).css({
                    background: "#B8F5B1"
                });
            }
        });

        if(enviar_ok) {
            var options = {
                success: function(msg) {
                    jQuery("#loading").hide("slow");
                    // sucesso no envio
                    if(jQuery.trim(msg) == "") {
                        if(jQuery('#frmMsg').length == 1) {
                            alert(jQuery('#frmMsg').val());
                        } else {
                            alert('E-mail enviado com sucesso. Em breve retornaremos. Obrigado!');
                        }
                        jQuery('#'+form_name).resetForm();
                        if(jQuery('#frmRedirect').length == 1) {
                            window.location.replace(jQuery('#frmRedirect').val());
                        }
                    } else {
                        alert(jQuery.trim(msg));
                    }
                }
            };

            jQuery(this).ajaxSubmit(options);

            return false; // faz o submit normal
        } else {
            jQuery("#loading").hide("slow");
            alert(vCamposErro);
            return false; //cancela submit normal
        }
    });
    
});