/*
 * jQuery theme functions file
 * https://www.themeinprogress.com
 *
 * Copyright 2024, ThemeinProgress
 * Licensed under MIT license
 * https://opensource.org/licenses/mit-license.php
 */

jQuery.noConflict()(function($){

	"use strict";
	
/* ===============================================
   Newsticker
   =============================================== */

	$('.news-ticker-marquee-init').marquee({
		duration: 150000,
		pauseOnHover : true,
		startVisible: true,
		delayBeforeStart: 0,
		duplicated:true

	});
	
/* ===============================================
   Header cart
   ============================================= */

	$('div.header-cart').hover(

		function () {
			$(this).children('div.header-cart-widget').stop(true, true).fadeIn(100);
		},
		function () {
			$(this).children('div.header-cart-widget').stop(true, true).fadeOut(400);
		}

	);

/* ===============================================
   Scroll sidebar
   =============================================== */
   
	function newsweek_scroll_sidebar() {
   
		$("#scroll-sidebar").niceScroll(".wrap", {
			cursorwidth: "5px",
			cursorborder: "1px solid #333",
			railpadding: {
				top: 0,
				left: 0,
				bottom: 0,
				right: 0
			}
		});
	
		$('nav.custommenu a.parent-item').click(function(){
			setTimeout(function(){
				$("#scroll-sidebar").getNiceScroll().resize();
			}, 500);
		});
	
		var pw = $(window).width();
	
		$(".open-modal-sidebar").click(function() {
	
			$('#overlay-body').fadeIn(600).addClass('visible');
			$('html, body').addClass('overlay-active');
			$('#wrapper').addClass('open-sidebar');
			
			setTimeout(function(){
				$('#scroll-sidebar a.close-modal-sidebar').focus();
			}, 100);
	
		});
	
		if ( pw < 992 ) {
	
			$("#sidebar-wrapper .close-modal-sidebar, #overlay-body").click(function() {
	
				$('#overlay-body').fadeOut(600);
				$('html, body').removeClass('overlay-active');
				$('#wrapper').removeClass('open-sidebar');
				$('a.open-modal-sidebar').focus();
	
			});
	
			$("#overlay-body").swipe({
	
				swipeRight:function(event, direction, distance, duration, fingerCount) {
	
					$('#overlay-body').fadeOut(600);
					$('html, body').removeClass('overlay-active');
					$('#wrapper').removeClass('open-sidebar');
					$('a.open-modal-sidebar').focus();

				},
	
				threshold:0
	
			});
	
		} else if ( pw > 992 ) {
	
			$("#sidebar-wrapper .close-modal-sidebar, #overlay-body").click(function() {
	
				$('#overlay-body').fadeOut(600);
				$('html, body').removeClass('overlay-active');
				$('#wrapper').removeClass('open-sidebar');
				$('a.open-modal-sidebar').focus();

			});
	
		}

	}

	$( document ).ready(newsweek_scroll_sidebar);
	$( window ).load(newsweek_scroll_sidebar);
	
/* ===============================================
   Open header search
   =============================================== */

	function newsweek_open_search_form() {

		$('.header-search .search-form').addClass('is-open');
		$('body').addClass('no-scrolling');
		setTimeout(function(){
		   $('.search-form  #header-searchform input#header-s').filter(':visible').focus();
		}, 100);

		return false;
	}

	$( ".header-search a.open-search-form").on("click", newsweek_open_search_form);

/* ===============================================
   Close header search
   =============================================== */

	function newsweek_close_search_form() {
		$('.header-search .search-form').removeClass('is-open');
		$('body').removeClass('no-scrolling');
		$( ".header-search a.open-search-form").focus();
	}

	$( ".header-search a.close-search-form").on("click", newsweek_close_search_form);

/* ===============================================
   TRAP TAB FOCUS ON MODAL SEARCH
   ============================================= */

	$('.search-form  #header-searchform :input').on('keydown', function (e) {
	    if ($("this:focus") && (e.which === 9)) {
	        e.preventDefault();
	        $(this).blur();
	        $('.search-form a.close-search-form').focus();

	    }
	});

	$('.search-form  a.close-search-form').on('keydown', function (e) {
	    if ($("this:focus") && (e.which === 9)) {
	        e.preventDefault();
	        $(this).blur();
	        $('.search-form  #header-searchform :input').focus();

	    }
	});

/* ===============================================
   Mobile menu
   =============================================== */

	 $('nav#mobilemenu ul > li').each(function(){

 		if( $('ul', this).length > 0 ) {

 			var element = $(this).children('a');
 			$( '<a class="sub-indicator" href="#"><span class="sf-sub-indicator"><i class="fa fa-caret-down"></i></span></a>' ).insertAfter(element);

 		}

 	});

	function newsweek_open_submenu_one(selector) {

		if(
			selector.closest('a').next('ul.sub-menu').css('display') === 'none' ||
			selector.closest('a').next('ul.children').css('display') === 'none' ||
			selector.next('ul.children').css('display') === 'none'
		) {
			selector.html('<i class="fa fa-caret-up"></i>');
		} else {
			selector.html('<i class="fa fa-caret-down"></i>');
		}

		selector
			.closest('a')
			.next('ul.sub-menu')
			.stop(true,false)
			.slideToggle(500);

		selector
			.closest('a')
			.next('ul.children')
			.stop(true,false)
			.slideToggle(500);

	}

	$('nav#mobilemenu ul > li .sub-indicator, nav#mobilemenu ul > li > ul > li .sub-indicator').click(function(e){

		e.preventDefault();
		var selector = $(this);
		newsweek_open_submenu_one(selector);

	});

	$('nav#mobilemenu ul > li .sub-indicator, nav#mobilemenu ul > li > ul > li .sub-indicator').keydown(function(e){

		var selector = $(this);
		if ( e.keyCode === 13 ) {
			e.preventDefault();
			newsweek_open_submenu_one(selector);
		}

	});

/* ===============================================
   Trap tab focus on modal sidebar
   ============================================= */

	var focusableElements = [
	  'button',
	  '[href]',
	  'input',
	  'select',
	  'textarea',
	  'textarea',
	  '[tabindex]:not([tabindex="-1"])',
	];

	$.each(focusableElements, function(index, value) {

		var elements = $('#scroll-sidebar').find(value);

		var firstEl = elements[0];
		var lastEl = elements[ elements.length - 1 ];

		$(document).on('keydown', function (event) {

			var tabKey = event.keyCode === 9;
			var shiftKey = event.shiftKey;
			var activeEl = document.activeElement;

			if ( ! shiftKey && tabKey && lastEl === activeEl ) {
				event.preventDefault();
				firstEl.focus();
			}

			if ( shiftKey && tabKey && firstEl === activeEl ) {
				event.preventDefault();
				lastEl.focus();
			}

		});

	});

/* ===============================================
   Footer fix
   =============================================== */

	function newsweek_footer() {

		var footerHeight = $('#footer').innerHeight();
		$('#wrapper').css({'padding-bottom':footerHeight});

	}

	$( document ).ready(newsweek_footer);
	$( window ).resize(newsweek_footer);

/* ===============================================
   Scroll to top Plugin
   =============================================== */

	$(window).scroll(function() {

		if( $(window).scrollTop() > 400 ) {
			$('#back-to-top').fadeIn(500);
		} else {
			$('#back-to-top').fadeOut(500);
		}

	});

	$('#back-to-top').click(function(){
		$("html, body").animate({scrollTop: 0}, 700);
		return false;
	});

/* ===============================================
   Masonry
   =============================================== */

	function newsweek_masonry() {

		$('.masonry').imagesLoaded(function () {

			$('.masonry').masonry({
				itemSelector: '.masonry-item',
				isAnimated: true
			});

		});

	}

	$(document).ready(function(){
		newsweek_masonry();
	});

	$(window).resize(function(){
		newsweek_masonry();
	});

/* ===============================================
   fitVids
   =============================================== */

	function newsweek_embed() {

		$('#wrapper').imagesLoaded(function () {
			$('.embed-container, .video-container, .maps-container').fitVids();
			newsweek_masonry();
		});

	}

	$(window).load(newsweek_embed);
	$(document).ready(newsweek_embed);

/* ===============================================
   Swipebox gallery
   =============================================== */

	$(document).ready(function(){

		var counter = 0;

		$('div.gallery').each(function(){

			counter++;

			$(this).find('.swipebox').attr('data-rel', 'gallery-' + counter );

		});

		$('.swipebox').swipebox();

	});
	
/* ===============================================
   Featured posts orizzontal slideshow
   ============================================= */

   	$(document).ready(function(){

		var slider_transition = $('.horizontal-featured-posts-slideshow').attr('data-transition') === 'fade' ? true : false;

		$('.horizontal-featured-posts-slideshow').not('.slick-initialized').slick({
			centerMode: false,
			slidesToShow: 1,
			adaptiveHeight:false,
			fade: slider_transition,
			nextArrow: '<div class="next-arrow"><span class="dashicons dashicons-arrow-right-alt2"></span></div>',
			prevArrow: '<div class="prev-arrow"><span class="dashicons dashicons-arrow-left-alt2"></span></div>',
			appendArrows: $(this).find('.horizontal-featured-posts-navigation'),
		});

	});

/* ===============================================
   Featured posts vertical carousel
   ============================================= */

	$(document).ready(function(){

		$('.vertical-featured-posts-section').each(function(){

			var vertical_featured_posts_slideshow = $(this).children('.vertical-featured-posts-slideshow');
			var n_items = $(this).children('.vertical-featured-posts-slideshow').hasClass('vertical-overlay-carousel') ? 2 : 7;
			var n_items_laptop = $(this).children('.vertical-featured-posts-slideshow').hasClass('vertical-overlay-carousel') ? 2 : 6;
			var n_items_mobile = $(this).children('.vertical-featured-posts-slideshow').hasClass('vertical-overlay-carousel') ? 1 : 3;

			vertical_featured_posts_slideshow.not('.slick-initialized').slick({
				
				slidesToShow: n_items,
				slidesToScroll: 1,
				fade: false,
				vertical: true,
				infinite: false,
				verticalSwiping: true,
				nextArrow: '<div class="next-arrow"><span class="dashicons dashicons-arrow-up-alt2"></span></div>',
				prevArrow: '<div class="prev-arrow"><span class="dashicons dashicons-arrow-down-alt2"></span></div>',
				appendArrows: $(this).find('.vertical-featured-posts-navigation'),
				responsive: [
					{
						breakpoint: 1170,
						settings: {
							slidesToShow: n_items_laptop,
						}
					},
					{
						breakpoint: 993,
						settings: {
							slidesToShow: n_items_mobile,
							slidesToScroll: 1,
						}
					},
					{
						breakpoint: 601,
						settings: {
							slidesToShow: 1,
							slidesToScroll: 1,
						}
					}

				]
		
			});
			
		});

	});

});
