jQuery(document).ready(function(){
	
	jQuery("input[name=portfolio],input[name=site]").change(function(){
		if(jQuery("input[name=portfolio]:checked,input[name=site]:checked").val() == 's') {
			jQuery("input#endereco").addClass("not_null");
			jQuery("label#for-end").show("slow");
		}else{
			jQuery("input#endereco").removeClass("not_null");
			jQuery("label#for-end").hide("slow");
		}
	});

	jQuery("input#ddd").keyup(function(){
		if(jQuery(this).val().length == 1 && jQuery(this).val() != 0){
			jQuery(this).val('0' + jQuery(this).val());
		};
		if(jQuery(this).val().length == 3){
			jQuery("input#fone").focus();
		};
	});
	
	jQuery("input[type=radio]").change(function(){
		jQuery(this).parent().siblings("legend").css("border","none");
	});
	
	/* /.+@.+\..+/ */
	
	//jQuery("form input:reset").live('click',function(){ jQuery(this).find('.not_set').removeClass('.not_set'); });
	jQuery("form").submit( function (){
		var retorna = true;
		jQuery(this).find("input.not_null,textarea").each(function(){
			if(jQuery(this).val() == "" || jQuery(this).val() == "Campo obrigatório."){
				//console.log("Campo obrigatório.");
				//console.log(jQuery(this).val());
				jQuery(this).val('Campo obrigatório.');
				jQuery(this).addClass('not_set');
				jQuery(this).focus(function(){
					jQuery(this).unbind('focus');
					jQuery(this).val('');
					jQuery(this).removeClass('not_set');
				});
				retorna = false;
			}
		});
		
		if(jQuery("input[name=disponibilidade]").length && jQuery("input[name=portfolio]").length){
			if(jQuery("input[name=disponibilidade]:checked").length != 1){
				jQuery("input[name=disponibilidade]").parent().siblings("legend").css("border","1px solid red");
				retorna = false;
			};
			if(jQuery("input[name=portfolio]:checked").length != 1){
				jQuery("input[name=portfolio]").parent().siblings("legend").css("border","1px solid red");
				retorna = false;
			};
		}
		if(retorna == true){
			if(jQuery(this).attr("id") == "form-conosco"){ return true; }
			jQuery(this).find("input:submit").attr('disabled','disabled');
			jQuery("div#alert").html('<img src="img/loading.gif" /><p id="click_to_close">Click em qualquer lugar para fechar</p>');
			jQuery("div#alert").fadeIn();
			jQuery.post(jQuery(this).attr('action'),jQuery(this).serialize(),
			   function (data) {
					jQuery("div#alert img").remove();
					jQuery("div#alert").prepend(data);
					jQuery("*").click(function(){
						jQuery("div#alert").fadeOut();
						jQuery(this).find("input:submit").removeAttr('disabled');
					});
				}
			);
			//alert('Bombou!');
		}
		//return retorna;
		return false;
	});

});