// JavaScript Document
jQuery.noConflict();
jQuery(function () {
    /* auto suggest */
    // Safely inject CSS3 and give the search results a shadow
    var cssObj = {
        'box-shadow' : '#888 5px 10px 10px', // Added when CSS3 is standard
        '-webkit-box-shadow' : '#888 5px 10px 10px', // Safari
        '-moz-box-shadow' : '#888 5px 10px 10px'
    }; // Firefox 3.5+
    jQuery("#suggestions").css(cssObj);

    // Fade out the suggestions box when not active
    jQuery("input").blur(function(){
        jQuery('#suggestions').fadeOut();
    });

    /* watermark */
    jQuery("#txtBuscar").watermark("Digite a palavra desejada e clique em pesquisar");
    
    /* 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
        }
    });
    
});

function lookup(inputString) {
    if(inputString.length == 0) {
        jQuery('#suggestions').fadeOut(); // Hide the suggestions box
    } else {
        jQuery.post("/inc.autosuggest.php", {
            queryString: ""+inputString+""
            }, function(data) { // Do an AJAX call
            jQuery('#suggestions').fadeIn(); // Show the suggestions box
            jQuery('#suggestions').html(data); // Fill the suggestions box
        });
    }
}

function lookupTradutorPortuguesIngles(inputString) {
    if(inputString.length == 0) {
        jQuery('#suggestions').fadeOut(); // Hide the suggestions box
    } else {
        jQuery.post("/inc.autosuggest_portugues_ingles.php", {
            queryString: ""+inputString+""
            }, function(data) { // Do an AJAX call
            jQuery('#suggestions').fadeIn(); // Show the suggestions box
            jQuery('#suggestions').html(data); // Fill the suggestions box
        });
    }
}

function lookupTradutorInglesPortugues(inputString) {
    if(inputString.length == 0) {
        jQuery('#suggestions').fadeOut(); // Hide the suggestions box
    } else {
        jQuery.post("/inc.autosuggest_ingles_portugues.php", {
            queryString: ""+inputString+""
            }, function(data) { // Do an AJAX call
            jQuery('#suggestions').fadeIn(); // Show the suggestions box
            jQuery('#suggestions').html(data); // Fill the suggestions box
        });
    }
}
