$(document).ready(function(){
	$('form.poll_form').bind('submit', function(e){
		var f = this;
		$(f).ajaxSubmit({ dataType: 'json',
			beforeSubmit: function(){
				$(':input', f).attr('disabled', 'disabled');
			},
			success: function(data){
				if (data.success) {
					$(f).hide();
					var result = $(f).parent().find('div.poll_results').empty();
					$('>div.poll_message', $(f).parent()).empty().hide();
					var sum_v = 0;
					var poll_line_width = 190;
					$.each(data.results.items, function(i, item){
						sum_v += parseInt(item.votes);
					});
					$.each(data.results.items, function(i, item){
						var v_proc = Math.round( (parseInt(item.votes) * 100) / sum_v );
						var v_width = Math.round( (poll_line_width * v_proc) / 100 );
						if (v_proc == 50) {
							var g = 255, r = 255;
						}else if (v_proc > 50) {
							var g = 255;
							var r = 255 - (Math.round( (v_proc * 255) / 50 ) - 255);
						} else if (v_proc < 50) {
							var g = Math.round( (v_proc * 255) / 50 );
							var r = 255;
						}
						$('<div/>').addClass('poll_result_row').append(
							$('<div/>').addClass('poll_result_title').text(item.title)
						).append(
							$('<div/>').addClass('poll_resilt_info').text(item.votes + ' - ' + v_proc + '%')
						).append(
							$('<div/>').addClass('poll_fclear')
						).append(
							$('<div/>').addClass('poll_result_line').append(
								$('<div/>').addClass('poll_result_indicator')
								.css({
									width: v_width + 'px',
									backgroundColor: 'rgb(' + r + ',' + g + ',0)'
								})
							)
						).appendTo(result)
					});
					result.append(
						$('<div/>').attr('align', 'center').html(total_votes_text + ': ' + '<b>' + sum_v + '</b>')
					).fadeIn('fast');
				}
				if (data.message) {
					$(f).parent().find('div.poll_message').text(data.message).show();
				}
			},
			complete: function(){
				$(':input', f).removeAttr('disabled');
			}
		});
		return false;
	});
	$('.poll_item_title').bind('click', function(){
		i = $(this).parent().find(':radio').click();
	});
});