$(document).ready(function($) {
  
  //  ===========================================================================
  //  = CSS Fixes =
  //  ===========================================================================
  
  $('#thumbnails .thumbnail:nth-child(5n)').css('margin-right', '0');
  
  $('ul#pagination li:last').css('margin-right', '0');
  
  $('#description').css("margin-top", $('#thumbnails').height() + 37 + "px");
  
  //  ===========================================================================
  //  = Functions =
  //  ===========================================================================
  
  $('ul.nav li').hover(
    function() { $(this).toggleClass('active') },
    function() { $(this).toggleClass('active') }
  );
  
  $('.thumbnail').hover(
    function() {
      $(this).toggleClass('active')
    },
    function() {
      $(this).toggleClass('active')
    }
  );
    
  $('.client-image a').click(function() {
    var href = this.href.match(/\?/) ? this.href.split("?").join(".js?") : this.href + ".js";
    
    $('.thumbnail.selected').toggleClass('selected');
    $(this).parent().toggleClass('selected');
    
    $.getJSON(href, function(json) {
      $('#sidebar').append('<img src="/images/' + json.slug + '.jpg" class="large-image" style="display: none" />');
      $('#sidebar img:first').fadeOut("slow", function() {
        $(this).next().fadeIn("slow")
        $(this).remove();
      });
    });

    return false;
  });
  
  $('.thumbnail a').not('body.clients .thumbnail a').click(function() {    
    var href = this.href.match(/\?/) ? this.href.split("?").join(".js?") : this.href + ".js";
    
    $('.thumbnail.selected').toggleClass('selected');
    $(this).parent().toggleClass('selected');
    
    $.getJSON(href, function(json) {
      $('#sidebar').append('<img src="/images/' + json.slug + '.jpg" class="large-image" style="display: none" />');
      $('#sidebar img:first').fadeOut("slow", function() {
        $('#description p').text(json.description);
        $('#description h1').text(json.name);
        
        if ( json.download ) {
          $('.download').html('<a href="/downloads/' + json.slug + '.pdf">Download</a>')
        } else {
          $('.download').html('')
        }
        
        $(this).next().fadeIn("slow")
        $(this).remove();
      });
    });
            
    return false;
  });
  
});
