//swiper_1.onResize(); / swiper_2.onResize(); 본문 리사이즈
jQuery(document).ready(function() {
	url = url.replace('.do', '').replace('List', '').replace('View', '');
	if (url.indexOf('?') > -1) {
		var hash = url.substring(url.lastIndexOf('/') + 1, url.indexOf('?'));
	} else {
		var hash = url.substring(url.lastIndexOf('/') + 1, url.length);
	}
	document.location.hash = hash;
	'use strict';
	/*
	setTimeout(function(){
		console.log('aaa');
		thumb();
	},250);
*/
	//	thumb();

	//setTimeout(function(){
	lnb();
	sch();
	tabLy();
	tabLySub();
	accessibility();
	tabSizeChk();

	let sel_title_value = $('.sub_page_titles').find('.title_sel .swiper-slide.active a').text();
	$('#depth_title').text(sel_title_value);
	//},100);


});


$(window).on('load, resize', function() {
	topMove();
	pageShare();
	scrollIndicator();
	tabLySub();
	tabSizeChk();

});

/*
function elementHh() {
	let conElement = $('.area_contents .concon');
	
	const observer = new ResizeObserver((entries) => {
		
		for (let entry of entries) {
			console.log('요소의 사이즈 변화를 감지했을 경우 !!!!!!!!!!!!!!!!');
			swiper_2.update();
		}
		entries.forEach((entry) => {
			console.log('요소의 사이즈 변화를 감지했을 경우');
			swiper_2.update();
		});
		console.log('요소의 사이즈 변화를 감지했을 경우......?');
	});
	
	observer.observe(conElement);
	
}
*/

function thumb() {
	// 숏폼(.short), 오디오(.autio) 썸네일 배경 변경 
	$('.thumb.custom').each(function() {
		var imgsrc = $(this).find('img').attr('src');
		$(this).css('background', 'url(' + imgsrc + ')');
	});

	// 영상 표시 아이콘
	$('.thumb.play').each(function() {
		$(this).parent().find('.ico_play').remove();
		if ($(this).parent().parent().find('.share_wrap').length > 0) {
			$(this).prepend('<span class="ico_play pos"><i>영상</i></span>');
		} else {
			$(this).prepend('<span class="ico_play"><i>영상</i></span>');
		}
	});
}

function lnb() {

	$(window).on('load scroll', function() {
		var egHeight = $('.eg_info').height();
		var hHeight = egHeight + $('#header').height();
		var fHeader = $('body:not(.main, .lnb-is-open) #header');
		if ($(window).scrollTop() > hHeight) {
			fHeader.addClass('fixed');
		}else {
			fHeader.removeClass('fixed');
		}
		
		if($(window).scrollTop() > egHeight){
			$('body').addClass('eg_none');
			$('.lnb').removeAttr('style');	
		}else{
			$('body').removeClass('eg_none');
			$('.lnb').css('top',($('.eg_info').height() - $(window).scrollTop()) + $('.header').height());
		}
	});

	$('.header .lnb_open').on('click', function() {
		var hh = $(document).scrollTop() - $('body').offset().top;
		var hp = $('#header').height() + $('.eg_info').height();
		$('.lnb, body').addClass('lnb-is-open');
		//$('body.lnb-is-open').css('top', - hh + 'px');
		$('body').addClass('hd_fixed');
		//$('body').css('padding-top', hp + 'px');
	});
	$('.header .lnb_close').on('click', function() {
		//var hh = $(document).scrollTop() - $('body').offset().top;
		$('body').removeAttr('style');
		$('.lnb, body, .nav > li').removeClass('lnb-is-open');
		$('body').removeClass('hd_fixed');
		//$('body').css('padding-top', '0');
		//$(document).scrollTop(hh);
		return false;
	});

	$('.nav > li > a').on('click', function(e) {
		if ($(this).attr('target') != '_blank') {
			$(this).next().stop().slideToggle('fast').parent('li').stop().toggleClass('active');
			$('.nav > li > a').not(this).next().stop().slideUp('fast').parent('li').removeClass('active');
			e.preventDefault();
			e.stopPropagation();
		}
	});

	$('.nav > li > ul li a').on({
		'touchstart': function() { $(this).parent().addClass('active'); },
		'touchend': function() { $(this).parent().removeClass('active'); }
	});

	$('.lnb').on('click', function() {
		$(this).add('body').removeClass('lnb-is-open');
		$('body').removeClass('hd_fixed');
		$('body').css('padding-top', '0');
	});

};

