jQuery(function() {
    $('a[href*=#]').click(function (e) {
        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') &&
            location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
            if ($target.length) {
                var targetOffsetY = $target.offset().top;
                if (targetOffsetY<0) {targetOffsetY=1;}
                e.preventDefault();
                $('html,body').stop().animate({ scrollTop: targetOffsetY }, 400);
            }
        }
    });
});