$(document).ready(function(){
	$('#show_answers').click(function() {
		$('#form').hide();
		$('#answers').show();
		$('#vote-success').hide();
		$('.button').toggle();
	});
	
	$('#show_form').click(function() {
		$('#form').show();
		$('#vote-success').hide();
		$('#answers').hide();
		$('.button').toggle();
	});
	
	$('#vote').click(function(){
		var form_values = $(this).closest('form#poll').serialize();
		var id = $(this).attr('id').substring(1);
		//get the ajax info about the picture to display it properly
		$.ajax({
		   cache: false,
		   type: "POST",
		   url: $('#baseUrl').val() + '/ajax/vote',
		   data: form_values,
		   dataType: 'html',
		   success: function(msg){
				$('#form').hide();
				$('#answers').show();
				$('#vote-success').show();
				$('.button').toggle();
		   }
		});
	});
	
	// calculate the correct length of the poll bars
	var overall_length = 0;
	var elements = new Array();
	$('#answers').find('span.votes').each( function(index) {
		overall_length += parseInt($(this).html());
		elements[index] = parseInt($(this).html());
	});
	// this is the fraction of the overall length
	var segments = 190/overall_length;
	$('#answers').find('span.bar').each( function(index) {
		var new_length = elements[index]*segments;
		$(this).css('width', new_length);
	});
});