jQuery(document).ready(function ($) {
	$restaurantmenus = $(document).find('.restaurant-menus');
	$menuNav = $('.restaurant-menu-nav');
	$menuNavCollapseTrig = $('.collapse-menu-title');
	$menulinks = $menuNav.find('.menu-link');
	$(window).load(function () {
		if (window.location.hash) {
			let $hash = window.location.hash;
			$hash = $hash.split('#');

			$('.menu-link.' + $hash[1]).addClass('is-selected');
			let target = window.location.hash;
			$('html, body').animate({scrollTop: $(target).offset().top - 200}, 200);
			removeHash();
		} else {
			$menulinks.first().addClass('is-selected')
		}
	});
	$restaurantmenus.each(function () {
		$menus = $(this).find('.restaurant-menu-layout');
		$menuCount = $menus.length;
		if ($menuCount > 1) {
			$(this).flickity({
				draggable: false,
				autoPlay: false,
				dragThreshold: 75,
				cellAlign: 'left',
				pageDots: false,
				cellSelector: '.restaurant-menu-layout',
				prevNextButtons: false,
				imagesLoaded: true,
				pauseAutoPlayOnHover: false,
				wrapAround: true,
				adaptiveHeight: true,
				hash: true,
			});
		}
	})
	$flkty = $restaurantmenus.data('flickity');
	$restaurantmenus.on('select.flickity', function () {
		$menulinks.filter('.is-selected').removeClass('is-selected');
		$menulinks.eq($flkty.selectedIndex).addClass('is-selected');
		removeHash();
	});
	$restaurantmenus.on('settle.flickity', function () {
		removeHash();
		$newText = $menuNav.find('.is-selected h2').text();
		$menuNavCollapseTrig.find('.text').text($newText);
		let target = $('.menu-area-title');
		if($width>1090){
			$('html, body').animate({scrollTop: $(target).offset().top - 225}, 500, function() {
				removeHash();
				//$('.navbar').addClass('hide');
			});
		}else{
			$('html, body').animate({scrollTop: $(target).offset().top - 175}, 500, function() {
				removeHash();
				//$('.navbar').addClass('hide');
			});
		}
	});
	$menuNavCollapseTrig.on('click', function () {
		$linkHeight = 0;
		if($(window).width() < 1090)	{
			$(this).find('.icon').removeClass('icon-caret-down').addClass('icon-caret-up');
			if ($(this).hasClass('activated')) {
				$menuNav.height(0).removeClass('grown');
				$(this).removeClass('activated');
				$(this).find('.icon').removeClass('icon-caret-up').addClass('icon-caret-down')
			} else {
				$menuNav.find('.menu-link').each(function () {
					$linkHeight += $(this).outerHeight(true);
				});
				$menuNav.height($linkHeight).addClass('grown');
				$(this).addClass('activated');
			}
		}
	});
	$menuNav.on('click', '.menu-link', function () {
		let index = $(this).index();
		$restaurantmenus.flickity('select', index);
		if ($(window).width() < 1090) {
			$menuNav.height(0).removeClass('grown');
			$menuNavCollapseTrig.removeClass('activated');
			$menuNavCollapseTrig.find('.icon').removeClass('icon-caret-up').addClass('icon-caret-down')
			$switchText = $(this).text();
			$menuNavCollapseTrig.find('.text').text($switchText);
		}
		if (window.location.hash) {
			let target = $('.menu-area-title');
			if($width>1090){
				$('html, body').animate({scrollTop: $(target).offset().top - 225}, 500, function() {
					removeHash();
					//$('.navbar').addClass('hide');
				});
			}else{
				$('html, body').animate({scrollTop: $(target).offset().top - 75}, 500, function() {
					removeHash();
					//$('.navbar').addClass('hide');
				});
			}
		}
	});
	$width = $(window).width();
	if ($width > 1091) {
		//scrollFixNav('#menu', '.restaurant-menu-nav', '200');
		doUnCollapseNav('restaurant-menu-nav','grown');
	}else{
		//scrollFixNav('#menu', '.menu-nav-wrapper', '200');
	}
	$(window).resize( function(){
		$width = $(window).width();
		if ($width > 1091) {
			doUnCollapseNav('.restaurant-menu-nav','grown');
		}
	});
	$(window).scroll( function(){
		$width = $(window).width();
		if ($width > 1091) {
			scrollFixNav('#menu', '.restaurant-menu-nav', 200);
		}else{
			scrollFixNavMobile('#menu', '.menu-nav-wrapper', 100);
		}
	});

	// $width = $(window).width();
	// if ($('#menu')) {
	// 	if ($width > 991) {
	// 		let distance = $('#menu').offset().top - 200,
	// 			$window = $(window);
	// 		$window.scroll(function () {
	// 			if ($window.scrollTop() >= distance) {
	// 				$('.restaurant-menu-nav').addClass('fixed-top');
	// 			}
	// 			if ($('.restaurant-menu-nav').hasClass('fixed-top')) {
	// 				let fixed = $('.restaurant-menu-nav.fixed-top');
	// 				let fixed_position = fixed.offset().top - 200;
	// 				let fixed_height = fixed.height();
	// 				let toCross_position = fixed.parent().offset().top;
	// 				let toCross_height = fixed.parent().height();
	// 				if (fixed_position + fixed_height <= toCross_position) {
	// 					fixed.removeClass('fixed-top');
	// 				} else if (fixed_position + fixed_height > toCross_position + (toCross_height - 200)) {
	// 					fixed.removeClass('fixed-top');
	// 				} else {
	// 					fixed.addClass('fixed-top');
	// 				}
	// 			}
	// 		});
	// 	}
	// }
});
function removeHash() {
	history.pushState('', document.title, window.location.pathname + window.location.search);
}
function doUnCollapseNav($element, $classToRemove){
jQuery($element).css('height','auto').removeClass($classToRemove);
}
function scrollFixNav($whereToFixIt, $whatToFix, $offset) {
	if (jQuery($whereToFixIt)) {
		let distance = jQuery($whereToFixIt).offset().top - $offset,
			$window = jQuery(window);
			if ($window.scrollTop() >= distance) {
				jQuery($whatToFix).addClass('fixed-top');
			}
			if (jQuery($whatToFix).hasClass('fixed-top')) {
				let fixed = jQuery($whatToFix + '.fixed-top');
				let fixed_position = fixed.offset().top - $offset;
				let fixed_height = fixed.height();
				let toCross_position = fixed.parent().parent().offset().top;
				let toCross_height = fixed.parent().parent().height();
				if (fixed_position + fixed_height <= toCross_position) {
					fixed.removeClass('fixed-top');
				} else if (fixed_position + fixed_height > toCross_position + (toCross_height - $offset)) {
					fixed.removeClass('fixed-top');
				} else {
					fixed.addClass('fixed-top');
				}
			}
	}
}

function scrollFixNavMobile($whereToFixIt, $whatToFix, $offset) {
	if (jQuery($whereToFixIt)) {
		let distance = jQuery($whereToFixIt).offset().top - $offset,
			$window = jQuery(window);
			if ($window.scrollTop() >= distance) {
				jQuery($whatToFix).addClass('fixed-top');
			}
			if (jQuery($whatToFix).hasClass('fixed-top')) {
				let fixed = jQuery($whatToFix + '.fixed-top');
				let fixed_position = fixed.offset().top + $offset;
				let fixed_height = fixed.height();
				let toCross_position = fixed.parent().offset().top;
				let toCross_height = fixed.parent().height();
				if (fixed_position + fixed_height <= toCross_position) {
					fixed.removeClass('fixed-top');
				} else if (fixed_position + fixed_height > toCross_position + (toCross_height - $offset)) {
					fixed.removeClass('fixed-top');
				} else {
					fixed.addClass('fixed-top');
				}
			}
	}
}
