$(window).on('load', function() {
  $('body').removeClass('_load');

  $('.alarm_list_box').mCustomScrollbar({
      autoHideScrollbar: false,
  });
});

function commonInit(){
  try {
    textEllipsis();
    familysite();
    menucommon();
    headerTop();
    fn_topBtn();
    act_search();
    fn_floatingWriter();
    $(window).scroll(headerTop);

  } catch (error) {
    console.error(error);
    $('body').removeClass('_load');

  }


}

// function fn_topBtn(){
//   if($('.top_btn').length > 0){
//     $( '.top_btn button' ).click( function() {
//       $( 'html, body' ).animate( { scrollTop : 0 }, 400 );
//         return false;
//     });
//   }
// }
//
function headerTop(){
    var st     = $(this).scrollTop();
    var w      = $(this).outerWidth();
    var h      = $('#header').outerHeight() / 2 ;

    if(st > h ){
        $('#header').addClass('_fixed');
    }else{
        $('#header').removeClass('_fixed').removeAttr('style');
    }
}
var topBtnTimer;
function fn_topBtn() {
	if($('.top_btn').length <= 0) return;
	$( window ).scroll( function() {
        // �곕줈��留�
        if(!topBtnTimer) {
            // �ㅼ젙�쒓컙�� 留욎떠 timer 珥덇린��
            topBtnTimer = setTimeout(function() {
                topBtnTimer = null;
                hasScrolled();
            }, 250);
        }



        var scrollBottom = $(document).height() - window.innerHeight - $(window).scrollTop();;
		var getBottomH = $('#footer').innerHeight();
		var winW = window.innerWidth;
        var marginB = 30;

		if($('.main_contents9').length != 0) {
            if(winW > 1024) {
                getBottomH += $('.main_contents9').innerHeight();
            }
        }
        var filter = "win16|win32|win64|mac|macintel";
        if ( navigator.platform ) {
             if ( filter.indexOf( navigator.platform.toLowerCase() ) < 0 ) {
                 marginB -= 30;
                 getBottomH -= 150;
                 scrollBottom = $(document).height() - screen.availHeight - $(window).scrollTop();
             }
         }
		if ( scrollBottom < getBottomH ) {
            if ( navigator.platform ) {
                 if ( filter.indexOf( navigator.platform.toLowerCase() ) < 0 ) {
                     getBottomH += 80;
                 }
             }
			$( '.top_btn button' ).css({
				bottom: (getBottomH + marginB) - scrollBottom + 'px'
			});
		}else{
			$( '.top_btn button' ).attr('style','');
		}
	});

    var lastScrollTop = 0;
    var delta = 5;
    function hasScrolled() {
        var st = $(this).scrollTop();

        if(Math.abs(lastScrollTop - st) <= delta) return;
        if (st > lastScrollTop){
            $( '.top_btn' ).stop().fadeIn(300);
        } else {
            // Scroll Up
            if(st + $(window).height() < $(document).height()) {
                $( '.top_btn' ).stop().fadeOut(300);
            }
        }
        lastScrollTop = st;

    }

	$('.top_btn button').click( function() {
		$( 'html, body' ).stop().animate( { scrollTop : 0 }, 400 );
		return false;
	});

}


var fWriterTimer;
function fn_floatingWriter() {
    if($('.writer_btn').length <= 0) return;

    var scrollBottom = $(document).height() - window.innerHeight - $(window).scrollTop();
    var getBottomH = $('#footer').innerHeight();
    var getTopBtnH = $('.top_btn button').height() + 8;
    var winW = window.innerWidth;
    var marginB = 30;

    var filter = "win16|win32|win64|mac|macintel";

    hasWriterScrolled();


    $( window ).scroll( function() {
        // �곕줈��留�
        if(!fWriterTimer) {
            // �ㅼ젙�쒓컙�� 留욎떠 timer 珥덇린��
            fWriterTimer = setTimeout(function() {
                fWriterTimer = null;
                hasWriterScrolled();
            }, 250);
        }

        scrollBottom = $(document).height() - window.innerHeight - $(window).scrollTop();
        getBottomH = $('#footer').innerHeight();
        getTopBtnH = $('.top_btn button').height() + 8;
        winW = window.innerWidth;
        marginB = 30;




        if ( navigator.platform ) {
             if ( filter.indexOf( navigator.platform.toLowerCase() ) < 0 ) {
                 marginB -= 30;
                 getBottomH -= 150;
                 scrollBottom = $(document).height() - screen.availHeight - $(window).scrollTop();
             }
         }
        if ( scrollBottom < getBottomH ) {
            if ( navigator.platform ) {
                 if ( filter.indexOf( navigator.platform.toLowerCase() ) < 0 ) {
                     getBottomH += 80;
                 }
             }
            $( '.writer_btn button' ).css({
                bottom: (getBottomH + marginB + getTopBtnH) - scrollBottom + 'px'
            });
        }else{
            $( '.writer_btn button' ).attr('style','');
        }
    });

    function hasWriterScrolled() {
        var st = $(this).scrollTop();

        if ( navigator.platform ) {
             if ( filter.indexOf( navigator.platform.toLowerCase() ) < 0 ) {
                 marginB -= 30;
                 getBottomH -= 150;
                 scrollBottom = $(document).height() - screen.availHeight - $(window).scrollTop();
             }
         }
         if(scrollBottom < getBottomH ) {
             if ( navigator.platform ) {
                  if ( filter.indexOf( navigator.platform.toLowerCase() ) < 0 ) {
                      getBottomH += 80;
                  }
              }
              $('.writer_btn').fadeOut(300);
         }else {
             $( '.writer_btn' ).fadeIn(300);
             lastScrollTop = st;
         }

    }
}



