// Global variable definitions
// DB column numbers
var OPT_ID = 0;
var OPT_TITLE = 1;
var OPT_VOTES = 2;
var OPT_QUESTION = 3;

var votedID;

$(document).ready(function(){
  $("#poll").submit(formProcess); // setup the submit handler
  $("#past_polls").submit(formProcess2); // setup the submit handler
  //$(".polls a").click(formProcess3);

  if ($("#poll-results").length > 0 ) {
    animateResults();
  }
  
  if ($.cookie('vote_id')) {
    //$("#poll-container").empty();
    votedID = $.cookie('vote_id');
    pollID = $.cookie('poll_id');
    //alert("Loading...")
    $.getJSON("inc/poll.php?vote=none&poll_id="+pollID,loadResults);
  }
});

function formProcess3(event){
  event.preventDefault();
  //alert("view past poll");
  var poll_id = $(this).attr("id");
  $("#poll-results").load("inc/poll.php?view_past_poll=" + poll_id);
}

function formProcess2(event){
  event.preventDefault();
  //alert("past polls...");
  $("#poll-results").load("inc/poll.php?view=past_polls");
}

function formProcess(event){
  event.preventDefault();
  
  var id = $("input[@name='poll']:checked").attr("value");
  var poll_id = $("input[@name='poll_id']").attr("value");
  id = id.replace("opt",'');
  //alert("pollID: " + poll_id + ", voteID: " + id);
  
  $("#poll-container").load("inc/poll.php?vote="+id+"&poll_id="+poll_id);

  //$("#poll-container").fadeOut("slow",function(){
    //$(this).empty();
    
    votedID = id;
    $.cookie('vote_id', id, {expires: 365});
    $.cookie('poll_id', poll_id, {expires: 365});
    //$.getJSON("inc/poll.php?vote="+id+"&poll_id="+poll_id,loadResults);
    //$.getJSON("inc/poll.php?vote="+id+"&poll_id="+poll_id,alert("pollID: " + poll_id + ", voteID: " + id));
    //$("#poll-container").fadeOut("slow");
    $("#poll-container").load("inc/poll.php?vote="+id+"&poll_id="+poll_id, animateResults);
    //alert("Submitted...");
    
  //});
}

function animateResults(){
//alert("Animate...");
  $("#poll-results div").each(function(){
      var percentage = $(this).next().text();
      $(this).css({width: "0%"}).animate({
				width: percentage}, 1500 );

  });
}

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<p>" + data[0][OPT_QUESTION] + "</p><dl class='graph'>\n";
  for (id in data) {
    percent = Math.round((parseInt(data[id][OPT_VOTES])/parseInt(total_votes))*100);
    if (data[id][OPT_ID] !== votedID) {
//alert("option ID: " + data[id][OPT_ID] + ", votedID: " + votedID);
      results_html = results_html+"<dt class='bar-title'>"+data[id][OPT_TITLE]+"</dt><dd class='bar-container'><div id='bar"+data[id][OPT_ID]+"'style='width:0%;background-color:#00cc66;'>&nbsp;</div><strong>"+percent+"%</strong> (" + data[id][OPT_VOTES] + " votes)</dd>\n";
    } else {
      results_html = results_html+"<dt class='bar-title'>"+data[id][OPT_TITLE]+"</dt><dd class='bar-container'><div id='bar"+data[id][OPT_ID]+"'style='width:0%;background-color:#0066cc;'>&nbsp;</div><strong>"+percent+"%</strong> (" + data[id][OPT_VOTES] + " votes)</dd>\n";
    }
  }
  
  results_html = results_html+"</dl><p>Total Votes: "+total_votes+"</p></div>\n";
//alert("Results...");
  
  $("#poll-container").append(results_html).fadeIn("slow",function(){
    animateResults();});
}

