myShows = { page: 1 };
fullLengthTVShows = { page: 1, sort: 'random' };
fullLengthMovies = { page: 1, sort: 'random' };

function TVHomeInit()
{
  $('.newestEpisodes li:first div.content').show();
  
  initNewEpisodes();
  
  $('.myShows .prev').click(function(){ myShowsPaginate( -1 ); });
  $('.myShows .next').click(function(){ myShowsPaginate( 1 ); });
  
  $('.fullLengthTVShows .prev').click(function(){ fullLengthTVShowsPaginate( -1 ); });
  $('.fullLengthTVShows .next').click(function(){ fullLengthTVShowsPaginate( 1 ); });
  $('.fullLengthTVShows select[@name="tvSort"]').change(function(){ fullLengthTVShows.page = 1; fullLengthTVShowsPaginate( 0 ); });
  
  $('.fullLengthMovies .prev').click(function(){ fullLengthMoviesPaginate( -1 ); });
  $('.fullLengthMovies .next').click(function(){ fullLengthMoviesPaginate( 1 ); });
  $('.fullLengthMovies select[@name="moviesSort"]').change(function(){ fullLengthMovies.page = 1; fullLengthMoviesPaginate( 0 ); });
}

function initNewEpisodes() 
{
  $('.newestEpisodes li:not(.selected) h3').click(function(){
    $('.newestEpisodes li.selected div.content').slideUp().parents('li.selected').removeClass('selected');
    $(this).parents('li').addClass('selected').find('div.content').slideDown();
    initNewEpisodes();
  });
  $('.newestEpisodes li.selected h3').unbind('click');
}

function myShowsPaginate( direction )
{
  if ( ! direction )
  {
    var direction = 0;
  }
  
  myShows.page += direction;
  
  $.ajax({
    url: '/json/home/myShows',
    dataType: 'json',
    type: 'post',
    data: myShows,
    success: function ( data ) {
      $('.myShows .contents').html( data.content );
    }
  });
}

function fullLengthTVShowsPaginate( direction )
{
  if ( ! direction )
  {
    var direction = 0;
  }
  
  fullLengthTVShows.page += direction;
  fullLengthTVShows.sort = $('.fullLengthTVShows option:selected').val();
  
  $.ajax({
    url: '/json/home/fullLengthTVShows',
    dataType: 'json',
    type: 'post',
    data: fullLengthTVShows,
    success: function ( data ) {
      $('.fullLengthTVShows .contents').html( data.content );
    }
  });
}

function fullLengthMoviesPaginate( direction )
{
  if ( ! direction )
  {
    var direction = 0;
  }
  
  fullLengthMovies.page += direction;
  fullLengthMovies.sort = $('.fullLengthMovies option:selected').val();
  
  $.ajax({
    url: '/json/home/fullLengthMovies',
    dataType: 'json',
    type: 'post',
    data: fullLengthMovies,
    success: function ( data ) {
      $('.fullLengthMovies .contents').html( data.content );
    }
  });
}