/* $Id: poll.js 123 2009-09-11 07:13:11Z perfilev $ */

$(function () {
	$('#poll-form').submit(function()
		{
			submitVote();
			return false;
		});
	function submitVote()
    {
		$.post('/ajax/poll/', $('#poll-form').serialize(), function(data){
			var max = 0;
			var votes = 0;
			var html = '<div class="poll-title">' + data.question.question + '</div>';
			for (var option in data.options) {
				max = Math.max(data.options[option].votes, max);
				votes += data.options[option].votes;
			}
			for (var option in data.options) {
				html += '<div class="poll-option">' + "\n";
				voteAlt = (data.options[option].votes == 1 ? data.options[option].votes + ' vote' : data.options[option].votes + ' votes');
				html += '<img src="/images/poll/poll.gif" alt="' + voteAlt + '" title="' + voteAlt + '" width="';
				width = Math.round(((0 != data.options[option].votes ? data.options[option].votes : 0) / max) * 100)
				html += (0 != width ? width : 5)+ '" height="10" /> ';
				html += Math.round((data.options[option].votes / votes) * 100) + '%<br />';
				html += '<small>';
				if (data.question.picked == data.options[option].id) {
					html += '<i>' + data.options[option].option + '</i>';
				} else {
					html += data.options[option].option;
				}
				html += '</small>';
				html += '</div>' + "\n";
			}
			$('#poll').html(html);
		}, 'json');
    }
});