// source --> https://tacta.io/wp-content/themes/tacta/assets/js/default.js?ver=20240604160914 
function hideArrows(){
	$('.swiper-controls').each(function(){
		var c = 0;
		$('span', this).each(function(){
			if($(this).hasClass('swiper-button-disabled')) c += 1;
		});
		if(c == 2) $(this).addClass('swiper-controls-hide'); else $(this).removeClass('swiper-controls-hide');
	});
}

function positionDot(){
	if($('.block-hiring-section').length){
		var _sps = $('.block-hiring-section')[0].getBoundingClientRect();
		var _cps = $('.col-process-style')[0].getBoundingClientRect();
		var st = _cps.y - _sps.y + $(window).height() / 2;
		var bt = _cps.height - parseFloat(st) - $(window).height() / 2 + parseInt($('.block-hiring-section').css('padding-bottom')) * 2;
		$('.following-dot-wrapper').css({ 'top': st+'px', 'height': bt+'px' });
	}
}

function selectArticleNav(){
	var aid = $('.js-floater.inview').parent().attr('id');
	if(aid != undefined){
		$('.article-nav a').removeClass('active');
		$('.article-nav [href="#'+aid+'"]').addClass('active');
	}
}

function growCount(el){
	t = parseFloat(el.data('target'));
	s = parseFloat(el.data('step'));
	v = parseFloat(el.text());
	if(v < t){
		n = v+s;
		if(Math.floor(v) != v) n = n.toFixed(1);
		el.text(n);
		setTimeout(function(){ growCount(el); }, 50);
	} else {
		el.text(t);
	}
}

