addLoadEvent(function() {
	SURVEY_INIT.inicio();
});

function surveyVoteResults(jsonData)
{
	var sum = 0;
	for(i=0;i<jsonData['answers'].length;i++)
	{
		$("#surveyCharsArea").prepend('<li><label>' + jsonData['answers'][i]['answer'] + '</label><div class="poll-result-bar"><div class="theBar" style="width:10%">' + jsonData['answers'][i]['votes'] + '</div></div></li>');
		sum = Number(jsonData['answers'][i]['votes'] + sum );
	}
	
	$("#surveyMsgArea").html(jsonData['msg']);
	
	if(jsonData['result'] == 'ok')
	{
		$("#surveyMsgArea").addClass('green');
	}
	else
	{
		$("#surveyMsgArea").addClass('red');
	}
	
	$("#surveyAnswers").fadeOut('fast',function()
	{
		$("#surveyCharsArea").fadeIn('slow', function()
		{
			$(".theBar").each(function() 
		    {
		        var capacity = Math.round((($(this).text())/sum)*100);
		        $(this).css({'width': capacity + '%'});
		    }).show();
		});
	});
	
}

SURVEY_INIT = {
	inicio : function ()
	{
		$(".radioSurvey").click(function()
		{
			sendPost('SurveyFrm','/ajax/survey/surveyvote/', surveyVoteResults, ajaxError);
		});
		return false;
	}
}
