$(document).ready(function(){
	
	/****************************************************************************************
	******************************* Navigation Controller ***********************************
	****************************************************************************************/
	
	$('.navigation__link-box > span').click(function(event){
		
		if(!$('.navigation__link-items').hasClass('active'))
		{
			$('.navigation__link-items').addClass('active');
		}
		else
		{
			$('.navigation__link-items').removeClass('active');
		}
			
		
	});
	
	$(document).mouseup(function (e){
		if (!$('.navigation__link-items').is(e.target) && $('.navigation__link-items').has(e.target).length === 0 && !$('.navigation__link-box > span').is(e.target) && $('.navigation__link-box > span').has(e.target).length === 0 && $('.navigation__link-items').hasClass('active')) {
			$('.navigation__link-items').removeClass('active');
		}
	});
	
	$(window).resize(function()
	{
		updateNavigation();
	});
	
	updateNavigation()

	function updateNavigation(){

		let wndSize = $(window).outerWidth();
		
		let offset = 150;
		if(wndSize < 1240){
			offset = 100;
		}
		
		$('.navigation__link').each(function() {

			let mainLinksWith = 0;

			$(".navigation__links").children(".navigation__link").each(function() {
				mainLinksWith += $(this).outerWidth(true);
			});

			let lastMainLink = $(".navigation__links").children(".navigation__link:not(.fix-pos)").last();
			let mainWith = $(".navigation > .content-area").outerWidth() - $(".navigation__links").children(".navigation__link-box").outerWidth(true) - offset;
			let item = $(this);

			if(mainLinksWith >= mainWith){
				lastMainLink = lastMainLink.detach();
				$('.navigation__link-items').prepend(lastMainLink);
			}

			if(($(".navigation__link-items").children(".navigation__link").eq(0).find("span").outerWidth(true) + mainLinksWith) < mainWith)
			{
				
				if($(".navigation__links").children(".navigation__link").length > 0)
				{
					$(".navigation__links").children(".navigation__link").last().after($(".navigation__link-items").children(".navigation__link").eq(0));
				}
				else
				{
					let nlink = $(".navigation__link-items").children(".navigation__link").eq(0).detach();
					$('.navigation__links').prepend(nlink);
				}
			}
		});
		
		if($(".navigation__link-items").children(".navigation__link").length <= 0){
			$(".navigation__link-box").css('opacity','0');
			$(".navigation__link-box").css('pointer-events','none');
		}
		else{
			$(".navigation__link-box").css('opacity','1');
			$(".navigation__link-box").css('pointer-events','auto');
		}

		$(".navigation > .content-area").css('opacity','1');
		$(".navigation > .content-area").css('overflow','visible');
	}

	/****************************************************************************************
	************************************* Scroll Top ****************************************
	****************************************************************************************/

	$('.footer__up-btn').click(function(event){
		$('html, body').animate({ 
			scrollTop: 0
		}, 500);
	});
	
});

