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

        $(".rd-load-more a").on("click", function() {
            var gets = $(this).attr('rd-id');
            var element = $(this).parents('[class*="rd-el"]');
            var masonry = $(this).parents('.macy-masonry');
            $(this).parent().addClass('rd-loading');
            var rd_this = $(this);
            var active_filter = element.find('.rd-tab-active').attr('data-filter');
            var rd_list = $(this).parents('[class*="rd-el"]').find('.rd-item-list');
            var data_option = $(this).next().html();
            var dataajax = jQuery.parseJSON(data_option);
            dataajax['count'] = true;
            dataajax['ajaxcount'] = '10';
            dataajax['cats'] = $(this).attr('data-cats');
            dataajax['orderby'] = $(this).attr('data-orderby');
            dataajax['max_page'] = $(this).attr('data-max_page');
            var max_pages = $(this).attr('data-max_page');
            var pageNumber = rd_this.attr('data-page');
            dataajax['pageNumber'] = pageNumber;


            $.ajax({
                type: "POST",
                dataType: "html",
                url: reza_js.ajaxurl,
                data: dataajax,
                success: function(data) {
                    var datanew = data.replaceAll("rd-post-", "rd-new-post rd-post-");
                    var $data = $(datanew);


                    if ($data.length) {
                        rd_list.append($data);
                        rd_this.parent().addClass('rd-loading');
                        rd_this.parent().removeClass('rd-loading');

                    }
                    pageNumber++;
                    rd_this.attr('data-page', pageNumber);
                    if (pageNumber > dataajax['max_page']) {
                        rd_this.parent().hide();
                    }
                    if (jQuery().reza_masonry) {
                        setTimeout(function() {
                            element.reza_masonry();
                        }, 1);
                    }
                    setTimeout(function() {
                        element.find('.rd-new-post').each(function(index, element) {
                            $(this).removeClass('rd-new-post');
                        });

                    }, 1);



                },
                error: function(jqXHR, textStatus, errorThrown) {
                    $loader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown);
                }
            });

        });

        $('.rd-load-more a').each(function(index, element) {
            var max_page = $(this).data('max_page');
            if (max_page > 1) {
                $(this).parent().show();
            }

        });

    };

})(jQuery);