@baseTemplate

jQuery(function ($) {
    'use strict';
    // shopping cart product name
    $('#shopping-cart-table h2.product-name a').unwrap();
    // buttons
    $('button.button').removeAttr('class');
});

window.productsGridReload = function productsGridReload(url, layoutTypes) {
    'use strict';

    jQuery('#ajax-loader').show();
    jQuery('.product-list').addClass('ajax-disabled');

    jQuery.ajax({
        type: 'GET',
        url: url,
        data: { ajax: 1 },
        dataType: 'html',
        success: function (response) {
            var dfds = [];
            jQuery('#ajax-loader').hide();
            jQuery('.product-list').removeClass('ajax-disabled');

            var replaceHtml = '';
            var list = jQuery(response).find('.product-list');
            if (list.length) {
                replaceHtml = list.html();
            } else {
                console.error('invalid ajax response');
                return;
            }
            jQuery('.product-list').html(replaceHtml);

            var listOffset = jQuery('.product-list').offset();
            if (listOffset && listOffset.top) {
                jQuery('body').scrollTop(listOffset.top);
            }

            jQuery('.product-list').find('img').each(function () {
                if (this.complete) return;
                var dfd = jQuery.Deferred();
                jQuery(this).load(dfd.resolve);
                dfds.push(dfd.promise());
            });

            jQuery.when.apply(jQuery, dfds).done(function () {
                window.buildTypeSelector(layoutTypes);
            });

            jQuery(window).resize();
        },
        error: function (xhr) {
            jQuery('#ajax-loader').hide();
            jQuery('.product-list').removeClass('ajax-disabled');
            window.console.log(xhr.status);
        }
    });
};

(function () {
    'use strict';

    var isBootstrapEvent = false;
    jQuery('*').on(
        'hide.bs.dropdown hide.bs.collapse hide.bs.modal hide.bs.tooltip hide.bs.popover' +
        'show.bs.dropdown show.bs.collapse show.bs.modal show.bs.tooltip show.bs.popover',
        function () { isBootstrapEvent = true; }
    );

    var protoHide = Element.hide,
        protoShow = Element.show;

    Element.addMethods(
        {
            hide: function (element) {
                if (isBootstrapEvent) {
                    isBootstrapEvent = false;
                    return element;
                }
                return protoHide(element);
            },
            show: function (element) {
                if (isBootstrapEvent) {
                    isBootstrapEvent = false;
                    return element;
                }
                return protoShow(element);
            }
        }
    );
})();

window.initProductsToolbar = function initProductsToolbar(selector, layoutTypes) {
    'use strict';

    var productList = jQuery(selector);

    productList.on('click', 'ul.products-pager li > a', function (e) {
        var clickedLink = jQuery(e.currentTarget);
        if (clickedLink.parent('li').hasClass('active')) return;
        window.productsGridReload(clickedLink.attr('href'), layoutTypes);
        e.preventDefault();
        e.stopPropagation();
        return false;
    });

    productList.on('change', '.products-sorter select', function (e) {
        window.productsGridReload(e.currentTarget.value, layoutTypes);
        e.preventDefault();
        e.stopPropagation();
        return false;
    });

    productList.on('click', '.products-sorter a.sort-order-link', function (e) {
        window.productsGridReload(jQuery(e.currentTarget).attr('href'), layoutTypes);
        e.preventDefault();
        e.stopPropagation();
        return false;
    });

    productList.on('change', '.products-per-page select', function (e) {
        window.productsGridReload(e.target.value, layoutTypes);
        e.preventDefault();
        e.stopPropagation();
        return false;
    });
}