$(function(){
	
	let app = {
		
		//Main nav
		initMainNav: function(){
			$(document).on('click', '.main_nav_trg', function(event){
				$(this).parent('.main_nav').toggleClass('opened');
				event.stopPropagation();
			});
			$(document).on('click', 'body', function() {
				if( $('.main_nav').hasClass('opened') ){
					$('.main_nav').removeClass('opened');
				}
			});
		},
		
		//Smooth scroll
		initSmoothScroll: function() {
			$('.s_scroll').click(function(e){
				e.preventDefault();
				$('html, body').animate({
					scrollTop: $( $(this).attr('href') ).offset().top - 50
				}, 600, 'swing');
				return false;
			});
		},
		
		//Nav scroll
		initNavScroll: function(){
			let now = location.href;
			
			//access with hash
			let hash = location.hash;
			
			if(hash){
				$('html, body').stop().scrollTop(0);

				setTimeout(function(){
					$('html, body').animate({
						scrollTop: $(hash).offset().top - 50
					}, 600, 'swing');
				}, 100);
			}
			
			//nav click
			$('#menu-main-nav a, #menu-top-nav a').click(function(e){
				let href = $(this).attr('href');
				let href_id = href.slice(1);
				let site_url = 'https://www.poppingemo.net/';
				
				if(href.startsWith('/#')){
					if( now === site_url || now.startsWith(site_url + '#') ){
						e.preventDefault();

						$('html, body').animate({
							scrollTop: $(href_id).offset().top - 50
						}, 600, 'swing');
						
						if( $('.main_nav').hasClass('opened') ){
							$('.main_nav').removeClass('opened');
						}
						
						return false;
					}
				}
			});
		},
		
		//Page top
		initPagetopScroll: function() {
			$('#footer_pt').click(function(e){
				e.preventDefault();
				$('html, body').animate({
					scrollTop: 0
				}, 600, 'swing');
				return false;
			});
		},
		
		//Scroll fade in
		initScrollFadein: function() {
			let wHeight = $(window).height();
			let scrollAmount = $(window).scrollTop();
			
			$('.s_fadein').each(function(){
				let targetElement = $(this).offset().top;
				
				if (scrollAmount > targetElement - wHeight + 10){
					$(this).css('opacity','1');
					$(this).css('transform','translateY(0)');
				}
			});
		},
		
	}

	//Execute
	app.initMainNav();
	app.initSmoothScroll();
	app.initNavScroll();
	app.initPagetopScroll();

	$(window).on('scroll', function(){
		app.initScrollFadein();
	});

	$(window).on('load', function(){
		app.initScrollFadein();
	});

	$(window).on('resize', function(){
	});

});