        document.addEventListener('DOMContentLoaded', function (){
const switcher=document.querySelector('.currency-switcher');
if(!switcher) return;
const toggleBtn=switcher.querySelector('.currency-toggle');
const labelSpan=toggleBtn ? toggleBtn.querySelector('.currency-label'):null;
const iconSpan=toggleBtn ? toggleBtn.querySelector('.fa-solid, i'):null;
const buttons=Array.from(switcher.querySelectorAll('.currency-dropdown button'));
if(!buttons.length) return;
const setCookie=(name, value, days=365)=> {
const d=new Date();
d.setTime(d.getTime() + (days*24*60*60*1000));
document.cookie=`${name}=${value};path=/;expires=${d.toUTCString()};SameSite=Lax`;
};
const getCookie=(name)=> {
const match=document.cookie.match(new RegExp('(^|)' + name + '=([^;]+)'));
return match ? match[2]:null;
};
const lang=document.documentElement.lang ? document.documentElement.lang.substring(0,2):'fr';
const params=new URLSearchParams(window.location.search);
const paramCurrency=params.get('currency');
let savedCurrency=null;
if(lang==='en'){
savedCurrency='EUR';
}else if(lang==='fr'){
savedCurrency=getCookie('irent_currency_fr')||paramCurrency||(window.IrentCurrency&&window.IrentCurrency.current)||'MAD';
}else if(lang==='ar'){
savedCurrency=getCookie('irent_currency_ar')||paramCurrency||(window.IrentCurrency&&window.IrentCurrency.current)||'MAD';
}else{
savedCurrency=paramCurrency||(window.IrentCurrency&&window.IrentCurrency.current)||'MAD';
}
const updateCurrencyDisplay=(currency)=> {
if(!currency) return;
if(labelSpan) labelSpan.textContent=currency;
const icon=switcher.querySelector(`button[data-currency="${currency}"] .fa-solid`);
if(icon&&iconSpan) iconSpan.className=icon.className;
buttons.forEach(btn=> {
const isActive=btn.getAttribute('data-currency')===currency;
btn.classList.toggle('active', isActive);
btn.classList.toggle('active-currency', isActive);
btn.style.display=isActive ? 'none':'';
});
if(toggleBtn) toggleBtn.classList.add('active');
};
updateCurrencyDisplay(savedCurrency);
if(toggleBtn){
toggleBtn.addEventListener('click', function(e){
e.preventDefault();
switcher.classList.toggle('open');
});
document.addEventListener('click', function(e){
if(!switcher.contains(e.target)){
switcher.classList.remove('open');
}});
}
buttons.forEach(btn=> {
btn.addEventListener('click', function(e){
e.preventDefault();
let currency=btn.getAttribute('data-currency');
if(lang==='en') currency='EUR';
if(lang==='fr') setCookie('irent_currency_fr', currency);
if(lang==='ar') setCookie('irent_currency_ar', currency);
updateCurrencyDisplay(currency);
document.body.style.opacity='0.5';
document.body.style.pointerEvents='none';
const url=new URL(window.location.href);
url.searchParams.set('currency', currency);
window.location.replace(url.toString());
});
});
});