var PREVIEW = true;

@baseTemplate


jQuery(function ($) {
    'use strict';
    var panels = $('@_accordion.selector @_accordion.content.selector').parent();
    panels.on('show.bs.collapse', function () {
        var actives = panels.filter('.in');

        $(this).prev().children('a').addClass('active');
        actives.prev().children('a').removeClass('active');

        if (actives && actives.length) {
            var hasData = actives.data('bs.collapse');
            if (!hasData || !hasData.transitioning) {
                actives.collapse('hide');
                if (!hasData) actives.data('bs.collapse', null);
            }
        }
    });
    panels.on('hidden.bs.collapse', function () {
        $(this).prev().children('a').removeClass('active');
    });
});

jQuery(function($) {
    'use strict';
    window.createClickForAddToCartButton = function() {
        'use strict';
        $('.add_to_cart_button').filter(function(){
            return '#' === $(this).attr('href');
        }).click(function (e) {
                e.preventDefault();
                var link = $(this),
                    formClone = $(this).parent('.product').clone(),
                    vmsiteurl = link.attr('data-vmsiteurl'),
                    vmlang = link.attr('data-vmlang'),
                    success = link.attr('data-vmsuccessmsg'),
                    carts = $('div[data-cart-position]'),
                    cartCustomFields = $('.product-field[data-cart-attribute="1"] *[name]'),
                    cloneCartCustomFields = cartCustomFields.clone(),
                    cart, position, style, id, url, datas;
                if (formClone.length) {
                    cartCustomFields.each(function(i) {
                        var element = this;
                        cloneCartCustomFields.eq(i).val($(element).val());
                    });
                    formClone.append(cloneCartCustomFields);
                    datas = formClone.serialize();
                    url = vmsiteurl + 'index.php?option=com_virtuemart&nosef=1&view=cart&task=addJS&format=json' + vmlang;
                    $.getJSON(url, datas, function(datas, textStatus) {
                        link.html(success);
                        if (carts.length > 0) {
                            carts.each(function () {
                                position = $(this).attr('data-cart-position') || '';
                                style = $(this).attr('data-style') || '';
                                id = $(this).attr('data-id') || '';
                                cart = $(this);
                                if ('' !== position) {
                                    url = vmsiteurl + 'index.php';
                                    (function(url, style, cart ) {
                                        $.ajax({
                                            url: url,
                                            type : 'get',
                                            data: {
                                                tmpl : 'modrender',
                                                modulename : 'mod_virtuemart_cart',
                                                modulestyle : style,
                                                moduleid : id,
                                                is_preview : PREVIEW ? 'on' : 'off'
                                            },
                                            dataType: 'html',
                                            success: function (data) {
                                                if (data)
                                                    cart.replaceWith(data);
                                            },
                                            error: function (xhr, status) {}
                                        });
                                    })(url, style, cart);
                                }
                            });
                        }
                    });
                }
                return false;
            });
    }
    window.createClickForAddToCartButton();
});

jQuery(function ($) {
    'use strict';
    if (PREVIEW) {
        var search = $('form[name*="search"]');
        search.submit(function() {
            return false;
        });

        $('#form-login').submit(function() {
            return false;
        });
        var logout = $('#form-login > input[type*="submit"]');
        logout.attr('link-disable', true);

        $('#checkoutForm').submit(function() {
            return false;
        });
        var checkout = $('#checkoutFormSubmit');
        checkout.attr('link-disable', true);

        var removeLinks = $('.removelink').filter(function () {
            if((this.getAttribute('name') + '').indexOf('delete.') === 0) return true;
            else return false;
        });
        removeLinks.attr('link-disable', true);

        var versions = $('.edit.item-page a[title=\'Versions\']');
        versions.attr('link-disable', true);
    }
});


// Fixing conflict Mootools.fx slide with Bootstap Carousel
if ('undefined' !== typeof jQuery && 'undefined' !== typeof MooTools) {
    Element.implement({
        slide: function (how, mode) {
            return this;
        },
        hide: function () {
            return this;
        },
        show: function () {
            return this;
        }
    });
}

(function ($) {
    'use strict';
    window.eventSetProductType = function(event) {

        var form = event.data.form,
            product = event.data.product,
            prices = product.find(".product-prices");

        if (prices.length === 0) {
            return false;
        }

        if (!event.data.form) {
            return false;
        }

        var formClone = form.clone(),
            cartCustomFields = $('.product-field[data-cart-attribute="1"] *[name]', product),
            cloneCartCustomFields = cartCustomFields.clone();

        cartCustomFields.each(function(i) {
            var element = this;
            cloneCartCustomFields.eq(i).val($(element).val());
        });
        formClone.append(cloneCartCustomFields);
        var datas = formClone.serialize();
        datas = datas.replace("&view=cart", "");

        prices.fadeTo("fast", 0.75);
        $.ajax({
            type: "POST",
            cache: false,
            dataType: "json",
            url: window.vmSiteurl + "index.php?&option=com_virtuemart&view=productdetails&task=recalculate&format=json&nosef=1" + window.vmLang,
            data: datas
        }).done(
            function (data, textStatus) {
                prices.fadeTo("fast", 1);
                // refresh price
                for (var key in data) {
                    var value = data[key];
                    if (key !== 'messages' && value !== 0) {
                        prices.find("span."+key).show().html(value);
                    }
                }
            }
        );

        return false; // prevent reload
    };

    window.onEventSetProductType = function(product) {
        product.children().eq(0).attr('data-updating-content', true);
        window.createClickForAddToCartButton();
        $("form.product", product).each(function() {
            var form = $(this),
                select = form.find('select:not(.no-vm-bind)'),
                selectOutForm = $('.product-field[data-cart-attribute="1"] select:not(.no-vm-bind)', product),
                radio = form.find('input:radio:not(.no-vm-bind)'),
                radioOutForm = $('.product-field[data-cart-attribute="1"] input:radio:not(.no-vm-bind)', product),
                virtuemart_product_id = form.find('input[name="virtuemart_product_id[]"]').val();

            $(select).off('change', window.eventSetProductType);
            $(select).on('change', {form : form, product : product}, window.eventSetProductType);
            $(selectOutForm).off('change', window.eventSetProductType);
            $(selectOutForm).on('change', {form : form, product : product}, window.eventSetProductType);

            $(radio).off('change', window.eventSetProductType);
            $(radio).on('change', {form : form, product : product}, window.eventSetProductType);
            $(radioOutForm).off('change', window.eventSetProductType);
            $(radioOutForm).on('change', {form : form, product : product}, window.eventSetProductType);
        });
    };
})(jQuery);
