// JavaScript Document
jQuery.noConflict();
jQuery(function () {
    // votacao, estrelas
    jQuery('.auto-submit-star').rating({
        callback: function(value, link){
            jQuery(".loading_voto").show();
            var options = {
                target: "#resultado_votacao",
                success: function() {
                    jQuery(".loading_voto").hide("slow");
                }
            };
            jQuery(this.form).ajaxSubmit(options);
        }
    });
    
    /* 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) == "") {
                        alert('E-mail enviado com sucesso. Em breve retornaremos. Obrigado!');
                        jQuery('#'+form_name).resetForm();
                    } 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
        }
    });
});
