window.onload = function() {
    // script
};

$(function () {
    ("use strict");


    $('[data-menu="open"]').click(function(){
        // $('.layout-header .menu-overlay').toggleClass('open');
        $('.layout-header .menu').toggleClass('open');

        $('.layout-header .topbar').toggleClass('open');
    });
    $('[data-menu="close"]').click(function(){
        // $('.layout-header .menu-overlay').removeClass('open');
        $('.layout-header .menu').removeClass('open');
        
        $('.layout-header .topbar').removeClass('open');
    });
    // $('.layout-header .menu .link').click(function(){
    //     // $('.layout-header .menu-overlay').removeClass('open');
    //     $('.layout-header .menu').removeClass('open');
    // });


    $(window).scroll(function() {
        if ($(window).scrollTop() > 300) {
          $('#backtop').addClass('show');
        } else {
          $('#backtop').removeClass('show');
        }
    });
    $('#backtop').click(function(){
        $('html, body').animate({
            scrollTop: 0
        }, 800);
        return false;
    });
    

    $('#collapseSitemap').on('shown.bs.collapse', function (e) {
        $("html, body").animate({ scrollTop: $(document).height()-$(window).height() }, 400);
    }); 

    
    $('.select-control').select2({
        minimumResultsForSearch: Infinity,
        placeholder: "Select"
    });
});









// About > Company Profile (TH)
$(function() {
    if($("#timeline").length){
    let timeline = $("#timeline").offset().top;
    let timeline_end  = $("#timeline").offset().top + ($(".editor-content").height() - $('.layout-header').height());
    let timelineScroll = $(".ck-timeline .timeline-scroll");
    
    $(window).scroll(function() {
        var scroll = $(window).scrollTop();
        if (scroll >= timeline - $('.layout-header').height() && scroll <=  timeline_end ) {
            timelineScroll.addClass('active');
        } else {
            timelineScroll.removeClass('active');
        }
    });
  }

  });
  
    $(document).on("scroll", onScroll);
        $('a.moveScroll').on('click', function(e) {
            e.preventDefault();
            $(document).off("scroll");
            
            $('a.moveScroll').each(function () {
                $(this).removeClass('active');
            });
            
            $(this).addClass('active');
            
            let target = $(this.hash);
            let headerDevice = $('.layout-header').height();
            target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
            if (target.length) {
                $('html,body').animate({
                scrollTop: target.offset().top - headerDevice
                }, 800, 'swing', function () {
                    $(document).on("scroll", onScroll);
                });
            }
        });
    // })
  
    function onScroll(event){
        let scrollPos = $(document).scrollTop();
        $('a.moveScroll').each(function () {
            let currLink = $(this);
            let refElement = $(currLink.attr("href"));
            let headerDevice = $('.layout-header').height();
            let headerDefault = $('.default-header').height();
            let s1 = $('.section.s1').height();
            let s2 = $('.section.s2').height();
            let s3 = $('.section.s3').height();
            let sTop = $('.section .sTop').height();
            let h = (headerDevice)-(headerDevice+headerDefault+s1+s2+s3+sTop);
            // let h = (headerDevice)-1;
            if(refElement.length){
                if (refElement.position().top-h <= scrollPos && refElement.position().top-h + refElement.height() > scrollPos) {
                    $('a.moveScroll').removeClass("active");
                    currLink.addClass("active");
                    console.log(refElement);
                }
                else{
                    currLink.removeClass("active");
                }
            }
        });
    }