
/* Expandable Page Sections */
/* -------------------------------------------------------------------- */

function mk_expandable_page_section() {
    var opened_expandable_sections = 0;

    $('.section-expandable-true').each(function() {
        var $this = $(this);
        var $container = $this.find('.mk-padding-wrapper');
        var $bg = $this.find('.bg-layer');

        $container.hide();

        $this.on('click', function(e) {
            if (!e.bubbles) return;// just added to fix IE11 bug
            if ($this.hasClass('active-toggle')) return;

            $this.addClass('active-toggle');
            $container.fadeIn(500);
            opened_expandable_sections++;

            // Repaint bg
            $bg.removeClass('clip');
            setTimeout($bg.addClass, 0, 'clip');

            setTimeout(mk_lightbox_init, 1000);
            
            $(window).trigger('page-sextion-expanded');
        });

    });

    $('body').on('click touchstart', function(e) {
        // On IE we have a problem that this event is fired constantly on scroll which causes auto closing open sections.
        // e.bubbles exist only on our directly triggered event, not on delegated one so we use a chance to quit it quickly
        if (!e.bubbles) return;
        if (opened_expandable_sections > 0) {
            var $target = $(e.target);
            if ( !$target.is('.section-expandable-true') && !$target.closest('.section-expandable-true').length && !$('.fancybox-wrap').length ) {
                $('.section-expandable-true').removeClass('active-toggle');
                $('.section-expandable-true .mk-padding-wrapper').slideUp(400);
                opened_expandable_sections--;
            }
        }
    });
}
