(function($) {
  $(function() {
    var featured_photo  = $("p#featured-photo img");
    var current         = $("li.current");

    $("ul#before, ul#after").each(function() {
      var kind = $(this).attr("id");

      $(this).find("a").each(function(i) {
        $(this).data("index", i).data("kind", kind);
      }).click(function() {
        current.removeClass("current");
        current = $(this).parent().addClass("current");

        featured_photo.attr("src", $(this).attr("href"));

        location.hash = $(this).data("kind") + "-" + ($(this).data("index") + 1);
        return false;
      });
    });

    var matcher = /^#(before|after)-(\d+)$/;
    if (location.hash && matcher.test(location.hash)) {
      var matches = location.hash.match(matcher);
      var kind    = matches[1];
      var index   = parseInt(matches[2]) - 1

      $("ul#" + kind + " li a").eq(index).click();
    }
  });
})(jQuery);
