var navOffset = function () {
  $('#content.container').css('margin-top', $('#nav').height() * 0.25);
};

var collapsedMenus = function() {
	$('#nav button.navbar-toggle').on('click', function() {
		if ($(this).hasClass('collapsed')) {
      var collapsedMenuHeight = 323.5;
      collapseOffset('#nav', collapsedMenuHeight);
    }
  });

  $('#nav #side-menu > a').on('click', function () {
    if ($(this).parent().not('.open')) {
      var collapsedSideMenuHeight;
      if ($('#nav .navbar-toggle').is(':visible')) {
        collapsedSideMenuHeight = 133 + 323.5;
      } else {
        collapsedSideMenuHeight = 133;
      }
      collapseOffset('#nav #side-menu > a', collapsedSideMenuHeight);
		}
	});
};

var collapseOffset = function(selector, size) {
  var $window = $(window);
  var windowHeight = $window.height();
  if ($window.scrollTop() < size) {
		var dest = (size < windowHeight) ? size : windowHeight;
		$('html, body').animate({
			scrollTop: dest
		}, 500, 'swing');
	}
};

navOffset();
collapsedMenus();

var navOffset = function () {
  $('#content.container').css('margin-top', $('#nav').height() * 0.25);
};

var collapsedMenus = function() {
	$('#nav button.navbar-toggle').on('click', function() {
		if ($(this).hasClass('collapsed') &&
		    !$($(this).data('target')).hasClass('collapsing')) {
      var collapsedMenuHeight = 323.5;
      collapseOffset('#nav', collapsedMenuHeight);
    }
  });

  $('#nav #side-menu > a').on('click', function () {
    if (!$(this).parent().hasClass('open')) {
      var collapsedSideMenuHeight;
      if ($('#nav .navbar-toggle').is(':visible')) {
        collapsedSideMenuHeight = 133 + 323.5;
      } else {
        collapsedSideMenuHeight = 133;
      }
      collapseOffset('#nav #side-menu > a', collapsedSideMenuHeight);
		}
	});
};

var collapseOffset = function(selector, size) {
  var $window = $(window);
  var windowHeight = $window.height();
  if ($window.scrollTop() < size) {
		var dest = (size < windowHeight) ? size : windowHeight;
		$('html, body').animate({
			scrollTop: dest
		}, 500, 'swing');
	}
};

navOffset();
collapsedMenus();

Fix wrong auto-scrolling when opening the menu.
Fix wrong auto-scrolling when opening the menu.

Fixed page scrolling down when main menu is closing.

Fixed incorrect logic for scrolling on submenu ("More") scrolling.
JQuery.not() is a filter/selector, not a class detector.
