(function(window, document, $) {
	var $win = $(window);
	var $doc = $(document);

	$('.collapse').collapse();

	$('.panel-collapse').on('show.bs.collapse', function() {
		$(this)
			.siblings('.panel-heading')
			.addClass('active');
	});

	$('.panel-collapse').on('hide.bs.collapse', function() {
		$(this)
			.siblings('.panel-heading')
			.removeClass('active');
	});

	// Tabs

	$win.on('resize load', function(event) {
		if($win.width() > 768) {
			$('.filter .filter__item-head a').removeClass('active');
			$('.filter .filter__nav a').off().on('click', function(event) {
				event.preventDefault();
				$(this).parent().toggleClass('active').siblings().removeClass('active');
				const targetTab = $(this).attr('data-target');
				$(targetTab).toggleClass('visible').siblings().removeClass('visible');
			});
		}


		if($win.width() < 767) {
			$('.filter .filter__section').removeClass('visible');
			$('.filter .filter__nav li').removeClass('active');
			$('.filter .filter__item-head a').removeClass('active');
			$('.filter .filter__item-head a').off().on('click', function(event) {
				event.preventDefault();
				$(this).parent().toggleClass('active').siblings().removeClass('active');
				const targetTabNew = $(this).attr('data-target');
				$(targetTabNew).toggleClass('visible').siblings().removeClass('visible');
			});
		}
	});

	$win.on('resize', function() {
		if($win.width() > 768) {
			$('.filter .filter__item-head a').removeClass('active');
			$('.filter .filter__section').removeClass('visible');
			$('.filter .filter__nav li').removeClass('active');
		}

		if($win.width() < 767) {
			$('.filter .filter__section').removeClass('visible');
			$('.filter .filter__nav li').removeClass('active');
			$('.filter .filter__item-head a').removeClass('active');
		}
	});


	/*$('.filter .filter__item-head').on('click', function(event) {
		$(this)
			.find('a')
			.addClass('active');

		if ($('.filter .filter__item-head a').hasClass('active')) {
			$(this)
				.siblings()
				.find('a')
				.removeClass('active');
		}
	});*/

	// Nav trigger

	$('.nav-trigger').on('click', function(event) {
		$(this).toggleClass('nav-trigger--active');
		$('.header .header__bar').toggleClass('active');
		$('.wrapper').toggleClass('active');

		event.preventDefault();
	});

	// Dropdown Nav
	$win.on('load resize', function(event) {
		if ($win.width() < 768) {
			$('.has-dropdown')
				.not('.has-dropdown--current')
				.find('.js-dropdown')
				.hide();

			$('.has-dropdown a').on('click', function(event) {
				event.preventDefault();
				$(this)
					.next()
					.stop()
					.slideToggle()
					.parent()
					.toggleClass('has-dropdown--current')
					.siblings()
					.removeClass('has-dropdown--current')
					.find('.js-dropdown')
					.slideUp();
			});
		}
	});

	// Clear input

	$('.clearable').each(function() {
		var $inp = $(this).find('select'),
			$cle = $(this).find('.js-clear');

		$inp.on('input', function() {
			$cle.toggle(!!this.value);
		});

		$cle.on('touchstart click', function(e) {
			e.preventDefault();
			$inp.val('').trigger('input');
		});
	});

	$('.field-select').on('click', function() {
		$(this)
			.parent()
			.find('.js-select-arrow')
			.toggleClass('active');
	});

	// Close Item

	$('.js-item-close').on('click', function(event) {
		event.preventDefault();
		$(this)
			.closest('.item')
			.addClass('hidden');
	});

	//  Equalize height

	$.fn.equalizeHeight = function() {
		var maxHeight = 0,
			itemHeight;

		this.height('');

		for (var i = 0; i < this.length; i++) {
			itemHeight = $(this[i])
				.height('auto')
				.height();
			if (maxHeight < itemHeight) {
				maxHeight = itemHeight;
			}
		}

		return this.height(maxHeight);
	};

	$win.on('load resize', function() {
		$('.js-feature').equalizeHeight();
	});

	// Sticky Sidebar

	function stickySidebar() {
		const stickyContainer = document.querySelectorAll('.js-sticky');
		if (stickyContainer.length) {
			const sidebar = new StickySidebar(stickyContainer[0], {
				containerSelector: '.js-sidebar-holder',
				innerWrapperSelector: '.sidebar .sidebar__inner',
				topSpacing: 0,
				bottomSpacing: 0
			});

			stickyContainer[0].addEventListener(
				'affix.container-bottom.stickySidebar',
				function() {
					console.log(5);
					this.classList.add('hidden');
				}
			);

			stickyContainer[0].addEventListener(
				'affix.top.stickySidebar',
				function() {
					console.log(5);
					this.classList.remove('hidden');
				}
			);
		}
	}

	stickySidebar();

	// Scrollspy

	$('.js-scrollspy-nav a').on('click', function(e) {
		e.preventDefault();

		const target = $(this).attr('href');
		const targetOffsetTop = $(target).offset().top;

		$('.js-scrollspy-nav')
			.add($('.js-sticky'))
			.removeClass('is-active');

		$('html, body').animate(
			{
				scrollTop: targetOffsetTop
			},
			500
		);
	});

	// Expand sidebar on click

	$('.js-toggle-sidebar').on('click', function(event) {
		event.preventDefault();
		$('.js-sidebar-holder .js-scrollspy-nav')
			.add($('.js-sticky'))
			.toggleClass('is-active');
	});
})(window, document, window.jQuery);