function isNumber(input) {
	var chars = "1234567890";
	return containsCharsOnly(input,chars);
}

function containsCharsOnly(input,chars) {

	for (var inx = 0; inx < input.value.length; inx++) {

		if (chars.indexOf(input.value.charAt(inx)) == -1) {
			if(chars == "1234567890"){
				alert("�レ옄留� �낅젰 媛��ν빀�덈떎.");
			}else{
				alert(chars + "留� �낅젰 媛��ν빀�덈떎.");
			}
			input.value = "";

			input.focus();
			return false;
		}
	}
	return true;
}



function textEllipsis(){

  $('.ellip_1').ellipsis({
    lines: 1,
    ellipClass: 'ellip',
    responsive: true
  });

  $('.ellip_2').ellipsis({
    lines: 2,
    ellipClass: 'ellip',
    responsive: true
  });

  $('.ellip_3').ellipsis({
    lines: 3,
    ellipClass: 'ellip',
    responsive: true
  });

}

function familysite(){
  $('.family_site_box a').each(function(){
    if( $(this).find('.sapn_wrap').length < 1){
      $(this).html('<span class="sapn_wrap">'+$(this).html()+'</span>');
    }
  });


  $('.family_site_box').off('click');
  $('.family_site_box').on('click', '.fs_btn', function(){
      $('.family_site_box').find('ul').slideToggle(200)
  });
}


function popupOpen(tg){

    scrollDisableSt2();
    $(tg).fadeIn(200);
    if($('.vb_bg').length >= 1) $('.vb_bg').remove();
    $(tg).before('<div class="vb_bg"></div>');
}
function popupClose(idNmae){
    $(idNmae).fadeOut(200);
    $('.vb_bg').remove();
    scrollAbleSt2();
}

function getScrollBarWidth () {//ScrollBarWidth
  return window.innerWidth - document.documentElement.clientWidth;
};

function handler(e){ //�대깽�� 留됯린
  e.preventDefault();
}

function scrollDisable(){ //scrollDisable on
  $('body').css( 'right', getScrollBarWidth() + 'px' ).addClass('scrollDisable')

  document.body.addEventListener('touchmove',   handler, {passive: false});
  document.body.addEventListener('mousewheel',  handler, {passive: false});
  document.body.addEventListener('scroll',      handler, {passive: false});
}

function scrollAble(){ //scrollDisable off
  $('body').removeAttr('style').removeClass('scrollDisable');
  document.body.removeEventListener('touchmove',  handler, {passive: true});
  document.body.removeEventListener('mousewheel', handler, {passive: true});
  document.body.removeEventListener('scroll',     handler, {passive: true});
}

function scrollDisableSt2(){ //scrollDisable on
  document.getElementsByTagName('body')[0].style.overflow = 'hidden';
}

function scrollAbleSt2(){ //scrollDisable off
  document.getElementsByTagName('body')[0].removeAttribute('style');
}


//寃��됰쾭��
function act_search() {
  var searchState = true;

  $(document).on('click','.act_search',function(){
      if(searchState){
          $('.search_box').slideDown();
          $('.act_search .icon_search').addClass('icon_cloes');
          $('.act_search .icon_search').removeClass('icon_search');
        }else {
          $('.search_box').slideUp();
          $('.act_search .icon_cloes').addClass('icon_search');
          $('.act_search .icon_cloes').removeClass('icon_cloes');
        }
      searchState = !searchState;
  });
}

