/**
 * Base Theme Customizer enhancements for a better user experience.
 *
 * Contains handlers to make Base Theme Customizer preview reload changes asynchronously.
 *
 * https://developer.wordpress.org/themes/customize-api/tools-for-improved-user-experience/#using-postmessage-for-improved-setting-previewing
 */

(function ($) {
	function createStyleSheet(settingName, styles) {
		var $styleElement;

		style = '<style class="' + settingName + '">';
		style += styles.reduce(function (rules, style) {
			rules += style.selectors + '{' + style.property + ':' + style.value + ';} ';
			return rules;
		}, '');
		style += '</style>';

		$styleElement = $('.' + settingName);

		if ($styleElement.length) {
			$styleElement.replaceWith(style);
		} else {
			$('head').append(style);
		}
	}

	//
	// Theme global colors
	//
	wp.customize('background_color', function (value) {
		value.bind(function (to) {
			createStyleSheet('background_color', [
				{
					property: 'background-color',
					value: to,
					selectors: '.widget-title',
				},
				{
					property: 'border-top-color',
					value: to,
					selectors: '.widget-title:after',
				}
			]);
		});
	});

	wp.customize('site_accent_color', function (value) {
		value.bind(function (to) {
			createStyleSheet('site_accent_color', [
				{
					property: 'color',
					value: to,
					selectors: '.woocommerce-MyAccount-navigation-link.is-active a',
				},
				{
					property: 'background-color',
					value: to,
					selectors:
					'.btn:hover,input[type="button"]:hover,input[type="submit"]:hover,input[type="reset"]:hover,button:hover,.button:hover,.comment-reply-link:hover,#navigation > li ul a:hover,#navigation > li ul .sfHover > a,#paging > span.current,#paging a.current,.page-numbers li > span.current,.page-numbers li a.current,#paging span.current,#paging a:hover,.page-numbers li span.current,.page-numbers li a:hover,.filters-nav .btn:hover,.filters-nav a.selected,.onsale,.woocommerce-tabs .tabs li.active a'
				},
				{
					property: 'border-color',
					value: to,
					selectors:
					'input:hover,input:focus,textarea:hover,textarea:focus,.btn,input[type="button"],input[type="submit"],input[type="reset"],button,.button,.comment-reply-link,#page'
				},
				{
					property: 'border-bottom-color',
					value: to,
					selectors:
					'#navigation > li > a:hover,#navigation > li.sfHover > a,#navigation > li.sfHover > a:active,#navigation > li.current_page_item > a,#navigation > li.current-menu-item > a,#navigation > li.current-menu-ancestor > a,#navigation > li.current-menu-parent > a,#navigation > li.current > a'
				}
			]);
		});
	});

	wp.customize('site_primary_color', function (value) {
		value.bind(function (to) {
			createStyleSheet('site_primary_color', [
				{
					property: 'color',
					value: to,
					selectors: 'a,a:hover,a:focus,.btn,input[type="button"],input[type="submit"],input[type="reset"],button,.button,.comment-reply-link,.btn.btn-inverted,input[type="button"].btn-inverted,input[type="submit"].btn-inverted,input[type="reset"].btn-inverted,button.btn-inverted,.button.btn-inverted,.comment-reply-link.btn-inverted,.btn.btn-inverted:hover,input[type="button"].btn-inverted:hover,input[type="submit"].btn-inverted:hover,input[type="reset"].btn-inverted:hover,button.btn-inverted:hover,.button.btn-inverted:hover,.comment-reply-link.btn-inverted:hover,.item-btn:hover,.item-timer .count b,.entry-title a:hover',
				},
				{
					property: 'background-color',
					value: to,
					selectors: '.sale-wrap,.price_slider .ui-slider-handle,.footer-separator',
				},
				{
					property: 'border-bottom-color',
					value: to,
					selectors: '.section-title:after'
				}
			]);
		});
	});

	wp.customize('site_secondary_color', function (value) {
		value.bind(function (to) {
			createStyleSheet('site_secondary_color', [
				{
					property: 'background-color',
					value: to,
					selectors: '.btn.btn-inverted,input[type="button"].btn-inverted,input[type="submit"].btn-inverted,input[type="reset"].btn-inverted,button.btn-inverted,.button.btn-inverted,.comment-reply-link.btn-inverted,.btn.btn-inverted:hover,input[type="button"].btn-inverted:hover,input[type="submit"].btn-inverted:hover,input[type="reset"].btn-inverted:hover,button.btn-inverted:hover,.button.btn-inverted:hover,.comment-reply-link.btn-inverted:hover,.item-btn,#paging > span,#paging a,.page-numbers li > span,.page-numbers li a',
				}
			]);
		});
	});

	wp.customize('site_text_color', function (value) {
		value.bind(function (to) {
			createStyleSheet('site_text_color', [
				{
					property: 'color',
					value: to,
					selectors: 'body,.booking-page .dk_theme_ci.dk_open .dk_toggle,.woocommerce-MyAccount-navigation-link a::after, blockquote cite,.form-allowed-tags,.comment-notes, input::placeholder',
				},
				{
					property: 'border-bottom-color',
					value: to,
					selectors: '.entry-content a:hover'
				}
			]);
		});
	});

	wp.customize('site_border_color', function (value) {
		value.bind(function (to) {
			createStyleSheet('site_border_color', [
				{
					property: 'border-color',
					value: to,
					selectors: 'input,textarea,.item-media-thumb,.item-meta,.item-timer .count,.entry-thumb,#paging > span,#paging a,.page-numbers li > span,.page-numbers li a,.widget-title,.quantity input,.entry-content table,.entry-content table caption,.entry-content table th,.entry-content table td,.entry-content img,.payment_box,.variations td'
				},
				{
					property: 'border-bottom-color',
					value: to,
					selectors: '.item-meta th,.item-meta td,.widget ul li,.shop-actions,.woocommerce-tabs .tabs,.shop_attributes th,.shop_attributes td,.shop_table th,.shop_table td,.cart_totals th,.cart_totals td,.woocommerce-checkout .woocommerce-info,.order_details li,.widget ul.product_list_widget li,.woocommerce-MyAccount-navigation-link a,.price_slider .ui-slider-range'
				},
				{
					property: 'border-top-color',
					value: to,
					selectors: '.widget-title:before,.widget ul ul.children li:first-child,.woocommerce-tabs .tabs li a,.shop_attributes tr:first-child th,.shop_attributes tr:first-child td,.woocommerce-checkout .woocommerce-info,.price_slider .ui-slider-range'
				},
				{
					property: 'border-left-color',
					value: to,
					selectors: '.item-meta th,.item-meta td'
				},
				{
					property: 'border-right-color',
					value: to,
					selectors: '.item-meta td,.woocommerce-tabs .tabs li a'
				}
			]);
		});
	});

	wp.customize('site_header_color', function (value) {
		value.bind(function (to) {
			createStyleSheet('site_header_color', [
				{
					property: 'color',
					value: to,
					selectors: 'h1, h2, h3, h4, h5, h6, .widget-title'
				}
			]);
		});
	});

	wp.customize('site_form_bg', function (value) {
		value.bind(function (to) {
			createStyleSheet('site_form_bg', [
				{
					property: 'background-color',
					value: to,
					selectors: 'input[type="text"],input[type="password"],input[type="date"],input[type="datetime"],input[type="email"],input[type="number"],input[type="search"],input[type="tel"],input[type="time"],input[type="url"],textarea, .booking-page .dk_theme_ci .dk_toggle,.booking-page .dk_theme_ci .dk_options li'
				}
			]);
		});
	});

	wp.customize('site_form_color', function (value) {
		value.bind(function (to) {
			createStyleSheet('site_form_color', [
				{
					property: 'color',
					value: to,
					selectors: 'input[type="text"],input[type="password"],input[type="date"],input[type="datetime"],input[type="email"],input[type="number"],input[type="search"],input[type="tel"],input[type="time"],input[type="url"],textarea, .booking-page .dk_theme_ci .dk_toggle, .booking-page .dk_theme_ci.dk_open .dk_toggle,.booking-page .ui-datepicker-trigger, input::placeholder'
				}
			]);
		});
	});

	wp.customize('site_form_border', function (value) {
		value.bind(function (to) {
			createStyleSheet('site_form_border', [
				{
					property: 'border-color',
					value: to,
					selectors: 'input[type="text"],input[type="password"],input[type="date"],input[type="datetime"],input[type="email"],input[type="number"],input[type="search"],input[type="tel"],input[type="time"],input[type="url"],textarea,.booking-page .dk_theme_ci .dk_toggle'
				}
			]);
		});
	});

	wp.customize('site_button_bg', function (value) {
		value.bind(function (to) {
			createStyleSheet('site_button_bg', [
				{
					property: 'background-color',
					value: to,
					selectors: '.btn,input[type="button"],input[type="submit"],input[type="reset"],button,button[type="submit"]',
				}
			]);
		});
	});

	wp.customize('site_button_bg_hover', function (value) {
		value.bind(function (to) {
			createStyleSheet('site_button_bg_hover', [
				{
					property: 'background-color',
					value: to,
					selectors: '.btn:hover,input[type="button"]:hover,input[type="submit"]:hover,input[type="reset"]:hover,button:hover,button[type="submit"]:hover',
				}
			]);
		});
	});

	wp.customize('site_button_text', function (value) {
		value.bind(function (to) {
			createStyleSheet('site_button_text', [
				{
					property: 'color',
					value: to,
					selectors: '.btn,input[type="button"],input[type="submit"],input[type="reset"],button,button[type="submit"]',
				}
			]);
		});
	});

	wp.customize('site_button_border_color', function (value) {
		value.bind(function (to) {
			createStyleSheet('site_button_border_color', [
				{
					property: 'border-color',
					value: to,
					selectors: '.btn,input[type="button"],input[type="submit"],input[type="reset"],button,.btn:hover,input[type="button"]:hover,input[type="submit"]:hover,input[type="reset"]:hover,button:hover,.btn.white:hover,input[type="button"].white:hover,input[type="submit"].white:hover,input[type="reset"].white:hover,button.white:hover',
				}
			]);
		});
	});
	
	wp.customize('header_bg', function (value) {
		value.bind(function (to) {
			createStyleSheet('header_bg', [
				{
					property: 'background-color',
					value: to,
					selectors: '#header',
				}
			]);
		});
	});

	wp.customize('header_menu_color', function (value) {
		value.bind(function (to) {
			createStyleSheet('header_menu_color', [
				{
					property: 'color',
					value: to,
					selectors: '#navigation a, .nav-secondary li a, .nav-secondary .cart-head',
				},
				{
					property: 'border-bottom-color',
					value: to,
					selectors: '#navigation > li > a:hover, #navigation > li.sfHover > a, #navigation > li.sfHover > a:active, #navigation > li.current_page_item > a, #navigation > li.current-menu-item > a, #navigation > li.current-menu-ancestor > a, #navigation > li.current-menu-parent > a, #navigation > li.current > a',
				}
			]);
		});
	});

	wp.customize('header_submenu_color', function (value) {
		value.bind(function (to) {
			createStyleSheet('header_submenu_color', [
				{
					property: 'color',
					value: to,
					selectors: '#navigation > li ul a, #navigation > li ul a:hover, #navigation > li ul .sfHover > a',
				}
			]);
		});
	});

	wp.customize('header_menu_background', function (value) {
		value.bind(function (to) {
			createStyleSheet('header_menu_background', [
				{
					property: 'background-color',
					value: to,
					selectors: '#navigation > li ul a',
				}
			]);
		});
	});

	wp.customize('header_menu_background_hover', function (value) {
		value.bind(function (to) {
			createStyleSheet('header_menu_background_hover', [
				{
					property: 'background-color',
					value: to,
					selectors: '#navigation > li ul a:hover, #navigation > li ul .sfHover > a',
				}
			]);
		});
	});

})(jQuery);