$(function() {

	/* ------------------------------ Logos Animation ------------------------------ */
	$('.logos').each(function(){
		var l = $('.logos-slide', this);
		$('a', this).each(function(){
			var t = $(this).clone().appendTo(l);
		});
	});

	/* ----------------------- Languages Toggle ----------------------- */
	$('.lang-toggle').on('click', function(e) {
		e.preventDefault();
		$('.lang').toggleClass('active');
	});

	/* --------------------------- Toggle Mobile Navigation ------------------------ */
	 $('.navigation-toggle').on('click', function(){
		$('body').toggleClass('navigation-active');
	});

	/* ----------------------- Accordion Toggle ----------------------- */
	var _accordionsRolling = false;
	$('.accordion-header').on('click', function() {
		_accordionsRolling = true;
		setTimeout(function(){ _accordionsRolling = false; }, 800);
		var _t = $(this);
		var _h = _t.next().find('p').height() + 20;
		var _p = _t.parents('.accordions');
		// var _sto = _t.parents('.row-benefits');
		// $([document.documentElement, document.body]).animate({ scrollTop: _sto.offset().top }, 500);
		if(!_t.parent().hasClass('active')){
			if($('.accordion.active', _p).length) _o = 400; else _o = 0;
			setTimeout(function(){ _t.next().css('max-height', _h+'px'); _t.parent().addClass('active'); }, _o);
		}
		$('.accordion', _p).removeClass('active').find('.style-text').css('max-height', '0px');
	});

	/* ------------------------------ Gallery Switcher ------------------------------ */
	$('.swiper-filters a').on('click', function(e){
		e.preventDefault();
		var _filter = $(this).text();
		$('.swiper-filters a').removeClass('active');
		$(this).addClass('active');
		$('[data-gallery]').addClass('d-none');
		$('[data-gallery="'+_filter+'"]').removeClass('d-none');
		$(this).parents('.swiper')[0].swiper.slideTo(0);
	});

	/* ------------------------------ Default Swiper ------------------------------ */
	var theSwipers = [];
	$('.swiper-default').each(function(){
		var spv = $('.swiper-wrapper', this).data('spv');
		var bps = $('.swiper-wrapper', this).data('breakpoints');
		var arrows = $('.swiper-wrapper', this).data('arrows');
		var args = {
			slidesPerView: spv,
			spaceBetween: 30,
			breakpoints: bps,
		};
		if(arrows){
			args.navigation = {
				nextEl: ".swiper-button-next",
				prevEl: ".swiper-button-prev",
			};
		}
		var s = new Swiper($(this)[0], args);
		theSwipers.push(s);
	});

	/* --------------------------------- Generic Inview --------------------------------- */
	inView('.iv').on('enter', function(e){
		e.classList.add('iv-on');
	}).on('exit', function(e){
		e.classList.remove('iv-on');
	});

	/* --------------------------------- Blog Inview --------------------------------- */
	inView('.js-floater').on('enter', function(e){
		e.classList.add('inview');
		selectArticleNav();
	}).on('exit', function(e){
		e.classList.remove('inview');
		selectArticleNav();
	});

	inView('.js-blog-bottom').on('enter', function(e){
		e.classList.add('inview');
	}).on('exit', function(e){
		e.classList.remove('inview');
	});

	if($('.js-floater').length){
		inView.threshold(1);
	}

	if($('.sticky').length && $(window).width() <= 600){
		inView.threshold(1);
		console.log('inView threshold');
	}

	/* ------------------------------ Counters Inview ------------------------------ */
	inView('.block-counters-section').on('enter', function(e){
		$('.block-counters-section .number > span').each(function(){ growCount($(this)); });
	}).on('exit', function(e){
		$('.block-counters-section .number > span').each(function(){ $(this).text('0'); });
	});

	/* --------------------------------- Copy Code --------------------------------- */
	$('.code-copy').on('click', function(e){
		e.preventDefault();
		var _b = $(this);
		var _v = _b.prev().find('code').text();
		navigator.clipboard.writeText(_v);
		_b.text('Copied');
		setTimeout(function(){ _b.text('Copy'); }, 1000);
	});

	/* ----------------------- Sticky handler ----------------------- */
	inView('[data-change-call]').on('enter', function(e){
		var _d = e.dataset.changeCall;
		$('[data-change-image]').removeClass('active');
		$('[data-change-image="'+_d+'"]').addClass('active');
	}).on('exit', function(e){
		var _d = e.dataset.changeCall;
		$('[data-change-image="'+_d+'"]').removeClass('active');
	});

	/* ------------------------------ Init ------------------------------ */
	hideArrows();
	positionDot();

	/* ------------------------------ Scroll ------------------------------ */
	var prevScrollpos = window.pageYOffset;
	$(window).on('scroll', function(){
		var currentScrollPos = window.pageYOffset;
		if(currentScrollPos > 100 && !_accordionsRolling){
			if (prevScrollpos > currentScrollPos) {
				$('body').addClass('scroll-top');
				$('body').removeClass('scroll-bottom');
			} else {
				$('body').removeClass('scroll-top');
				$('body').addClass('scroll-bottom');
				$('header .lang').removeClass('active');
			}
			prevScrollpos = currentScrollPos;
		}
		if(currentScrollPos <= 0) {
			$('body').removeClass('scroll-top').removeClass('scroll-bottom');
		}
		/* --------------------------------- The Following Dot --------------------------------- */
		if($('.following-dot').length){ var dot = $('.following-dot > div')[0].getBoundingClientRect(); }
		$('.iv-dottrigger.iv-on').each(function(){
			var _o = $(this);
			var o = _o[0].getBoundingClientRect();
			obj_top = o.y;
			obj_bottom = obj_top + o.height;
			if(dot.y > obj_top && dot.y < obj_bottom && obj_bottom < $(window).height())
				_o.addClass('iv-isin');
			else
				_o.removeClass('iv-isin');
		});

		if($('.iv-dottrigger.iv-on.iv-isin').length)
			$('.following-dot > div').addClass('active');
		else
			$('.following-dot > div').removeClass('active');
	});

	/* ------------------------------ Resize ------------------------------ */
	$(window).on('resize', function(){ 
		hideArrows(); 
		positionDot(); 
	});

});