$(function() {

    /* Init */

    fnSetSubW();
    fnSetSitemap();
    fnSetSitemapToM();
    fnSetList();

    // Sitemap 열고 닫기
    $(".btn_sitemap").off().on("click", function() {
        if ($(".btn_sitemap").hasClass("on")) {
            $(".btn_sitemap").removeClass("on");
            $(".sitemap").hide();
            $(".btn_sitemap").focus();
        } else {
            $(".btn_sitemap").addClass("on");
            $(".sitemap").show();
            $(".sitemap h1 a").focus();
            fnHideTotalSearchBar();
        }
    });
    $(".sitemap .inner .close").off().on("click", function(e) {
        e.preventDefault();
        $(".btn_sitemap").removeClass("on");
        $(".sitemap").hide();
        $(".btn_sitemap").focus();
    });

    // 메인 : 공지 & 자료 탭
    $(".notice_pds .tab_list li a").off().on('click', function(e) {
        e.preventDefault();
        var idx = $(".notice_pds .tab_list li a").index(this);
        $(".notice_pds .tab_list li a").removeClass("on");
		$(".notice_pds .tab_list li a").attr("title", "");
        $(this).addClass("on");
		$(this).attr("title", "선택됨");
        $(".notice_pds .bot > div").hide();
        $("#" + $(this).attr("href")).show();
    });

    // 메인 : 자주묻는질문 / 묻고답하기
    $(".faq_qna .tab_list li a").off().on('click', function(e) {    // 221230   김혜준 웹접근성 갱신 선택 시 title 속성 추가 삭제 로직 추가
        e.preventDefault();
        var idx = $(".faq_qna .tab_list li a").index(this);
        $(".faq_qna .tab_list li a").removeClass("on");
        $(".faq_qna .tab_list li a").attr("title", "");
        $(this).addClass("on");
        $(this).attr("title", "선택됨");
        $(".faq_qna .tab_item").hide();
        $("#" + $(this).attr("href")).show();
    });

    $(window).resize(function() {

        // Sub menu width resize
        fnSetSubW();
        // Sitemap width resize
        fnSetSitemap();
        // Sitemap mode change WEB / Mobile
        fnSetSitemapToM();
        // 리스트 이벤트 처리
        fnSetList();

    });



    /*
        Action
    */

    // 주메뉴 서브 열림
    $(".gnb > ul > li > a").off().on("mouseenter focusin", function() {
        fnShowSubmn();

        //통합검색 레이어가 떠있는 경우
        if ($(".total_search").is(":visible")) {
            $(".btn_search_all").click();
            $("#totSearchValue").val("");
        }
    });

    // 주메뉴 서브 닫힘
    $(".gnb").off().on("mouseleave", function() {
        fnHideSubmn();
    });
    $(".gnb").prev().find("a").off().on("focusin", function() {
        fnHideSubmn();
    });
    $(".gnb").next().find("a").off().on("focusin", function() {
        fnHideSubmn();
    });

    // 전체검색
    $(".btn_search_all").off().on("click", function() {
        if ($("#totalSearch").css("display") == "block") {
            fnHideTotalSearchBar();
            $(".btn_search_all").focus();       /* 220126 웹 접근성 수정 */
        } else {

            $(".btn_sitemap").removeClass("on");
            $(".sitemap").hide();

            fnShowTotalSearchBar();
            fnRollToSrchwrd();
        }
    });
    $(".search_close").click(function () {      /* 220126 웹 접근성 수정 */
        fnHideTotalSearchBar();
        $(".btn_search_all").focus();
    })

    // 위치정보
    $(".location .disp").off().on("click", function(e) {
        e.preventDefault();

        $(".location .disp").next().slideUp(100);
        $(".location .disp").removeClass("on");

        if ($(this).next().css("display") == "none") {
            $(this).next().slideDown(100);
            $(this).addClass("on");
        } else {
            $(this).next().slideUp(100);
            $(this).removeClass("on");
        }
    });


	// Accordion 메뉴
    $(".accordion_list01 .list_group .head span a").off().on("click", function(e) {
        e.preventDefault();
        if ($(this).parents(".list_group").find(".content").css("display") == "none") {
            $(this).parents(".accordion_list01").find(".content").slideUp(100);
            $(this).parents(".list_group").find(".content").slideDown(100);
            $(this).parents(".accordion_list01").find(".head").removeClass("on");
            $(this).parents(".head").addClass("on");
        } else {
            $(this).parents(".list_group").find(".content").slideUp(100);
            $(this).parents(".head").removeClass("on");
        }
    });

	// 주메뉴 새창열기 아이콘
    $(".gnb a").each(function() {
        if ( $(this).attr("target") == "_blank") {
            $(this).addClass("new_window")
        }
    });



    //서브메뉴 keydown 이벤트
    $(".gnb > ul > li > ul > li > a").on("keydown", function (e) {
        var code = e.keyCode || e.which;

        //탭키
        if (!e.shiftKey && code === 9) {

            //마지막 서브메뉴이고, 다음 루트메뉴가 없는경우
            if ($(this).parent().is(":last-child") && $(this).parents("li.root").next().length == 0) {

                //ie 패치 : 이벤트 동작 중지
                e.preventDefault();

                fnHideSubmn();

                $(".gnb").next().find("button:first").focus();
            }
        }
    });

    $(".header").find(".btn_search_all").on("keydown", function (e) {
        var code = e.keyCode || e.which;

        //이전탭키
        if (e.shiftKey && code === 9) {

            //ie 패치 : 이벤트 동작 중지
            e.preventDefault();

            var idx = $(".gnb > ul > li.root:last").index();
            fnShowSubmn(idx, true);
        }
    });

	// 사이트맵 새창열기 아이콘
    $(".sitemap a").each(function() {
        if ( $(this).attr("target") == "_blank") {
            $(this).addClass("new_window")
        }
    });

});

