$(document).ready(function() { 
	var options = { 
		dataType:  'json',
		beforeSubmit: show_loading,
		success: form_response
	}
	
	function show_loading(){
		$("#bgSite").css('height', $(document).height());
		$("#bgSite").fadeTo("slow", 0.5);
		$("#bgSite").show();
		$("#alertError").html('Carregando...');
		$("#alertSite").css('top', ($(window).height()/3)+$(window).scrollTop())
		$("#alertSite").css('left', $(window).width()/3)
		$("#alertSite").show();
	}
	
	function form_response(data){ 
		/*if(data.redirect_to){
			window.location.href = data.redirect_to;
			return true;
		}*/
		if(data.sucesso){
			$('.form_ajax').clearForm();
			$('.form_ajax').clearFields();
			//$('.form_ajax').resetForm();
			$("#alertSite").fadeOut("fast", function(){
				$("#alertError").html(data.msgsucesso);
				$(this).fadeIn();
			});
			return false;
		}else{
			var error_message = '';
			
			for(i=0; i<data.length; i++){
				error_message += '<li>' + data[i].label + ': <span>' + data[i].message + '</span></li>';
			}
			
			$("#alertSite").fadeOut("fast", function(){
				$("#alertError").html('<ul>' + error_message + '</ul>');
				$(this).fadeIn();
			});
			return false;
		}
	}
		
	if($('.form_ajax')){	
		$('.form_ajax').ajaxForm(options); 
	}
});


