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

    var ajaxurl = videocast_pagination.ajax_url;

    function videocast_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.TWP_JS || {};
    var paged = parseInt(videocast_pagination.paged) + 1;
    var maxpage = videocast_pagination.maxpage;
    var nextLink = videocast_pagination.nextLink;
    var loadmore = videocast_pagination.loadmore;
    var loading = videocast_pagination.loading;
    var nomore = videocast_pagination.nomore;
    var pagination_layout = videocast_pagination.pagination_layout;

    var permalink_structure = videocast_pagination.permalink_structure;

    function videocast_load_content_ajax() {

        if ((!$('.theme-no-posts').hasClass('theme-no-posts'))) {

            $('.theme-loading-button .loading-text').text(loading);
            $('.theme-loading-status').addClass('theme-ajax-loading');
            $('.theme-loaded-content').load(nextLink + ' .article-panel-blocks', function () {
                paged++;
                if (paged < 10) {
                    var newlink = nextLink.substring(0, nextLink.length - 2);
                } else {
                    var newlink = nextLink.substring(0, nextLink.length - 3);
                }
                if (permalink_structure == '') {
                    newlink = newlink.replace('=', '');
                    nextLink = newlink + "=" + paged + '/';
                } else {
                    nextLink = newlink + paged + '/';
                }
                if (paged > maxpage) {
                    $('.theme-loading-button').addClass('theme-no-posts');
                    $('.theme-loading-button .loading-text').text(nomore);
                } else {
                    $('.theme-loading-button .loading-text').text(loadmore);
                }

                $('.theme-loaded-content .twp-archive-items-main').each(function () {
                    $(this).addClass(paged + '-twp-article-ajax after-ajax-load');
                });

                if ($('.wrapper-inner').hasClass('wrapper-inner-small')) {

                    if ($('.wrapper-inner-small').length > 0) {


                        var loadedContent = $('.theme-loaded-content').html();
                        $('.theme-loaded-content').html('');
                        console.log(loadedContent);
                        console.log('loadedContent');
                        var content = $(loadedContent);
                        var grid = $('.wrapper-inner-small');


                        var filterContainer = $('.wrapper-inner-small');
                        var content = $(loadedContent);
                        filterContainer.append( content );


                        // Background
                        var pageSection = $(".data-bg");
                        pageSection.each(function (indx) {
                            if ($(this).attr("data-background")) {
                                $(this).css("background-image", "url(" + $(this).data("background") + ")");
                            }
                        });

                    }

                } else {

                    $('.content-area .wrapper-inner').append(loadedContent);

                }

                $('.theme-loading-status').removeClass('theme-ajax-loading');



            });

        }
    }

    $('.theme-loading-button').click(function () {

        videocast_load_content_ajax();

    });

    if (pagination_layout == 'auto-load') {
        $(window).scroll(function () {

            if (!$('.theme-loading-status').hasClass('theme-ajax-loading') && !$('.theme-loading-button').hasClass('theme-no-posts') && maxpage > 1 && videocast_is_on_screen('.theme-loading-button')) {

                videocast_load_content_ajax();

            }

        });
    }

});