//smooth scrolling

jQuery.easing.bounceout = function(p, n, firstNum, delta, duration) {
	if ((n/=duration) < (1/2.75)) {
		return delta*(7.5625*n*n) + firstNum;
	} else if (n < (2/2.75)) {
		return delta*(7.5625*(n-=(1.5/2.75))*n + .75) + firstNum;
    } else if (n < (2.5/2.75)) {
		return delta*(7.5625*(n-=(2.25/2.75))*n + .9375) + firstNum;
    } else {
		return delta*(7.5625*(n-=(2.625/2.75))*n + .984375) + firstNum;
	}
};
  
$(document).ready(function() {

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

  var locationPath = filterPath(location.pathname);
  $('body').click(function(event) {
    var $tgt = $(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 = $(link.hash), target = link.hash;
      if ($target.length) {
        var targetOffset = $target.offset().top;

		$('html, body').animate({scrollTop: targetOffset}, 2000, 'bounceout', function() {
          location.hash = target;
        });
      }
    }

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