( function ( $ ) {

    var WidgetTFLPortfolioHandler = function ($scope, $) {

        if ($().isotope === undefined) {
            return;
        }

        var portfolioElem = $scope.find('.tfl-portfolio:not(.tfl-custom-grid)');
        if (portfolioElem.length === 0) {
            return; // no items to filter or load and hence don't continue
        }

        var rtl = portfolioElem.attr('dir') === 'rtl';

        var isotopeOptions = portfolioElem.data('isotope-options');

        portfolioElem.isotope({
            // options
            itemSelector: isotopeOptions['itemSelector'],
            layoutMode: isotopeOptions['layoutMode'],
            originLeft: !rtl,
        });

        // layout Isotope after each image load
        portfolioElem.imagesLoaded().progress( function() {
            portfolioElem.isotope('layout');
        });

        /* -------------- Taxonomy Filter --------------- */

        $scope.find('.tfl-taxonomy-filter .tfl-filter-item a').on('click', function (e) {
            e.preventDefault();

            var selector = $(this).attr('data-value');
            portfolioElem.isotope({filter: selector});
            $(this).closest('.tfl-taxonomy-filter').children().removeClass('tfl-active');
            $(this).closest('.tfl-filter-item').addClass('tfl-active');
            return false;
        });

    };

    // Make sure you run this code under Elementor..
    $( window ).on( 'elementor/frontend/init', function () {

        elementorFrontend.hooks.addAction( 'frontend/element_ready/tfl-portfolio.default', WidgetTFLPortfolioHandler );

    } );

} )( jQuery );