$(function(){
	$('input[type=submit]').click(function() {
		do_post();
		return false;
	});

	$('.classes > .class').eq(-1).css({marginBottom:0});
	//$('.classes').jScrollPane({scrollbarWidth:10, dragMaxHeight:70, dragMinHeight:70, scrollbarMargin:10});
	scroll_height();
	$('a[rel^=prettyPhoto]').prettyPhoto();
});

var is_sending = false;

function scroll_height() {
	
	var f_height = $('#form').innerHeight(),
		h1_height = 126;
	
	$('.classes').jScrollPaneRemove();
	$('.classes').height(f_height - h1_height);
	$('.classes').jScrollPane({scrollbarWidth:10, dragMaxHeight:70, dragMinHeight:70, scrollbarMargin:10});
}

function do_post() {
	scroll_height();
	var msg = $('.msg');
	if (is_sending) {
		return false;
	}
	msg.hide(0).html('Aguarde...').show();
	$('.formx ul').remove();
	scroll_height();
	is_sending = true;
	$.ajax({
		type :  'POST',
		url  :  'form_action.php',
		data :  $('.formx').serialize() + '&ajax=1',
		dataType : "json",
		success: function(data){
			is_sending = false;
			msg.hide(0).html('');
			if (data.sucess) {
				msg.hide(0).html('Seu contato foi enviado, <br /> em breve entraremos em contato.').show();
				$('input[type=reset]').click();
				scroll_height();
			}else{
				
				$.each(data.error, function (field, msg){
					$('#formx_' + field).parent().append('<ul><li><p>' + msg + '</p></li></ul>');
				});
				scroll_height();
			}
		}
	});
}

