var OPT_ID=0;var OPT_TITLE=1;var OPT_VOTES=2;var OPT_QUESTION=3;var votedID;$(document).ready(function(){var pollType=$("#pollType").text();if(!pollType){pollType=2;}
$("#poll").submit(formProcess);if($("#poll-results").length>0){animateResults();}
$.getJSON("/ajaxgetrandompoll?type="+pollType,null,loadPoll);});function formProcess(event){event.preventDefault();var id=$("input[@name='poll']:checked").attr("value");if(id){id=id.replace("opt",'');$("#poll-container").fadeOut("slow",function(){var pollId=$("#pollId").text();$(this).empty();votedID=id;$.getJSON("/ajaxpoll?vote="+id+"&id="+pollId,loadResults);$.cookie('poll_id'+pollId,id,{expires:365});});}}
function animateResults(){$("#poll-results .resultbar").each(function(){var percentage=$(this).next().text();if(percentage)
$(this).css({width:"0%"}).animate({width:percentage},'slow');});}
function loadPoll(data){$("#pollId").text(data.id);if($.cookie('poll_id'+data.id)){$("#poll-container").empty();votedID=$.cookie('poll_id'+data.id);$.getJSON("/ajaxpoll?vote=-1&id="+data.id,loadResults);}else{$("#pollQuestion").text(data.question);var results_html="";for(var i=0;i<data.answers.length;i++){results_html=results_html+"<div class='answer'>";results_html=results_html+"<table><tr><td>"+"<input type='radio' name='poll' class='radio' value='"+data.answers[i].answerid+"' id='"+data.answers[i].answerid+"'/></td><td> <label for='"+data.answers[i].answerid+"'>"+data.answers[i].answertext+"</label></rd></tr></table></div>";}
$("#answers").append(results_html);$("#answers").after("<input type='submit' class='send-btn' value='Vote'/>")
$("poll-container").show();}}
function loadResults(data){var total_votes=0;var percent;var name;for(id in data){total_votes=total_votes+parseInt(data[id][OPT_VOTES]);name=data[id][OPT_QUESTION];}
var results_html="<div id='poll-results'><p id='pollQuestion' class='blogHeader'>"+name+"</p>\n";for(id in data){if(total_votes!=0){percent=Math.round((parseInt(data[id][OPT_VOTES])/parseInt(total_votes))*100);}else{percent=0;}
var showpercent=percent;results_html=results_html+"<p class='bar-title'>"+data[id][OPT_TITLE]+":</p>"
results_html=results_html+"<table class='graph'><tr><td class='bar-container'>";results_html=results_html+"<div class='resultbardefault'>";results_html=results_html+"<div class='resultbar' id='bar"+data[id][OPT_ID]+"'style='width:0%;'>";results_html=results_html+"&nbsp;</div><strong style='display:none;'>"+showpercent+"%</strong></div></td><td class='percent'><span>"+percent+"%</span></td>\n";results_html=results_html+'</tr></table>';}
results_html=results_html+""+"<p class='total_votes'>Total Votes: "+total_votes+"</p></div>\n";$("#poll-container").append(results_html).fadeIn("slow",function(){animateResults();});}
