$(function() {
  // Go to appropriate area of hash
  if (location.hash) {
    $(location.hash).ScrollTo({
      offset: -50,
      duration: 1,
    });
  }

  // Ext links
  $("a").filter(function() {
    return this.hostname && this.hostname !== location.hostname;
  }).addClass('external');

  // Hrefs
  $('a[href^="#"]').click(function() {
    $($(this).attr('href')).ScrollTo({
      offset: -50,
    });
    return false;
  });

  // Scrollspy
  $('#topbar').scrollSpy();
  $('#topbar').find('a').click(function() {
    var target = $($(this).attr('href'));
    $(target).ScrollTo({
      offset: -50,
    });
    return false;
  });

  // Image hovers
  $('img[data-alt]').each(function(i,img) {
    var $img = $(img);
    var origSrc = $img.attr('src');
    var newSrc = $(this).data('alt');

    // On hover switch image
    $(this).hover(function() {
      $(this).attr('src', newSrc);
    }, function() {
      $(this).attr('src', origSrc);
    });

    // Preload
    $(document.body).append($('<img>').attr('src', newSrc).hide());
  });
})