function menucommon(){

  $(function(){
    act_allMenuBtn();
    AllMenu();
  })

  var gnb = $('.gnb');
  var depth_1 = $('.depth_1');
  var header = $('#header');
  var menu_box = $('.menu_box').html();
  var allBtnOnOff= true;
  var allbg;
  var model;
  var delta = 150;
  var timer = null;


  //  怨듯넻
  function act_allMenuBtn(){
    $('.act_menu').click(function(){

        AllmenuSize();
        $(this).toggleClass('menu_btn');
        $('.all_menu_bg').toggleClass('on');
      if(allBtnOnOff){
        scrollDisableSt2();//common.js
      }else{
        scrollAbleSt2();//common.js
      }

      if(model === 'pc'){
          $('.all_menu_bg').slideToggle();
          mobileUnEvent();
      }
      if(model === 'mo'){
          $('.all_menu_wrap .depth_1').find($('.depth2_box')).hide();
          $('.all_menu_bg').fadeIn();
          scrollDisableSt2();//common.js
          act_mobileAllMenu();

      }
      allBtnOnOff = !allBtnOnOff;
    });
  }

  function act_mobileAllMenu(){
    $('.mo_box').off('click').on('click', function(){

      mobileMenuEvent();
    });
    $('.all_menu_bg.on').off('click').on('click', function(e) {
        mobileMenuEvent();
    })


    function mobileMenuEvent(){
      $('.all_menu_bg').fadeOut(function() {
          $('.all_menu_bg').removeClass('on');
          $('.depth_1').removeClass('on');
      });
      $('.act_menu').removeClass('menu_btn');


      console.log($('.all_menu_bg').css('display'));
      // setTimeout(function(){
      //   $('.all_menu_bg').removeClass('on');
      //   $('.depth_1').removeClass('on');
      //   $('.all_menu_bg').css('display') == 'block' || $('.all_menu_bg').css('opacity') != 1 ? $('.all_menu_bg').addClass('on') : '';
      //
      // },500);


      scrollAbleSt2();
      allBtnOnOff = true;
    }
  }



  function AllMenu(){
    $(window).trigger('resize');
    var menuHtml = gnb.html();
    var allbox = '<div class="all_menu_bg"><div class="all_menu_wrap"><div class="mo_nav_head"><span class="mo_nav_log"><img src="../common/images/common/logo_type02.png"></span><div class="menu_box mo_box">'+menu_box+'</div></div>'+menuHtml+'</div>';
    $('#header').append(allbox);
    AllmenuSize();
  }

  function AllmenuSize(){
    var headerHeight = header.height();
    var windowHiehgt = $(window).innerHeight();
    var allBgHeight  = windowHiehgt - headerHeight;
        allbg = $('.all_menu_bg');
        allbg.height(allBgHeight);
  }



  // pc 留덉슦�� �대깽��
  function pcMousEvent(){
    $('.depth_1').on('mouseover',function(){
        $(this).addClass('on');
    });
    $('.depth_1').on('mouseout',function(){
        depth_1.removeClass('on');
    });
  }




  function mobileEvent(){
    $('.mo_all').on('click',function(e){
        e.stopPropagation();
        var depthTarget = $(e.target);

        if(depthTarget.is('a')){
          menu('.depth_1','.depth2_box');
          depthTarget.parents('.depth_1').find('.depth_3').hide();
          depthTarget.parents('.depth_1').find('.dept_3_li').removeClass('on');
        }

        if(depthTarget.is('.dept_3_li span')){
            menu('.dept_3_li','.depth_3');
        }

        function menu(parent,find){
            depthTarget.parents(parent).find(find).slideToggle(200);
            depthTarget.parents(parent).toggleClass('on');
            depthTarget.parents(parent).siblings().find(find).slideUp(200);
            depthTarget.parents(parent).siblings().removeClass('on');
        }
    });
  }


  //mo�먯꽌 pc �대깽�� 諛⑹�
    function pcUnEvent(e){
      $('.depth_1 > a[href],.dept_3_li > a[href]').click(function(e){
        e.preventDefault();
      })
      $('.all_menu_wrap').addClass('mo_all');
      $('.depth_1').removeClass('on');
      $('.all_menu_wrap .depth_1').find($('.depth2_box')).hide();
    }


//pc�먯꽌 紐⑤컮�� �대깽�� 諛⑹�
  function mobileUnEvent(){
    $('.depth_1 > a[href],.dept_3_li > a[href]').unbind('click');
    $('.all_menu_wrap').removeClass('mo_all');
    $('.all_menu_wrap .depth_1').children().show();
    $('.all_menu_wrap .dept_3_li').children().show();
  }




  $(window).resize(function(){
    // var  windowWidth =  $(window).innerWidth();
    var windowWidth = window.innerWidth;
        clearTimeout( timer );
        timer = setTimeout( function(){
          $('.depth_1').off('mouseover');
          $('.mo_all').off('click');
              if(windowWidth > 1024){
                  model= 'pc';
                  mobileUnEvent();
                  pcMousEvent();
                  if(model === 'pc'){return;}
              }else  {
                  model= 'mo';
                  pcUnEvent();
                  mobileEvent();
                  act_mobileAllMenu();
                  if(model === 'pc'){return}
              }
        }, delta );

});


}

