/*
 * 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";
	
/* ===============================================
	News ticker initialization
	=============================================== */

	$('.ticker').ticker({
		cursorSpeed: 10,
	});

/* ===============================================
	News ticker fix
	=============================================== */

	function citymag_ticker_max_width() {

		var ticket_offset;
		var ticker_width = $('.ticker-section .ticker').width();
		var ticker_title_width = $('.ticker-section .ticker strong').width();

		if ($('body').hasClass('is_minimal_layout') == true) {
			ticket_offset = 30;
		} else {
			ticket_offset = 60;
		}

		var ticker_max_width = ticker_width - ticker_title_width - ticket_offset;
		$('.ticker-section .ticker div').css({'max-width': ticker_max_width});

	}

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

/* ===============================================
   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 citymag_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(citymag_scroll_sidebar);
	$( window ).load(citymag_scroll_sidebar);
	
/* ===============================================
   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 citymag_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);
		citymag_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();
			citymag_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 citymag_footer() {

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

	}

	$( document ).ready(citymag_footer);
	$( window ).resize(citymag_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 citymag_masonry() {

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

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

		});

	}

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

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

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

	function citymag_embed() {

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

	}

	$(window).load(citymag_embed);
	$(document).ready(citymag_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 : 4;
			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 : 2;

			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,
						}
					}

				]
		
			});
			
		});

	});

});
