(function() {jQuery(document).ready(function(){

    jQuery("#s").bind("click", function(){
        if(jQuery("#s").attr("value") == "Buscar..."){
            jQuery("#s").attr("value", "");
        }
    });
    
    jQuery("#s").bind("blur", function(){
        if(jQuery("#s").attr("value") == ''){
            jQuery("#s").attr("value", "Buscar...");    
        };
    });

    var email = (window.location.href.split("?").length > 1) ? (window.location.href.split("?")[1]).split("=")[1] : '';
    if(email != ''){
        jQuery("input[name=ne]").attr("value", unescape(email));
    }
    
});
})();

function isReadyToNewsletter(){
    return (jQuery("#wpcf7-f1-p52-o1").find("div.wpcf7-mail-sent-ok").length > 0);
}
function submitNewsletterForm(){
    jQuery("#email-manager-form").submit();
}

function copyValuesToNewsletter(){
    var correspondingValues = {"your-name":"name", "your-email":"email", "your-city":"extra1"};
	for(var key in correspondingValues){
	    jQuery("input[name='" + correspondingValues[key] + "']").attr("value", jQuery("input[name='" + key + "']").attr("value"));
	}
}

jQuery(document).ready(function(){
    jQuery("#wpcf7-f1-p52-o1").find('input:submit').click(function(){
	    if(jQuery("#aprovacao-newsletter").is(":checked")){
			copyValuesToNewsletter();
			jQuery(this).everyTime(3000, "isReadyToNewsletter", function(){
				if(isReadyToNewsletter()){
					jQuery(this).stopTime("isReadyToNewsletter");
					submitNewsletterForm();
				}
			});		
		}
    });
});
