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

    var ajaxurl = vintuna_pagination.ajax_url;

    function vintuna_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(vintuna_pagination.paged) + 1;
    var maxpage = vintuna_pagination.maxpage;
    var nextLink = vintuna_pagination.nextLink;
    var loadmore = vintuna_pagination.loadmore;
    var loading = vintuna_pagination.loading;
    var nomore = vintuna_pagination.nomore;
    var pagination_layout = vintuna_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 (!$('.vintuna-auto-pagination').hasClass('wpi-ajax-loading') && !$('.vintuna-auto-pagination').hasClass('wpi-no-posts') && maxpage > 1 && vintuna_is_on_screen('.vintuna-auto-pagination')) {
                $('.vintuna-auto-pagination').addClass('wpi-ajax-loading');
                $('.vintuna-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) {
                        $('.vintuna-auto-pagination').addClass('wpi-no-posts');
                        $('.vintuna-auto-pagination').text(nomore);
                    } else {
                        $('.vintuna-auto-pagination').removeClass('wpi-ajax-loading');
                        $('.vintuna-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);

                    }

                    $('.vintuna-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 && vintuna_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': 'vintuna_single_infinity',
                '_wpnonce': vintuna_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">'+vintuna_custom.next_svg+'</button>',
                            prevArrow: '<button type="button" class="slide-btn slide-btn-bg slide-prev-icon">'+vintuna_custom.prev_svg+'</button>',
                            dots: false
                        });
                    });

                }

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

            });

        }

    });

});