/**
 * customizer.js
 *
 * Theme Customizer enhancements for a better user experience.
 *
 * Contains handlers to make Theme Customizer preview reload changes asynchronously.
 */

(function ($) {

	wp.customize('blogname', function (value) {
		value.bind(function (to) {
			$('.site-title a').text(to);
		});
	});
	wp.customize('blogdescription', function (value) {
		value.bind(function (to) {
			$('.site-description').text(to);
		});
	});
	wp.customize('header_textcolor', function (value) {
		value.bind(function (to) {
			if ('blank' === to) {
				$('.site-title a, .site-description').css({
					'clip': 'rect(1px, 1px, 1px, 1px)',
					'position': 'absolute'
				});
			} else {
				$('.site-title a, .site-description').css({
					'clip': 'auto',
					'position': 'relative'
				});
				$('.site-title a, .site-description').css({
					'color': to
				});
			}
		});
	});

	wp.customize('footer_bg_color', function (value) {
		value.bind(function (to) {
			var selectors = '#colophon, footer.site-footer, .site-info';


			var styleEl = document.getElementById('cleanlogic-footer-bg-css');
			if (!styleEl) {
				styleEl = document.createElement('style');
				styleEl.id = 'cleanlogic-footer-bg-css';
				document.head.appendChild(styleEl);
			}

			styleEl.textContent = selectors + '{background:' +to+ ';}';
		});
	});

	wp.customize('cleanlogic_contact_email', function (value) {
		value.bind(function (to) {
			var $mail = jQuery('.top_header_left a[href^="mailto:"]');
			if ($mail.length) {
				$mail.find('span').text(to || '');
				$mail.attr('href', 'mailto:' + (to || ''));
			}
		});
	});

	wp.customize('cleanlogic_contact_phone', function (value) {
		value.bind(function (to) {
			var $tel = jQuery('.top_header_left a[href^="tel:"]');
			if ($tel.length) {
				$tel.attr('href', 'tel:' + (to || ''));
			}
		});
	});

	wp.customize('cleanlogic_contact_phone_label', function (value) {
		value.bind(function (to) {
			var $tel = jQuery('.top_header_left a[href^="tel:"]');
			if ($tel.length) {
				$tel.find('span').text(to || '');
			}
		});
	});

})(jQuery);
