jQuery(document).ready(function(){
    var pageNumber = 1;
    // Ideas post load more archive
    jQuery('.load_posts_more').click(function(){
       jQuery(this).addClass('show');
        
        // alert();
        pageNumber++;
        var Total_page=jQuery(this).attr('total_page');
        // alert(Total_page);
        jQuery.ajax({
            type: "POST",
            dataType: "html",
            url: ajax_adminurl,
            data: {
                action: 'load_more_ideas_posts',
                pageNumber: pageNumber
            },
            beforeSend: function(){
                jQuery('.thumbnails').css('opacity', '0.6');
              },
            success: function(data){
                // alert(Total_page +" "+pageNumber);
                 if (Total_page > pageNumber) {
                    jQuery(".thumbnails").append(data);
                    jQuery('.thumbnails').css('opacity', '1');
                    jQuery('.load_posts_more').removeClass('show');
                }else {
                    // If there is no more data, hide the "Load more" button
                    jQuery('.load_posts_more').hide();
                    jQuery('.thumbnails').css('opacity', '1');

                }
                    // console.log(data);
                
            },
            error : function(jqXHR, textStatus, errorThrown) {
                // $loader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown);
            }
        });
    });

 jQuery('.cat_load_posts_more').click(function(){
    pageNumber++; // Increment page number
    jQuery(this).addClass('show'); // Add the show class to the button

    var Post_type = jQuery(this).attr('texonomy_name');
    var texonomy_name = jQuery(this).attr('texonomy_name');
    var terms = jQuery(this).attr('terms');

    jQuery.ajax({
        type: "POST",
        dataType: "json", // Expect JSON response
        url: ajax_adminurl,
        data: {
            action: 'load_ideas_cat_posts',
            Post_type: Post_type,
            texonomy_name: texonomy_name,
            terms: terms,
            pageNumber: pageNumber
        },
        beforeSend: function(){
            jQuery('.gallery').css('opacity', '0.6');
        },
        success: function(response){
            if (response) {
                jQuery(".gallery").append(response.output); // Append the new posts

                // If we are at the last page, hide the "Load More" button
                if (pageNumber >= response.total_pages) {
                    jQuery('.cat_load_posts_more').hide();
                } else {
                    jQuery('.cat_load_posts_more').removeClass('show');
                }

                // Optionally, show the total posts (you can remove this if unnecessary)
                console.log('Total posts: ' + response.total_posts);
            }
            jQuery('.gallery').css('opacity', '1');
        },
        error : function(jqXHR, textStatus, errorThrown) {
            console.log('Error: ' + textStatus + ', ' + errorThrown);
        }
    });
});


    // archive page from-studio post type
    jQuery('.load_posts_more_studio').click(function(){
        pageNumber++;
        jQuery(this).addClass('show');
        var Total_page=jQuery(this).attr('total_page');
        jQuery.ajax({
            type: "POST",
            dataType: "html",
            url:ajax_adminurl,
            data: {
                action: 'load_more_post_studio',
                pageNumber: pageNumber
            },
            beforeSend: function(){
                jQuery('.thumbnails').css('opacity', '0.6');
              },
            success: function(data){
                if (Total_page > pageNumber) {
                    jQuery(".thumbnails").append(data);
                    jQuery('.thumbnails').css('opacity', '1');
                    jQuery('.load_posts_more_studio').removeClass('show');

                }else {
                    // If there is no more data, hide the "Load more" button
                    jQuery('.load_posts_more_studio').hide();
                    jQuery('.thumbnails').css('opacity', '1');

                }
                
            },
            error : function(jqXHR, textStatus, errorThrown) {
                // $loader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown);
            }
        });
    });

    // archive page illustration post type
    jQuery('.load_posts_more_illustration').click(function(){
        // alert();
        pageNumber++;
        jQuery(this).addClass('show');
        var Total_page=jQuery(this).attr('total_page');
        jQuery.ajax({
            type: "POST",
            dataType: "html",
            url: ajax_adminurl,
            data: {
                action: 'load_more_post_illustration',
                pageNumber: pageNumber
            },
            beforeSend: function(){
                jQuery('.thumbnails').css('opacity', '0.6');
              },
            success: function(data){
                if (Total_page > pageNumber) {
                    jQuery(".thumbnails").append(data);
                    jQuery('.thumbnails').css('opacity', '1');
                    jQuery('.load_posts_more_illustration').removeClass('show');

                }else {
                    // If there is no more data, hide the "Load more" button
                    jQuery('.load_posts_more_illustration').hide();
                    jQuery('.thumbnails').css('opacity', '1');

                }
                
            },
            error : function(jqXHR, textStatus, errorThrown) {
                // $loader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown);
            }
        });
    });

     // archive page press post type
     jQuery('.load_posts_more_press').click(function(){
        // alert();
        pageNumber++;
        jQuery(this).addClass('show');
        var Total_page=jQuery(this).attr('total_page');
        jQuery.ajax({
            type: "POST",
            dataType: "html",
            url: ajax_adminurl,
            data: {
                action: 'load_more_post_press',
                pageNumber: pageNumber
            },
            beforeSend: function(){
                jQuery('.thumbnails').css('opacity', '0.6');
              },
            success: function(data){
                if (Total_page > pageNumber) {
                    jQuery(".thumbnails").append(data);
                    jQuery('.thumbnails').css('opacity', '1');
                    jQuery('.load_posts_more_press').removeClass('show');

                }else {
                    // If there is no more data, hide the "Load more" button
                    jQuery('.load_posts_more_press').hide();
                    jQuery('.thumbnails').css('opacity', '1');

                }
            },
            error : function(jqXHR, textStatus, errorThrown) {
                // $loader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown);
            }
        });
    })

});