(function($) {
	"use strict";

	jQuery(function ($) {
		if (typeof woocommerce_price_slider_params === 'undefined') {
			return false;
		}
		var apbmc_format = "%v %s";
		switch (apbd_wmc_vars.active_currency.position) {
			case 'left':
				apbmc_format = "%s%v";
				break;
			case 'left_space':
				apbmc_format = "%s %v";
				break;
			case 'right':
				apbmc_format = "%v%s";
				break;
			case 'right_space':
				apbmc_format = "%v %s";
				break;
			default:
				apbmc_format = "%s%v";
		}
		try {
			woocommerce_price_slider_params.currency_format_symbol = apbd_wmc_vars.currency_symbol;
			woocommerce_price_slider_params.currency_format = apbmc_format;

		}catch (e) {}

		$(document.body).bind('price_slider_create price_slider_slide', function (event, min, max) {

			min = apbmc_convert_price_slider(min);
			max = apbmc_convert_price_slider(max);

			$('.price_slider_amount span.from').html(accounting.formatMoney(min, {
				symbol: woocommerce_price_slider_params.currency_format_symbol,
				decimal: woocommerce_price_slider_params.currency_format_decimal_sep,
				thousand: woocommerce_price_slider_params.currency_format_thousand_sep,
				precision: woocommerce_price_slider_params.currency_format_num_decimals,
				format: woocommerce_price_slider_params.currency_format
			}));

			$('.price_slider_amount span.to').html(accounting.formatMoney(max, {
				symbol: woocommerce_price_slider_params.currency_format_symbol,
				decimal: woocommerce_price_slider_params.currency_format_decimal_sep,
				thousand: woocommerce_price_slider_params.currency_format_thousand_sep,
				precision: woocommerce_price_slider_params.currency_format_num_decimals,
				format: woocommerce_price_slider_params.currency_format
			}));

			$(document.body).trigger('price_slider_updated', [min, max]);
		});

		function price_filter_initialize() {
			$('input#min_price, input#max_price').hide();
			$('.price_slider, .price_label').show();

			var min_price = $('.price_slider_amount #min_price').data('min'),
				max_price = $('.price_slider_amount #max_price').data('max'),
				current_min_price = $('.price_slider_amount #min_price').val(),
				current_max_price = $('.price_slider_amount #max_price').val();

			$('.price_slider:not(.ui-slider)').slider({
				range: true,
				animate: true,
				min: min_price,
				max: max_price,
				values: [current_min_price, current_max_price],
				create: function () {

					$('.price_slider_amount #min_price').val(current_min_price);
					$('.price_slider_amount #max_price').val(current_max_price);

					$(document.body).trigger('price_slider_create', [current_min_price, current_max_price]);
				},
				slide: function (event, ui) {

					$('input#min_price').val(ui.values[0]);
					$('input#max_price').val(ui.values[1]);

					$(document.body).trigger('price_slider_slide', [apbmc_convert_price_slider(ui.values[0]), apbmc_convert_price_slider(ui.values[1])]);
				},
				change: function (event, ui) {

					$(document.body).trigger('price_slider_change', [ui.values[0], ui.values[1]]);
				}
			});

		}

		price_filter_initialize();

		var hasSelectiveRefresh = (
			'undefined' !== typeof wp &&
			wp.customize &&
			wp.customize.selectiveRefresh &&
			wp.customize.widgetsPreview &&
			wp.customize.widgetsPreview.WidgetPartial
		);
		if (hasSelectiveRefresh) {
			wp.customize.selectiveRefresh.bind('partial-content-rendered', function () {
				price_filter_initialize();
			});
		}
	});

	function apbmc_convert_price_slider(price) {
		var label = price;

		if (apbd_wmc_vars.active_currency.rate !== 1) {
			label = Math.ceil(label * parseFloat(apbd_wmc_vars.active_currency.rate));
		}

		//+++
		return label;
	}
})(jQuery);