
// gnb fix 2023-06-23 김태현
var gnbFix = document.body.dataset.fixed;
var gnbFixHeight;
if (gnbFix != undefined) {
  gnbFixHeight = document.getElementById('top_layout').clientHeight;
  window.addEventListener("scroll", function () {
    var _scrollTop = window.scrollY || document.documentElement.scrollTop;
    if (_scrollTop > gnbFixHeight + 100) {
      document.getElementById('top_layout').classList.add('gnbFix')
    } else if (_scrollTop >= 100 ) {
      document.getElementById('top_layout').classList.remove('gnbFix');
    }
  });
}


$(".topgo_link a").click(function (e) {
	e.preventDefault()
	// document.getElementById('top_layout').classList.remove('gnbFix');
	$("html, body").animate({ "scrollTop": 0 }, 900);
	$("#skipnavi a").eq(0).focus()

});

/* 신 코드 : template_d | 구 코드 : ct_type */
function gnb(param,obj,btn,wrap,dur,meth){
	var param = $(param);
	var btn = param.find(btn);
	var obj = param.find(obj);
	var wrap = $(wrap);
	var data = false;
	var prev_elem = -1;
	var elem;
	var targetHeight = [];
	var targetHeightResult;
	var heightResult = btn.outerHeight(true);


	function gnb_height(){
		$.each(obj,function(idx){
			targetHeight[idx] = $(this).parent().find(".item").outerHeight(true) + $("#gnb").outerHeight();
			//targetHeight[idx] = $(this).parent().find(".item").outerHeight(true) + $("#gnb").outerHeight() + 50;
		});
		//targetHeightResult = Math.max.apply(null,targetHeight)+80;
	}


	function _open(){
		if(prev_elem == undefined) prev_elem = elem;

		if(prev_elem != elem) {
			prev_elem = elem;
			btn.parent().find(".item").removeAttr("style");

			btn.not(elem).removeClass("current").eq(elem).addClass("current");
			obj.not(elem).css({display:"none",opacity:0});
			gnb_height();
			obj.eq(elem).show().stop().animate({opacity:1},{duration: 300,easing:'swing'});
			param.stop(true,false).animate({"height":targetHeight[elem]},{duration: 300,easing:'swing'});
			param.addClass("open");
			$("#top_layout").addClass("on");

		}



	}



	function _close(){
		prev_elem = -1;

		btn.removeClass("current");
		obj.css({display:"none",opacity:0});
		param.stop(true,false).animate({"height":heightResult},{duration: 300,easing:'swing'});
		param.removeClass("open");
		obj.btn.removeClass("on");
		$("#top_layout").removeClass("on");
	}

	btn.on('mouseover focusin', function(){
		elem = $(this).parent().index();
		_open();
	});

	obj.item = obj.find(".depth2_ul");
	obj.btn = obj.find(".depth2_ul>li>a");
	obj.item.child = obj.item.find(".depth3_ul");
	obj.item.child_btn = obj.item.find(".depth3_ul li a");

	//2st:S
	obj.find("a").last().on("focusout",function(){
		_close();
	});

	obj.btn.on("mouseenter focus",function(){
		var t = $(this);
		obj.btn.removeClass("on");
		t.addClass("on");
		//$(".depth3_ul").hide();
		t.next().show();
		t.find(".depth3_ul").show().addClass('open');
	});


	//2st:E

	//3st:S
	obj.item.child_btn.on("mouseenter focus",function(){
		obj.btn.removeClass("on");
		$(this).parent().parent().prev().addClass("on");
	});
	//3st:E


	obj.btn.parent().on("mouseleave",function(){
		$(this).removeClass("on");
	});

	obj.find("a").last().on("focusout",function(){
		_close();
		btn.removeClass("current");
	});

	wrap.on("mouseleave",function(){
		_close();
	});
}

