// Global variable definitions
// default target action
var ACTION = '/poll/vote/'; 
// DB column numbers
var OPT_ID = 0;
var OPT_TITLE = 1;
var OPT_VOTES = 2;

var votedID;
var poll_active;

$(document).ready(function(){
  $("#poll").submit(formProcess); // setup the submit handler
  
  if ($("#poll-results").length > 0 ) {
    animateResults();
  }

	$.ajax({
	   async: false,
	   type: "POST",
	   url: "/poll/getactive",
	   success: function(msg){
	     //alert( "Data Saved: " + msg );
	     poll_active = msg; 
	   }
	 });	 
	 
  if ($.cookie('vote_id') && $.cookie('poll_active') == poll_active && $.cookie('poll_question')) {
    $("#poll-container").empty();
    votedID = $.cookie('vote_id');
    //$.getJSON("poll.php?vote=none",loadResults);
    $.getJSON(ACTION+"none",loadResults);
  }
  
});

function formProcess(event){
  event.preventDefault();
  
  var id = $("input[@name='poll']:checked").attr("value");
  id = id.replace("opt",'');
  
  var active = $("input#poll_active").attr("value");
  var question = $("#poll-container h4").text();
  
  $("#poll-container").fadeOut("slow",function(){
    $(this).empty();
    
    votedID = id;
    //$.getJSON("poll.php?vote="+id,loadResults);
    $.getJSON(ACTION+id,loadResults);
    
    $.cookie('vote_id', id, {expires: 365});
    $.cookie('poll_active', active, {expires: 365});
    $.cookie('poll_question', question, {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>Wyniki:</h3>\n";
  results_html = results_html+"<br /><b>"+$.cookie('poll_question')+"</b>\n";
  for (id in data) {
    percent = Math.round((parseInt(data[id][OPT_VOTES])/parseInt(total_votes))*100);
    if (data[id][OPT_ID] == votedID) {
      results_html = results_html+"\n<br /><br />Wybrałes/aś opcję: <b>"+data[id][OPT_TITLE]+"</b>\n";
    }
  }
  results_html = results_html+"<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) {
      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%;'>&nbsp;</div><strong>"+percent+"%</strong></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:#F57309;'>&nbsp;</div><strong>"+percent+"%</strong></dd>\n";
    }
  }
  
  results_html = results_html+"</dl><p>Oddano glosow: "+total_votes+"</p></div>\n";
  
  results_html = results_html+"<br /><p class='sondaarchiwum'><a href='wyniki-glosowan'>Pokaż archiwum ankiet</a></p>\n";
  
  $("#poll-container").append(results_html).fadeIn("slow",function(){
    animateResults();});
}
