jQuery(document).ready(function ($) {

    var ajaxurl = newsnation_pagination.ajax_url;

    function newsnation_is_on_screen(elem) {

        if ($(elem)[0]) {

            var tmtwindow = jQuery(window);
            var viewport_top = tmtwindow.scrollTop();
            var viewport_height = tmtwindow.height();
            var viewport_bottom = viewport_top + viewport_height;
            var tmtelem = jQuery(elem);
            var top = tmtelem.offset().top;
            var height = tmtelem.height();
            var bottom = top + height;
            return (top >= viewport_top && top < viewport_bottom) ||
                (bottom > viewport_top && bottom <= viewport_bottom) ||
                (height > viewport_height && top <= viewport_top && bottom >= viewport_bottom);
        }
    }

    var n = window.WPI_JS || {};
    var paged = parseInt(newsnation_pagination.paged) + 1;
    var maxpage = newsnation_pagination.maxpage;
    var nextLink = newsnation_pagination.nextLink;
    var loadmore = newsnation_pagination.loadmore;
    var loading = newsnation_pagination.loading;
    var nomore = newsnation_pagination.nomore;
    var pagination_layout = newsnation_pagination.pagination_layout;

    $('.wpi-loading-button').click(function () {
        var pageSection = $(".data-bg");
        pageSection.each(function (indx) {

            if ($(this).attr("data-background")) {

                $(this).css("background-image", "url(" + $(this).data("background") + ")");

            }

        });
        if ((!$('.wpi-no-posts').hasClass('wpi-no-posts'))) {

            $('.wpi-loading-button').text(loading);
            $('.wpi-loging-status').addClass('wpi-ajax-loading');
            $('.wpi-loaded-content').load(nextLink + ' .theme-article-area', function () {
                if (paged < 10) {
                    var newlink = nextLink.substring(0, nextLink.length - 2);
                } else {

                    var newlink = nextLink.substring(0, nextLink.length - 3);
                }
                paged++;
                nextLink = newlink + paged + '/';
                if (paged > maxpage) {
                    $('.wpi-loading-button').addClass('wpi-no-posts');
                    $('.wpi-loading-button').text(nomore);
                } else {
                    $('.wpi-loading-button').text(loadmore);
                }

                $('.wpi-loaded-content .theme-article-area').each(function(){
                    $(this).addClass(paged + '-wpi-article-ajax');
                });

                var lodedContent = $('.wpi-loaded-content').html();
                $('.wpi-loaded-content').html('');

                if ($('.article-wraper').hasClass('archive-layout-masonry')) {

                    if ($('.archive-layout-masonry').length > 0) {
                        var content = $(lodedContent);
                        content.hide();
                        grid = $('.archive-layout-masonry');
                        grid.append(content);
                        grid.imagesLoaded(function () {
                            content.show();

                            var winwidth = $(window).width();
                            $(window).resize(function () {
                                winwidth = $(window).width();
                            });

                            if (winwidth > 990) {
                                grid.masonry('appended', content).masonry();
                            } else {
                                grid.masonry('appended', content);
                            }

                        });
                    }

                } else {

                    $('.content-area .article-wraper').append(lodedContent);

                }

                $('.wpi-loging-status').removeClass('wpi-ajax-loading');

                $('.theme-article-area').each(function () {

                    if (!$(this).hasClass('theme-article-loaded')) {

                        $(this).addClass(paged + '-wpi-article-ajax');
                        $(this).addClass('theme-article-loaded');
                        $(this).find('.theme-video-panel').addClass( paged + '-wpi-video-ajax' );
                        $(this).find('.theme-video-panel').removeClass('video-main-wraper');
                    }

                });
            });

        }
    });

    if (pagination_layout == 'auto-load') {
        $(window).scroll(function () {
            var pageSection = $(".data-bg");
            pageSection.each(function (indx) {

                if ($(this).attr("data-background")) {

                    $(this).css("background-image", "url(" + $(this).data("background") + ")");

                }

            });
            if (!$('.newsnation-auto-pagination').hasClass('wpi-ajax-loading') && !$('.newsnation-auto-pagination').hasClass('wpi-no-posts') && maxpage > 1 && newsnation_is_on_screen('.newsnation-auto-pagination')) {
                $('.newsnation-auto-pagination').addClass('wpi-ajax-loading');
                $('.newsnation-auto-pagination').text(loading);

                $('.wpi-loaded-content').load(nextLink + ' .theme-article-area', function () {

                    if (paged < 10) {
                        var newlink = nextLink.substring(0, nextLink.length - 2);
                    } else {

                        var newlink = nextLink.substring(0, nextLink.length - 3);
                    }
                    paged++;
                    nextLink = newlink + paged + '/';
                    if (paged > maxpage) {
                        $('.newsnation-auto-pagination').addClass('wpi-no-posts');
                        $('.newsnation-auto-pagination').text(nomore);
                    } else {
                        $('.newsnation-auto-pagination').removeClass('wpi-ajax-loading');
                        $('.newsnation-auto-pagination').text(loadmore);
                    }

                    $('.wpi-loaded-content .theme-article-area').each(function(){
                        $(this).addClass(paged + '-wpi-article-ajax');
                    });

                    var lodedContent = $('.wpi-loaded-content').html();

                    $('.wpi-loaded-content').html('');

                    if ($('.article-wraper').hasClass('archive-layout-masonry')) {

                        if ($('.archive-layout-masonry').length > 0) {
                            var content = $(lodedContent);
                            content.hide();
                            grid = $('.archive-layout-masonry');
                            grid.append(content);
                            grid.imagesLoaded(function () {
                                content.show();

                                var winwidth = $(window).width();
                                $(window).resize(function () {
                                    winwidth = $(window).width();
                                });

                                if (winwidth > 990) {
                                    grid.masonry('appended', content).masonry();
                                } else {
                                    grid.masonry('appended', content);
                                }

                            });
                        }

                    } else {

                        $('.content-area .article-wraper').append(lodedContent);

                    }

                    $('.newsnation-auto-pagination').removeClass('wpi-ajax-loading');

                    $('.theme-article-area').each(function(){
                        $(this).removeClass(paged + '-wpi-article-ajax');
                    });


                });
            }

        });
    }

    $(window).scroll(function () {
        var pageSection = $(".data-bg");
        pageSection.each(function (indx) {

            if ($(this).attr("data-background")) {

                $(this).css("background-image", "url(" + $(this).data("background") + ")");

            }

        });
        if (!$('.wpi-single-infinity').hasClass('wpi-single-loading') && $('.wpi-single-infinity').attr('loop-count') <= 3 && newsnation_is_on_screen('.wpi-single-infinity')) {

            $('.wpi-single-infinity').addClass('wpi-single-loading');
            var loopcount = $('.wpi-single-infinity').attr('loop-count');
            var postid = $('.wpi-single-infinity').attr('next-post');

            var data = {
                'action': 'newsnation_single_infinity',
                '_wpnonce': newsnation_pagination.ajax_nonce,
                'postid': postid,
            };

            $.post(ajaxurl, data, function (response) {

                if (response) {
                    var content = response.data.content.join('');
                    var content = $(content);
                    $('.wpi-single-infinity').before(content);
                    var newpostid = response.data.postid['0'];
                    $('.wpi-single-infinity').attr('next-post', newpostid);

                    $('article#post-' + postid + ' ul.wp-block-gallery.columns-1, article#post-' + postid + ' .wp-block-gallery.columns-1 .blocks-gallery-grid, article#post-' + postid + ' .gallery-columns-1').each(function () {
                        $(this).slick({
                            slidesToShow: 1,
                            slidesToScroll: 1,
                            fade: true,
                            autoplay: true,
                            autoplaySpeed: 8000,
                            infinite: true,
                            nextArrow: '<button type="button" class="slide-btn slide-btn-bg slide-next-icon">'+newsnation_custom.next_svg+'</button>',
                            prevArrow: '<button type="button" class="slide-btn slide-btn-bg slide-prev-icon">'+newsnation_custom.prev_svg+'</button>',
                            dots: false
                        });
                    });

                }

                $('.wpi-single-infinity').removeClass('wpi-single-loading');
                loopcount++;
                $('.wpi-single-infinity').attr('loop-count', loopcount);

            });

        }

    });

});