function sch() {

	// 알림 설정 이동
	$('.header .notify_open').on('click', function(e) {
		location.href = '/etc/newsNotifyGuide.do?pageSe=setting';
	});

	$('.header .sch_open').on('click', function(e) {
		var hh = $(document).scrollTop() - $('body').offset().top;
		$(this).next().stop().fadeIn(200);
		$('body').addClass('sch-is-open');
		$('body.sch-is-open').css('top', - hh + 'px');
	});
	$('.header .sch_btn_close').on('click', function(e) {
		var hh = $(document).scrollTop() - $('body').offset().top;
		$(this).parent().parent().stop().fadeOut(200);
		$('.dtl_sch_post').stop().fadeOut(200);
		$('body').removeClass('sch-is-open');
		$('.dtl_sch_open').removeClass('active');
		$(document).scrollTop(hh);
	});

	$('.header .dtl_sch_open').on('click', function(e) {
		if ($(this).hasClass('active')) {
			$(this).removeClass('active').next().stop().fadeOut(200);
			$(this).find('i').text('열기');
		} else {
			$(this).addClass('active').next().stop().fadeIn(200);
			$(this).find('i').text('닫기');
		}
	});
	$('.header .dtl_sch_close button').on('click', function(e) {
		$(this).parent().parent().prev().removeClass('active').next().stop().fadeOut(200);
		$(this).parent().parent().prev().find('i').text('열기');
	});
	$('.dtl_sch_post .tab button').on('click', function() {
		$(this).parent().addClass('active').siblings().removeClass('active');
		var idx = $(this).parent().index();
		$(this).parent().parent().next('.tab_cont').children('.cont').eq(idx).show().siblings('.cont').hide();
	});

	$('.dtl_sch_post .sbtn').on('click', function() {
		if ($(this).hasClass('close')) {
			$(this).removeClass('close');
			$(this).parent().parent().removeClass('active');
			$(this).find('span i').text('펼치기');
			$(this).parent().next('ul').hide();
		} else {
			$(this).addClass('close');
			$(this).parent().parent().addClass('active');
			$(this).find('span i').text('접기');
			$(this).parent().next('ul').show();
		}
	});

};

function scrollIndicator() {

	$('body:not(.main) #header').append('<i class="scroll_indicator"><span></span></i>');
	var hHeight = $('.eg_info').height() + $('#header').height();

	$(window).on("scroll", function() {
		if ($('body:not(.main) .scroll_indicator').length && $('body:not(.main) #main').height() > 200) {
			var articleHeight = $('#footer').offset().top - $('#main').offset().top - window.innerHeight - hHeight;
			var articleStart = $(window).scrollTop() - $('#main').offset().top;
			var percentage = (articleStart / articleHeight) * 100;
			percentage = percentage > 100 ? 100 : percentage;
			$('.scroll_indicator span').css('width', Math.floor(percentage) + '%');
			$('.scroll_indicator').removeClass('hide');
		}
	});

};

function topMove() {

	$('.footer .control .top').on('click', function() {
		//$('html, body').animate({ scrollTop: 0 }, 100);
		$('html, body').scrollTop(0);
	});

	$(window).on('scroll', function() {
		var goTop = $('.footer .control .top');
		var scrollTop = $(window).scrollTop();
		scrollTop > 0 ? goTop.addClass('active') : goTop.removeClass('active');
	});

};

function pageShare() {

	// 공유 퀵메뉴
	$('footer .control .ft_share .sh_btn').on('click', function() {
		$(this).parent('.ft_share').toggleClass('active');
		$('#footer').toggleClass('on');
	});

	$('footer .control .ft_share .close').on('click', function() {
		$(this).parents('.ft_share').removeClass('active');
		$('#footer').removeClass('on');
	});

};

var swiper_1_1;
var swiper_1_2;
var swiper_1_3;
var swiper_2;
var swiper_3;

