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

(function ($) {

	const rise_magazine_section_lists = {
		hero_banner: 'rise_magazine_header_content_section',
		tabs: 'rise_magazine_tab_section',
		breaking_news: 'rise_magazine_breaking_news_section',
		featured_posts: 'rise_magazine_magazine_featured_posts',
		top_news: 'rise_magazine_top_news-section',
		popular_posts: 'rise_magazine_magazine_popular_posts',
		latest_posts: 'rise_magazine_latest_posts_section',
		recent_posts: 'rise_magazine_most_recent_posts',
		editor_choice: 'rise_magazine_posts_slider_section',
		featured_slider: 'rise_magazine_featured_slider_section',
		latest_products: 'rise_magazine_latest_products',
		about_us: 'rise_magazine_about_us',
		popular_products: 'rise_magazine_popular_products',
		products_collection: 'rise_magazine_products_collection',
		recent_products: 'rise_magazine_recent_products',
		featured_products: 'rise_magazine_featured_products',
		counter: 'rise_magazine_counter_section',
		our_services: 'rise_magazine_our_services',
		gallery: 'rise_magazine_gallery_slider_section',
		team: 'rise_magazine_our_team',
		testimonial: 'rise_magazine_testimonial_section',
		subscribe: 'rise_magazine_subscribe',
		our_partners: 'rise_magazine_our_partners',
		related_posts: 'rise_magazine_related_posts_section'
	};

	Object.keys(rise_magazine_section_lists).forEach(rise_magazine_homepage_scroll_preview);

	function rise_magazine_homepage_scroll_preview(item, index) {
		// Collect information from customize-controls.js about which panels are opening.
		wp.customize.bind('preview-ready', function () {

			// Initially hide the theme option placeholders on load.
			$('.panel-placeholder').hide();
			wp.customize.preview.bind(item, function (data) {
				// Only on the front page.
				if (!$('body').hasClass('home')) {
					return;
				}

				// When the section is expanded, show and scroll to the content placeholders, exposing the edit links.
				if (true === data.expanded) {
					var scroll = $('#' + rise_magazine_section_lists[item]);
					if (scroll.length) {
						$('html, body').animate({
							'scrollTop': scroll.position().top
						});
					}
				}
			});

		});
	}

	// Site title and description.
	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);
		});
	});

	// Header text color.
	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'
				});
			}
		});
	});

	// Header title color.
	wp.customize('rise_magazine_theme_options[header_title_color]', function (value) {
		value.bind(function (to) {
			if ('blank' === to) {
				$('.site-title a').css({
					'clip': 'rect(1px, 1px, 1px, 1px)',
					'position': 'absolute'
				});
			} else {
				$('.site-title a').css({
					'clip': 'auto',
					'position': 'relative'
				});
				$('.site-title a').css({
					'color': to
				});
			}
		});
	});

	// Header tagline color.
	wp.customize('rise_magazine_theme_options[header_tagline_color]', function (value) {
		value.bind(function (to) {
			if ('blank' === to) {
				$('.site-description').css({
					'clip': 'rect(1px, 1px, 1px, 1px)',
					'position': 'absolute'
				});
			} else {
				$('.site-description').css({
					'clip': 'auto',
					'position': 'relative'
				});
				$('.site-description').css({
					'color': to
				});
			}
		});
	});

	// Color scheme.
	wp.customize('rise_magazine_theme_options[colorscheme]', function (value) {
		value.bind(function (to) {

			// Update color body class.
			$('body')
				.removeClass('colors-light colors-dark colors-custom')
				.addClass('colors-' + to);
		});
	});

	// Custom color hue.
	wp.customize('rise_magazine_theme_options[colorscheme_hue]', function (value) {
		value.bind(function (to) {

			// Update custom color CSS
			var style = $('#custom-theme-colors'),
				color = style.data('color'),
				css = style.html();
			css = css.split(color).join(to);
			style.html(css)
				.data('color', to);
		});
	});

	//custom field real time refresh

	wp.customize('rise_magazine_theme_options[topbar_bg_color]', function (value) {
		value.bind(function (to) {
			$('#top-navigation').css('background-color', to);
		});
	});

	wp.customize('rise_magazine_theme_options[topbar_social_icon_color]', function (value) {
		value.bind(function (to) {
			$('#top-navigation .social-icons li a svg').css('fill', to);
		});
	});

	wp.customize('rise_magazine_theme_options[topbar_menu_item_color]', function (value) {
		value.bind(function (to) {
			$('#top-navigation .main-navigation ul.nav-menu > li > a, #top-navigation .main-navigation ul.nav-menu > li.current-menu-item > a').css('color', to);
		});
	});

	wp.customize('rise_magazine_theme_options[topbar_seperator_color]', function (value) {
		value.bind(function (to) {
			$('#top-navigation').css('border-bottom', '1px solid' + to);
		});
	});

	wp.customize('rise_magazine_theme_options[menu_bg_color]', function (value) {
		value.bind(function (to) {
			$('#masthead').css('background-color', to);
		});
	});

	wp.customize('rise_magazine_theme_options[menu_item_color]', function (value) {
		value.bind(function (to) {
			$('.main-navigation ul#primary-menu li.current-menu-item > a, .main-navigation ul.nav-menu > li > a').css('color', to);
		});
	});

	wp.customize('rise_magazine_theme_options[gallery_section_bg_color]', function (value) {
		value.bind(function (to) {
			$('#blogendar_gallery_slider_section').css('background-color', to);
		});
	});

	wp.customize('rise_magazine_theme_options[footer_bg_color]', function (value) {
		value.bind(function (to) {
			$('#colophon').css('background-color', to);
		});
	});

	wp.customize('rise_magazine_theme_options[footer_social_icon_color]', function (value) {
		value.bind(function (to) {
			$('#colophon .social-icons li a svg').css('fill', to);
		});
	});

	wp.customize('rise_magazine_theme_options[footer_widgets_hr_color]', function (value) {
		value.bind(function (to) {
			$('#colophon .footer-logo-wrapper').css('border-bottom', '1px solid' + to);
		});
	});

	wp.customize('rise_magazine_theme_options[footer_widgets_title_color]', function (value) {
		value.bind(function (to) {
			$('#colophon .widget-title').css('color', to);
		});
	});

	wp.customize('rise_magazine_theme_options[footer_widgets_content_color]', function (value) {
		value.bind(function (to) {
			$('#colophon a, #colophon p, #colophon li').css('color', to);
		});
	});

	wp.customize('rise_magazine_theme_options[footer_hr_color]', function (value) {
		value.bind(function (to) {
			$('#colophon .site-info').css('border-top', '1px solid' + to);
		});
	});

	wp.customize('rise_magazine_theme_options[copyright_text_color]', function (value) {
		value.bind(function (to) {
			$('#colophon .site-info span').css('color', to);
		});
	});

	wp.customize('rise_magazine_theme_options[back_to_top_color]', function (value) {
		value.bind(function (to) {
			$('.backtotop').css('background-color', to);
		});
	});

	wp.customize('rise_magazine_theme_options[banner_image_overlay_color]', function (value) {
		value.bind(function (to) {
			$('#page-site-header .overlay').css('background-color', to);
		});
	});

	wp.customize('rise_magazine_theme_options[banner_bg_color]', function (value) {
		value.bind(function (to) {
			$('#page-site-header').css('background-color', to);
		});
	});

	wp.customize('rise_magazine_theme_options[breadcrumb_title_color]', function (value) {
		value.bind(function (to) {
			$('#page-site-header .page-title, #breadcrumb-list .trail-items li').css('color', to);
		});
	});

	wp.customize('rise_magazine_theme_options[breadcrumb_path_color]', function (value) {
		value.bind(function (to) {
			$('#breadcrumb-list .trail-items li a').css('color', to);
		});
	});

	wp.customize('rise_magazine_theme_options[breadcrumb_alignment]', function (value) {
		value.bind(function (to) {
			$('#page-site-header .wrapper').css('text-align', to);
		});
	});

})(jQuery);
