$(document).ready(function() {
  // Smooth scrolling for anchor links
  $('a[href^=#]').each(function() {
    var $target = $(this.hash), target = this.hash;
    var targetOffset = (target) ? $target.offset().top : 0;
    $(this).click(function(event) {
      event.preventDefault();
      $('html,body').animate({scrollTop: targetOffset}, 600, function() {
        location.hash = target;
      });
    });
  });
  // Search submit link
  $('#search p').click(function() {
    $('#search').submit();
  });
  // Image galleries in the blog
  if ($('.gallery').length > 0) {
    $('.gallery').each(function() {
      // enable some js specific styles
      $(this).addClass('js');
      // create space for the images at the top
      var imageHeight = $(this).attr('class').match(/ (\d+)/)[1] ? $(this).attr('class').match(/ (\d+)/)[1] : 0;
      $(this).css({paddingTop: parseInt(imageHeight) + 10 + 'px'});
      // show the loading animation
      
      // insert the target images for each image link
      $(this).find('a').each(function() {
        var container = $(this).closest('li');
        var source = $(this).attr('href');
        $('<img>').attr({
          'src': source,
          'class': 'full-size'
        }).hide().appendTo(container).load(function() {
          if (container.index() == 0) {
            container.addClass('current');
            // hide loading animation
            $(this).fadeIn(500);
          }
          if (this.width < 500) {
            $(this).css('left', ((500 - this.width) / 2));
          }
        });
      });
      // add the click behavior for the thumbnails
      $(this).find('a').click(function(event) {
        event.preventDefault();
        var container = $(this).closest('li');
        var visible = $(this).closest('ol').find('img.full-size:visible');
        var target = $(this).siblings('img.full-size');
        if (target.is(':not(:visible)')) {
          visible.fadeOut(500);
          target.fadeIn(500);
        }
        container.addClass('current').siblings().removeClass('current');
      });
      // create the rollover navigation for paging through images
      // Messy -- needs some DRYING up, simplification of selectors
      $('<div class="next"></div><div class="previous"></div>').appendTo($(this));
      $(this).find('.next, .previous').css({height: imageHeight + 'px'});
      $(this).find('.next').click(function() {
        var visible = $(this).parent().find('img.full-size:visible');
        var container = visible.closest('li');
        var count = container.closest('ol').find('li').length;
        var target = (count == container.index() + 1) ? container.siblings().first().find('img.full-size') : container.next().find('img.full-size');
        visible.fadeOut(500);
        target.fadeIn(500);
        container.removeClass('current');
        target.parent().addClass('current');
      });
      $(this).find('.previous').click(function() {
        var visible = $(this).parent().find('img.full-size:visible');
        var container = visible.closest('li');
        var count = container.closest('ol').find('li').length;
        var target = (container.index() == 0) ? container.siblings().last().find('img.full-size') : container.prev().find('img.full-size');
        visible.fadeOut(500);
        target.fadeIn(500);
        container.removeClass('current');
        target.parent().addClass('current');
      });
      
    });
  }
  // Fancybox zooms
  $('a.zoom').fancybox({
    'titlePosition': 'inside',
    'titleShow'    : true
  });
});