function tabLy() {

	$('.sub_nav > ul').addClass('swiper-wrapper');
	$('.sub_nav > ul > li').addClass('swiper-slide');
	$('section#containerForSwiper > div').addClass('swiper-wrapper');
	$('section#containerForSwiper > div > div').addClass('swiper-slide');
	$('.title_sel > ul').addClass('swiper-wrapper');
	$('.title_sel > ul > li').addClass('swiper-slide');

	swiper_1_1 = new Swiper('.policy_nav ', {
		slidesPerView: 'auto',
		paginationClickable: false,
		spaceBetween: 0,
		freeMode: true,
		pagination: false,
		observer: true,
		observeParents: true,
		touchReleaseOnEdges: true,
	});

	swiper_1_2 = new Swiper('.keyword_nav ', {
		slidesPerView: 'auto',
		paginationClickable: false,
		spaceBetween: 0,
		freeMode: true,
		pagination: false,
		observer: true,
		observeParents: true,
		touchReleaseOnEdges: true,
	});

	swiper_1_3 = new Swiper('.media_nav ', {
		slidesPerView: 'auto',
		paginationClickable: false,
		spaceBetween: 0,
		freeMode: true,
		pagination: false,
		observer: true,
		observeParents: true,
		touchReleaseOnEdges: true,
	});
	
	/*swiper_nav = swiper_1_1;*/

	swiper_3 = new Swiper('.title_sel ', {
		direction: 'vertical',
		slidesPerView: 'auto',
		paginationClickable: false,
		spaceBetween: 0,
		freeMode: true,
		pagination: false,
		observer: true,
		observeParents: true,
		touchReleaseOnEdges: true,
		//passiveListeners: false,
	});


	if ($('section#containerForSwiper').length > 0) {
		/*setInterval(function(){
			location.reload();
		},10000);*/

		$('#depth_title').addClass('down_arr');

		$('#depth_title').on('click', function(e) {
			$(this).parent(".sub_page_titles").addClass('on');
			$('body').css("overflow", "hidden");
			return false;
		});

		$('.title_hidden_chk .close_btn').on('click', function() {
			$('.sub_page_titles').removeClass('on');
			$('body').css("overflow", "visible");
		});

		$('.title_sel ul > li > a').on('click', function(e) {
			$(".sub_page_titles").removeClass('on');
			$('body').css("overflow", "visible");
			$(this).parent().addClass('active').siblings().removeClass('active');
			var tabIdx = $(this).parent().index();
			swiper_2.slideTo(tabIdx + 1, 0);
			/*swiper_1_1.slideTo(tabIdx - 1, 0);*/
			e.preventDefault();
		});
		/*swiper_1_1.slideTo(actIdx - 1, 0);*/
		var tabLen = $('.title_sel ul > li').length;
		swiper_2 = new Swiper('section#containerForSwiper', {
			autoHeight: true,
			slidesPerView: 1,
			spaceBetween: 0,
			pagination: false,
			loop: true,
			speed: 200,
			spaceBetween: 10,
			hashnav: true,
			hashnavWatchState: true,
			longSwipesRatio: 0.2,
			touchAngle: 20,
			observer: true,
			observeParents: true,
			lazyLoading: true,
			lazyLoadingInPrevNext: true,
			// watchSlidesProgress: true,
			// watchSlidesVisibility: true,
			passiveListeners: false,
			onSlideChangeStart: function(swiper) {
				var idx = swiper.activeIndex - 1;

				if (url.indexOf('newsLetterMain') > -1 && idx != 1) {
					$('.area_page').removeClass('active');
				} else if (url.indexOf('newsLetterMain') > -1 && idx == 1) {
					// $('.area_page').addClass('active');
				}

				// if(!$('.title_sel .swiper-slide').hasClass('active')) {
				// 	$('.title_sel .swiper-slide.swiper-slide-active').addClass('active');
				// }

				if (idx < 0) {
					idx = tabLen - 1;
				} else if (idx == tabLen) {
					idx = 0;
				}
				$('.title_sel ul > li').removeClass('active').eq(idx).addClass('active');
				if (idx < tabLen) {
					/*swiper_1_1.slideTo(idx - 1, 200);*/
				}

				let sel_title_value = $('.sub_page_titles').find('.title_sel .swiper-slide.active a').text();
				$('#depth_title').text(sel_title_value);

				if ($('.article_wrap .article.swiper-slide-active').data('hash') === 'policyNews') {
					$('.sub_nav').hide().filter('.policy_nav').show();

					tabSizeChk()
				} else if ($('.article_wrap .article.swiper-slide-active').data('hash') === 'customizedNews') {
					$('.sub_nav').hide().filter('.keyword_nav').show();

				} else if ($('.article_wrap .article.swiper-slide-active').data('hash') === 'mediaNews') {
					// active 된 3depth가 없을 경우 첫번째(영상)로 active 처리
					//if ($('.li_mediaNews.active').length < 1) {
					$('.li_mediaNews').eq(0).addClass('active');
					//}
					$('.sub_nav').hide().filter('.media_nav').show();

					tabSizeChk()
				} else if ($('.article_wrap .article.swiper-slide-active').data('hash') === 'shortsNews'
					|| $('.article_wrap .article.swiper-slide-active').data('hash') === 'visualNews'
					|| $('.article_wrap .article.swiper-slide-active').data('hash') === 'policyPhoto'
					|| $('.article_wrap .article.swiper-slide-active').data('hash') === 'cartoon'
					|| $('.article_wrap .article.swiper-slide-active').data('hash') === 'policyAudio') {
					$('.sub_nav').hide().filter('.media_nav').show();

					tabSizeChk()
				} else {
					$('.sub_nav').hide();
				}


			},
			onSlideChangeEnd: function(swiper) {
				var idx = swiper.activeIndex;
				var swiperLen = swiper.slidesGrid.length - 1;
				if (idx == swiperLen) {
					swiper.slideTo(1, 0);
				} else if (idx == 0) {
					swiper.slideTo(swiperLen - 1, 0);
				}
				var panel = swiper.slides[idx];
				var datahash = $(panel).attr('data-id');
				var datahash2 = $(panel).attr('data-page');
			}
		});

		swiper_List = swiper_2;

	} else if ($('body:not(.main)').length > 0) {
		var act = $('.title_sel ul > li.active');
		var inx = $('.title_sel ul > li').index(act);

		$('#depth_title').addClass('down_arr');

		$('#depth_title').on('click', function(e) {
			$(this).parent(".sub_page_titles").addClass('on');
			$('body').css("overflow", "hidden");
			return false;
		});

		$('.title_hidden_chk .close_btn').on('click', function() {
			$('.sub_page_titles').removeClass('on');
			$('body').css("overflow", "visible");
		});

		$('.title_sel ul > li > a').on('click', function(e) {
			$(".sub_page_titles").removeClass('on');
			$('body').css("overflow", "visible");
			$(this).parent().addClass('active').siblings().removeClass('active');
			var tabIdx = $(this).parent().index();
			swiper_2.slideTo(tabIdx + 1, 0);
			/*swiper_1_1.slideTo(tabIdx - 1, 0);*/
			e.preventDefault();
		});

		$('.title_sel ul > li > a').on('click', function(e) {
			$(this).parent().addClass('active').siblings().removeClass('active');
			var tabIdx = $(this).parent().index();
			/*swiper_1_1.slideTo(tabIdx - 1, 0);*/
		});
		// swiper_1.slideTo(actIdx-1, 0);

		/*if(url.indexOf("multi/cartoon") > -1){
			actIdx = actIdx - 1;
		}*/

		$('.title_sel ul > li').removeClass('active').eq(actIdx - 1).addClass('active');

		/*
		if($('.area_nav > .title_sel ul li').length > 1){
			swiper_1.unlockSwipes();
		}else{
			swiper_1.lockSwipes();
		}
			*/

	}
	/*
	if(swiper_2 != undefined){
		if($('.area_nav > .title_sel ul li').length > 1){
			swiper_2.unlockSwipes();
		}else{
			swiper_1.lockSwipes();
			swiper_2.lockSwipes();
		}
	}
		*/
	/* slick slide touch */
	$(".article_wrap").on("touchmove", ".slick-slider", function() {
		event.stopPropagation();
	});

};

