منو
۰
/* BELSINO UI v4 — lightweight interactions only */
(function(){
'use strict';
var doc = document;
var body = doc.body;
function qs(sel, root){ return (root || doc).querySelector(sel); }
function qsa(sel, root){ return Array.prototype.slice.call((root || doc).querySelectorAll(sel)); }
/* Header + offcanvas menu */
var header = qs('[data-bxh2]');
var menu = qs('[data-bxh2-menu]');
var openBtn = qs('[data-bxh2-open]');
var closeBtns = qsa('[data-bxh2-close]');
var lastFocus = null;
function setHeaderState(){
if(!header) return;
header.classList.toggle('stuck', window.scrollY > 24);
}
setHeaderState();
window.addEventListener('scroll', setHeaderState, {passive:true});
function openMenu(){
if(!menu) return;
lastFocus = doc.activeElement;
menu.classList.add('open');
body.classList.add('bxm2-lock');
if(openBtn) openBtn.setAttribute('aria-expanded','true');
var panel = qs('.bxm2-pn', menu);
if(panel) panel.setAttribute('aria-hidden','false');
var first = qs('a,button,summary,[tabindex]:not([tabindex="-1"])', menu);
if(first) setTimeout(function(){ first.focus(); }, 40);
}
function closeMenu(){
if(!menu) return;
menu.classList.remove('open');
body.classList.remove('bxm2-lock');
if(openBtn) openBtn.setAttribute('aria-expanded','false');
var panel = qs('.bxm2-pn', menu);
if(panel) panel.setAttribute('aria-hidden','true');
if(lastFocus && typeof lastFocus.focus === 'function') lastFocus.focus();
}
if(openBtn){
openBtn.setAttribute('aria-haspopup','dialog');
openBtn.setAttribute('aria-expanded','false');
openBtn.addEventListener('click', function(e){ e.preventDefault(); openMenu(); });
}
closeBtns.forEach(function(btn){ btn.addEventListener('click', function(e){ e.preventDefault(); closeMenu(); }); });
doc.addEventListener('keydown', function(e){
if(e.key === 'Escape') closeMenu();
if(e.key === 'Tab' && menu && menu.classList.contains('open')){
var focusables = qsa('a,button,summary,input,select,textarea,[tabindex]:not([tabindex="-1"])', menu).filter(function(el){ return !el.disabled && el.offsetParent !== null; });
if(!focusables.length) return;
var first = focusables[0];
var last = focusables[focusables.length - 1];
if(e.shiftKey && doc.activeElement === first){ e.preventDefault(); last.focus(); }
else if(!e.shiftKey && doc.activeElement === last){ e.preventDefault(); first.focus(); }
}
});
/* Native details accordion: all closed by default, including Adult Bedroom */
qsa('.bxm2-cat').forEach(function(detail){
detail.removeAttribute('open');
var summary = qs('summary', detail);
if(summary){
summary.setAttribute('role','button');
summary.setAttribute('aria-expanded','false');
summary.addEventListener('click', function(){
setTimeout(function(){ summary.setAttribute('aria-expanded', detail.open ? 'true' : 'false'); }, 0);
});
}
});
/* Hotspot click support */
qsa('.bvs-hotspot,.belsino-hotspot,.belsino-room-point').forEach(function(spot){
var trigger = qs('button,a,[role="button"]', spot);
if(!trigger) return;
trigger.addEventListener('click', function(e){
if(trigger.tagName !== 'A') e.preventDefault();
qsa('.bvs-hotspot.is-open,.belsino-hotspot.is-open,.belsino-room-point.is-open').forEach(function(item){ if(item !== spot) item.classList.remove('is-open'); });
spot.classList.toggle('is-open');
});
});
doc.addEventListener('click', function(e){
if(e.target.closest('.bvs-hotspot,.belsino-hotspot,.belsino-room-point')) return;
qsa('.bvs-hotspot.is-open,.belsino-hotspot.is-open,.belsino-room-point.is-open').forEach(function(item){ item.classList.remove('is-open'); });
});
/* WoodMart gallery safety fallback: keep one main slide visible, thumbnails clickable */
function initGalleryFallback(root){
var gallery = root || qs('body.single-product .woocommerce-product-gallery');
if(!gallery || gallery.dataset.buiReady === '1') return;
var mainItems = qsa('.wd-gallery-images .wd-carousel-item', gallery);
var thumbItems = qsa('.wd-gallery-thumb .wd-carousel-item', gallery);
if(!mainItems.length) return;
gallery.dataset.buiReady = '1';
gallery.classList.add('bui-gallery-fallback');
function show(index){
if(index = mainItems.length) index = 0;
mainItems.forEach(function(item, i){
item.classList.toggle('bui-active-slide', i === index);
item.setAttribute('aria-hidden', i === index ? 'false' : 'true');
});
thumbItems.forEach(function(item, i){
item.classList.toggle('bui-active-thumb', i === index);
item.setAttribute('aria-current', i === index ? 'true' : 'false');
});
gallery.dataset.buiIndex = String(index);
}
show(0);
thumbItems.forEach(function(item, index){
item.addEventListener('click', function(e){
e.preventDefault();
e.stopPropagation();
show(index);
});
});
var nextSelectors = '.wd-btn-arrow.wd-next,.wd-arrow-next,.owl-next,.swiper-button-next';
var prevSelectors = '.wd-btn-arrow.wd-prev,.wd-arrow-prev,.owl-prev,.swiper-button-prev';
gallery.addEventListener('click', function(e){
var current = parseInt(gallery.dataset.buiIndex || '0', 10);
if(e.target.closest(nextSelectors)){ e.preventDefault(); show((current + 1) % mainItems.length); }
if(e.target.closest(prevSelectors)){ e.preventDefault(); show((current - 1 + mainItems.length) % mainItems.length); }
});
}
if(body.classList.contains('single-product')){
if(doc.readyState === 'loading') doc.addEventListener('DOMContentLoaded', function(){ initGalleryFallback(); });
else initGalleryFallback();
/* Re-run after variation/gallery ajax updates without touching WooCommerce logic */
var observerTarget = qs('body.single-product .product-images');
if(observerTarget && 'MutationObserver' in window){
var obs = new MutationObserver(function(){
var gallery = qs('body.single-product .woocommerce-product-gallery');
if(gallery && gallery.dataset.buiReady !== '1') initGalleryFallback(gallery);
});
obs.observe(observerTarget, {childList:true, subtree:true});
}
}
})();