/*
	Global Functon
*/

function fnShowTotalSearchBar() {
    $(".btn_search_all").addClass("on");
    $(".btn_search_all").text("전체검색닫기");
    $("#totalSearch").fadeIn(100, function() {
        /*$("#totSearchValue").focus();*/               /* 220127 웹 접근성 수정 */
    });
}
function fnHideTotalSearchBar() {
    $(".btn_search_all").removeClass("on");
    $(".btn_search_all").text("전체검색열기");
    $("#totalSearch").hide();
}

// 서브메뉴 가로길이를 window size에 맞춤
function fnSetSubW() {
    $(".gnb.web .sub_menu").width($(window).width());
}

/*
	Sitemap
*/
// 가로길이를 window size에 맞춤
function fnSetSitemap() {
    //$(".sitemap").width($(window).width());
}
function fnSetSitemapToM() {
    if ($(window).width() <= 800) {
        $(".sitemap").addClass("mobile");
    } else {
        $(".sitemap").removeClass("mobile");
    }

    // Mobile일때 열고닫기 기능
    $(".sitemap.mobile > .inner .sub > div h2 a").off().on("click", function(e) {
        e.preventDefault();
        $(".sitemap > .inner .sub > div > ul").removeClass("on");
        if (!$(this).hasClass("on")) {
            $(".sitemap > .inner .sub > div > ul").slideUp(300);
            $(".sitemap > .inner .sub > div h2 a").removeClass("on");
            $(this).parent().next().addClass("on").slideDown(300);
            $(this).addClass("on");

        } else {
            $(this).parent().next().removeClass("on").slideUp(100);
            $(this).removeClass("on");
        }
    });
}
function fnSetList() {
    //모바일인 경우
    if ($(window).width() <= 800) {

        if ($(".board_list").length > 0) {
            $(".board_list > .result > .list_item a.lnk").each(function() {
                var anchor = $(this);
                var href = anchor.attr("href");
                if (href != "") {
                    anchor.on("click", function(e){
                        e.preventDefault();
                    });

                    var listItem = anchor.parents(".list_item");
                    listItem.css({"cursor":"pointer"});
                    listItem.attr("data-href", href);
                    listItem.on("click", function() {
                        var dataHref = $(this).attr("data-href");
                        window.location = dataHref;
                    });
                }
            });
        }

    //그외 이벤트 해제
    } else {
        if ($(".board_list").length > 0) {
            $(".board_list > .result > .list_item").off("click");
            $(".board_list > .result > .list_item").css({"cursor":"auto"});
            $(".board_list > .result > .list_item a.lnk").off();
        }
    }
}
function fnShowSubmn(rootMenuIndex, focusLast) {

    $(".header").addClass("on");

    var cnt = 1;
    $(".gnb > ul > li > ul").show(100, function(){
        if (cnt == $(".gnb > ul > li > ul").length) {
            if (typeof focusLast != "undefined" && focusLast == true) {
                $(".gnb > ul > li.root").eq(rootMenuIndex).find("ul > li:last > a").focus();
            }
        }
        cnt++;
    });
}
function fnHideSubmn() {
	$(".gnb > ul > li > ul").hide();
    $(".header").removeClass("on");

    //현재 메뉴 표시 초기화
    if ($("#rootMenuNo").val() != "") {
        $(".gnb > ul > li > a > div.current").remove();
		$("#rootItem_" + $("#rootMenuNo").val()).addClass("on");
        $("#rootItem_" + $("#rootMenuNo").val()).attr("title", "선택됨");      // 230104   김혜준 대메뉴 선택 타이틀 추가
    }
}


// Layer popup
function fnOpenLayer(id) {
    $("#" + id).show();
}
function fnCloseLayer(obj) {
    $(obj).parents(".l_pop").hide();
}