/*
 * AJAX Poll
 *
 * This lets you add an AJAX driven poll to your website. To use this script
 *
 * 1) Add your poll options to 'poll.php'. Also be sure to specify a place for
 * saving the file, preferably outside of the web root.
 * 2) Insert a poll div into your page somewhere. See examples on other sites for details
 * 3) Include this poll.js Javascript + the jQuery cookie javascript in your code
 * 4) Include the poll CSS in your CSS page. Again, see examples on other sites for details
 * 5) Theoretically, everything should now work. Note that this poll only works with Javascript
 * turned on.
 *
 * Based on http://net.tutsplus.com/javascript-ajax/creating-a-dynamic-poll-with-jquery-and-php/
 *
 * @requires http://plugins.jquery.com/project/Cookie
 * 
 */

// Global variable definitions
// DB column numbers
var OPT_ID = 0;
var OPT_TITLE = 1;
var OPT_VOTES = 2;

var votedID;

$(document).ready(function(){
  $("#poll").submit(formProcess); // setup the submit handler
  
  if ($("#poll-results").length > 0 ) {
    animateResults();
  }
  
  if ($.cookie('vote_id')) {
    $("#poll-container").empty();
    votedID = $.cookie('vote_id');
    $.getJSON("/poll/poll.php?vote=none",loadResults);
  }
});

function formProcess(event){

  event.preventDefault();
  
  var id = $("input[@name='poll']:checked").attr("value");
  id = id.replace("opt",'');
  
  $("#poll-container").fadeOut("slow",function(){
    $(this).empty();
    
    votedID = id;
    $.getJSON("/poll/poll.php?vote="+id,loadResults);
    
    $.cookie('vote_id', id, {expires: 365});
    });
}

function animateResults(){
  $("#poll-results div").each(function(){
      var percentage = $(this).next().text();
      $(this).css({width: "0%"}).animate({
				width: percentage}, 'slow');
  });
}

function loadResults(data) {
  var total_votes = 0;
  var percent;
  
  for (id in data) {
    total_votes = total_votes+parseInt(data[id][OPT_VOTES]);
  }
  
  var results_html = "<div id='poll-results'><h3>Poll Results</h3>\n<dl class='graph'>\n";
  for (id in data) {
    percent = Math.round((parseInt(data[id][OPT_VOTES])/parseInt(total_votes))*100);
    results_html = results_html+"<dt class='bar-title' id='bar"+data[id][OPT_ID]+"-title'>"+data[id][OPT_TITLE]+"</dt><dd class='bar-container'><div id='bar"+data[id][OPT_ID]+"' style='width:0%;'>&nbsp;</div><strong>"+percent+"%</strong></dd>\n";
  }
  
  // results_html = results_html+"</dl><p>Total Votes: "+total_votes+"</p></div>\n";
  
  $("#poll-container").append(results_html).fadeIn("slow",function(){
    animateResults();});
}