function tabLySub() {
/*	let cateSubWd = $('.sub_nav').width();
	let cateWrapWd = $('.sub_nav .swiper-wrapper').width();
	let lastCate = $('.sub_nav li:last-child');

	// 서브 카테고리 마지막 요소 위치
	if (lastCate.hasClass('active') == true) {
		lastCate.parents('.swiper-wrapper').css({"transform":"translate3d(" +  (cateSubWd - cateWrapWd) - 10 + "px,0,0)"});
	} else {
		lastCate.parents('.swiper-wrapper').css({"transform":"translate3d(0,0,0)"});
	}*/
}

function tabSizeChk(){

	var tabFind = $('.sub_nav').filter(function (){
		return $(this).css("display") === "block";
	});

	var tabWidth = tabFind.width();
	var tabWrapWidth = tabFind.find('.swiper-wrapper').width();
	var childWidth = tabFind.find("li").width();
	var totalWidth = tabWidth - tabWrapWidth

	if(tabWrapWidth > tabWidth){
		var indexChk = tabFind.find("li").each(function (index){
			if(index > 3 && $(this).hasClass('active')){
				$(this).parent("ul").css({"transform":"translateX(" + (totalWidth) + "px)"});
			}
		});
	}

}


function accessibility() {

	$('img[usemap]').rwdImageMaps(); //이미지맵
	$('a[target="_blank"]').attr('title', '새창으로 열림');
	$('[class*="xi-"], [class*="xe-"]').attr('aria-hidden', 'true'); //아이콘폰트 스크린리더가 읽지 못하도록 하는 속성

	//국민이 말하는 정책 이미지테이블 스타일 제거
	$('main.sub .view_cont table.imageWrap, main.sub .view_cont table.imageWrap .captions, main.sub .view_cont table.imageWrap img, main.sub .view_cont div.imageWrap figure, main.sub .view_cont div.imageWrap .captions').removeAttr('style');

	$(".view-cont table").each(function() {
		if ($(this).width() > 200) {
			$(this).css("width", "100%");
		}
	});

	$(".imageWrap").each(function() {
		if ($(this).find(".captions b").text().length <= 1) {
			$(this).find(".captions").remove();
		}
	});

};