$(document).ready(function() {



    footerBottom();
    scrollUp();
    callMobile();
    modalForms();
    openMobMenuNew();
    closeMobMenu();
    phoneMask();
    catalogMenu();
    serviceTables();
    formsService();
    mobCatalogService();

    function footerBottom(){
        var heightHeader = document.getElementById('header_').offsetHeight
        var heightFooter = document.getElementById('footer_').offsetHeight
        var windowHeight = window.innerHeight
        document.getElementById('content_').style.minHeight = windowHeight-heightHeader-heightFooter+'px';
    }

    function mobOverlayAdd() {
        if($('body').find('.mob-overlay').length > 0) {
        } else {
            $('body').append('<div class="mob-overlay"></div>')
        }
        $('.mob-overlay').fadeIn(300);
    }

    function mobOverlayRemove() {
        $('.mob-overlay').fadeOut(300);
        $('body').removeClass('show-block-mobile');
        setTimeout(function () {
            $('body').removeClass('body-fixed');
        },510)

    }

    function openMobMenuNew() {
        $('.menu_mob').click(function () {
            mobOverlayAdd()
            $('body').addClass('body-fixed show-block-mobile');


        })
    }

    function closeCallMobileHide() {
        $('.call-mobile-wr').removeClass('visible')
        setTimeout(function () {
            $('.call-mobile-wr').removeClass('open')
        },200)
    }

    function closeMobMenu() {
        $('body').on('click','.mob-overlay',function () {
            mobOverlayRemove();
        })

        $('.close-mobile-menu').click(function () {
            mobOverlayRemove();
        })
    }


    function scrollUp() {
        $('.btn_up').click(function () {
            $('body, html').animate({scrollTop:0}, 500);
        })
        btnScroll()
        function btnScroll() {
            btnScrollPos()
            function btnScrollPos() {

                var containerPos = $('#content_ .container').offset().left
                // console.log($('.container').offset().left)
                $('.btn_up').css({right:containerPos})
            }

            btnScrollShowHide()
            function btnScrollShowHide() {
                var windowHeight = $(window).height()

                if($(this).scrollTop() < windowHeight)
                {
                    $('.btn_up').removeClass('visible')
                } else {
                    $('.btn_up').addClass('visible')
                }
                var scrpos = $(this).scrollTop()
                var documentHeight = $(document).height()
                var fHe = ($('.section-box-footer').height())-39
                var fPos = $('.section-box-footer').offset().top
                var docFooterHeight = documentHeight-fHe-windowHeight

                if(($(this).scrollTop())>=docFooterHeight){
                    $('.btn_up').css({bottom:(fHe+16)-((documentHeight-scrpos)-($(window).height()))})
                } else  {
                    $('.btn_up').css({bottom:16})
                }
            }

            $(window).resize(function () {
                btnScrollPos()
                btnScrollShowHide()
            })

            $(window).scroll(function () {
                btnScrollShowHide()
            })


            $('.btn_scroll').click(function () {
                $('body,html').animate( { scrollTop: 0 }, 500 );
            })

        }
    }

    function callMobile() { //new
        $('.btn_call').click(function() {
            $('.btn_call').addClass('close');
            $('.btn_call_close').addClass('visible');
            mobOverlayAdd();

            $('.call-mobile-wr').addClass('visible');
            setTimeout(
                function() {
                    $('.call-mobile-wr').addClass('open')
                }, 20
            )
        })

        $('.btn_call_close').click(function () {
            $(this).removeClass('visible');
            mobOverlayRemove();
            closeCallMobileHide();
            $('.btn_call') .removeClass('close');
            $('.call-mobile-wr').removeClass('open');
            $('body').removeClass('off-scroll');
        })

    }



    function phoneMask() {
        var phoneInput = '.phones_mask input'

        if($('body').find(phoneInput).length>0){
            $(phoneInput).mask('+38(000)000-00-00',{placeholder:'+38(0__)___-__-__'});
            $(phoneInput).focus(function () {
                if(($(this).val())== '') {$(this).val('+38(0')}
            })
            $(phoneInput).focusout(function () {
                var phoneVal = $(this).val()
                //if(phoneVal == '+38(0' || phoneVal == '+38(' || phoneVal == '+38' || phoneVal == '+3' || phoneVal == '+') {$(this).val('')}
                if(phoneVal.length <17) {$(this).val('')}
            })
        }
    }

    function catalogMenu() {

        $('.mobi-menu-catalog').children('ul').children('li')
            .each(function() {
                if ($(this)
                        .find('ul').length > 0) {
                    $(this)
                        .addClass('has-list');
                }
            })


        $('.mobi-menu-catalog .has-list a').click(function() {
                if ($(this).parent().hasClass('active-menu')) {
                    $(this).parent().removeClass('active-menu');
                } else {
                    $(this).parent().addClass('active-menu');
                }
            })
    }

    function serviceTables() {
        var status = true;
        $('body').on('click','.service-title',function () {
            var this_ = $(this);
            if(status) {
                status = false;
                var height = 0;
                var maxHeight = +(this_.parent().parent().find('table').height() + this_.parent().parent().height());
                this_.parent().parent().toggleClass('active')
                if(this_.parent().parent().hasClass('active')) {

                    var interval = setInterval(function () {
                        if(height<=maxHeight) {
                            height =  height+15;
                            this_.parent().parent().css({height: height + 'px'});

                        } else {
                            this_.parent().parent().css({height: maxHeight + 1 + 'px'});
                            clearInterval(interval);
                            status = true;
                        }
                    },1)


                } else {
                    var newHeight = this_.parent().parent().height();
                    var interval2 = setInterval(function () {
                        if(newHeight>=1) {
                            newHeight =  newHeight-20;
                            this_.parent().parent().css({height: newHeight + 'px'})

                        } else {
                            clearInterval(interval2);
                            this_.parent().parent().css({height:  this_.height() + 1 + 'px'});
                            status = true;
                        }
                    },1)
                }
            }
       })
    }

    
    $(window).resize(function () {
        // heightSliderCol();
        footerBottom();
        if(window.innerWidth>=992){
            mobOverlayRemove()
            $('#menu-mob-hidden').removeClass('visible')
            setTimeout(function () {
                $('#menu-mob-hidden').removeClass('opens')
            },200)
            $('body').removeClass('off-scroll')
            $('body').removeClass('hidden_scroll-y')

        }

        if(window.innerWidth <= 767) {
            $('.for-hidden-mob').mCustomScrollbar("destroy");
        } else {
            $('.for-hidden-mob').mCustomScrollbar(
                {
                    autoHideScrollbar: true,
                    theme: 'minimal'
                }
            );
        }

    })

    function modalForms() {
        $('body').on('click','.modal-link', function (e) {

            e.preventDefault();
            e.stopPropagation();


            mobOverlayRemove()
            $('#menu-mob-hidden').removeClass('visible')
            setTimeout(function () {
                $('#menu-mob-hidden').removeClass('opens')
            },200)
            $('body').removeClass('off-scroll')

            mobOverlayRemove()
            closeCallMobileHide()
            $('.btn_call').removeClass('close')
            $('.call-mobile-wr').removeClass('open')
            $('body').removeClass('off-scroll')


            var idForm = $(this).data('form');


            var pos = ($(window).scrollTop()) + 30;
            if($(this).hasClass('fixed-modal')){
                pos = 30;
                $('#'+idForm).css({position:'fixed'})
            }
            if($(this).hasClass('pos-top')){
                pos = 30;
            }
            $('#overlay').removeClass('overlay_cloned');
            $('#overlay').fadeIn(400,
                function(){
                    $('#'+idForm)
                        .css('display', 'block')
                        .animate({opacity: 1, top: pos}, 200);
                });
        })
    }

    closeForms();
    function closeForms() {
        $('#modal_close, #overlay').click( function(){
            $('.forms_, .basket_modal').animate({opacity: 0, top: '0'}, 200,function(){
                $(this).css('display', 'none');
                $(this).prev("#overlay").css('display', 'none');
                $('#overlay').fadeOut(400);
            });
            $('#success_form').animate({opacity: 0, top: '0'}, 200,function(){
                $(this).css('display', 'none');
                $(this).prev("#overlay").css('display', 'none');
                $(this).css({top:'50%'});
            });
        });
    }
    //после удачной отправки формы запускать success()
    // success()
    function success() {
        var pos = ($(window).scrollTop()) + 30;
        $('.forms_').animate({opacity: 0, top: '0'}, 200,function(){
            $(this).css('display', 'none');
        });
        setTimeout(function () {
            $('#overlay').fadeIn(400);
            $('#success_form').css('display', 'block').animate({opacity: 1, top: pos}, 700);
        },400)
    }

    $('body, html').animate({scrollTop:0},20,function () {

    });


    window.success = success;
    if($(window).width() >= 768) {
        animations();

    }
    function animations() {

        function animBox3() {
            if($('.section-box-3').length) {
                $('.section-box-3').addClass('anim')
                var scrollPos = $(window).scrollTop();
                var windHeight = $(window).height();
                var bl = $('.section-box-3');
                var pos = bl.offset().top;
                if(scrollPos >= (pos - (windHeight - 200 ))) {
                    bl.addClass('start')
                }
             }
        }


    function animBox4() {
        if($('.section-box-4').length) {
            $('.section-box-4').addClass('anim')
            var scrollPos = $(window).scrollTop();
            var windHeight = $(window).height();
            var bl = $('.section-box-4');
            var pos = bl.offset().top;
            if (scrollPos >= (pos - (windHeight - 200 ))) {
                bl.addClass('start');
                setTimeout(function () {
                    bl.addClass('start2');
                }, 1100)
                setTimeout(function () {
                    bl.addClass('start3');
                }, 1100 + 2500)
            }
        }
    }





    function animBox2() {
        if($('.section-box-2').length) {
            $('.section-box-2').addClass('anim');
            $('.categories-home .img').click(function () {
                var link = $(this).parent().find('.categories-home-links-title a').attr('href');
                document.location.href = link;
            });
            var scrollPos = $(window).scrollTop();
            var windHeight = $(window).height();
            var bl = $('.section-box-2');
            var pos = bl.offset().top;
            if(scrollPos >= (pos - (windHeight - 200 ))) {
                bl.addClass('start');
                setTimeout(function () {
                    bl.addClass('start2');
                },1100)
            }
         }
    }


        animBox3();
        animBox4();
        animBox2();

        $(window).scroll(function () {
            animBox3();
            animBox4();
            animBox2();
        })
    }

    showTextHome()
    function showTextHome() {
        $('.show-txt-mo span').click(function () {
            if($(this).parent().hasClass('hide-status')){
                $('.for-hidden-mob').removeClass('status-hide');
                $(this).parent().removeClass('hide-status');
                var text =  $(this).parent().data('langhide');
                $(this).find('b').html(text);
            } else {
                $('.for-hidden-mob').addClass('status-hide');
                $(this).parent().addClass('hide-status');
                var text =  $(this).parent().data('langshow');
                $(this).find('b').html(text);
            }
        })
    }

    function formsService() {
        $('body').on('click','.service-c-a-btns span',function () {
            $(this).parent().parent().find('.hidden-form-c-a-wr').removeClass('hidden')
        })

        $('body').on('click','.submit-close-c-a span',function () {
            if($(this).parent().hasClass('submit-close-c-a-page')){
                $(this).parents('.hidden-answer-comment-form').addClass('hidden')
            } else {
                $(this).parents('.hidden-form-c-a-wr').addClass('hidden')
            }

        });

        $('body').on('click','.add-answer-forms span',function () {
            $(this).parent().parent().find('.hidden-answer-comment-form').removeClass('hidden')
        })
    }

    function mobCatalogService() {
        $('body').on('click','.all-catalog-mob',function () {
            $(this).parent().toggleClass('active');
        })
    }


    // $('nav').addClass('hide_');
    // $('body').addClass('start-preloader');



    $(document).on('submit', '#total_question_form, #total_comment_form', function(e) {
       e.preventDefault();
       var form = $(this);
       var id = form.attr('id');
       var url = form.attr('action');
       if (id == 'total_question_form'){
           dataLayer.push({'event': 'formsend_question'});
           console.log('formsend_question');
       }else{
           dataLayer.push({'event': 'formsend_review'});
           console.log('formsend_review');
       }
        $.post(
            $(this).attr("action"), $(this).serialize(), function(data) {
                document.getElementById(id).reset();
                form.find('.submit-close-c-a span').click();
                $(".add-answer-forms").after("<p class='success-message'>"+data.message+"</p>");
                var dat = form.data('yiiActiveForm');
                dat.validate = false;
            }).fail(function(){

        });
    });

    $(document).on('change', '#blog-categories', function(e) {
        var value = $(this).val();
        if (value !== ''){
            window.location.href = value;
        }else{
            window.location.href = 'blog/index';
        }
    });

    $(document).on('submit', '#callback-form', function(e) {
        e.preventDefault();
        e.stopImmediatePropagation();
        $.post($(this)
            .attr("action"), $(this)
            .serialize(), function(data) {
            document.getElementById("callback-form")
                    .reset();
            var data_ = $("#callback-form")
                .data('yiiActiveForm');
            data.validated = false;
            success()
        })
         .fail();
    });
});


jQuery.fn.load = function(callback) {
  $(window).on("load", callback)
};

window.onload = function () {
    // console.log('start onload');
    //
    //  $('body').addClass('end-preloader')
    //  $('nav').addClass('start')
    //  $('body').addClass('disabled-preloader')

    //альтернативный с задержкой выключения
//    setTimeout(function () {
//        $('body').addClass('end-preloader')
//    },2700);
//
//    setTimeout(function () {
//        console.log('start');
//        $('nav').addClass('start');
//        $('body').addClass('disabled-preloader')
//    },2700 + 2000);


};

