$(document).ready(function(){
    //var basket = new ArtboxBasket();
    footerBottom();
    slidersItemsPositionFix();
    callMobile();
    openMobMenu();
    closeCallMobileHide();
    closeMobMenu();
    addArrowMobMenuAndOpen();
    modalForms();
    popularRequest();
    hoverMainMenu();
    seoTxt();
    scrollUp();
    headerPhonesAndMap();
    disabledTagsCatalog();
    sortList();
    pagination();
    filters();
     priceSlider();
    priceSliderFixInputTxt();
    menuAllPage();
    filtersShowMobil();
    searchShow();
    openCardParams();
    openComments();
    checkedFilter();
    phoneMask();
    blogListTags();
    scrollToCommentsWrite();
    basketTabs();
    basketHiddenRadio();

    footerBottom();
    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() {
        $('body').append('<div class="mob-overlay visible-xs visible-sm"></div>')
    }
    function mobOverlayRemove() {
        $('.mob-overlay').remove()
    }

    function openMobMenu() {
        $('.menu_mob').click(function () {
            mobOverlayAdd()
            $('body').addClass('off-scroll')
            $('#menu-mob-hidden').addClass('opens')
            setTimeout(function () {
                $('#menu-mob-hidden').addClass('visible')
            },10)
        })
    }

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

    function closeMobMenu() {
        $('body').on('click','.mob-overlay',function () {
            mobOverlayRemove()
            if($('#menu-mob-hidden').hasClass('visible')) {
                $('#menu-mob-hidden').removeClass('visible')
                setTimeout(function () {
                    $('#menu-mob-hidden').removeClass('opens')
                },200)
                $('body').removeClass('off-scroll')
            }
            if($('.btn_call').hasClass('close')) {
                $('.btn_call').removeClass('close')
                closeCallMobileHide()
            }

        })
        $('.close-menu-mob .close_mob').click(function () {
            mobOverlayRemove()
            $('#menu-mob-hidden').removeClass('visible')
            setTimeout(function () {
                $('#menu-mob-hidden').removeClass('opens')
            },200)
            $('body').removeClass('off-scroll')
            $('#menu-mob-hidden ul li.has_ul').removeClass('open')
        })
    }

    function addArrowMobMenuAndOpen() {
        $('#menu-mob-hidden ul li').each(function () {
            if($(this).find('ul').length>0){
                $(this).addClass('has_ul')
                $(this).parent().addClass('sub_m')
                $('#menu-mob-hidden>ul').removeClass('sub_m').addClass('sub_m-first');
            }
        })


        
            $('body').on('click','#menu-mob-hidden ul li.has_ul a',function () {
                if($(this).parent().parent().hasClass('sub_m')){  //если li элемент вложженый
                    if($(this).parent().hasClass('open')){
                        $(this).parent().removeClass('open')
                    } else {
                        $('.sub_m').find('.has_ul').removeClass('open')
                        $(this).parent().addClass('open')
                    }
                }

                if($(this).parent().parent().hasClass('sub_m-first')) {  //если ли родительский открываем/закрываем
                    if($(this).parent().hasClass('open')) {
                        $(this).parent().removeClass('open')
                        $(this).parent().find('.open').removeClass('open')
                    } else {
                        $(this).parent().addClass('open')
                    }

                }

        })



    }


    
    var scrollPos = 0;
    var posY = 0;
    scrollItemsPos()
    function scrollItemsPos() {
      scrollPos = $(this).scrollTop()
        // console.log(scrollPos)
    }

    $(window).scroll(function () {
        scrollItemsPos()
        $('.items_.cloned').css({top:posY-scrollPos})
    })



    function slidersItemsPositionFix() {
        $('.items_.clone').remove()

        $('.sliders .items_').mouseover(function () {
            $('.items_.clone').remove()
            $('#overlay').addClass('overlay_cloned')
            if(($(window).width())>=768) {

                var width = $(this).width();
                var posX = $(this).offset().left;
                posY = $(this).offset().top;
                var this_ = $(this);
                this_.clone().appendTo('body').addClass('cloned no-transition').css({
                    width:width + 29.5,
                    top:posY - scrollPos,
                    left:posX
                })
            }
        })

        $('#overlay').hover(function () {
            $('.items_.cloned').remove();
            $(this).removeClass('overlay_cloned')
        }, function () {
            //console.log('leave hover')
        })
    }




    window.onresize = function () {
        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')
            $('.mob_filters_wr').attr('style','')
        }
        hoverMainMenu()
    }



    function callMobile() {
        $('.btn_call').click(function() {

            if(!($(this).hasClass('close'))){
                $('.btn_call').addClass('close')
                mobOverlayAdd()

                $('.call-mobile-wr').addClass('visible')
                setTimeout(
                    function() {
                        $('.call-mobile-wr').addClass('open')
                    }, 20
                )
            } else {
                mobOverlayRemove()
                closeCallMobileHide()
                $('.btn_call')
                    .removeClass('close')
                $('.call-mobile-wr')
                    .removeClass('open')
                $('body')
                    .removeClass('off-scroll')
            }

        })

    }




    function popularRequest() {
        $('.popular-requests span, .popular-requests a').click(function () {
            var thisRequest = $(this).html();
            $(this).parent().parent().find('form input').val(thisRequest);
            $(this).parent().parent().find('form').submit();
        })
    }
    
    function hoverMainMenu() {
        if(window.innerWidth>=992){
            var widthMenu = ($('.section-box-content .container').width())-($('.section-box-content .main-menu').width());
            $('.main-menu').addClass('full');
            $('.main-menu ul li ul').css({width:widthMenu});
        }
        $('.section-box-content .main-menu ul li').each(function () {
            if($(this).find('ul').length<=0){
                $(this).addClass('not-list')
            }
        })

    }

    function seoTxt() {
        var txt1 = $('.all_seo_text').data('show');
        var txt2 = $('.all_seo_text').data('hide');
        $('.all_seo_text').click(function (e) {
            e.preventDefault()
            $('.seo-text').toggleClass('show')
            $(this).toggleClass('show')
            if($(this).hasClass('show')) {
                $(this).html(txt2)
            } else {
                $(this).html(txt1);
            }
        })
    }

    function scrollUp() {
        $('.btn_up').click(function () {
            $('body, html').animate({scrollTop:0}, 500);
        })
        btnScroll()
        function btnScroll() {
            btnScrollPos()
            function btnScrollPos() {
                var containerPos = $('.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 headerPhonesAndMap() {


        $('.cities-wr .s_').click(function () {
            var thisCity = $(this).html()
            var thisPhones = $(this).next().html()
            if(!($(this).parents('.cities-wr').hasClass('cities-wr-map'))){
                $('.header-phones p').remove()
                $('.header-phones').append(thisPhones)

            } else { //блок с картой
                
                $('.adress-map span').remove()
                $('.adress-map').append(thisPhones)

                var thisDataLat = $(this).data('lat')//определяем координаты lat
                var thisDataLng = $(this).data('lng')//определяем координаты lng

                var magaz = $(this).next().find('span');

                function initialize() {
                    var start_position  = new google.maps.LatLng(thisDataLat, thisDataLng);
                    var styles = [{
                        stylers: [
                            { saturation: -100 }
                        ]
                    }];
                    var settings = {
                        styles: styles,
                        zoom: 12,
                        scrollwheel: false,
                        center: start_position,
                        mapTypeControl: true,
                        mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
                        navigationControl: false,
                        navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
                        scaleControl: true,
                        streetViewControl: true,
                        rotateControl: true,
                        zoomControl: true,
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    };
                    var map = new google.maps.Map(document.getElementById("map_canvas"), settings);


                    var image1 = new google.maps.MarkerImage('images/markers/marker-we-1.png',
                        new google.maps.Size(34, 48),
                        new google.maps.Point(0,0),
                        new google.maps.Point(16, 35)
                    );

                    var markers = [];

                    magaz.each(function () {
                        var newLat = $(this).data('lat');//определяем координаты lat
                        var newLng = $(this).data('lng');//определяем координаты lng

                        var newName =  $(this).text();//title



                        var marker = new google.maps.Marker({
                            position: new google.maps.LatLng(newLat, newLng),
                            map: map,
                            title: newName,
                            icon: image1
                        });
                        markers.push(marker);
                    })

                    var clusterStyles = [
                        {
                            url: 'images/markers/clasters.png',
                            height: 36,
                            width: 36
                        }
                    ];
                    markerClusterer = new MarkerClusterer(map, markers,
                        {
                            maxZoom: 10,
                            gridSize: 100,
                            styles: clusterStyles
                        });
                }

                if($(window).width()>=768){
                    initialize()
                }
                $(window).resize(function () {
                    if($(window).width()>=768){
                        initialize()
                    }
                })

            }

            $(this).parents('.cities-wr').find('.addCity i').html(thisCity)
            $(this).parents('.cities-wr').find('.hidden_shops').css({display:'none'})
            $(this).parents('.cities-wr').find('.hidden_shops ul li').removeClass('active')
            $(this).parent().addClass('active')
           
        })

        $('body').on('click','.adress-map span',function () {

            var thisDataLat = $(this).data('lat')//определяем координаты lat
            var thisDataLng = $(this).data('lng')//определяем координаты lng
            var newName =  $(this).text();//название

            function initialize() {
                var start_position  = new google.maps.LatLng(thisDataLat, thisDataLng);
                var styles = [{
                    stylers: [
                        { saturation: -100 }
                    ]
                }];
                var settings = {
                    styles: styles,
                    zoom: 16,
                    scrollwheel: false,
                    center: start_position,
                    mapTypeControl: true,
                    mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
                    navigationControl: false,
                    navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
                    scaleControl: true,
                    streetViewControl: true,
                    rotateControl: true,
                    zoomControl: true,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                var map = new google.maps.Map(document.getElementById("map_canvas"), settings);


                var image1 = new google.maps.MarkerImage('images/markers/marker-we-1.png',
                    new google.maps.Size(34, 48),
                    new google.maps.Point(0,0),
                    new google.maps.Point(16, 35)
                );


                var marker = new google.maps.Marker({
                    position: new google.maps.LatLng(thisDataLat, thisDataLng),
                    map: map,
                    title: newName,
                    icon: image1
                });


            }

            if($(window).width()>=768){
                initialize()
            }
            $(window).resize(function () {
                if($(window).width()>=768){
                    initialize()
                }
            })

        })
    }
    
    function disabledTagsCatalog() {
        $('body').on('click','.tags-catalog .disabled',function (e) {
            e.preventDefault();
        })
    }

    function sortList() {
        $('.sort-cat>a').click(function (e) {
            if($(this).hasClass('category-sort')){
                e.preventDefault();
                $('.sort-cat').toggleClass('active')
            }
        })

        $('.sort-cat ul li a').click(function () {
            $('.category-sort').html($(this).text())
            $('.sort-cat ul li a').removeClass('desc')
            $(this).addClass('desc')
        })

        var timeoutSort
        $('.sort-cat').hover(function () {
            clearTimeout(timeoutSort)
        },function () {
            timeoutSort = setTimeout(function () {
                $('.sort-cat').removeClass('active')
            },320)

        })
    }

    function pagination() {
        $('body').on('click','.pagination li.active',function (e) {
            e.preventDefault();
        })
    }


    function filters() {
        //удаляем выбранне фильтра(серые кнопки)
        $('body').on('click','.wrapp_clear-link a',function () {
            var thisParents = $(this).parent().parent();
            $(this).remove();
            if($('.wrapp_clear-link a').length<=0){
                thisParents.remove();
            }
        })


        $('body').on('click','.buttonsSort a.active',function (e) {
            e.preventDefault();
        })

        //открываем/скрываем все фильтра
        $('.close_filter').click(function () {
            var desiredBlock = $(this).parents('.input-blocks');
            desiredBlock.toggleClass('hide_filter');
            desiredBlock.find('.s_c_h').removeClass('visible_s_c_h');
            desiredBlock.find('.more_six-btn span').removeClass('show_all');
        })

        //открываем/скрываем группу фильтров(6 штук)
        $('.more_six-btn span').click(function () {
            var desiredBlock = $(this).parents('.more_six').find('.s_c_h');
            desiredBlock.toggleClass('visible_s_c_h');
            $(this).toggleClass('show_all');
        })
    }

    function priceSlider(){

        var $price_interval = $('#price_interval');
        var $filter_prices_min = $('#filter-prices-min');
        var $filter_prices_max = $('#filter-prices-max');


        if($price_interval.length){

            var block = $('#price_block');
            var link = block.data('url');
            var min = block.data('min');
            var max = block.data('max');
            var from = block.data('from');
            var to = block.data('to');


            $price_interval.ionRangeSlider({
                type: 'double',
                min: min,
                max: max,
                from: from,
                to: to,
                grid: false,
                hide_min_max: true,
                hide_from_to: true,
                onChange: function (e) {
                    $filter_prices_min.val(e.from);
                    $filter_prices_max.val(e.to)
                },
                onFinish: function(e) {

                }
            });

            $('#btn_ok span').click(function () {
                var from = $filter_prices_min.val();
                var to = $filter_prices_max.val();
                $.pjax({url: link.replace('{from}', from).replace('{to}', to), container: "#list-container",timeout:5000, scrollTo: 0})
            });

            var slider = $price_interval.data("ionRangeSlider");

            $filter_prices_min.change(function () {
                var newVal = $(this).val();
                slider.update({
                    from: newVal
                });
            });

            $filter_prices_max.change(function () {
                var newVal = $(this).val();
                slider.update({
                    to: newVal
                });
            })


        }
    }
    
    function priceSliderFixInputTxt() {
        $('.price_inputs input').keypress(function(e) {
            if (!(e.which==8 ||(e.which>47 && e.which<58))) return false;
        });
        $('.price_inputs input').focusout(function () {
            var thisVal = +($(this).val());
            var thisMin = +($(this).attr('min'));
            var thisMax = +($(this).attr('max'));
              if(thisVal<thisMin) {
                  $(this).val(thisMin);
              }
                if(thisVal>thisMax) {
                  $(this).val(thisMax);
              }
        })


    }

    function  menuAllPage() {
        $('.arrow-catalog').click(function () {
            $(this).toggleClass('active')
            if($(this).hasClass('active')){
                $('.menu_all_page').addClass('show_menu_all')
                setTimeout(function () {
                    $('.menu_all_page').addClass('opacity_off')
                },100)
            } else {
                $('.menu_all_page').removeClass('show_menu_all opacity_off')
            }
        })
        var timeoutMenu
        $('.menu_all_page').hover(function () {
            clearTimeout(timeoutMenu)
        },function () {
            timeoutMenu = setTimeout(function () {
                $('.menu_all_page').removeClass('show_menu_all opacity_off')
                $('.arrow-catalog').removeClass('active')
            },320)

        })
    }
    
    function filtersShowMobil() {
        $('.filter_mobile_').click(function () {
            $('.mob_filters_wr').animate({left:0,opacity:1},300)
            $('body').addClass('hidden_scroll-y')
        })

        $('.close_mob_filters').click(function () {
            $('body').removeClass('hidden_scroll-y')
            $('.mob_filters_wr').animate({left:'-100%',opacity:0},300)
        })
    }

    function searchShow() {
        $('.search-btn-menu-mob').click(function (e) {
            e.preventDefault()
           $('.search-header-wr').addClass('show_search')
        })
        $('.close_search').click(function () {
            $('.search-header-wr').removeClass('show_search')
        })
    }

    function openCardParams() {
        $('.p_wrapp').click(function () {
            var windWidth = $(window).width();
            if(windWidth<768){
                if(($(this).hasClass('open_p'))){
                    $(this).removeClass('open_p')
                } else {
                    $('.p_wrapp').removeClass('open_p')
                    $(this).addClass('open_p')
                }
            }
        })
        $(window).resize(function () {
            if($(window).width()>=768) {
                $('.p_wrapp').removeClass('open_p')
            }
        })
    }

    function  openComments() {
        var linkComments = '#comments';
        var commentsAnchor = $('.comments_anchor');

        function scrollComments() {
            var timeScrollComments = 850;
            if($(window).width()>=768) {
                $('body, html').animate({scrollTop:(commentsAnchor.offset().top)-38}, timeScrollComments);
            } else {
                $('.p_wrapp').removeClass('open_p')
                $('.comments_anchor').addClass('open_p')
                $('body, html').animate({scrollTop:(commentsAnchor.offset().top)-3}, timeScrollComments);
            }

        }

        if(window.location.hash == linkComments){
            scrollComments()
        }

        $('.card_code_wr .cat_comments-link').click(function () {
            scrollComments()
        })
    }
    
    function checkedFilter() {
        $('.sidebar_checks a').click(function () {
            $(this).parent().addClass('checked')
        })
    }

    function scrollToCommentsWrite() {
            $('.write_comments span').click(function () {
                var pos = $(this).parents('.comments-wr-blog').find('#commentmodel-username');
                pos.focus();
                $('body, html').animate({scrollTop:(pos.offset().top)-30}, 500);
            })
    }

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

        if($('body').find(phoneInput).length>0){
            $(phoneInput).mask('+38(000)000-00-00',{placeholder:'+38(000)000-00-00'});
            $(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 blogListTags() {
        $('.blog-cat-tags-title').click(function () {
            var tagsParents = $(this).parents('.blog-cat-tags-wr.hidden-mob');
            if(tagsParents.hasClass('active')){
                tagsParents.removeClass('active')
            } else  {
                tagsParents.addClass('active')
            }
        })
    }
    
    function basketTabs() {
        $('.basket-tabs_list-wr ul li').click(function (e) {
            e.preventDefault();
            var indexbasketTabs = $(this).index()
            $('.basket-tabs_list-wr ul li').removeClass('active');
            $(this).addClass('active');
            var mobTabsBasket = $('.tabs_basket-mobile');
            mobTabsBasket.removeClass('active-mobile')
            $(mobTabsBasket[indexbasketTabs]).addClass('active-mobile')
        })
        $('.btn_mobil_show_desk').click(function (e) {
            e.preventDefault();
            var indexbasketTabsMob = $(this).parent().index();
            $('.btn_mobil_show_desk').parent().removeClass('active-mobile');
            $(this).parent().addClass('active-mobile');
            var mobTabsBasket = $('.basket-tabs_list-wr ul li')
            mobTabsBasket.removeClass('active')
            $(mobTabsBasket[indexbasketTabsMob]).addClass('active')
        })

        if(($(window).width())<768) {
            $('.tabs_basket-mobile').removeClass("active-mobile");
        }
    }
    
    function basketHiddenRadio() {
        $('.delivery-wrapper .radio_custom input[type="radio"], .payment-wrapper .radio_custom input[type="radio"]').click(function () {
            if($(this).parents('.delivery-wrapper').hasClass('delivery-wrapper')){

                if($(this).parents('.hidden_form_radio').length){
                    $('.delivery-wrapper .hidden_form_radio .visible_hidden_radio').removeClass('visible_hidden_radio');
                    $('.delivery-wrapper .hidden_form_radio .visible_hidden_radio-child').removeClass('visible_hidden_radio-child');
                    $(this).parent().addClass('visible_hidden_radio-child')
                } else {
                    $('.delivery-wrapper .visible_hidden_radio').removeClass('visible_hidden_radio');
                    $(this).parent().addClass('visible_hidden_radio')
                }

            } else {
                if($(this).parents('.hidden_form_radio').length){
                    $('.payment-wrapper .hidden_form_radio .visible_hidden_radio').removeClass('visible_hidden_radio');
                    $(this).parent().addClass('visible_hidden_radio-child')
                } else {
                    $('.payment-wrapper .visible_hidden_radio').removeClass('visible_hidden_radio');
                    $(this).parent().addClass('visible_hidden_radio')
                }

            }

            //$(this).parent().addClass('visible_hidden_radio')

        })
    }

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


            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);
                });
        })
    }

    $(document).on('click', '.remove_ico', confirmRemove);
    $(document).on('click', '.remove_confirm a', removeBasket);
    $(document).on('click', '.quantity-wr span', changeBasket);
    $(document).on('change', '.quantity-wr input', setBasket);
    $(document).on('keypress', '.quantity-wr input', setControl);


     $(document).on('click', '.btn_buy_basket', addBasket);

    function addBasket() {
        $.post("basket-modal.html", function (data) {
            $(".basket_modal").empty().append(data);
        });
    }


    $(document).on('click', '.basket-modal-bg #modal_close, #modal_close-2, #overlay', closeHideBasket);

    function confirmRemove(e) {
        e.preventDefault();
        $(this)
            .parent()
            .addClass('confirm');
    }

    function removeBasket(e) {
        e.preventDefault();
        if ($(this).hasClass('remove-yes')) {
            var variant = $(this).parents('tr.variant_tr').data('variant');
            //удаление ячейки "tr" в корзине
            basket.remove(variant);
            $(this).parents('.confirm').parent().remove()
        } else {
            $(this)
                .parents('.confirm').removeClass('confirm')
        }
    }

    function changeBasket(e) {
        e.preventDefault();
        var variant = $(this).parents('tr.variant_tr').data('variant');
        var input = $(this).parent().find('input');
        var oldVal = input.val();
        if ($(this).hasClass('minus')) {
            if (oldVal > 1) {
                basket.add(variant, -1);
            }
        } else {
            basket.add(variant, 1);
        }
    }
    
    function setBasket(e) {
        e.preventDefault();
        var variant = $(this).parents('tr.variant_tr').data('variant');
        var count = $(this).val();
        basket.set(variant, count);
    }

    function setControl(e) {
        if (e.which == 13) {
            $(this)
                .trigger('change');
            return false;
        } else if (!(e.which == 8 || (e.which > 47 && e.which < 58))) {
            return false;
        }
    }

    function closeHideBasket() {
        $('#overlay').fadeOut(400);
        $('.basket_modal').animate({opacity: 0, top: '0'}, 200,function(){
            $(this).css('display', 'none');
            $('#overlay').fadeOut(400);
        });
    }

    closeForms();
    function closeForms() {
        $('#modal_close, #overlay').click( function(){
            $('.forms_, .basket_modal').animate({opacity: 0, top: '0'}, 200,function(){
                $(this).css('display', 'none');
                $('#overlay').fadeOut(400);
            });
            $('#success_form').animate({opacity: 0, top: '0'}, 200,function(){
                $(this).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)
    }



    


});




