window.elxTrack=function (n, p){
try { if(typeof gtag==='function') gtag('event', n, p||{});} catch (e){}
try { if(typeof clarity==='function') clarity('event', n); } catch (e){}};
(function (){
'use strict';
document.addEventListener('click', function (e){
var a=e.target.closest ? e.target.closest('a'):null;
if(a&&a.href){
if(a.href.indexOf('wa.me')!==-1) window.elxTrack('whatsapp_click', { link_url: a.href });
if(a.href.indexOf('add-to-cart')!==-1) window.elxTrack('add_to_cart_click', { link_url: a.href });
}
if(e.target.closest&&e.target.closest('.opt')) window.elxTrack('pack_selected');
if(e.target.closest&&e.target.closest('#elx-chat-bubble')) window.elxTrack('chat_open');
}, { passive: true });
if(document.body.classList.contains('woocommerce-order-received')){
window.elxTrack('purchase_thankyou');
}
if('IntersectionObserver' in window){
var io=new IntersectionObserver(function (entries){
entries.forEach(function (e){
if(e.isIntersecting){ e.target.classList.add('in'); io.unobserve(e.target); }});
}, { threshold: 0.12 });
document.querySelectorAll('.reveal').forEach(function (el){ io.observe(el); });
}else{
document.querySelectorAll('.reveal').forEach(function (el){ el.classList.add('in'); });
}
var toggle=document.querySelector('.menu-toggle');
if(toggle){
toggle.addEventListener('click', function (){
var ul=document.querySelector('nav.main ul');
if(ul) ul.classList.toggle('open');
});
}
var v=document.getElementById('herovid');
var sb=document.getElementById('soundbtn');
if(v&&sb){
sb.addEventListener('click', function (){
if(v.muted){ v.muted=false; v.currentTime=0; sb.textContent='🔇 Silenciar'; }else{ v.muted=true; sb.textContent='🔊 Escuchar'; }});
}
var opts=document.querySelectorAll('.opt');
if(opts.length){
opts.forEach(function (o){
o.addEventListener('click', function (){
opts.forEach(function (x){ x.classList.remove('active'); });
o.classList.add('active');
var url=o.dataset.url;
var pnow=document.getElementById('pnow');
var pwas=document.getElementById('pwas');
var addbtn=document.getElementById('addbtn');
var stickyadd=document.getElementById('stickyadd');
var stickyprice=document.getElementById('stickyprice');
if(pnow) pnow.textContent=o.dataset.price;
if(pwas){ pwas.textContent=o.dataset.was||''; pwas.style.display=o.dataset.was ? 'inline':'none'; }
if(addbtn) addbtn.href=url;
if(stickyadd) stickyadd.href=url;
if(stickyprice) stickyprice.textContent='· ' + o.dataset.price + ' MXN';
});
});
}
window.elxSetImg=function (btn, src){
var main=document.getElementById('mainimg');
if(main) main.src=src;
document.querySelectorAll('.thumbs button').forEach(function (b){ b.classList.remove('active'); });
btn.classList.add('active');
};
var sameBilling=document.getElementById('elx-same-billing');
if(sameBilling){
var billingCard=document.getElementById('elx-billing-card');
var campos=['first_name', 'last_name', 'company', 'country', 'address_1', 'address_2', 'city', 'state', 'postcode'];
function copiarEnvioAFacturacion(){
campos.forEach(function (c){
var s=document.getElementById('shipping_' + c);
var b=document.getElementById('billing_' + c);
if(!s||!b) return;
b.value=s.value;
if(b.tagName==='SELECT'&&window.jQuery){
window.jQuery(b).val(s.value).trigger('change');
}});
}
function actualizarToggle(){
if(sameBilling.checked){
billingCard.style.display='none';
copiarEnvioAFacturacion();
}else{
billingCard.style.display='';
}}
sameBilling.addEventListener('change', actualizarToggle);
campos.forEach(function (c){
var s=document.getElementById('shipping_' + c);
if(!s) return;
s.addEventListener('input', function (){ if(sameBilling.checked) copiarEnvioAFacturacion(); });
s.addEventListener('change', function (){ if(sameBilling.checked) copiarEnvioAFacturacion(); });
});
var form=document.querySelector('form.woocommerce-checkout');
if(form){
form.addEventListener('submit', function (){ if(sameBilling.checked) copiarEnvioAFacturacion(); });
}
if(window.jQuery){
window.jQuery(document.body).on('checkout_place_order', function (){
if(sameBilling.checked) copiarEnvioAFacturacion();
return true;
});
}
actualizarToggle();
}
var sticky=document.getElementById('stickybuy');
var box=document.getElementById('buybox');
if(sticky&&box){
window.addEventListener('scroll', function (){
var r=box.getBoundingClientRect();
sticky.classList.toggle('show', r.bottom < 0);
}, { passive: true });
}})();