// 鍮꾨�踰덊샇 �좏슚�깆껜��
function CheckPassWord(ObjUserPassWord) {

	//alert(ObjUserPassWord.length);

	if(ObjUserPassWord.length < 8) {
		alert("鍮꾨�踰덊샇�� 臾몄옄, �レ옄, �뱀닔臾몄옄 議고빀�쇰줈 8�먮━ �댁긽�쇰줈 �낅젰�댁<�몄슂.");
		return false;
	}



	if(ObjUserPassWord.length > 12) {
		alert("鍮꾨�踰덊샇�� 臾몄옄, �レ옄, �뱀닔臾몄옄 議고빀�쇰줈 20 �먮━ �댄븯濡� �낅젰�댁<�몄슂.");
		return false;
	}

	var icnt = 0;
	var scnt = 0;

	for(var i=0; i < ObjUserPassWord.length; i++) {
		ch = ObjUserPassWord.charCodeAt(i);
		// charCodeAt �쇰줈 諛쏆� �꾩뒪�� 肄붾뱶媛믪쑝濡� �� �쒕� 諛뷀깢�쇰줈 踰붿쐞 �ㅼ젙
		// �レ옄�대㈃ 移댁슫�몃� �щ┝
		if(ch > 47 && ch < 58) {
			icnt = icnt + 1;
		}
		// 臾몄옄�대㈃ 移댁슫�몃� �щ┝
		if((ch > 64 && ch < 91) || (ch > 96 && ch <123)) {
			scnt = scnt + 1;
		}
	}



	var special_pattern = /[`~!@#$%^&*|\\\'\";:\/?]/gi;
	if( special_pattern.test(ObjUserPassWord) == true ){
		//
	} else {
		alert('�섎굹 �댁긽�� �뱀닔臾몄옄媛� �ы븿�섏뼱 �덉뼱�� �⑸땲��.');
		return false;
	}

	if(icnt >= 1 && scnt >= 1) {
	}else {
		// �レ옄�� 臾몄옄媛� �④퍡 �ㅼ뼱媛�吏� �딆븯�� �� 泥섎━遺�遺�
		alert("鍮꾨�踰덊샇�� 臾몄옄, �レ옄, �뱀닔臾몄옄 議고빀�쇰줈 8~12�먮━濡� �낅젰�댁<�몄슂.");
		return false;
	}

	var SamePass_0 = 0; //�숈씪臾몄옄 移댁슫��
	var SamePass_1 = 0; //�곗냽��(+) 移댁슫��
	var SamePass_2 = 0; //�곗냽��(-) 移댁슫��

	var chr_pass_0;
	var chr_pass_1;

	for(var i=0; i < ObjUserPassWord.length; i++) {
		chr_pass_0 = ObjUserPassWord.charAt(i);
		chr_pass_1 = ObjUserPassWord.charAt(i+1);

		//�숈씪臾몄옄 移댁슫��
		if(chr_pass_0 == chr_pass_1) {
			SamePass_0 = SamePass_0 + 1
		}

		//�곗냽��(+) 移댁슫��
		if(chr_pass_0.charCodeAt(0) - chr_pass_1.charCodeAt(0) == 1) {
			SamePass_1 = SamePass_1 + 1
		}

		//�곗냽��(-) 移댁슫��
		if(chr_pass_0.charCodeAt(0) - chr_pass_1.charCodeAt(0) == -1) {
			SamePass_2 = SamePass_2 + 1
		}
	}

	if(SamePass_0 > 4) {
		alert("�숈씪臾몄옄瑜� 4踰� �댁긽 �ъ슜�� �� �놁뒿�덈떎.");
		return false;
	}

	if(SamePass_1 > 4 || SamePass_2 > 2 ) {
		alert("�곗냽�� 臾몄옄��(123 �먮뒗 321, abc, cba ��)�� 3�� �댁긽 �ъ슜 �� �� �놁뒿�덈떎.");
		return false;
	}
	return true;
}