var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ? true : false;

var LUI = window.LUI || (function(){
    return {
        init: function(){
            LUI.baseUI($(document));
            LUI.gnbUI();
        },
        baseUI: function($this) {
            var _ = $this;
            
            var scrollLUI = _.find('.o-scrollbar');
            var selectLUI = _.find('.select-base');
            
            if (scrollLUI.length) {
                if (!isMobile) {
                    _.find('.o-scrollbar').mCustomScrollbar({
                        theme:"minimal-dark",
                    });
                } else {
                    _.find('.o-scrollbar').addClass('mo')
                }
            }

            if (selectLUI.length) {
                LUI.selectUI();
                LUI.selectScript();
            }
        },
        gnbUI: function() {
            var window_width = $(window).width();
            var depth2_swiper = undefined;
            function responSwiper() {
            //swiper È£ÃâÅ¸ÀÔÀ¸·Î ±¸ºÐ 
            if (window_width < 1024 && depth2_swiper == undefined) {
                depth2_swiper = new Swiper(".depth2-swiper", {
                    slidesPerView: 'auto',
                    observer: true,
                    observeParents: true,
                    observeSlideChildren: true,
                    simulateTouch: true,     
                    //scrollbar: {
                    //    el: '.depth2-swiper .swiper-scrollbar',
                    //    draggable: true,
                    //},                    
                });
            } else if (window_width >= 1024 && depth2_swiper != undefined) {
                depth2_swiper.destroy();
                depth2_swiper = undefined;
            }
            }
            responSwiper();
            
            //½Ç½Ã°£ ºê¶ó¿ìÀú width °»½Å
            $(window).on('resize', function () {
                window_width = $(window).width();
                responSwiper();
            });

            //»ó´Ü »çÀÌÆ® ¸µÅ©
            $(".hd-top .link-wrap .link-btn").click(function() {
                $(".hd-top .link-list,.hd-top .link-btn").toggleClass("on")
            });

            //¸ð¹ÙÀÏ
            if ($(".depth2-wrp:not(.on)").length) {
                var posDP2Flag = false;
                var posDP2 = $(".depth2-wrp").offset().top;
                
                $(window).on('scroll', function(){
                    if($(window).scrollTop() > posDP2 && !posDP2Flag){
                        posDP2Flag = true;
                        $('#headertop .depth2-wrp > div').addClass('fixed');
                    
                    }
                    if ($(window).scrollTop() <= posDP2 && posDP2Flag){
                        posDP2Flag = false;
                        $('#headertop .depth2-wrp > div').removeClass('fixed');
                        // var index = $('.depth2-swiper .swiper-slide.on').index();
                        // depth2_swiper.slideTo(index, 0);
                    }
                });
            }
            
            
            var $depth3 = $(".depth3");
            $depth3.on('click', function(){
                var _this = $(this);
                var _thisDepth2Warp = _this.closest('.depth3-box')
                var _thisOtherDepth2 = _thisDepth2Warp.children('li:not(.depth3)');
                var _thisOtherDepth2_lng = _thisOtherDepth2.length;
        
                //Æì°í Á¢±â onÀ¸·Î Á¦¾î
                if(_this.hasClass('on')){
                    _this.removeClass('on');
                }else{
                    $depth3.removeClass('on');
                    _this.addClass('on');	
                    
                };

                // ¼­ºêµª½º ¾øÀ»¶§ depth3 ¿¡ on Áö¿ì±â
                if(_thisOtherDepth2_lng === 0){
                    $depth3.removeClass('on');
                }
                
        
                //Æí »óÅÂ¿¡¼­ ¸Þ´º °í¸£±â
                _thisDepth2Warp.on("click", "li:not(.depth3)", function() {
                    //´Ù½Ã Á¢°í °í¸¥¸Þ´º¸í ÃÖ»ó´ÜÀ¸·Î ±³Ã¼
                    $('.depth3').removeClass('on');
                    _thisDepth2Warp.children('.depth3').html($(this).html());
                    
                    //¸Þ´º Àç¼±ÅÃ½Ã on2Á¦¾î
                    _thisDepth2Warp.children('.depth3').addClass('on2')
                    _thisOtherDepth2.hide();
                });

                //¸Þ´º Àç¼±ÅÃ
                if(_this.hasClass('on2')){
                    _thisOtherDepth2.show();
                    _thisDepth2Warp.children('.depth3').removeClass('on2');
                    $('.depth3').addClass('on');

                    //¸Þ´º Áß »õÃ¢ÀÌµ¿ ÀÖÀ»°æ¿ì »õÃ¢ÀÌµ¿ ¸·À½ [ex. ±âÃâ¹®Á¦ > ÇØ¼³°­ÀÇÃÑÆò]
                    $('.depth3.on').find('a').attr('href',"javascript:void(0)");
                    $('.depth3.on').find('a').removeAttr('target');
                }
            });
            
            // ¿ÜºÎ¿µ¿ª Å¬¸¯ ½Ã ÆË¾÷ ´Ý±â
            $(document).mouseup(function (e){
                var LayerPopup = $(".depth3-box");
                if(LayerPopup.has(e.target).length === 0){
                    LayerPopup.find('.depth3').removeClass("on");
                }
            });        

            // $(window).on('load', function(){ //edge,chrome °£ÇæÀûÀ¸·Î ·Îµå ¾È µÊ
            var $target = $('.depth3-wrp').find('.depth3-box li.current');
            $target.closest('.depth3-box').find('.depth3 a').html($target.children('a').html());
            // })
            
            //$(window).on('load resize', function(){ //load°¡ Àû¿ë ¾È µÊ
            var activeSlide = $('.depth2-swiper .swiper-slide.on');
            var activeIndex = activeSlide.index();
            if (activeSlide.length) {
                if (depth2_swiper) {
                    depth2_swiper.slideTo(activeIndex, 0);
                }
                $('.depth3-wrp').css({marginTop : 50,})  
                $('.depth3-box').eq(activeIndex).show();
            }

            $(window).on('resize', function(){
                var activeSlide = $('.depth2-swiper .swiper-slide.on');
                var activeIndex = activeSlide.index();
                if (activeSlide.length) {
                    if (depth2_swiper) {
                        depth2_swiper.slideTo(activeIndex, 0);
                    }
                }
            });
            //});

            //full down menu
            $(document).ready(function () {                
                $('.depth2-swiper.swiper-container .swiper-slide a').on('mouseenter', function(){
                    $('.fulldown-wrp').fadeIn(200);
                })
                $('.fulldown-wrp').on('mouseleave', function(){
                    $('.fulldown-wrp').fadeOut(100);
                })
            });
        },
        tabUI: function(){
            // swiper ÅÇ ³Êºñ ÁöÁ¤
            var upperTab_total = $('.tab-upper .swiper-slide').length;
            var upperTab_width = 100 / upperTab_total;        
            var upperTab_mowidth = 100 / (upperTab_total/2);        

            // swiper pc ver µå·¡±ë x
            var tabUpper_swiper = undefined;
            var tabUpper_active_index = $('.tab-upper-swiper .swiper-slide.on').index()
            function responSwiper() {
                if ($(window).width() < 1024 && tabUpper_swiper === undefined) {
                    tabUpper_swiper = new Swiper(".tab-upper-swiper", {
                    slidesPerView: 'auto',
                    observer: true,
                    observeParents: true,
                    observeSlideChildren: true,
                    simulateTouch: true,     
                    scrollbar: {
                        el: '.tab-upper-swiper .swiper-scrollbar',
                        draggable: true,
                    },        
                });
                } else if ($(window).width() >= 1024 && tabUpper_swiper != undefined) {
                    tabUpper_swiper.destroy();
                    tabUpper_swiper = undefined;
                }
            }

            $(window).on('load resize', function () {
                responSwiper();

                if($(window).width() <= 768) { 		 
                    $('.tab-upper .swiper-slide').css('width', upperTab_mowidth + '%')  
                }else { 
                    $('.tab-upper .swiper-slide').css('width', upperTab_width + '%')  
                }              
            });

            // swiper È°¼ºÈ­ÁöÁ¡À¸·Î ÀÌµ¿
            $(window).on('load resize', function(){
                //var tabUpper_index = $('.tab-upper-swiper .swiper-slide').index(); //0
                var tabUpper_slide = $('.tab-upper-swiper .swiper-slide.on');
                var tabUpper_activeIndex = tabUpper_slide.index(); //5 0 1 2 3 4
                
                if(tabUpper_slide.length){
                    if (tabUpper_swiper) {
                        tabUpper_swiper.slideTo(tabUpper_activeIndex, 0)
                    }
                }                        
            });

            // »ó´Ü 7°³ ÅÇ
            var $tabUpper = $('.tab-upper .swiper-slide');
            var $tabSub = $(".tab-con .tab-subwrp .tab-sub li");

            $tabUpper.on('click', function(){
                if ($(this).closest('.tab-upper').hasClass('tab-codingmap')) {
                    return;
                }
                var _this = $(this);
                var tabIdx =_this.index();

                $tabUpper.removeClass("on")
                _this.addClass("on");
                if ($(window).width() <= 768) {
                    tabUpper_swiper.slideTo(tabIdx);
                }

                $(".tab-con .tab-subwrp").hide().removeClass("on");
                $(".tab-con .tab-subwrp").eq(tabIdx).show().addClass("on");
            });

            //!=-1 => 0ÀÏ¶§ Á¶°Ç¹® ½ÇÇà
            if ($('.tab-upper .swiper-slide.on').index() != -1) {
                $(".tab-con .tab-subwrp").hide().removeClass("on");
                $(".tab-con .tab-subwrp").eq($('.tab-upper .swiper-slide.on').index()).show().addClass("on");
            }

            // ÃÊ±â ·Îµå ½Ã ·ÎµåÇÒ ÄÁÅÙÃ÷ ¼³Á¤
            $(".tab-con .tab-subwrp .tab-sub").each(function(){
                var initNumber = ($(this).children('li.on').index())
                if (initNumber != -1) {
                    $(this).siblings('.tab-subcon').eq(initNumber).addClass('on');
                }
            });
            // ¼­ºêÅÇ Å¬¸¯ ½Ã ·ÎµåÇÒ ÄÁÅÙÃ÷ ¼³Á¤
            $(".tab-con .tab-subwrp .tab-sub li").on('click', function(){
                var index = $(this).index();
                $(this).addClass('on');
                $(this).siblings().removeClass('on');
                $(this).closest('.tab-sub').siblings('.tab-subcon.on').removeClass('on');
                $(this).closest('.tab-sub').siblings('.tab-subcon').eq(index).addClass('on');
            });
        },

        tabSerialUI: function(){
            // Á÷·ÄÅÇ ³Êºñ ÁöÁ¤ ex) 9±Þ, 7±ÞÅÇ
            var upperTab_total = $('.tab-series li').length;
            var upperTab_width = 100 / upperTab_total;        
            $('.tab-series li').css('width', upperTab_width + '%')              
            // $(window).on('load resize', function () {
            // });
        },

        selectUI: function(){
            $('.select-base select').each(function(){
                if ($(this).closest('.select-base').hasClass('disabled')) {
                    return true;
                }
                if ($(this).find('option:eq(0)').attr('value') === undefined) {
                    $(this).find('option:eq(0)').val(0);
                }
                if (parseInt($(this).val()) === 0) {
                    $(this).closest('.select-base').removeClass('active');
                } else {
                    $(this).closest('.select-base').addClass('active');
                }
                $(this).prev().html(function(){
                    return $(this).next().find("option:selected").text();
                })
            });
        },
        selectScript: function(){
            $(document).on('click', '.select-base', function(){
                console.log(1);
                $(this).closest('.choice-box').toggleClass("on");
            })
            $(document).on('blur', '.select-base', function(){
                $(this).closest('.choice-box').removeClass("on");
            });
            $(document).on('change', 'select', function(){
                if ($(this).closest('.select-base').hasClass('disabled')) {
                    return true;
                }
                if (parseInt($(this).val()) === 0) {
                    $(this).closest('.select-base').removeClass('active');
                } else {
                    $(this).closest('.select-base').addClass('active');
                }
                $(this).prev().html($(this).find("option:selected").text());
                setTimeout(function(){
                    $(this).closest('.choice-box').removeClass("on");
                }, 250);
            });
        }
    }
}());

$(function(){
    LUI.init();
});

$.urlParam = function(name){
    var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (results == null){
      return null;
    } else {
        return decodeURI(results[1]) || 0;
    }
}