function init_poll_box(){
	$('#poll_box_form').submit(function(){
					var answ=$(this).find('input[checked]').val();
					var id=$('#poll_box_id').val();
					
//					if(answ)
//					{
						var posturl=lang+'/poll/poll/';
						var postdata= {id: id, number: answ};
						$.getJSON(posturl, postdata, function(cbData){
							$.cookie('vote_id['+id+']', id, {path:'/', expires: 365});
							drawPollResults(cbData, '#poll_box_form_container');
						});
//					}
					return false;
				});
}

function getPollResults(postdata, dest)
{
	$.getJSON(lang+'/poll/archive/', postdata, function(data){drawPollResults(data, dest)});
}

function init_poll_lister()
{			
	var flag=true;
	$('#poll_lister').find('li').each(function(){
		var id=$(this).attr('id').split('_')[1];
		var postdata={id: id};
		if(flag)
		{
			$("#poll_print").attr('href', lang+'/poll/print/id/'+id);
			getPollResults(postdata, '#poll_results');
//			$.getJSON(lang+'/poll/archive/', postdata, function(data){drawPollResults(data, '#poll_results')});
			flag=false;
		}
		$(this).click(function(){
			$("#poll_print").attr('href', lang+'/poll/print/id/'+id);
			getPollResults(postdata, '#poll_results');
//			$.getJSON(lang+'/poll/archive/', postdata, function(data){drawPollResults(data, '#poll_results')});
		});
	});
}

function drawPollResults(cbData, destID)
{
	var total_votes = 0;
	$.each(cbData.polls, function(i, item){
		total_votes=total_votes+parseInt(item.pr_counter);
	});
	var results_html = '<h4 class=\'poll_box\'>'+cbData.question+'</h4>';
	if(total_votes)
	{
		results_html += '<div id=\'poll-results\'><div class=\'graph\'>';
		$.each(cbData.polls, function(i, item){
			percent = Math.round((parseInt(item.pr_counter)/parseInt(total_votes))*100);
			results_html = results_html+'<div style=\'clear: left\'><div class\'bar-title\'>'+item.pa_text+'</div><div class=\'bar-container\'><div id=\'bar'+item.pa_number+'\' style=\'width: '+percent+'%; display: block;\'>&nbsp;</div><strong>'+percent+'%</strong></div></div>';
		})
		results_html = results_html+'</div><div style=\'clear: left\'>'+cbData.total_votes+': '+total_votes+'</div></div>';
	}
	else
	{
		results_html += cbData.no_poll_results;
	}
	$(destID).html(results_html);
}