document.addEventListener('drproperty-currency-switch', event=> {
let formatter=new Intl.NumberFormat(document.documentElement.lang, {
style: 'currency',
currency: event.detail,
minimumFractionDigits: 0,
maximumFractionDigits: 0,
maximumSignificantDigits: 3
});
forAny('[data-price]', element=> {
let originalTHB=element.dataset.price;
let newPrice=originalTHB / drproperty_currency_data.rates['THB'] * drproperty_currency_data.rates[event.detail];
element.innerText=formatter.format(newPrice);
})
})
function drproperty_after_currency_switched(currency){
let currency_selectors=document.querySelectorAll('select.currency-selector');
currency_selectors.forEach(function (select){
let value=Number(select.value);
let index=select.selectedIndex;
let hidden_holder=select.nextElementSibling;
hidden_holder.querySelectorAll('[data-currency="' + currency + '"]').forEach(function (option){
return select.appendChild(option);
});
select.querySelectorAll(':not([data-currency="' + currency + '"]):not(:first-child)').forEach(function (option){
return hidden_holder.appendChild(option);
});
if(index&&index > 0&&(value!==''||value > 0)){
for (const option of select.querySelectorAll('[data-currency="' + currency + '"]')){
if(Number(option.value) >=value){
select.selectedIndex=option.index;
break;
}}
}else{
select.selectedIndex=0;
}});
const event=new CustomEvent('drproperty-currency-switch', {detail: currency});
document.dispatchEvent(event);
}
document.addEventListener('DOMContentLoaded', _=> {
let currency=localStorage.getItem('selected-currency');
if(!currency){
currency='THB';
}else if(currency!=='THB'){
drproperty_switch_currency(currency, false);
}
forAny('.drproperty-currency-switcher-select select', select=> select.value=currency);
onAny('.drproperty-currency-switcher-select select', 'change', (element, event)=> {
drproperty_switch_currency(element.value, false);
})
onAny('.currency-switcher', 'click', (element, event)=> {
let new_currency=element.querySelector('a').getAttribute('href');
new_currency=new_currency.replace("#", "").toUpperCase();
drproperty_switch_currency(new_currency, false);
})
});
function drproperty_switch_currency(currency, triggerPopup){
localStorage.setItem('selected-currency', currency);
document.body.setAttribute('data-currency-selected', currency);
forAny('.drproperty-currency-switcher-select select', select=> select.value=currency);
if(triggerPopup){
document.querySelector('.drproperty-popover-currency-switcher').click();
}
drproperty_after_currency_switched(currency);
return false;
};