(function ($) {
$(function () {
// Hamburger menu toggle (works on all breakpoints) per il menu ad ancore.
var $nav = $('.singlepage-nav');
var $toggle = $('.singlepage-menu-toggle');
function updateFixedHeaderHeight() {
if (!$('body').hasClass('has-tents-fixed-header')) {
document.documentElement.style.removeProperty('--tents-fixed-header-height');
return;
}
var $fixedNavigation = $('.navigation-top');
var height = $fixedNavigation.length ? $fixedNavigation.outerHeight() : 0;
document.documentElement.style.setProperty('--tents-fixed-header-height', height + 'px');
}
updateFixedHeaderHeight();
$(window).on('load resize orientationchange', updateFixedHeaderHeight);
if ($('body').hasClass('has-tents-fixed-header')) {
$('.navigation-top').addClass('site-navigation-fixed');
}
$toggle.on('click', function () {
var expanded = $(this).attr('aria-expanded') === 'true';
$(this).attr('aria-expanded', expanded ? 'false' : 'true');
$nav.toggleClass('is-open');
updateFixedHeaderHeight();
});
function getSectionHashFromHref(href) {
if (!href) {
return '';
}
var hashIndex = href.indexOf('#');
if (hashIndex === -1) {
return '';
}
var hash = href.slice(hashIndex);
return hash.indexOf('#section-') === 0 ? hash : '';
}
function scrollToSection(hash, animate) {
var $target = $(hash);
if (!$target.length) {
return false;
}
updateFixedHeaderHeight();
var navHeight = $('.navigation-top').outerHeight() || 0;
var offset = navHeight + 20;
var targetTop = $target.offset().top - offset;
var $scrollEl = $('html, body');
if (animate) {
$scrollEl.animate({ scrollTop: targetTop }, 500);
} else {
$scrollEl.scrollTop(targetTop);
}
return true;
}
// Primo livello: sempre ancore sezione in homepage (href assoluto o relativo #section-*).
$('#top-menu > li > a').on('click', function (e) {
var sectionHash = getSectionHashFromHref(this.getAttribute('href'));
if (!sectionHash) {
return;
}
// Su pagine interne: lascia navigare a /#section-* (scroll dopo il load).
if (!$('body').hasClass('home')) {
return;
}
if (scrollToSection(sectionHash, true)) {
e.preventDefault();
if (window.history && window.history.replaceState) {
window.history.replaceState(null, '', sectionHash);
}
}
});
// Arrivo dalla home con hash (es. da una pagina interna) o refresh con #section-*.
function scrollToSectionHashOnLoad() {
var sectionHash = getSectionHashFromHref(window.location.hash);
if (!sectionHash || !$('body').hasClass('home')) {
return;
}
window.setTimeout(function () {
scrollToSection(sectionHash, false);
}, 0);
}
scrollToSectionHashOnLoad();
$(window).on('load', scrollToSectionHashOnLoad);
});
})(jQuery);