/*
 * 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 urbamag_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(urbamag_scroll_sidebar);
	$( window ).load(urbamag_scroll_sidebar);
	
/* ===============================================
   Open header search
   =============================================== */

	function urbamag_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", urbamag_open_search_form);

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

	function urbamag_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", urbamag_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();

	    }
	});

/* ===============================================
   Header menu
   =============================================== */

   $('nav.header-menu ul > li').each(function(){

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

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

 		}

 	});

/* ===============================================
   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 urbamag_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);
		urbamag_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();
			urbamag_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 urbamag_footer() {

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

	}

	$( document ).ready(urbamag_footer);
	$( window ).resize(urbamag_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 urbamag_masonry() {

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

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

		});

	}

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

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

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

	function urbamag_embed() {

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

	}

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

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

	$(document).ready(function(){

		var counter = 0;

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

			counter++;

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

		});

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

	});

});