// Global variable definitions
// DB column numbers
var OPT_ID = 0;
var OPT_TITLE = 1;
var OPT_VOTES = 2;
var OPT_PREGUNTA = 3;
var OPT_PAGINA = "scf_modulos/encuesta/helpers/poll.php";

var votedID;

$(document).ready(function(){
  //$("#poll").submit(formProcess); // setup the submit handler
  $("a.enviar_form").click(formProcess);
  

  
  if ($.cookie('vote_id')) {
    $("#poll_container").empty();
	$("#poll_container").html("<div style='text-align:center; width:100%'><img src='/scf/imagenes/jquery/loading.gif' width='16' height='16'></div>").fadeIn("slow");    
    votedID = $.cookie('vote_id');
    //alert(OPT_PAGINA);
    $.getJSON(OPT_PAGINA+"?vote=none",loadResults);
  }
});

function formProcess(event){
  event.preventDefault();
  
  var id = $("input[@name='poll']:checked").attr("value");
  if(id == undefined){
	 alert("Seleccione una respuesta");
	 return false;
  }else{
  	id = id.replace("opt",'');
  }
  //alert(id);
  $("#poll_container").fadeOut("slow",function(){
    $(this).empty();
	$("#poll_container").append("<div style='text-align:center; width:100%'><img src='/scf/imagenes/jquery/loading.gif' width='16' height='16'></div>").fadeIn("slow");
    votedID = id;
    //alert(id);
    $.getJSON(OPT_PAGINA+"?vote="+id,loadResults);
    
    $.cookie('vote_id', id, {expires: 1});
    });
}

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;
  //alert(data);
  for (id in data) {
    total_votes = total_votes+parseInt(data[id][OPT_VOTES]);
  }
  
  var results_html = "<div class=\"contenido_encuesta_titulo\">"+data[id][OPT_PREGUNTA]+"</div>\n<dl class='graph'>\n<div id='poll-results'>\n";
  var primero = true;
  var style = "";
  for (id in data) {
    percent = Math.round((parseInt(data[id][OPT_VOTES])/parseInt(total_votes))*100);
	style = (primero)?"style='margin-top:0px;'":"";
	primero=false;
    if (data[id][OPT_ID] != votedID) {    		  
      results_html = results_html+"<dt class='bar-title' "+style+">"+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><strong>"+percent+"%</strong><br clear='all'/>\n";
    } else {
    	//alert("data["+id+"][OPT_ID] = "+data[id][OPT_ID]+" == "+votedID);
      results_html = results_html+"<dt class='bar-title' "+style+">"+data[id][OPT_TITLE]+"</dt><dd class='bar-container'><div id='bar"+data[id][OPT_ID]+"'style='width:0%;' class='bar-select'>&nbsp;</div><strong>"+percent+"%</strong></dd><strong>"+percent+"%</strong><br clear='all'/>\n";
    }
  }
  results_html = results_html+"</dl></div>\n";
 results_html = results_html+"</dl><p class='total_encuesta'>Total de votos: "+total_votes+"</p></div>\n";
  //$("#poll_container").empty();
  //alert(results_html);
  $("#poll_container").html(results_html);
  //alert($("#poll_container").html(results_html));
  $("#poll_container").fadeIn("slow",function(){animateResults();});
}
