$(document).ready(function(){

  // Choose a random print to display
  var display_index = Math.floor(Math.random() * ($('#featured-prints img').length))

  // Display the print
  $('#featured-prints .featured-print:eq('+display_index+')').show();
  // Display the associated nav link
  $('#featured-prints #featured-links a.imglink:eq('+display_index+')').addClass('displayed');

  // Bind the navigation events
  $('#featured-prints #featured-links a.imglink').bind('click', function() {
    $('#featured-prints #featured-links a.imglink').removeClass('displayed');
    $(this).addClass('displayed');

    // Drop off the #
    var img = $(this).attr('href').substring(1);
    $('#featured-prints .featured-print').hide();
    $('#featured-prints .featured-print#'+img).show();
    return false;
  });

  // Bind the next and previous navigation events
  $('#featured-prints #featured-links a.nav').bind('click', function() {
    // Find the current image
    var current = $('#featured-prints #featured-links a.imglink.displayed');
    var target;

    if ($(this).hasClass('next')) {
      target = current.next('.imglink');
    } else {
      target = current.prev('.imglink');
    }

    if (target.length != 0) {
      target.click();
    }

    return false;
  });

});
