(function($) {
    $.fn.reza_tabs_transition = function() {

        $('.rez-transition').on('click', '.rd-tab-item:not(.rd-view-all)', function(index, element) {
            var transition = $(this).parents('.rez-transition');
            transition.addClass('rez-all-transition-active');

            transition.find('.rd-tab-active').removeClass('rd-tab-active');
            $(this).addClass('rd-tab-active');

            var data_filter = $(this).attr('data-filter');

            var list_html = transition.find('.rd-gap-warp').html();
            transition.find('.rd-gap-warp').append('<div class="rd-append">' + list_html + '</div>');
            var append = transition.find('.rd-append .rd-item-list');

            var list = transition.find('.rd-gap-warp > .rd-item-list');
            var old_list_height = list.height().toFixed(0);
            list.css({
                "--rd-old-list-ht": old_list_height + 'px'
            });
            list.find('.rd-item').each(function(i) {
                var count = i + 1;
                $(this).attr('data-count', count);

                var width = $(this).width();
                var height = $(this).height();
                if (transition.hasClass('macy-masonry')) {
                    var horizontal = $(this).position().left;
                    var vertical = $(this).position().top;

                } else {
                    var horizontal = $(this).offset().left - (window.scrollX);
                    var vertical = $(this).offset().top - (window.scrollY);
                }
                $(this).css({
                    "--rd-old-wt": width.toFixed(0) + 'px',
                    "--rd-old-ht": height.toFixed(0) + 'px',
                    "--rd-old-hor": horizontal.toFixed(0) + 'px',
                    "--rd-old-ver": vertical.toFixed(0) + 'px'
                });

            });


            setTimeout(function() {
                if (data_filter !== 'all') {
                    transition.addClass('rd-filter-not-all');
                }
                if (data_filter === 'all') {
                    transition.removeClass('rd-filter-not-all');
                }

                transition.addClass('rez-transition-active');

                append.find('.rd-item').each(function(i) {
                    var count = i + 1;

                    var main_post_item = transition.find('.rd-gap-warp > .rd-item-list .rd-item[data-count="' + count + '"]');
                    var append_item = $(this);

                    if (append_item.hasClass(data_filter) || data_filter === 'all') {
                        if (append_item.hasClass('rez-has-hide')) {
                            main_post_item.addClass('rez-to-show');
                        } else {
                            main_post_item.addClass('rez-to-move');
                        }
                        main_post_item.removeClass('rez-has-hide');
                        append_item.removeClass('rez-has-hide');
                    } else {
                        if (!append_item.hasClass('rez-has-hide')) {
                            main_post_item.addClass('rez-to-hide');
                        }
                        append_item.addClass('rez-has-hide');
                    }

                });
            }, 1);


            if (jQuery().reza_macy) {
                setTimeout(function() {
                    append.reza_macy();
                }, 2);
            }
            setTimeout(function() {
                append.find('.rd-item').each(function(i) {
                    var count = i + 1;
                    var main_post_item = transition.find('.rd-gap-warp > .rd-item-list .rd-item[data-count="' + count + '"]');
                    var append_item = $(this);
                    var width = append_item.width();
                    var height = append_item.height();

                    if (transition.hasClass('macy-masonry')) {
                        var horizontal = append_item.position().left;
                        var vertical = append_item.position().top;

                    } else {
                        var horizontal = append_item.offset().left - (window.scrollX);
                        var vertical = append_item.offset().top - (window.scrollY);

                    }
                    main_post_item.css({
                        "--rd-wt": width.toFixed(0) + 'px',
                        "--rd-ht": height.toFixed(0) + 'px',
                        "--rd-hor": horizontal.toFixed(0) + 'px',
                        "--rd-ver": vertical.toFixed(0) + 'px'
                    });


                });



                var list_height = append.height().toFixed(0);
                list.css({
                    "--rd-list-ht": list_height + 'px'
                });
                if (list_height < old_list_height) {
                    transition.addClass('rd-old-list-height');
                }

            }, 10);

            setTimeout(function() {
                transition.addClass('rez-animte');
            }, 15);

            setTimeout(function() {

                transition.find('.rd-gap-warp > .rd-item-list .rd-item').each(function(index, element) {
                    if ($(this).hasClass('rez-to-hide')) {

                        $(this).addClass('rez-has-hide');
                    }
                    $(this).removeClass('rez-to-move rez-to-show rez-to-hide');
                    $(this).removeAttr('data-count');
                    $(this).css({
                        '--rd-wt': '',
                        '--rd-ht': '',
                        '--rd-hor': '',
                        '--rd-ver': '',
                        '--rd-old-wt': '',
                        '--rd-old-ht': '',
                        '--rd-old-hor': '',
                        '--rd-old-ver': ''
                    });

                });
                transition.removeClass('rez-animte rez-transition-active rez-all-transition-active rd-old-list-height');
                $(this).css({
                    '--rd-old-list-ht': '',
                    '--rd-list-ht': ''
                });
                if (jQuery().reza_macy) {
                    list.reza_macy();
                }
                transition.find('.rd-item-list').removeAttr('style');
                transition.find('.rd-append').remove();

            }, 400);

        });

    };

})(jQuery);