$(document).ready(function(){
  $("div.below_poll").show();
});


function viewResults(pollId) { 
  // get poll result info via ajax
  $.get("/poll-results.php", { poll_id: pollId }, function(data){  
    var tmp = "div#poll" + pollId;
    $(tmp).find("div.below_poll").fadeTo('fast',0);
    $(tmp).find("table.answers").fadeTo('fast', 0, function() { 
      $(tmp).find("table.answers").html(data);
      $(tmp).find("table.answers").fadeTo('fast', 1);
      $(tmp).find('div.below_poll').hide();
    });
  });
}

function submitPoll(pollId){ 
  // figure out which answer was chosen
  var tmp = "div#poll" . pollId;
  var vote = "";
  $(tmp).find("table.answers td input.answer").each(function(i){ 
    if ($(this).attr('checked')) { vote = $(this).val(); }
  });
  

  $.get("/poll-vote.php", { poll_id: pollId, vote: vote }, function(data) { 
    alert('Your vote has been recorded!');
    viewResults(pollId);
  });
}

