$().ready( function() {

	poll();

});

/////////////////////////////////////////////////////* Enquete */////////////////////////////////////////////////////

function poll() {
	
	$('.results').hide();
	
	pollSubmit();
	pollParcial();
	pollVoltar();

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////* Enquete - Submit */////////////////////////////////////////////////

function pollSubmit() {
	
	$('input.votar').live('click', function() {

		idPoll = $(this).parent().parent().parent().find('input[name=id]').attr('value');

		$(this).parent().parent().parent().find("input[type=radio][name=voto]").each(function() {

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

		});

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

	});
	
}

function pollSubmittedResponse(response) {

	$('.poll' + idPoll + ' .votation').hide('slow');
	$('.poll' + idPoll + ' .return').html(response).fadeIn(500);
	
	jQuery.ajax({
		type: 'GET',
		url: 'modules/widgets/polls/ajax-polls-parcial.php',
		data: 'poll=' + idPoll,
		success: pollParcialSuccess
	});
	
	return false;

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////* Enquete - Parcial *////////////////////////////////////////////////

function pollParcial() {
	
	$('.parcial').live('click', function() {

		idPoll = $(this).parent().parent().parent().find('input[name=id]').attr('value');

		jQuery.ajax({
			type: 'GET',
			url: 'modules/widgets/polls/ajax-polls-parcial.php',
			data: 'poll=' + idPoll,
			success: pollParcialSuccess
		});

		return false;

	});
}

function pollParcialSuccess(response){

	$('.poll' + idPoll + ' .votation').hide('slow');
	$('.poll' + idPoll + ' .results').html(response).show('slow');

	return false;

}

function pollVoltar() {

	$('.poll .voltar').live('click', function() {

		idPoll = $(this).parent().parent().parent().find('input[name=id]').attr('value');

		$('.poll' + idPoll + ' .votation').show('slow');

		$('.poll' + idPoll + ' .results').hide('slow');

		return false;

	});

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
