/* Accordions */

// Refactored
(function($) {
    'use strict';

    var $accordion = $('.mk-accordion');
    // Do not keep constructor in memory until we have something to construct
    if(!$accordion.length) return;

    var Accordion = function(el) { 
        // Private
        var that = this,
            $el = $(el),
            initial = $el.data('item-index') || 0,
            timeout;

        // Public
        this.$el = $el;
        this.$single = $('.' + this.dom.single, $el);
        this.isExpendable = ($el.data('style') === 'toggle-action');

        // Init 
        this.bindClicks();
        // Reveal initial tab 
        if(initial !== -1) this.show(this.$single.eq(initial));

        $(window).on('resize', function() {
            clearTimeout(timeout);
            timeout = setTimeout(that.bindClicks.bind(that), 500);
        });
    };

    Accordion.prototype.dom = {
        // only class names please!
        single        : 'mk-accordion-single',
        tab           : 'mk-accordion-tab',
        pane          : 'mk-accordion-pane',
        current       : 'current',
        mobileToggle  : 'mobile-false',
        mobileBreakPoint : 767
    };

    Accordion.prototype.bindClicks = function() {
        // Prevent multiple events binding
        this.$single.off('click', '.' + this.dom.tab);

        if( !(window.matchMedia('(max-width: ' + this.dom.mobileBreakPoint +'px)').matches  && this.$el.hasClass(this.dom.mobileToggle)) ) {

            this.$single.on('click', '.' + this.dom.tab, this.handleEvent.bind(this));
            // When website is loaded in mobile view and resized to desktop 'current' will 
            // inherit display: none from css. Repair it by calling show() on this element
            var $current = $('.' + this.dom.current, this.$el);
            if($('.' + this.dom.pane, $current).css('display') === 'none') this.show($current);
        }
    };

    Accordion.prototype.handleEvent = function(e) {
        e.preventDefault();
        e.stopPropagation();

        var $single = $(e.delegateTarget);

        if(!$single.hasClass(this.dom.current)) {
            this.show($single);
        }
        else {
            if(this.isExpendable) this.hide($single);
        }
    };

    Accordion.prototype.hide = function($single) {
        $single.removeClass(this.dom.current);
        $('.' + this.dom.pane, $single).slideUp();
    };

    Accordion.prototype.show = function($single) {
        // hide currently opened tab
        if(!this.isExpendable) {
            var that = this;
            this.hide($('.' + this.dom.current, that.$el));
        }

        $single.addClass(this.dom.current);
        $('.' + this.dom.pane, $single).slideDown();
    };



    $(window).on('load reload', function() {
        $accordion.each(function() {
            new Accordion(this);
        });
    });

})(jQuery);





function mk_accordion() {

    // if ($.exists('.mk-accordion')) {
    //     $(".mk-accordion").each(function() {
    //         if (window.matchMedia('(max-width: 767px)').matches){

    //         }else{
    //             var $this = $(this),
    //             accordion_section = $this.find('.mk-accordion-single'),
    //             all_panes = $this.find('.mk-accordion-pane').hide();

    //             accordion_section.first().addClass('current-item').find('.mk-accordion-pane').slideDown(300);


    //             $this.find('.mk-accordion-tab').click(function() {
    //                 var $this = $(this),
    //                     $this_item = $this.parent();
    //                 if (!($this_item.hasClass('current-item'))) {
    //                     $this_item.siblings().removeClass('current-item').end().addClass('current-item');
    //                     all_panes.slideUp(300);
    //                     $this.parent().children('.mk-accordion-pane').slideDown(300);
    //                 }
    //                 return false;
    //             });
    //         }
    //     });
    // }

    /* Toggles */

    if ($.exists('.mk-toggle-title')) {
        if (window.matchMedia('(max-width: 767px)').matches){
            $('.mk-toggle-title').next().css('display', 'block');
        }else{
            $(".mk-toggle-title").toggle(
                function() {
                    $(this).addClass('active-toggle');
                    $(this).siblings('.mk-toggle-pane').slideDown("fast");
                },

                function() {
                    $(this).removeClass('active-toggle');
                    $(this).siblings('.mk-toggle-pane').slideUp("fast");
                }
            );   
        }
    }
}
