/* Initialize isiotop  */
/* -------------------------------------------------------------------- */
function throttle(delay, callback) {
    var previousCall = new Date().getTime();
    return function() {
        var time = new Date().getTime();
        if( time - previousCall >= delay ) {
            previousCall = time;
            callback.apply( null, arguments );
        }
    };
}

function loops_iosotop_init() {

    $('.loop-main-wrapper').each(function() {
        var $this = $(this),
            $mk_container = $this.find('.mk-theme-loop'),
            $mk_container_item = '.' + $mk_container.attr('data-style') + '-' + $mk_container.attr('data-uniqid'),
            $load_button = $this.find('.mk-loadmore-button'),
            $pagination_items = $this.find('.mk-pagination');

        if ($mk_container.hasClass('isotop-enabled')) {
            $mk_container.imagesLoaded(function() {
                $mk_container.isotope({
                    itemSelector: $mk_container_item,
                    animationEngine: "best-available",
                    masonry: {
                        columnWidth: 1
                    }
                });

                var header = $( '.header-structure-vertical' )[0],
                    resizeCallback = false;

                var update = function() {
                    if( resizeCallback ) return;
                    resizeCallback = true;
                    setTimeout( function() {
                        $mk_container.isotope( 'layout' );
                        resizeCallback = false;
                    }, 300);
                };

                if( header ) window.addResizeListener( header, update );
            });
        }

        $('.mk-isotop-filter').on('click', 'a', function() {
            var $this;
            $this = $(this);

            TweenLite.to($this.parents('.portfolio-grid').find('.ajax-container'), 0.5, { height: 0, opacity: 0 });

            if ($this.hasClass('.current')) return false;

            var $optionSet = $this.parents('.mk-isotop-filter ul');
            $optionSet.find('.current').removeClass('current');
            $this.addClass('current');

            var selector = $(this).attr('data-filter');
            $mk_container.isotope({ filter: ''});
            $mk_container.isotope({ filter: selector });

            return false;
        });

        $load_button.hide();

        if ($this.find('.mk-theme-loop').hasClass('scroll-load-style') || $this.find('.mk-theme-loop').hasClass('load-button-style')) {
            if ($pagination_items.length > 0) {
                $load_button.css('display', 'block');
            }
            $pagination_items.hide();

            $load_button.on('click', function() {
                if (!$(this).hasClass('pagination-loading')) {
                    $(this).addClass('pagination-loading');
                }
            });

            $mk_container.infinitescroll({
                    navSelector: $pagination_items,
                    nextSelector: $this.find('.mk-pagination a:first'),
                    itemSelector: $mk_container_item,
                    bufferPx: 70,
                    loading: {
                        finishedMsg: "",
                        msg: null,
                        msgText: "",
                        selector: $load_button,
                        speed: 300,
                        start: undefined
                    },
                    errorCallback: function() {
                        $load_button.html(mk_no_more_posts).addClass('disable-pagination');
                    },
                },

                function(newElements) {
                    var $newElems = $(newElements);
                    $newElems.imagesLoaded(function() {
                        $load_button.removeClass('pagination-loading');

                        $mk_container.isotope('appended', $newElems);
                        $mk_container.isotope({filter: ''});
                        var selected_item = $('.mk-isotop-filter ul').find('.current').attr('data-filter');
                        $mk_container.isotope({filter: selected_item});

                        $mk_container.isotope('layout');
                        loop_audio_init();
                        mk_lightbox_init();
                        mk_portfolio_image();
                        mk_gallery_image();
                        mk_swipe_slider();
                        // mk_portfolio_ajax();
                        mk_center_caption();
                        mk_portfolio_hovers();
                        $('.portfolio-grid.portfolio-ajax-enabled').ajaxPortfolio();
                        mk_section_parallax();
                    });
                }
            );

            /* Loading elements based on scroll window */
            if ($this.find('.mk-theme-loop').hasClass('load-button-style')) {
                $(window).unbind('.infscr');
                $load_button.click(function() {
                    $mk_container.infinitescroll('retrieve');
                    return false;
                });
            }

        } else {
            $load_button.hide();
        }
    });
}
