$(function() {
    var basket = new ArtboxBasket({
        'cartSelector': '#cart'
    });

    /**
     * Modal form submit code
     */
    $(document)
        .on('beforeSubmit', '#feedback-form', function(e) {
            var f = this;
            var form = $(this);
            var formData = form.serialize();
            $.ajax({
                url: form.attr("action"),
                type: form.attr("method"),
                data: formData,
                success: function(data) {
                    f.reset();
                    $('#feedback-modal')
                        .modal('hide');
                    $('#success-modal')
                        .modal('show');
                },
                error: function() {
                    $('#feedback-modal')
                        .modal('hide');
                }
            });
        })
        .on('submit', '#feedback-form', function(e) {
            e.preventDefault();
        });

    /**
     * Contact form submitting
     */
    $(document)
        .on('beforeSubmit', '#contact-form', function(e) {
            var f = this;
            var form = $(this);
            var formData = form.serialize();
            $.ajax({
                url: form.attr("action"),
                type: form.attr("method"),
                data: formData,
                success: function(data) {
                    f.reset();
                    form.replaceWith(data.alert);
                },
                error: function() {

                }
            });
        })
        .on('submit', '#contact-form', function(e) {
            e.preventDefault();
        });

    /**
     * Button UP code
     */
    if ($('#back-to-top').length) {
        var scrollTrigger = 100, // px
            backToTop = function() {
                var scrollTop = $(window)
                    .scrollTop();
                if (scrollTop > scrollTrigger) {
                    $('#back-to-top')
                        .addClass('show');
                } else {
                    $('#back-to-top')
                        .removeClass('show');
                }
            };
        backToTop();
        $(window)
            .on('scroll', function() {
                backToTop();
            });
        $('#back-to-top')
            .on('click', function(e) {
                e.preventDefault();
                $('html,body')
                    .animate({
                        scrollTop: 0
                    }, 700);
            });
    }

    $(document)
        .on('click', '.add-to-basket', function(e) {
            e.preventDefault();
            var id = $(this)
                .data('id');
            var xhr = basket.add(id, 1);
            xhr.done(function() {
                $.pjax.reload({
                    container: '#basket-modal',
                    fragment: '#basket-modal',
                    timeout: 5000
                });
            });
            if ($('.alert-cart').length > 0) {
            } else {
                $('body')
                    .prepend($("<div class='alert-cart alert alert-success alert-dismissible'>Товар добавлен в корзину.</div>"));
                setTimeout(function() {
                    $(".alert-cart")
                        .addClass("active");
                }, 100);
                setTimeout(function() {
                    $(".alert-cart")
                        .removeClass("active");
                }, 3500);
                setTimeout(function() {
                    $(".alert-cart")
                        .remove();
                }, 3600);
            }
        });

    $(document)
        .on('click', '.remove-product-cart', function(e) {
            e.preventDefault();
            var id = $(this)
                .parents('.product-row-basket')
                .data('id');
            showLoader('#basket');
            var xhr = basket.remove(id);
            xhr.done(function() {
                $.pjax.reload({
                    container: '#basket',
                    fragment: '#basket',
                    timeout: 5000
                });
            })
        });

    $(document)
        .on('change', '.increase-product-basket', function(e) {
            var id = $(this)
                .parents('.product-row-basket')
                .data('id');
            showLoader('#basket');
            showLoader('#basket-modal');
            var xhr = basket.set(id, $(this)
                .val());
            xhr.done(function() {
                if ($('#basket').length) {
                    $.pjax.reload({
                        container: '#basket',
                        fragment: '#basket',
                        timeout: 5000
                    });
                }
                $.pjax.reload({
                    container: '#basket-modal',
                    fragment: '#basket-modal',
                    timeout: 5000
                });
            });
        });

    $(document)
        .on('click', 'li.disabled a', function(e) {
            e.preventDefault();
        });

    $(document)
        .on('submit', '#fast-buy-form', function(e) {
            e.preventDefault();
        });

    $(document)
        .on('afterValidate', '#fast-buy-form', function(e, m, errors) {
            if (!errors.length) {
                var form = $(e.target);
                var action = form.attr('action');
                $.post(action, form.serialize(), function(data) {
                    if (data.success) {
                        $('#oneclick-modal')
                            .find('.modal-body')
                            .text(data.msg);
                    }
                });
            }
        }.bind(this));

    $(document)
        .on(
            'click', '.wishlist-rm, .wishlist-add', function() {
                var button = $(this);
                $.ajax(
                    {
                        url: button.data('url'),
                        type: "POST",
                        data: {
                            'variant': button.data('variant'),
                            'product': button.data('product'),
                            'user': button.data('user')
                        },
                        success: function(data) {
                            button.replaceWith(data.button);
                            new PNotify(
                                {
                                    title: 'Info',
                                    text: data.message,
                                    type: 'info',
                                    styling: 'fontawesome'
                                }
                            );

                        }
                    }
                )
            }
        );

    $(document)
        .on(
            'click', '.wishlist-delete', function(e) {
                e.preventDefault();
                var button = $(this);
                $.ajax(
                    {
                        url: button.data('url'),
                        type: "POST",
                        data: {
                            'product': button.data('product'),
                            'variant': button.data('variant'),
                            'user': button.data('user')
                        },
                        success: function(data) {
                            if (data.success) {
                                $.pjax.reload({container: '#wishlist-products'});
                                new PNotify(
                                    {
                                        title: 'Info',
                                        text: data.message,
                                        type: 'info',
                                        styling: 'fontawesome'
                                    }
                                );
                            }
                        }
                    }
                );
            }
        );

});
function showLoader(container) {
    $(container)
        .prepend('<div class="loader-wrapper"><div class="loader"></div></div>');
}

$(document).ready(function ($) {
    $('.vcovers').perfectScrollbar({wheelSpeed: 0.5});
});