$(document).ready(function($){
  function updatePlayer($wrapper, $thumbs, index)
  {
    var offset = $thumbs.eq(index).position().top - $thumbs.eq(0).position().top;
    $wrapper.animate({marginTop: -offset}, 200);
  }

  function updatePlayerHorizontal($wrapper, $thumbs, index, size)
  {
    $thumbs.css('width', size/2)
      .removeClass('current')
      .eq(index)
        .addClass('current')
        .css('width', size)
        .end()
      .filter('.large')
        .find('img')
          .effect('scale', {percent: 50 , speed: 500})
          .end()
        .removeClass('large')
        .addClass('small')
        .end()
      .eq(index)
      .find('img')
        .effect('scale', {percent: 200, speed: 500 })
        .end()
      .addClass('large')
      .removeClass('small');
    var localOffset = $wrapper.parent().width()/2 - size/2;
    var offset = $thumbs.eq(index).position().left - $thumbs.eq(0).position().left - localOffset;
    $wrapper.animate({marginLeft: -offset}, 200);
  }

  //photo player
  var $p_thumbs = $('#photo-thumbs > div');
  var p_size = $p_thumbs.find('img').width();
  $('#photo-thumbs .small').live('click', function() {
    updatePlayerHorizontal($('#photo-thumbs'), $p_thumbs, $(this).index(), p_size);
    return false;
  });
  $p_thumbs.find('img').effect('scale', {percent: 50, speed: 0 }).end().addClass('small').eq(0).trigger('click');

  //video player
  var $v_thumbs = $('#video-thumbs > div');
  var v_size = $v_thumbs.find('img').width();
  $('#video-thumbs .small').live('click', function() {
    updatePlayerHorizontal($('#video-thumbs'), $v_thumbs, $(this).index(), v_size);
    return false;
  });
  $v_thumbs.find('img').effect('scale', {percent: 50, speed: 0 }).end().addClass('small').eq(0).trigger('click');

  //Magazine player
  var m_per_page = 3;
  var m_car_pos = 0;
  var m_car_num = $('#media-magazine .thumb').size();
  var m_car_max_pos =  Math.ceil(m_car_num / m_per_page);
  if (m_car_num <= m_per_page)
  {
    $('#magazine-nav-next').hide();
  }
  $('#media-magazine .thumb-thumb:first-child').addClass('selected');


  $('#media-magazine .embed').html($('#media-magazine .thumb:first-child .placeholder').html());
  $('#media-magazine .thumb:first-child').addClass('selected');
  $('#media-magazine .thumb a').click(function () {
    $('#media-magazine .embed').html($(this).siblings('.placeholder').html());
    $(this).parent().siblings().removeClass('selected');
    $(this).parent().addClass('selected');
    return false;
  });

  //Carousel
  $('#magazine-nav-prev').live('click', function() {
    m_car_pos--;
    if (m_car_pos == 0)
    {
      $(this).hide();
    }
    var m_car_offset = $('#media-magazine .carousel-thumbs-wrapper').width() + parseInt($('#media-magazine .carousel-thumbs').children().css('margin-right'));
    var offset = m_car_offset * m_car_pos * -1;
    $('#media-magazine .carousel-thumbs').animate({marginLeft: offset}, 200);
    $('#magazine-nav-next').show();
    return false;
  });

  $('#magazine-nav-next').live('click', function() {
    if (++m_car_pos == m_car_max_pos - 1)
    {
      $(this).hide();
    }
    var m_car_offset = $('#media-magazine .carousel-thumbs-wrapper').width() + parseInt($('#media-magazine .carousel-thumbs').children().css('margin-right'));
    var offset = m_car_offset * m_car_pos * -1;
    $('#media-magazine .carousel-thumbs').animate({marginLeft: offset}, 200);
    $('#magazine-nav-prev').show();
    return false;
  });
});