/**
* Animation to play once the voting is done
**/
function after_vote_anim (driver, type) {
  $(driver).find('table.vote-doit').slideUp(750, function() {
    $(driver).find('table.vote-results').slideDown(750, function() {
      $(driver).find('img.poll').attr('src', 'images/user_vote_' + type + '.gif').fadeIn(1000);
    })
  })
}


$(document).ready(function() {
  $('.vote-doit a').click(function() {
    
    var td = $(this).closest('td').eq(0);
    var driver = $(this).closest('div.driver').eq(0);
    
    var type;
    if ($(td).is('.poll-red')) type = 'red';
    if ($(td).is('.poll-black')) type = 'black';
    if ($(td).is('.poll-blue')) type = 'blue';
    
    var optns = {
      type: type,
      id: $(driver).attr('data')
    };
    
    $.getJSON('ajax/vote_driver.php', optns, function(data) {
      if (data.result == 1) {
        // success
        $(driver).find('span.poll-red').text(data.red + '%');
        $(driver).find('span.poll-black').text(data.black + '%');
        $(driver).find('span.poll-blue').text(data.blue + '%');
        
        after_vote_anim(driver, type);
        
      } else {
        // failure
        
      }
    });
    
    return false;
  });
  
  $('.vote-results').hide();
  $('.poll').hide();
});
