
var pagew = $(document).outerWidth(true) + (window.innerWidth - document.body.clientWidth);
var pageh = $(window).innerHeight();


try {
    var _vstop = $(document).scrollTop();

    var _p2 = $(".page2").offset().top;
    var _p2_h = _p2 + $(".page2").innerHeight();

    var _p3 = $(".page3").offset().top;
    var _p3_h = _p3 + $(".page3").innerHeight();

    $(window).scroll(function () {
        _vstop = $(document).scrollTop();
        animate_fun(_vstop);
        var _left = $(document).scrollLeft()
        $(".wrap").css({ left: -_left })
    })
    animate_fun(_vstop);
}
catch (e) { console.log(e); }


function animate_fun(_vstop) {
    console.log(_vstop + "===" +  _p2 + "====" + _p3 + "====" + pageh);
    console.log(_p2_h + "====" + _p3_h + "====" + pageh + '111');
    _vstop = _vstop + pageh;
    // console.log(_vstop <= _p1_h, _vstop, _p1_h, "_vstop >= 0 && _vstop <= _p1_h")
    if (_vstop >= _p2/4 && _vstop <= _p2_h) {
        if ($(".page2").attr("is") != "1") {

            $(".p2c_l p.p1").stop(true, true).velocity("transition.slideLeftIn", { duration: 650 });
            setTimeout(function () {
                $(".p2c_l p.p2").stop(true, true).velocity("transition.slideLeftIn", { duration: 650 });
            }, 300);

            $(".p2c_r").addClass("on");

            $(".p2c_c .p2cc_c ul li label span.sp1").each(function () {
                countup($(this));
            });

            $(".p2ccr_c p.p3 a").addClass("on");

            $(".p2ccr_c p.p1").stop(true, true).velocity("transition.slideLeftIn", { duration: 650 });
            $(".p2ccr_c p.p2").stop(true, true).velocity("transition.slideUpIn", { duration: 650 });
            $(".p2ccr_c p.p3").stop(true, true).velocity("transition.slideUpIn", { duration: 650 });


            $(".page2").attr({ "is": "1" });
        }
    }


    if (_vstop >= _p3/4 && _vstop <= _p3_h) {
        if ($(".page3").attr("is") != "1") {
            $(".p3c_l p.p1").stop(true, true).velocity("transition.slideLeftIn", { duration: 650 });
            setTimeout(function () {
                $(".p3c_l p.p2").stop(true, true).velocity("transition.slideLeftIn", { duration: 650 });
            }, 300);
            $(".page3").attr({ "is": "1" });
        }
    }

    if (isMobile() || $(".page2").height() < pageh) {

        if ($(".page2").attr("is") != "1") {

            $(".p2c_l p.p1").stop(true, true).velocity("transition.slideLeftIn", { duration: 650 });
            setTimeout(function () {
                $(".p2c_l p.p2").stop(true, true).velocity("transition.slideLeftIn", { duration: 650 });
            }, 300);

            $(".p2c_r").addClass("on");

            $(".p2c_c .p2cc_c ul li label span.sp1").each(function () {
                countup($(this));
            });

            $(".p2ccr_c p.p3 a").addClass("on");

            $(".p2ccr_c p.p1").stop(true, true).velocity("transition.slideLeftIn", { duration: 650 });
            $(".p2ccr_c p.p2").stop(true, true).velocity("transition.slideUpIn", { duration: 650 });
            $(".p2ccr_c p.p3").stop(true, true).velocity("transition.slideUpIn", { duration: 650 });


            $(".page2").attr({ "is": "1" });
        }

        if ($(".page3").attr("is") != "1") {
            $(".p3c_l p.p1").stop(true, true).velocity("transition.slideLeftIn", { duration: 650 });
            setTimeout(function () {
                $(".p3c_l p.p2").stop(true, true).velocity("transition.slideLeftIn", { duration: 650 });
            }, 300);
            $(".page3").attr({ "is": "1" });
        }
    }

}


function isMobile() {
    let userAgentInfo = navigator.userAgent;
    let Agents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad', 'iPod'];
    let getArr = Agents.filter(i => userAgentInfo.includes(i));
    return getArr.length ? true : false;
}


function countup(_obj) {
    try {
        var settings = {
            'time': 800,
            'delay': 10
        };

        var $this = _obj;
        var $settings = settings;
        if ($this.attr("data-num") != "") {
            $this.data('counterupTo', $this.attr("data-num"));
        }
        if (!$this.data('counterupTo')) {
            $this.data('counterupTo', $this.text());
        }
        var time = parseInt($this.data("counter-time")) > 0 ? parseInt($this.data("counter-time")) : $settings.time;
        var delay = parseInt($this.data("counter-delay")) > 0 ? parseInt($this.data("counter-delay")) : $settings.delay;
        var divisions = time / delay;
        var num = $this.data('counterupTo');
        var nums = [num];
        var isComma = /[0-9]+,[0-9]+/.test(num);
        num = num.replace(/,/g, '');
        var isInt = /^[0-9]+$/.test(num);
        var isFloat = /^[0-9]+\.[0-9]+$/.test(num);
        var decimalPlaces = isFloat ? (num.split('.')[1] || []).length : 0;

        for (var i = divisions; i >= 1; i--) {

            var newNum = parseInt(Math.round(num / divisions * i));

            if (isFloat) {
                newNum = parseFloat(num / divisions * i).toFixed(decimalPlaces);
            }
            if (isComma) {
                while (/(\d+)(\d{3})/.test(newNum.toString())) {
                    newNum = newNum.toString().replace(/(\d+)(\d{3})/, '$1' + ',' + '$2');
                }
            }

            nums.unshift(newNum);
        }

        $this.data('counterup-nums', nums);
        $this.text('0');

        var f = function () {
            try {
                if ($this.data('counterup-nums') != "") {
                    $this.text($this.data('counterup-nums').shift());
                }

                if ($this.data('counterup-nums').length) {
                    setTimeout($this.data('counterup-func'), delay);
                } else {
                    delete $this.data('counterup-nums');
                    $this.data('counterup-nums', null);
                    $this.data('counterup-func', null);
                }
            }
            catch (e) { }
        };
        $this.data('counterup-func', f);

        setTimeout($this.data('counterup-func'), delay);
    }
    catch (e) { }
}