function gnb_open(){
	var param = $('#mobile-menu');
	var str = location.href;
	var n = 0;
	var dur = 300;
	var meth = "easeOutExpo";


	param.find(".depth2_ul").hide();
	/*
	param.find(".depth1_ul > .n1 .depth2_ul").show();
	param.find(".depth1_ul > .n1 > a").addClass("ov");
	*/
	param.find(".depth1_ul a.ov").next("ul").show();

	param.find("a").on("click",function(event){
		if(param.is(":animated") && param.find(":animated").size()){ return false; }
	});

	function hideMenu() {
		param.find('.collapse').hide().removeClass('ov');
	}
	param.find(".depth1_ul>li>a").on("click",function(event){
		n = $(this).parent().index();
		if($(this).next().attr("class").match("depth2_ul")){
			if($(this).next().css("display") == "none"){
				param.find(".depth1_ul>li>a").not(n).removeClass("ov").next().stop(true,true).delay(150).slideUp(150);
				param.find(".depth1_ul>li>a").eq(n).addClass("ov").next().stop(true,true).slideDown(150);
			}
		}
		event.preventDefault();
	});

	param.find(".depth2_ul>li>a").on("click",function(event){
		var t = $(this);

		if(t.next().attr("class").match("depth3_ul")){
			if(t.next().css("display") != "block"){
				t.addClass("ov");
				t.next().slideDown(150);
			}else{
				t.removeClass("ov");
				t.next().slideUp(150);
			}

			event.preventDefault();
		}
	});

	$(window).on('resize', function () {
		$('.mobile-close').click();
		$('#gnb_layout').removeAttr('style');
	});

}
$("#mobile-menu .depth3_ul").prev().addClass("dep");


// 모바일 메뉴 부트스트랩 제거 
var html = $("html"),
  body = $("body"),
  mobileMenu = $("#mobile-menu");
$(".allmenu-link.mo .btn-allmenu").click(function () {
  $("#top_layout").prepend('<div class="new-modal-backdrop m-fade" ></div>')
  html.addClass("modal-open-n");
  body.addClass("modal-open-n");
  mobileMenu.show();
  setTimeout(function () {
    $('.new-modal-backdrop').addClass("m-in");
    mobileMenu.addClass("m-in");
  }, 200);
  // 
});

$(".mobile-close").click(function () {
  mobileClose()
});

/*
window.resizeBy(function(){
  mobileClose()
})
*/

function mobileClose() {
  mobileMenu.removeClass("m-in");
  $('.new-modal-backdrop').removeClass("m-in");
  html.removeClass("modal-open-n");
  body.removeClass("modal-open-n");
  setTimeout(function () {
    $('.new-modal-backdrop').remove()
    mobileMenu.hide()
  }, 200);
}

$(".search-link").each(function() {
	var $selWrap = $(this),
		$curBtn = $selWrap.find(".open"),
		$close = $selWrap.find(".clickClose");

	$curBtn.on("click", function(e) {
		var $thisBtn = $(this),
			$selList = $thisBtn.siblings("div");

		if ($selWrap.hasClass("on")) {
			$selWrap.removeClass("on");
			$(this).find('em i').text('닫기');

		} else {
			$selWrap.addClass("on");
			$(this).find('em i').text('열기');
		}

		$('body').on("click", function(e){
			if( !$selWrap.has(e.target).length ){
				$selWrap.removeClass('on');
				$(this).find('em i').text('닫기');
			}
		});

		$selWrap.find("a, button").filter(":first").on("keydown", function(e) {
			if (e.shiftKey && e.keyCode === 9) {
				$selWrap.removeClass('on');
				$(this).find('em i').text('닫기');
			}
		});
		$selWrap.find("a, button").filter(":last").on("keydown", function(e) {
			if (!e.shiftKey && e.keyCode ===  9) {
				$selWrap.removeClass('on');
				$(this).find('em i').text('닫기');
			}
		});

		e.preventDefault();
	});
	$close.on("click", function(e) {
		$selWrap.removeClass('on');
		$curBtn.find('em i').text('닫기');

		e.preventDefault();
	});
});