function sendRating( origin, id, rating )
{
  $.ajax({
    url: '/json/rating',
    type: 'post',
    dataType: 'json',
    data: { 
      id: id, 
      rating: rating
    },
    success: function(data){
      if ( data.result ) {
        origin.find('.rating').text( data.message + '%' );
        var msg = 'Vote counted.';
      } else {
        var msg = data.message;
        // Error!
      }
      $('.ack .content').html( msg ).parent().show();
    }
  });
}

$(document).ready(function(){
  initViewer();
});

function initViewer()
{
  $('.footer li.Comment a[@href="#Comments"]').click(function(){
    var tArea = $('div.comments .postComment textarea');

    if ( tArea.size() > 0 )
    {
      tArea.focus();
    }
    return false;
  });
  $('.ack .close').click(function(){
    $(this).parent().hide();
  });
  $('.rateOptions .good').click(function(){
    var origin = $(this).parents('.TVViewer');
    sendRating( origin, origin.find('.id').text(), 1 );
  });
  $('.rateOptions .bad').click(function(){
    var origin = $(this).parents('.TVViewer');
    sendRating( origin, origin.find('.id').text(), 0 );
  });
  $('.footer li.fan').click(function(){
    var id = $(this).parents('.TVViewer').children('.seriesID').text();
    $.ajax({
      url: '/json/myShows/add',
      dataType: 'json',
      type: 'post',
      data: { id: id },
      success: function(data){
        if ( data.result )
        {
          var msg = 'Successfully added to "My Shows"';
        } else {
          var msg = data.message;
        }
        $('.ack .content').html( msg ).parent().show();
      }
    });
  });
  
  $('.footer li.popout').click(function(){
    var id = $(this).parents('.TVViewer').find('.id').text();
    var newWindow = window.open( '/popout/' + id, "TVPopout", "status=0,toolbar=0,width=614,height=398,resizable=0,menubar=0,location=0" );
  });
}