jQuery(document).ready(function() {
  function filterPath(string) {
  return string
    .replace(/^\//,'')
    .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
    .replace(/\/$/,'');
  }

  var locationPath = filterPath(location.pathname);
  jQuery('body').click(function(event) {
    var $tgt = jQuery(event.target);
    var $link = ($tgt.is('a') && $tgt) || ($tgt.parents('a').length && $tgt.parents('a:first')) || null  ;
    //stop if it's not a link
    if (!$link || $link.parent().is('.reply')) { return; }
  
    var link = $link[0];
    var thisPath = filterPath(link.pathname) || locationPath;
    if (  locationPath == thisPath
    && (location.hostname == link.hostname || !link.hostname)
    && link.hash.replace(/#/,'') ) {
      event.preventDefault();
      var $target = jQuery(link.hash), target = link.hash;
      if ($target.length) {
        var targetOffset = $target.offset().top;

        jQuery('html, body').animate({scrollTop: targetOffset}, 400, function() {
          location.hash = target;
        });
      }
    }

  });
  jQuery('a[href*=#]').each(function() {
    var thisPath = filterPath(this.pathname) || locationPath;
  });
});

