$(document).ready(function(){
  $('.faceOff .face_off_vote').click(function(){
    var faceOffId = $(this).parent().parent().find('.faceOffId').val();
    voteFaceOff(faceOffId);
  });
  $('.faceOff .face_off_results').click(function(){
    //var faceOffId = $('.faceOff .faceOffId').val(); 
    var faceOffId = $(this).parent().parent().find('.faceOffId').val();
    viewResults(faceOffId);
  });
});


function viewResults(faceOffId) { 
  // get face-off result info via ajax
  $.get("/face-off-results.php", { id: faceOffId }, function(data){  
    tmp = "#faceOff" + faceOffId;

    //var tmp = "div#face-off" + faceOffId;
    $(tmp).find("div.below_face_off").fadeTo('fast',0);
    $(tmp).find(".below_face_off").html(data);
    $(tmp).find("div.below_face_off").fadeTo('fast',1);
  });
}

function voteFaceOff(faceOffId){ 
  //var faceOffId = $('.faceOff .faceOffId').val(); 
  var vote = '';
  var tmp = "#faceOff" + faceOffId;

  // figure out which answer was chosen
  $(tmp).find(".options span input").each(function(i){ 
    if ($(this).attr('checked')) { vote = $(this).val(); }
  });

  if (vote != '') { 
    $.get("/face-off-vote.php", { id: faceOffId,  vote: vote }, function(data) { 
      alert('Your vote has been recorded!');
      viewResults(faceOffId);
    });
  }
}


