var buy = {

    init: function () {
        this.simple();
        this.show();
        this.controls();
        $('.orderPromoForm').keypress(function (e) {
            if (e.which == '13') {
                e.preventDefault();
                $('.orderPromoSubmit').click();
            }
        });
    },

    simple: function () {
        if (document.querySelector(".cardBuy")) {
            document.querySelectorAll(".cardBuy").forEach(function (el, i) {
                el.addEventListener('click', function (event) {
                    event.preventDefault();
                    this.classList.add("loading");
                    document.querySelector("body").classList.add("showSimpleCart");
                    document.querySelector(".cartSimple").classList.add("loading");
                    var id = this.getAttribute("data-id");


                    $.ajax({
                        type: "POST",
                        url: '/ajax/buy.php?id=' + id,
                        dataType: 'html',
                        success: function (response) {

                            $("#cartSimpleContent").html(response);
                            $(".cartSimple").removeClass('loading');
                            $(".cardBuy").removeClass('loading');
                        }
                    });


                });
            });
        }
        if (document.querySelector(".cardAddBuy")) {
            document.querySelectorAll(".cardAddBuy").forEach(function (el, i) {
                el.addEventListener('click', function (event) {
                    event.preventDefault();
                    this.classList.add("loading");
                    document.querySelector("body").classList.add("showSimpleCart");
                    document.querySelector(".cartSimple").classList.add("loading");
                    var id = this.getAttribute("data-id");


                    $.ajax({
                        type: "POST",
                        url: '/ajax/buy.php?id=' + id,
                        dataType: 'html',
                        success: function (response) {

                            $("#cartSimpleContent").html(response);
                            $(".cartSimple").removeClass('loading');
                            $(".cardAddBuy").removeClass('loading');
                        }
                    });


                });
            });
        }
    },

    show: function () {
        if (document.querySelector(".mainHeaderPrBasket:not(.mainHeaderPrBasket--off)")) {
            document.querySelectorAll(".mainHeaderPrBasket").forEach(function (el, i) {
                el.addEventListener('click', function (event) {
                    event.preventDefault();
                    document.querySelector("body").classList.add("showSimpleCart");
                    document.querySelector(".cartSimple").classList.remove("loading");
                });
            });
        }
    },

    controls: function () {
        if (document.querySelector(".cartSimpleOverlay")) {
            document.querySelectorAll(".cartSimpleOverlay").forEach(function (el, i) {
                el.addEventListener('click', function (event) {
                    event.preventDefault();
                    document.querySelector("body").classList.remove("showSimpleCart");
                });
            });
        }
        if (document.querySelector(".cartSimpleClose")) {
            /*     document.querySelectorAll(".cartSimpleClose").forEach(function (el, i) {
                     el.addEventListener('click', function (event) {
                         event.preventDefault();
                         document.querySelector("body").classList.remove("showSimpleCart");
                     });
                 });*/

        }
        $("body").on("click", ".cartSimpleClose", function () {
            document.querySelector("body").classList.remove("showSimpleCart");
        });

    }

};

buy.init();