$().ready( function() {

	poll();

});

/* Enquete */

var votacao = '#poll .answers';
var result = '#poll div.results';
var bt_vote = '#poll input.votar';
var bt_parcial = '#poll input.parcial';

function poll() {
	$(result).hide();
	$('.buttons-results').hide();
	pollSubmit();
	pollParcial();
}

function pollVoltar() {
	
	$(votacao).show('slow');

	$('.buttons').show('slow');
	$('.buttons-results').hide('slow');

	$(result).hide('slow');

}

/* Enquete - Submit */

function pollSubmit() {
	
	$(bt_vote).bind('click', function() {

		var inputs = "input[type=radio][name=voto]";

		$(inputs).each(function() {

			if ( $(this).is(':checked') ) voto = $(this).attr('value');

		});

		jQuery.ajax({
			type: "POST",
			url: "modules/home/polls/polls_ajax.php",
			data: 'voto='+voto,
			success: pollSubmittedResponse
		});
		
		return false;

	});
	
}

function pollSubmittedResponse(response) {
	$(votacao).hide('slow');
	$('.buttons').hide('slow');
	$('.buttons-results').show('slow');

	$("#mensagem").html(response).fadeIn(500);
	pollGetParcial();
	return false;
}


/* Enquete - Parcial */

function pollParcial() {
	$(bt_parcial).bind('click', function() {
		pollGetParcial();
		return false;
	});
	return false;
}

function pollGetParcial() {
	
	var options = {
		type: 'GET',
		url: 'modules/home/polls/parcial.php',
		data: 'poll='+$('#poll input[name=id]').attr('value'),
		success: function(msg) { pollParcialSuccess(msg) }
	}
	
	$.ajax(options);
	return false;

}

function pollParcialSuccess(msg){
	$(votacao).hide('slow');
	$('.buttons').hide('slow');
	$('.buttons-results').show('slow');
	
	$(result).html(msg).show('slow');
	return false;
}
