$(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('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Carregando...');
		$("#alertSite").css('top', $(window).height()/3 + $(window).scrollTop())
		$("#alertSite").css('left', $(window).width()/3)
		//$('#alertSite').css({'left':$(window).width()/2-$('#alertSite').width()/2, 'top':($(window).height()/10)+$(window).scrollTop()});
		$("#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();
			$("#alertSite").fadeOut("fast", function(){
                $("#alertError").html('<div style="padding:40px 0 0 90px">'+data.msgsucesso+'</div>');
				$(this).fadeIn();
			});
			return false;
		}else{
			var error_message = '';
			
			for(i=0; i<data.length; i++){
				error_message += '<li style="padding:1px 0 1px 0; border-bottom:dashed 1px #e7e7e7; list-style: none; "><span style="color:#FAA317">' + data[i].label + '</span> &raquo; <span>' + data[i].message + '</span></li>';
			}

			$("#alertSite").fadeOut("fast", function(){
                $("#alertError").html('<ul style="margin: 0">' + error_message + '</ul>');
				$(this).fadeIn();
			});
			return false;
		}
	}

	if($('.form_ajax')){	
		$('.form_ajax').ajaxForm(options); 
	}
});
