$(document).ready(function() {
  poll_vote_submit();
});

function poll_vote_submit()
{ 
    // bind form using ajaxForm 
    $('#poll_vote').ajaxForm({
      
        // dataType identifies the expected content type of the server response 
        dataType:  'json', 
 
        // success identifies the function to invoke when the server response 
        // has been received 
        success:   vote_process
    }); 
}


function vote_process(data)
{

// print data;
 var total_votes = 0;
 var aantal_opties = data.length;
 var percentage = 0;
 var afbeelding;

 
   $('#poll_vote_div').html('');
   
  // totaal aantal stemmen tellen
  for (var i=0; i < aantal_opties; i++)
    total_votes = total_votes+parseInt(data[i].answer_votes);
  // poll vote div leegmaken
  results_html = '<h2>' + data[0].question +'</h2>';

  
  for(var i=0; i < aantal_opties; i++)
    {
     
      percentage = Math.round((data[i].answer_votes / total_votes )*100);
      results_html +=  "<div class='poll_bar_titel'>" + data[i].answer_text + "</div>";
      results_html +=  '<div class="poll_bar_afbeelding"><img id="poll_bar'+i+'" src="/images/polls/' + data[i].answer_color +'.gif" style=" width:10%; height:9px;  padding: 3px 0px"></div>';
      results_html += '<span class="percent'+ i +'">' + percentage + '%</span><div style="clear:left"></div>';
      
    }
       results_html = results_html + "Totaal aantal stemmen: " + total_votes;
     
     $("#poll_vote_div").append(results_html).fadeIn("slow",function(){
    animateResults();});
  
}

function animateResults(){
  var i = 0;
 $("#poll_vote_div img").each(function(){
      
    
      var percentage = $(".percent"+i).text();
     
      $("#poll_bar" + i).css({width: "0%"}).animate({
				width: percentage}, 2000);
		  i++;
  });
}
