var usingStickyNav = false;
var stickybar = document.getElementById("stickybar");
if (stickybar != null) {
  usingStickyNav = true;
}
var boundarybox = stickybar.getBoundingClientRect();
var sticky = boundarybox.top;
var contentHolder = document.getElementById("featurecontent");
var hamburgerButton = document.getElementById("hamburgernav");
var hamburgerMenu = document.getElementById("hamburgermenu");
var menuIsVisible = false;

function stickyNav() {
  if (window.pageYOffset >= sticky) {
    stickybar.classList.add("sticky");
    contentHolder.classList.add("isSticky");
    $('#hamburgermenu').addClass('hidden').removeClass('at-top').attr('data-visible','false');
  } else {
    stickybar.classList.remove("sticky");
    contentHolder.classList.remove("isSticky");
    $('#hamburgermenu').addClass('hidden').addClass('at-top').attr('data-visible','false');
  }
}

$('.collection-nav-item').click(function() {
  let newSectionName = $(this).attr('data-id');
  jumpToSection(newSectionName);
});
$('h2.collection-item-title').click(function() {
  let newSectionName = $(this).attr('data-id');
  jumpToSection(newSectionName);
});
$('#hamburgernav').click(function() {
  if ($('#hamburgermenu').attr('data-visible') == "true") {$('#hamburgermenu').addClass('hidden').attr('data-visible','false');} else {
    $('#hamburgermenu').removeClass('hidden').attr('data-visible','true');
    $('#hamburgermenu').find('.collection-nav-item')[0].focus();
  }
});

window.onscroll = function() {
  if (usingStickyNav) {stickyNav();}
};

function jumpToSection(newSectionName) {
  $('#hamburgermenu').addClass('hidden').attr('data-visible','false');
  window.history.replaceState(null, null, (featureURL + newSectionName));
  let newSectionOffset = $('#'+newSectionName).offset();
  window.scrollTo(newSectionOffset);
  if (usingStickyNav) {stickyNav();}
  console.log(newSectionOffset);
  $('.collection-item-title[data-id="'+newSectionName+'"]').focus();
}