(function(){
// var nav = document.getElementById('main-nav');
var mq = window.matchMedia('(max-width:600px)');
/* ── hamburger toggle ── */
// btn.addEventListener('click', function(){
/* (btn).addEventListener('click', function(){
var open = nav.classList.toggle('nav-open');
btn.classList.toggle('open', open);
btn.setAttribute('aria-expanded', String(open));
btn.setAttribute('aria-label', open ? 'Close navigation' : 'Open navigation');
}); */
$( ".hamburger" ).each(function( index ) {
// console.log( index + ": " + $( this ).text() );
var nav;
$( this ).click(function () {
var navid= $(this).attr("data-navid") ;
var id= $(this).attr("id") ;
console.log(navid) ;
var btn = $("#"+id);
nav = $("#"+navid);
var open = nav.toggleClass('nav-open');
btn.toggleClass('open', open);
// btn.classList.toggle('open', open);
btn.attr('aria-expanded', String(open));
btn.attr('aria-label', open ? 'Close navigation' : 'Open navigation');
});
/* ── mobile: tap su .nav-link ▾ (livello 1) → espande dropdown ── */
document.querySelectorAll('.nav-item > .nav-link').forEach(function(link){
var item = link.parentElement;
var dd = item.querySelector(':scope > .dropdown');
if(!dd) return;
link.addEventListener('click', function(e){
if(!mq.matches) return;
e.preventDefault();
item.classList.toggle('expanded');
});
});
/* ── mobile: tap su .sub-trigger (livello 2) → espande submenu ── */
document.querySelectorAll('.nav-item--sub > .sub-trigger').forEach(function(trigger){
var subItem = trigger.parentElement;
var subDd = subItem.querySelector(':scope > .dropdown--sub');
if(!subDd) return;
trigger.addEventListener('click', function(e){
if(!mq.matches) return;
e.preventDefault();
subItem.classList.toggle('expanded');
});
});
/* ── chiudi nav quando si clicca un link foglia ── */
document.querySelectorAll('.dropdown > a, .dropdown--sub > a').forEach(function(a){
a.addEventListener('click', function(){
if(mq.matches){
nav.classList.remove('nav-open');
btn.classList.remove('open');
btn.setAttribute('aria-expanded','false');
}
});
});
/* ── reset su resize ── */
mq.addEventListener('change', function(e){
if(!e.matches){
nav.classList.remove('nav-open');
btn.classList.remove('open');
document.querySelectorAll('.nav-item, .nav-item--sub').forEach(function(i){
i.classList.remove('expanded');
});
}
});
});
})();
/* ── ACCORDION LEVEL 1 — independent, all can be open ── */
function toggleAcc(id) {
var item = document.getElementById(id);
var body = item.querySelector(':scope > .acc-body');
var btn = item.querySelector(':scope > .acc-trigger');
var isOpen = item.classList.contains('open');
if (isOpen) {
item.classList.remove('open');
body.style.maxHeight = null;
btn.setAttribute('aria-expanded', 'false');
} else {
item.classList.add('open');
body.style.maxHeight = body.scrollHeight + 2000 + 'px';
btn.setAttribute('aria-expanded', 'true');
}
}
/* ── ACCORDION LEVEL 2 — independent, all can be open ── */
function toggleSub(id, e) {
if (e) e.stopPropagation();
var item = document.getElementById(id);
var body = item.querySelector(':scope > .sub-body');
var btn = item.querySelector(':scope > .sub-trigger');
var isOpen = item.classList.contains('open');
if (isOpen) {
item.classList.remove('open');
body.style.maxHeight = null;
btn.setAttribute('aria-expanded', 'false');
} else {
item.classList.add('open');
body.style.maxHeight = body.scrollHeight + 500 + 'px';
btn.setAttribute('aria-expanded', 'true');
/* expand parent acc-body to fit */
var parentBody = item.closest('.acc-body');
if (parentBody) {
parentBody.style.maxHeight = parseInt(parentBody.style.maxHeight || 0) + body.scrollHeight + 500 + 'px';
}
}
}
function selectLang(el) {
var val = el.dataset.value;
var img = el.querySelector('img');
var text = el.textContent.trim();
document.getElementById('gf-lang').value = val;
document.getElementById('lang-placeholder').outerHTML =
'' +
'' + text + '';
document.querySelectorAll('.lang-option').forEach(function (o) {
o.classList.remove('selected');
});
el.classList.add('selected');
document.getElementById('lang-select').classList.remove('open');
}
/* ── custom language selector ── */
function toggleLangDropdown() {
document.getElementById('lang-select').classList.toggle('open');
}
/* ── AUTO-RENDER data-img images on triggers ── */
document.addEventListener('DOMContentLoaded', function(){
/* Level 1 triggers */
document.querySelectorAll('.acc-trigger[data-img]').forEach(function(btn){
var url = btn.getAttribute('data-img');
var emoji = btn.getAttribute('data-emoji') || '📁';
var left = btn.querySelector('.acc-trigger-left');
if (!left) return;
var el;
if (url) {
el = document.createElement('img');
el.src = url; el.alt = ''; el.className = 'acc-img';
el.onerror = function(){
this.outerHTML = '