; (function ($) {
	'use strict'

	/**
	 * JavaScript code for admin dashboard.
	 *
	 */
	$(function () {

		/* License activation */
		$('.spsp-license input.btn-license-save-activate').on('click', function () {
			$('.spsp-license input.btn-license-activate').trigger('click')
		});

		/* Preloader */
		$("#sp_pcp_view_options .spf-metabox").css("visibility", "hidden");

		// // Hide meta icon field custom dependency.
		// $('.pcp_post_meta .pcp_custom_group_design .spf-cloneable-wrapper .spf-cloneable-item').find('.spf-cloneable-content .select_meta_icon').show();
		// $('.pcp_post_meta .pcp_custom_group_design .spf-cloneable-wrapper .spf-cloneable-item').each(function () {
		//   if ($(this).find('.spf-cloneable-content .select_post_meta select').val() == 'taxonomy') {
		//     if ($(this).find('.spf-cloneable-content .pcp_meta_position .spf--button.spf--active input').val() == 'above_title' || $(this).find('.spf-cloneable-content .pcp_meta_position .spf--button.spf--active input').val() == 'over_thumb') {
		//       $(this).find('.spf-cloneable-content .select_meta_icon').hide();
		//     }else{
		//       $(this).find('.spf-cloneable-content .select_meta_icon').show();
		//     }
		//   }
		// })
		// $(document).on('change', '.pcp_post_meta .pcp_custom_group_design .spf-cloneable-wrapper .spf-cloneable-item .spf-cloneable-content .select_post_meta', function (event) {
		//   var sp_this = $(this);
		//   setTimeout(function(){
		//       if (sp_this.parents('.spf-cloneable-item').find('.spf-cloneable-content .select_post_meta select').val() == 'taxonomy') {
		//         if ( ( sp_this.parents('.spf-cloneable-item').find('.spf-cloneable-content .pcp_meta_position .spf--button.spf--active input').val() == 'above_title' ) || ( sp_this.parents('.spf-cloneable-item').find('.spf-cloneable-content .pcp_meta_position .spf--button.spf--active input').val() == 'over_thumb' ) ) {
		//           sp_this.parents('.spf-cloneable-item').find('.spf-cloneable-content .select_meta_icon').hide();
		//         }else{
		//           sp_this.parents('.spf-cloneable-item').find('.spf-cloneable-content .select_meta_icon').show();
		//         }
		//       }else{
		//         sp_this.parents('.spf-cloneable-item').find('.spf-cloneable-content .select_meta_icon').show();
		//       }
		//     }, 300);
		//   });
		//   $(document).on('change', '.pcp_post_meta .pcp_custom_group_design .spf-cloneable-wrapper .spf-cloneable-item .spf-cloneable-content .pcp_meta_position', function (event) {
		//     var sp_this = $(this);
		//     setTimeout(function(){
		//     if (sp_this.parents('.spf-cloneable-item').find('.spf-cloneable-content .select_post_meta select').val() == 'taxonomy') {
		//       if ( ( sp_this.parents('.spf-cloneable-item').find('.spf-cloneable-content .pcp_meta_position .spf--button.spf--active input').val() == 'above_title' ) || ( sp_this.parents('.spf-cloneable-item').find('.spf-cloneable-content .pcp_meta_position .spf--button.spf--active input').val() == 'over_thumb' ) ) {
		//         sp_this.parents('.spf-cloneable-item').find('.spf-cloneable-content .select_meta_icon').hide();
		//       }else{
		//         sp_this.parents('.spf-cloneable-item').find('.spf-cloneable-content .select_meta_icon').show();
		//       }
		//     }else{
		//       sp_this.parents('.spf-cloneable-item').find('.spf-cloneable-content .select_meta_icon').show();
		//     }
		//   }, 300);
		// });
		/**
		 * Show/Hide tabs according to selected layout.
		 */
		// Filter layout.
		// var PCP_layout_type = $('.pcp_layout_preset .spf--sibling');
		// var PCP_get_layout_value = $('.pcp_layout_preset .spf--sibling.spf--active').find('input').val();

		// if (PCP_get_layout_value !== 'filter_layout') {
		// 	$(
		// 		'#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_4'
		// 	).hide()
		// } else {
		// 	$(
		// 		'#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_4'
		// 	).show()
		// }
		// // Carousel Layout.
		// if (PCP_get_layout_value !== 'carousel_layout') {
		// 	$(
		// 		'#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_3'
		// 	).hide()
		// } else {
		// 	$(
		// 		'#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_3'
		// 	).show()
		// }
		// // Timeline Layout.
		// if (PCP_get_layout_value !== 'timeline_layout') {
		// 	$(
		// 		'#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_5'
		// 	).hide()
		// } else {
		// 	$(
		// 		'#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_5'
		// 	).show()
		// }
		// // Collapsible Layout.
		// if (PCP_get_layout_value !== 'accordion_layout') {
		// 	$(
		// 		'#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_6'
		// 	).hide()
		// } else {
		// 	$(
		// 		'#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_6'
		// 	).show()
		// }
		// // Block Layout.
		// if (PCP_get_layout_value !== 'large_with_small') {
		// 	$(
		// 		'#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_10'
		// 	).hide()
		// } else {
		// 	$(
		// 		'#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_10'
		// 	).show()
		// }

		// /**
		//  * Show/Hide tabs on changing of layout.
		//  */
		// $(PCP_layout_type).on('change', 'input', function (event) {
		// 	//event.stopPropagation()
		// 	var PCP_get_layout_value = $(this).val();
		// 	// Filter layout.
		// 	if (PCP_get_layout_value !== 'filter_layout') {
		// 		$(
		// 			'#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_4'
		// 		).hide()
		// 		if ($("#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_4 a").hasClass('spf-section-active')) {
		// 			$("#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_1 a").trigger('click');
		// 		}
		// 	} else {
		// 		$(
		// 			'#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_4'
		// 		).show()
		// 	}
		// 	// Carousel Layout.
		// 	if (PCP_get_layout_value !== 'carousel_layout') {
		// 		$(
		// 			'#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_3'
		// 		).hide()
		// 		if ($("#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_3 a").hasClass('spf-section-active')) {
		// 			$("#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_1 a").trigger('click');
		// 		}
		// 	} else {
		// 		$(
		// 			'#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_3'
		// 		).show()
		// 	}
		// 	// Timeline Layout.
		// 	if (PCP_get_layout_value !== 'timeline_layout') {
		// 		$(
		// 			'#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_5'
		// 		).hide()
		// 		if ($("#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_5 a").hasClass('spf-section-active')) {
		// 			$("#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_1 a").trigger('click');
		// 		}
		// 	} else {
		// 		$(
		// 			'#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_5'
		// 		).show()
		// 	}
		// 	// Collapsible Layout.
		// 	if (PCP_get_layout_value !== 'accordion_layout') {
		// 		$(
		// 			'#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_6'
		// 		).hide()
		// 		if ($("#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_6 a").hasClass('spf-section-active')) {
		// 			$("#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_1 a").trigger('click');
		// 		}
		// 	} else {
		// 		$(
		// 			'#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_6'
		// 		).show()
		// 	}
		// 	// Block Layout.
		// 	if (PCP_get_layout_value !== 'large_with_small') {
		// 		$(
		// 			'#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_10'
		// 		).hide()
		// 	} else {
		// 		$(
		// 			'#sp_pcp_view_options .spf-nav ul li.menu-item_sp_pcp_view_options_10'
		// 		).show()
		// 	}
		// })

		// Define layout value to menu item mapping
		var displayToMenuItem = {
			'timeline_layout': '.spf-tabbed-nav .pcp_timeline_settings',
			'filter_layout': '.spf-tabbed-nav .pcp_isotope_settings',
			'accordion_layout': '.spf-tabbed-nav .pcp_accordion_settings',
		};

		// Function to toggle visibility of menu items based on layout value.
		function toggleMenuItems(layoutValue, overlayPosition, timelineStyle) {
			// Determine whether to hide the pagination menu item
			var isCarouselSliderThumbnail = (layoutValue === 'carousel_layout' || layoutValue === 'slider_layout' || layoutValue === 'thumbnails_slider');
			var isTimelineStyle = layoutValue === 'timeline_layout' && 'timeline_style_2' === timelineStyle;
			var glossaryLayout = isCarouselSliderThumbnail || layoutValue === 'glossary_layout' || isTimelineStyle;
			$('.spf-tabbed-nav .pcp_ajax_pagination').toggle(!glossaryLayout); 

			// Show or hide the common menu item for carousel, slider, and thumbnail
			$('.menu-item_sp_pcp_view_options_3').toggle(isCarouselSliderThumbnail);

			for (var key in displayToMenuItem) {
				var menuItemSelector = displayToMenuItem[key];
				var shouldBeVisible = (layoutValue === key);
				$(menuItemSelector).toggle(shouldBeVisible);
				// Trigger click event on the first menu item if it's active and hidden
				if ($(menuItemSelector + ' a').hasClass('spf-section-active') && !shouldBeVisible) {
					$(".menu-item_sp_pcp_view_options_1 a").trigger('click');
				}
			}
			$('.pcp-display-tabs .spf-tabbed-nav a:nth-child(1)').trigger('click');

			var radioInputs = $(document).find('[name="sp_pcp_view_options[post_content_orientation]"]');
			if ('thumbnails_slider' === layoutValue || 'slider_layout' === layoutValue) {
				if ('in_middle' === overlayPosition) {
					radioInputs.filter('[value="overlay-box"]').prop('checked', true).trigger('click');
				} else {
					radioInputs.filter('[value="overlay"]').prop('checked', true).trigger('click');
				}
			}
		}

		// Initial setup
		var layoutValue = $('.pcp_layout_preset .spf--sibling.spf--active').find('input').val();
		var overlayPosition = $('.thumbnails_slider_content_position .spf--sibling.spf--active').find('input').val();
		var timelineStyle = $('.timeline_other_style .spf--sibling.spf--active').find('input').val();
		toggleMenuItems(layoutValue, overlayPosition, timelineStyle);

		// "Advance Filter" dependency for 'thumbnails_slider', 'filter_layout' and 'glossary_layout'.
		let advanceFilterSelectedLayouts = ['thumbnails_slider', 'filter_layout', 'glossary_layout'];

		/**
		 * Updates the visibility and state of taxonomy filter checkboxes based on the current layout.
		 *
		 * This function is dependent on the "Advance Filter" setting and checks if the current 
		 * layout requires hiding the taxonomy filter options. If hiding is required, the function 
		 * iterates over each cloneable item, unchecks the relevant checkbox fields,
		 * and updates their values. The visibility of each checkbox is toggled according to the 
		 * layout requirements.
		 */
		function ajaxLiveFilterDependency() {
			// "Advance Filter" dependency.
			let hideLayoutOnChange = advanceFilterSelectedLayouts.includes(layoutValue);

			// Reusable function to handle the checkbox toggling and unchecking.
			function handleFilterCheckboxToggling(parentSelector, innerSelector, endsWithSelector) {
				$(parentSelector).each(function () {
					let cloneableItem = $(this);

					// Checkbox selectors.
					let filterCheckbox = cloneableItem.find(innerSelector);
					let filterCheckboxPseudo = cloneableItem.find(innerSelector + ' [name="_pseudo"]');
					let filterCheckboxValue = cloneableItem.find(innerSelector + ' [name^="sp_pcp_view_options"][name$="['+ endsWithSelector +']"]');
		
					// Uncheck the checkbox field if hiding is required.
					if (hideLayoutOnChange) {
						if (filterCheckboxValue.val() != 0) {
							filterCheckboxPseudo.trigger('click');
							filterCheckboxValue.trigger('click');
							filterCheckboxPseudo.prop('checked', false);
							filterCheckboxValue.val('0');
						}
					}
		
					// Toggle checkbox visibility.
					filterCheckbox.toggle(!hideLayoutOnChange);
				});
			}
		
			// Iterate through .pcp-filter-by-taxonomy items.
			handleFilterCheckboxToggling('.pcp-filter-by-taxonomy .spf-cloneable-item', '.spf-field-checkbox', 'add_filter_post');
		
			// Iterate through .pcp-filter-custom-field items.
			handleFilterCheckboxToggling('.pcp-filter-custom-field .spf-cloneable-item', '.spf-field-checkbox', 'add_filter_post');

			// Iterate through single tabs.
			handleFilterCheckboxToggling('.pcp-filter-by-author', '.pcp-add-author-filter-post', 'add_author_filter_post');
			handleFilterCheckboxToggling('.pcp-filter-by-order', '.pcp-add-orderby-filter-post', 'add_orderby_filter_post');
			handleFilterCheckboxToggling('.pcp-filter-by-order', '.pcp-add-order-filter-post', 'add_order_filter_post');
			handleFilterCheckboxToggling('.pcp-filter-by-keyword', '.pcp-add-search-filter-post', 'add_search_filter_post');
		}
		
		// Isotope and Glossary selector.
		let pcpIsotopeSettingsNotice = $(document).find('.spf-tabbed-sections .notice-help-image');
		let pcpIsotopeSettings = $(document).find('.spf-tabbed-nav .pcp_isotope_settings');
		let isotopeGlossaryLayouts = ['glossary_layout', 'filter_layout'];

		/**
		 * Updates the text of the Isotope settings tab based on the selected layout type.
		 * @param {string} layoutType - The currently selected layout type.
		 */
		function updateIsotopeSettingsTabText( layoutType ) {
			if ('glossary_layout' === layoutType) {
				pcpIsotopeSettings.contents().filter(function () {
					return this.nodeType === 3;
				}).replaceWith("Glossary Filter Styles");
			} else {
				pcpIsotopeSettings.contents().filter(function () {
					return this.nodeType === 3;
				}).replaceWith("Isotope Filter Styles");
			}
		}

		// Event listener for layout change.
		$('.pcp_layout_preset .spf--sibling').on('change', 'input', function (event) {
			layoutValue = $(this).val();
			overlayPosition = $('.thumbnails_slider_content_position .spf--sibling.spf--active').find('input').val();
			timelineStyle = $('.timeline_other_style .spf--sibling.spf--active').find('input').val();
			toggleMenuItems(layoutValue, overlayPosition, timelineStyle);

			// "Advance Filter" dependency for 'thumbnails_slider', 'filter_layout' and 'glossary_layout'.
			ajaxLiveFilterDependency();

			/**
			 * Isotope and Glossary settings dependency.
			 */
			pcpIsotopeSettings.toggle(isotopeGlossaryLayouts.includes(layoutValue));
			updateIsotopeSettingsTabText( layoutValue );

			// Notice control for Isotope or Glossary settings.
			pcpIsotopeSettingsNotice.toggle(layoutValue !== 'glossary_layout');

		});

		/**
		 * "Advance Filter" dependency on initial load for 'thumbnails_slider', 'filter_layout' and 'glossary_layout'.
		 */
		ajaxLiveFilterDependency();

		/**
		 * Isotope and Glossary dependency on initial load.
		 */
		pcpIsotopeSettings.toggle(isotopeGlossaryLayouts.includes(layoutValue));
		updateIsotopeSettingsTabText( layoutValue );
		
		// Notice control for Isotope or Glossary settings.
		pcpIsotopeSettingsNotice.toggle(layoutValue !== 'glossary_layout');

		// Event listener for content position change.
		$('.thumbnails_slider_content_position .spf--sibling').on('change', 'input', function (event) {
			layoutValue = $('.pcp_layout_preset .spf--active').find('input').val();
			overlayPosition = $(this).val();
			toggleMenuItems(layoutValue, overlayPosition,'');
		});
		// Event listener for timeline style change.
		$('.timeline_other_style .spf--sibling').on('change', 'input', function (event) {
			layoutValue = $('.pcp_layout_preset .spf--active').find('input').val();
			timelineStyle = $(this).val();
			toggleMenuItems(layoutValue, '', timelineStyle);
		});

		// Function to update meta icon preview.
		function updateMetaIconType(selector,metaValue) {
			var iconType = $(selector).parents('.spf-cloneable-content.ui-accordion-content-active').find('.select_meta_icon .spf-icon-preview i');
			var selectedIconType = $(selector).parents('.spf-cloneable-content.ui-accordion-content-active').find('.select_meta_icon input');
			switch (metaValue) {
					case 'author':
						iconType.removeAttr('class').addClass('fa fa-user');
					    selectedIconType.val('fa fa-user');
						break;
					case 'date':
						iconType.removeAttr('class').addClass('fa fa-calendar');
						selectedIconType.val('fa fa-calendar');
						break;
					case 'taxonomy':
						iconType.removeAttr('class').addClass('fa fa-folder-open-o');
						selectedIconType.val('fa fa-folder-open-o');
						break;
					case 'comment_count':
						iconType.removeAttr('class').addClass('fa fa-comments-o');
						selectedIconType.val('fa fa-comments-o');
						break;
					case 'view_count':
						iconType.removeAttr('class').addClass('fa fa-eye');
						selectedIconType.val('fa fa-eye');
						break;
					case 'like':
						iconType.removeAttr('class').addClass('fa fa-heart-o');
						selectedIconType.val('fa fa-heart-o');
						break;
					case 'reading_time':
						iconType.removeAttr('class').addClass('fa fa-clock-o');
					    selectedIconType.val('fa fa-clock-o');
						break;
				}
		}

		// Event listener for layout change
		$('.select_post_meta select').on('change', function (event) {
			var metaValue = $(this).val();
			updateMetaIconType('.select_post_meta select',metaValue);
		});

		$('.post_content_sorter .spf--sortable .spf--sortable-item:nth-child(1)').find('.spf-accordion-title').trigger('click');

		// Function to toggle visibility of menu items based on layout value
		function toggleTabsMenuItems(style) {
			if ('ticker' === style) {
				$('.pcp-carousel-tabs .spf-tabbed-nav > a:first-child').trigger('click');
				$('.pcp-carousel-tabs .spf-tabbed-nav').hide()
				$('.spf-field-tabbed.pcp-carousel-tabs').css({ "border": "none", "margin": "0" });
			} else {
				$('.pcp-carousel-tabs .spf-tabbed-nav').show()
			}
		}
		// Initial setup
		var carousel_style_value = $('.pcp_carousel_mode .spf--sibling.spf--active').find('input').val();
		toggleTabsMenuItems(carousel_style_value);

		// Event listener for layout change
		$('.pcp_carousel_mode .spf--sibling').on('change', 'input', function (event) {
			var carouselStyleValue = $(this).val();
			toggleTabsMenuItems(carouselStyleValue);
		});

		/* Preloader js */
		$("#sp_pcp_view_options .spf-metabox").css({ "backgroundImage": "none", "visibility": "visible", "minHeight": "auto" });
		$("#sp_pcp_view_options .spf-nav-metabox li").css("opacity", 1);

		/* Copy to clipboard */
		$('.pcp-shortcode-selectable').on('click', function (e) {
			e.preventDefault();
			pcp_copyToClipboard($(this));
			pcp_SelectText($(this));
			$(this).trigger("focus").select();
			$('.pcp-after-copy-text').animate({
				opacity: 1,
				bottom: 25
			}, 300);
			setTimeout(function () {
				jQuery(".pcp-after-copy-text").animate({
					opacity: 0,
				}, 200);
				jQuery(".pcp-after-copy-text").animate({
					bottom: 0
				}, 0);
			}, 2000);
		});
		$('.sp_pcp_input').on('click', function (e) {
			e.preventDefault();
			/* Get the text field */
			var copyText = $(this);
			/* Select the text field */
			copyText.select();
			document.execCommand("copy");
			$('.pcp-after-copy-text').animate({
				opacity: 1,
				bottom: 25
			}, 300);
			setTimeout(function () {
				jQuery(".pcp-after-copy-text").animate({
					opacity: 0,
				}, 200);
				jQuery(".pcp-after-copy-text").animate({
					bottom: 0
				}, 0);
			}, 2000);
		});
		function pcp_copyToClipboard(element) {
			var $temp = $("<input>");
			$("body").append($temp);
			$temp.val($(element).text()).select();
			document.execCommand("copy");
			$temp.remove();
		}
		function pcp_SelectText(element) {
			var r = document.createRange();
			var w = element.get(0);
			r.selectNodeContents(w);
			var sel = window.getSelection();
			sel.removeAllRanges();
			sel.addRange(r);
		}
	})
})(jQuery)