$(function() {
  
  // Hide Submit button on Artist name search, auto-submit on change
  $('input[type=submit]', '#director-search').remove();
  $('select', '#director-search').change(function() {
    url = $(this).parent('form').attr('action') + '/' + $(this).val();
    window.location = url;
  });
  
  // Make category listing on artists/show dynamically filter list of works
  $('a', '#categories').hover(function() {
    $(this).addClass('hover');
  }, function() {
    $(this).removeClass('hover');
  }).click(function() {
    $('a', '#categories').removeClass('active');
    $(this).addClass('active');
    
    category = $(this).attr('rel');
    $('li', '#artist-works').not('.'+category).hide();
    $('li.'+category, '#artist-works').show()
      .removeClass('first').removeClass('last');
    window.location.hash = category;
  });
  // Pre-select category if its in the hash
  if (window.location.hash) {
    $('a[rel='+window.location.hash.replace('#', '')+']', '#categories').click();
  }
  
  // Turn works listing on artists/show into links to load videos
  $('#content', 'body.artist').append('<div id="video-player"></div>');
  $video_player = $('#video-player');
  $('li', '#artist-works').each(function() {
    $(this).wrapInner('<a rel="'+this.id+'"></a>').removeAttr('id');
  });
  $('a', '#artist-works').click(function() {
    work = $(this).attr('rel');
    base = $('base').attr('href');
    url = base + 'artists/work/' + work.replace('work-', '');
    $video_player.empty().show().addClass('loading');
    
    $video_player.load(url, function(responseText, textStatus, XMLHttpRequest) {
      if (textStatus == 'error') {
        // Do something?
      }
      $video_player.removeClass('loading');
    });
    
    window.location.hash = work;
  });
  // Pre-load work if its in the hash
  if (window.location.hash) {
    $('a[rel='+window.location.hash.replace('#','')+']', '#artist-works').click();
  }
  
  // Resize Recent Works list to fit all contents
  if ($('#recent-works-list').length > 0) {
    sum = 0;
    $('dl', '#recent-works-list').each(function() { sum += $(this).outerWidth(); });
    $('div', '#recent-works-list').width(sum);
  }
  
  // Link to videos for Recent Works
  $('#content', 'body.recent-works').append('<div id="video-player"></div>');
  $video_player = $('#video-player');
  $('dl', '#recent-works-list').each(function() {
    $('dd.img', this).wrapInner('<a rel="'+this.id+'"></a>');
  });
  $('dd.img a', '#recent-works-list').click(function() {
    work = $(this).attr('rel');
    base = $('base').attr('href');
    url = base + 'recent_works/' + work.replace('work-', '');
    
    $('#recent-works-list').animate({ width: 178 });
    $video_player.empty().addClass('loading').animate({ width: 558 }, function() {
      $('#recent-works-list').scrollTo($('#'+work), { duration: 500 });
      $video_player.load(url, function(responseText, textStatus, XMLHttpRequest) {
        if (textStatus == 'error') {
          // Do something?
        }
        $video_player.removeClass('loading').find('#close').click(function() {
          $('#recent-works-list').animate({ width: 736 });
          $video_player.animate({ width: 0 }, function() {
            $(this).hide();
          });
        });
      });
    });
    
    window.location.hash = work;
  });
  
  // Pre-load work if its in the hash
  if (window.location.hash) {
    $('a[rel='+window.location.hash.replace('#','')+']', '#recent-works-list').click();
  }
  
});

