function showForm()
{
    $('#overlay').fadeIn(
        400, function()
        {
            $('#modal_form-2')
            .css('display', 'block')
            .animate(
                {
                    opacity : 1,
                    top : '80px'
                }, 200
            );
        }
    );
    $(".scrollbar_").mCustomScrollbar();
}

function hideForm()
{
    $('#modal_form, #modal_form-2, #success-form').animate(
        {
            opacity : 0,
            top : 20
        }, 200, function()
        {
            $(this).css('display', 'none');
            $('#overlay').fadeOut(400);
        }
    );
}
$(
    function()
    {
        footer();
        search();
        phonesTab();
        openMobMenu();
        closeMobMenu();
        openSubMenuMob();
        homeSlider();
        hoverCatList();
        expandAllCatList();
//        loadCollection();
        tabs();
        var basket = new ArtboxBasket();
        if(device.mobile())
        {
            deviceCheck();
        } else if(device.tablet())
        {
            deviceCheck();
        }
        $('.gall-1').lightGallery();

        var width = $(window).width();

        function deviceCheck()
        {
            var meta = document.createElement('meta');
            meta.name = "viewport";
            meta.content = "width=device-width";
            document.getElementsByTagName('head')[0].appendChild(meta);
        }

        function footer()
        {
            footerBottom();
            resizeFooterBottom();

            function footerBottom()
            {
                var heightHeader = $('.section-box-header').height();
                var heightFooter = $('.section-box-footer').height();
                var windowHeight = $(window).height();
                $('.section-box-content>.container.shadow').css({minHeight : windowHeight - heightHeader - heightFooter - 3})
            }

            function resizeFooterBottom()
            {
                $(window).resize(
                    function()
                    {
                        footerBottom();
                    }
                )
            }
        }

        function mobOverlayAdd()
        {
            $('body').append('<div class="mob-overlay"></div>')
        }

        function mobOverlayRemove()
        {
            $('.mob-overlay').remove()
        }

        function search()
        {
            $('.search-btn').click(
                function()
                {

                    $('.hidden-search').addClass('vis')

                }
            );
            $('.close-search-btn').click(
                function()
                {
                    $('.hidden-search').removeClass('vis')
                }
            )

        }

        function phonesTab()
        {
            $('.phones-table-wr').click(
                function()
                {
                    $(this).toggleClass('vis-phone')
                }
            );

            var timerPhone;
            $('.phones-table-wr').hover(
                function()
                {
                    clearTimeout(timerPhone)
                }, function()
                {
                    timerPhone = setTimeout(
                        function()
                        {
                            $('.phones-table-wr').removeClass('vis-phone')
                        }, 320
                    )

                }
            )

        }

        function openMobMenu()
        {
            $('.menu_mob').click(
                function()
                {
                    mobOverlayAdd()
                    $('body').addClass('off-scroll')
                    $('.xs-sm-menu').addClass('open')
                    setTimeout(
                        function()
                        {
                            $('.xs-sm-menu').addClass('visible')
                        }, 10
                    )
                }
            )
        }

        function openSubMenuMob()
        {
            $('.xs-sm-menu ul.main-menu li a').click(
                function(e)
                {
                    var checkSubMEnu = $(this).parent().find('ul')
                    if(checkSubMEnu.length > 0)
                    {
                        e.preventDefault();
                        $(this).parent().toggleClass('show-sub-menu')
                    }
                }
            )
        }

        function closeMobMenu()
        {
            $('body').on(
                'click', '.mob-overlay', function()
                {
                    mobOverlayRemove()
                    if($('.xs-sm-menu').hasClass('visible'))
                    {
                        $('.xs-sm-menu').removeClass('visible')
                        setTimeout(
                            function()
                            {
                                $('.xs-sm-menu').removeClass('open')
                            }, 200
                        )
                        $('body').removeClass('off-scroll')
                    }
                    $('.xs-sm-menu .show-sub-menu').removeClass('show-sub-menu')

                }
            )
            $('.close-menu-mob').click(
                function()
                {
                    mobOverlayRemove()
                    $('.xs-sm-menu').removeClass('visible')
                    setTimeout(
                        function()
                        {
                            $('.xs-sm-menu').removeClass('open')
                        }, 200
                    )
                    $('body').removeClass('off-scroll')
                    $('.xs-sm-menu .show-sub-menu').removeClass('show-sub-menu')
                }
            )
        }

        function homeSlider()
        {
            var slideA = $('.slider-img a')
            var numA = slideA.length

            setInterval(
                function()
                {
                    var activeNumA = $('.slider-img a.active-slide').index() + 1;

                    if(activeNumA < numA)
                    {
                        $('.slider-img a').removeClass('active-slide')
                        $(slideA[activeNumA]).addClass('active-slide')
                    } else
                    {
                        $('.slider-img a').removeClass('active-slide')
                        $(slideA[0]).addClass('active-slide')
                    }
                }, 4000
            )
        }

        function hoverCatList()
        {
            $('.cat-list-wr').hover(
                function()
                {
                    $(this).addClass('hover')
                }, function()
                {
                    $(this).removeClass('hover')
                }
            )
        }

        function expandAllCatList()
        {
            $('.expand_brands').click(
                function()
                {
                    var oldTxt = 'развернуть';
                    var newTxt = 'свернуть';

                    if($(this).parents('.cat-list-wr').hasClass('expand'))
                    {
                        $('.cat-list-wr').removeClass('expand');
                        $(this).find('p').html(oldTxt);
                    } else
                    {
                        $('.cat-list-wr').removeClass('expand');
                        $('.expand_brands').find('p').html(oldTxt);
                        $(this).parents('.cat-list-wr').addClass('expand');

                        $(this).find('p').html(newTxt);
                    }

                }
            );
        }

        function removeLoadCollection()
        {
            $('.load_collection').animate(
                {opacity : 0}, 500, function()
                {
                    $('.load_collection').remove();
                }
            )
        }

        function loadCollection()
        {
            $('body').append('<div class="load_collection"><div></div></div>');
        }

        function tabs()
        {
            var tabsLi = $('.collect-tabs ul li')
            var tabsContent = $('.tabs-content')
            tabsContent.css({display : 'none'});
            $(tabsContent[0]).css({display : 'block'});
            $(tabsLi[0]).addClass('active-tab');
            tabsLi.click(
                function()
                {
                    tabsLi.removeClass('active-tab');
                    $(this).addClass('active-tab');
                    var thisIndex = $(this).index();
                    tabsContent.css({display : 'none'});
                    $(tabsContent[thisIndex]).css({display : 'block'});
                }
            )
        }

        $(document).on('pjax:send', function()
        {
            loadCollection();
        });

        $(document).on('pjax:complete', function()
        {
            removeLoadCollection();
        });

        $(document).on(
            'click', '.question-form', function(e)
            {
                e.preventDefault();
                showForm();
            }
        );

        $(document).on(
            'click', '.to_order', function(e)
            {
                e.preventDefault();
                var product_variant_id = $(this).data('id');
                basket.add(product_variant_id);
            }
        );

        $(document).on(
            'click', '._more_, #close-form, #overlay', function()
            {
                hideForm();
            }
        );

        $(document).on(
            'submit', '#basket_form', function(e)
            {
                e.preventDefault();
                hideForm();
                $.post(
                    '/order/make', $(this).serialize(), function(data)
                    {
                        basket.updateModal(data.message, false);
                        basket.init(false, true);
                        window.setTimeout(showForm, 1000);
                    }
                );
            }
        );

        $(document).on(
            'click', '.form-product-wr .remove_', function()
            {
                var product_variant_id = $(this).data('id');
                basket.remove(product_variant_id);
                var container = $(this).parents('table');
                var scroller = $(this).parents('.form-product-wr');
                var tr = $(this).parents('tr').first().remove();
                if($(container).find('tr').length === 1) {
                    $(scroller).removeClass('product_2');
                    $(scroller).removeClass('product_0');
                    $(scroller).addClass('product_1');
                } else if($(container).find('tr').length === 2) {
                    $(scroller).removeClass('product_1');
                    $(scroller).removeClass('product_0');
                    $(scroller).addClass('product_2');
                } else if($(container).find('tr').length === 0) {
                    $(scroller).removeClass('product_1');
                    $(scroller).removeClass('product_2');
                    $(scroller).addClass('product_0');
                } else {
                    $(scroller).removeClass('product_2');
                    $(scroller).removeClass('product_1');
                    $(scroller).removeClass('product_0');
                }
            }
        );
    }
);




