(function ($) {
    'use strict';
    /**
     * Fixes a bug with Bootstrap collapses on elements like checkboxes which may leave the checkbox in
     * the opposite state of the collapsed content when being doubleclicked or clicked too fast while the animation is still active.
     *
     * Bootstrap collapse will not react on elements that are in transition.
     */
    $(document).ready(function () {
        $('[data-toggle="collapse"]').each(function () {
            const $control = $(this);
            const $target = $($control.attr("data-target"));
            $target.on('show.bs.collapse hide.bs.collapse', function () {
                $control.prop('disabled', true);
                // Fallback to make sure the element is re-enabled even if the "releasing" event does not fire
                window.setTimeout(() => { $control.prop('disabled', false); }, 400);
            }).on('shown.bs.collapse hidden.bs.collapse', function () {
                $control.prop('disabled', false);
            });
        });
    });

    /**
     * Fixes wrong device detection on Ipads that use the same User Agent as Desktop Macs.
     */
    $(document).ready(function () {
        if (document.cookie.indexOf('et360device') !== -1) {
            // A device detection cookie was set before, do nothing.
            return;
        }
        const detectedDevice = $('body').data('device');
        if (detectedDevice === 'desktop') {
            if (navigator.platform && navigator.platform.indexOf("Mac") === 0 && navigator.maxTouchPoints && navigator.maxTouchPoints > 2
                || navigator.platform && navigator.platform === 'iPad') {
                // This is actually a falsely classified Ipad, set this session cookie to force ET360v2 to always consider this device to be a tablet
                document.cookie = 'et360device=tablet;path=/';
                // Only reload if setting the cookie worked to prevent reload loops (cookies might be disabled or not work for whatever reason)
                if (document.cookie.indexOf('et360device') !== -1) {
                    window.location.reload();
                }
            }
        }
    });
})(jQuery);

