$(document).ready(function(){

    $(document).on('beforeSubmit', 'form', function(e) {

        $.post($(this).attr('action'), $(this).serialize(), function(data) {
            if (data.success) {
                console.log('ok');
                $('.modal').each(function() {
                    $(this).modal('hide');
                });
                $('#success_modal')
                .modal('show');
            }
        });

        return false;
    });

    function initScroll(event) {
        event.preventDefault();
        var id  = $(this).attr('href'),
            top = $(id).offset().top;
        $('body,html').animate({scrollTop: top}, 1500);
    }
    $("#menu").on("click","a", initScroll);
    $('#menu1').on("click","a", initScroll);
    $('#menu2').on("click","a", initScroll);

    // vGovnokod here
    function addNameYear(){
        $(document).on('click', '.loadcars .btn' ,function(e){
            $('#orderform-model').val($(this).parent().parent().attr('data-key'));
            e.preventDefault();
            var carmodel = $(".models_select .nav-tabs-main").find(".active").text();
            var caryear = $(".models_select .nav-tabs-year").find(".active .year-picker").text();
            $("h4.model-car-name").html(carmodel+' '+caryear);
        });
    }
    addNameYear();

    $('.models_select ul.nav-tabs-main li').click(function (e) {
        e.preventDefault();
        $(".models_select ul.nav-tabs-main li").removeClass("active");
        $(this).addClass('active');
        var link = $(this).find('a');
        var model = link.data('model');
        var year = link.data('year');
        $('a.year-picker').data('model', model);
        var data = {
            'CarSearch[model]': model, 'CarSearch[year]': year
        };
        data = $.param(data);
        $('.cartabscont .tab-pane.active').load('/?'+data+' .cartabscont .tab-pane.active .owl-carousel', { 'Car[model]': link.data('model'), 'Car[year]': link.data('year') }, function() {
//            scrollInit();
            owlCar();
            $("h4.model-car-name").html(model+' '+year);
        });
    })

    $('.years_select ul li').click(function (e) {
        e.preventDefault();
        $(".models_select ul.nav-tabs-year li").removeClass("active");
        $(this).addClass('active');
        var link = $(this).find('a');
        var model = link.data('model');
        var year = link.data('year');
        $('a.model-picker').data('year', year);
        var data = {
            'CarSearch[model]': model, 'CarSearch[year]': year
        };
        data = $.param(data);
        var lang_attr = $('html').attr('lang');
        var lang = 'ru';
        if(lang_attr !== undefined) {
            lang = lang_attr.substr(0, 2);
        }
        $('.cartabscont .tab-pane.active').load('/?'+data+' .cartabscont .tab-pane.active .owl-carousel', { 'Car[model]': link.data('model'), 'Car[year]': link.data('year'), 'lang': lang }, function() {
            //            scrollInit();
            owlCar();
            $("h4.model-car-name").html(model+' '+year);
        });
    })

    function scrollInit()
    {
        var amount = Math.max.apply(
            Math, $(".cartabscont").map(
                function()
                {
                    return $(this).outerWidth(true);
                }
            ).get()
        );

        $(".cartabscont").mCustomScrollbar(
            {
                axis : "x",
                theme : "inset",
                advanced : {
                    autoExpandHorizontalScroll : true
                },
                scrollButtons : {
                    enable : true,
                    scrollType : "stepped"
                },
                keyboard : {scrollType : "stepped"},
                snapAmount : amount,
                mouseWheel : {scrollAmount : amount}
            }
        );
    }

    //govnokod ends

    function owlCar()
    {
        $('.owl-carousel').owlCarousel({
            margin:60,
            responsive:{
                0:{
                    nav: true,
                    dots: false,
                    items:1
                },
                480:{
                    nav:false,
                    dots: true,
                    items:2
                },
                768:{
                    nav:false,
                    dots: true,
                    items:3
                },
                992:{
                    nav:false,
                    dots: true,
                    items:4
                }
            }
        })
    }
    owlCar();
    });
var avatarElem = document.getElementById('contrast');
var avatarElem1 = document.getElementById('section1');
var avatarSourceBottom = avatarElem1.getBoundingClientRect().bottom + window.pageYOffset - 1;

window.onscroll = function() {
    if (avatarElem.classList.contains('contrast') && window.pageYOffset < avatarSourceBottom) {
        avatarElem.classList.remove('contrast');
    } else if (window.pageYOffset > avatarSourceBottom) {
        avatarElem.classList.add('contrast');
    }


};
