jQuery(document).ready(function($){'use strict';function initCurrencySwitcher(){$('[data-currency-switcher]').each(function(){var $container=$(this);var $currencyOptions=$container.find('.currency-option[data-currency]');$currencyOptions.on('click',function(e){e.preventDefault();var $this=$(this);var currencyCode=$this.data('currency');if(currencyCode&&typeof togo_currency_ajax!=='undefined'){switchCurrency(currencyCode,$container);}});});}
function switchCurrency(currencyCode,$container){if(typeof togo_currency_ajax==='undefined'){console.error('Currency AJAX data not found');return;}
var $clickedOption=$container.find('.currency-option[data-currency="'+currencyCode+'"]');var exchangeRate=$clickedOption.data('exchange-rate')||1;var useManualRate=true;var thousandSeparator=$clickedOption.data('thousand-separator')||'.';var decimalSeparator=$clickedOption.data('decimal-separator')||',';var rawValue=$clickedOption.data('number-of-decimals');var numberDecimals=rawValue!==undefined&&rawValue!==null&&rawValue!==''?parseInt(rawValue,10):2;var formattingSettings={thousand_separator:thousandSeparator,decimal_separator:decimalSeparator,number_of_decimals:numberDecimals};$container.addClass('loading');$.ajax({url:togo_currency_ajax.ajax_url,type:'POST',data:{action:'togo_switch_currency',currency:currencyCode,exchange_rate:exchangeRate,thousand_separator:thousandSeparator,decimal_separator:decimalSeparator,number_of_decimals:numberDecimals,nonce:togo_currency_ajax.nonce},success:function(response){if(response.success){updateCurrencyIndicator($container,currencyCode);updatePagePrices(currencyCode,exchangeRate,useManualRate,formattingSettings);showCurrencyMessage('Currency switched to '+currencyCode,'success');updateCurrencyLabel($container,currencyCode);window.location.reload();}else{showCurrencyMessage(response.data.message||'Failed to switch currency','error');}},error:function(){showCurrencyMessage('Network error. Please try again.','error');},complete:function(){$container.removeClass('loading');}});}
function updateCurrencyIndicator($container,currencyCode){var $currencyOptions=$container.find('.currency-option[data-currency]');$currencyOptions.removeClass('current');$currencyOptions.find('.current-indicator').remove();var $selectedOption=$currencyOptions.filter('[data-currency="'+currencyCode+'"]');$selectedOption.addClass('current');$selectedOption.append('<span class="current-indicator">✓</span>');}
function updateCurrencyLabel($container,currencyCode){var $label=$container.find('.lc-button span:not(.togo-svg-icon)');var currentText=$label.text();var parts=currentText.split(' / ');if(parts.length>1){$label.text(parts[0]+' / '+currencyCode);}else{$label.text(currencyCode);}}
function updatePagePrices(currencyCode,manualRate,useManualRate,formattingSettings){$('[data-price]').each(function(){var $this=$(this);var originalPrice=parseFloat($this.data('price'));var baseCurrency=$this.data('base-currency')||'USD';if(originalPrice&&baseCurrency!==currencyCode){var rate=1;if(useManualRate&&manualRate>0){rate=manualRate;var convertedPrice=originalPrice*rate;var formattedPrice=formatPrice(convertedPrice,currencyCode,formattingSettings);$this.text(formattedPrice);}else{getExchangeRate(baseCurrency,currencyCode,function(rate){if(rate>0){var convertedPrice=originalPrice*rate;var formattedPrice=formatPrice(convertedPrice,currencyCode,formattingSettings);$this.text(formattedPrice);}});}}});if(typeof wc_add_to_cart_params!=='undefined'){updateWooCommercePrices(currencyCode,manualRate,useManualRate,formattingSettings);}}
function getExchangeRate(fromCurrency,toCurrency,callback){var fallbackRates={'USD':{'EUR':0.92,'GBP':0.79,'JPY':150.0,'AUD':1.52,'CAD':1.35,'CHF':0.88,'CNY':7.20,'VND':24000.0,'INR':83.0,'KRW':1300.0,'SGD':1.35,'THB':35.0,'MYR':4.70,'IDR':15500.0,'PHP':55.0,'BRL':5.0,'MXN':17.0,'RUB':90.0,'ZAR':18.0,'TRY':30.0},'EUR':{'USD':1.09,'GBP':0.86,'JPY':163.0,'AUD':1.65,'CAD':1.47,'CHF':0.96,'CNY':7.85,'VND':26200.0,'INR':90.5,'KRW':1417.0,'SGD':1.47,'THB':38.2,'MYR':5.13,'IDR':16900.0,'PHP':60.0,'BRL':5.45,'MXN':18.5,'RUB':98.0,'ZAR':19.6,'TRY':32.7},'GBP':{'USD':1.27,'EUR':1.16,'JPY':189.0,'AUD':1.92,'CAD':1.71,'CHF':1.12,'CNY':9.14,'VND':30400.0,'INR':105.0,'KRW':1647.0,'SGD':1.71,'THB':44.4,'MYR':5.96,'IDR':19600.0,'PHP':69.7,'BRL':6.35,'MXN':21.6,'RUB':114.0,'ZAR':22.8,'TRY':38.0}};var rate=1;if(fallbackRates[fromCurrency]&&fallbackRates[fromCurrency][toCurrency]){rate=fallbackRates[fromCurrency][toCurrency];}else if(fallbackRates[toCurrency]&&fallbackRates[toCurrency][fromCurrency]){rate=1/fallbackRates[toCurrency][fromCurrency];}
callback(rate);}
function formatPrice(price,currencyCode,formattingSettings){var symbol=currencyCode;if(typeof togo_currency_ajax!=='undefined'&&togo_currency_ajax.available_currencies){if(typeof wc_add_to_cart_params!=='undefined'&&wc_add_to_cart_params.currency_symbol){symbol=wc_add_to_cart_params.currency_symbol;}else{symbol=currencyCode;}}else{var symbols={'USD':'$','EUR':'€','GBP':'£','JPY':'¥','VND':'₫','AUD':'A$','CAD':'C$','CHF':'CHF','CNY':'¥','INR':'₹','KRW':'₩','SGD':'S$','THB':'฿'};symbol=symbols[currencyCode]||currencyCode;}
var thousandSeparator='.';var decimalSeparator=',';var numberOfDecimals=2;if(formattingSettings){thousandSeparator=formattingSettings.thousand_separator||'.';decimalSeparator=formattingSettings.decimal_separator||',';numberOfDecimals=parseInt(formattingSettings.number_of_decimals)||2;}
var formattedNumber=numberFormat(price,numberOfDecimals,decimalSeparator,thousandSeparator);return symbol+formattedNumber;}
function numberFormat(number,decimals,decimalSeparator,thousandSeparator){var parts=number.toFixed(decimals).split('.');parts[0]=parts[0].replace(/\B(?=(\d{3})+(?!\d))/g,thousandSeparator);return parts.join(decimalSeparator);}
function updateWooCommercePrices(currencyCode,manualRate,useManualRate,formattingSettings){$('.woocommerce-Price-amount').each(function(){var $this=$(this);var priceText=$this.text();var price=parseFloat(priceText.replace(/[^\d.-]/g,''));if(price){if(useManualRate&&manualRate>0){var convertedPrice=price*manualRate;var formattedPrice=formatPrice(convertedPrice,currencyCode,formattingSettings);$this.text(formattedPrice);}else{getExchangeRate('USD',currencyCode,function(rate){if(rate>0){var convertedPrice=price*rate;var formattedPrice=formatPrice(convertedPrice,currencyCode,formattingSettings);$this.text(formattedPrice);}});}}});}
function showCurrencyMessage(message,type){console.log(type+': '+message);if(type==='error'){alert('Error: '+message);}}
initCurrencySwitcher();$(document).on('elementor/popup/show',function(){initCurrencySwitcher();});});