var StudioPage = {
    $news: null,
    $loader: null,
    $grid: null,
    $keywords: null,
    $window: null,
    $document: null,
    page: 1,
    $mans: null,
    autoLoad: false,
    init: function () {
        StudioPage.$loader   = $('#studio-load-more');
        StudioPage.$grid     = $('#studio-grid');
        StudioPage.$keywords = $('#studio--keywords');
        StudioPage.$sector_selection = $('#studio--sector');
        StudioPage.$country = $('#studio-country');
        StudioPage.$shape = $('.shape--design');
        StudioPage.$loadMoreBtn = $('#studios--load-btn');


        StudioPage.$keywords.keyup(StudioPage.filterProgrammes);
        StudioPage.$sector_selection.change(StudioPage.filterProgrammes);
        StudioPage.$country.change(StudioPage.filterProgrammes);

        if(StudioPage.$shape.length) {
            StudioPage.$loadMoreBtn.click(StudioPage.nextPage);
        } else {
            StudioPage.$window   = $(window);
            StudioPage.$document = $(document);
            StudioPage.$window.on('DOMContentLoaded load resize scroll', StudioPage.onScroll);
            StudioPage.enableAutoLoad();
            StudioPage.showLoader();
            var scrollTop = StudioPage.$window.scrollTop();

            if (StudioPage.autoLoad && StudioPage.isAtBottom(scrollTop)) {
                StudioPage.nextPage();
            }
        }


    },
    onScroll: function () {
        var scrollTop = StudioPage.$window.scrollTop();

        if (StudioPage.autoLoad && StudioPage.isAtBottom(scrollTop)) {
            StudioPage.nextPage();
        }
    },
    nextPage: function () {
        StudioPage.disableAutoLoad();
        StudioPage.addPage();
        StudioPage.loadMore();
        StudioPage.showLoader();
    },
    loadMore: function () {
        $.ajax({
            dataType: 'html',
            type: 'GET',
            data: {
                page_no: StudioPage.page,
                isAjax: true,
                keywords:StudioPage.getKeywords(),
                sector:StudioPage.getSector(),
                country:StudioPage.getCountry(),
                type:'studios',
            },
            url: getCurrentPagePath(),
            success: function (data) {
                if (data) {
                    var $data = $(data);
                    StudioPage.$grid.append($data);
                    StudioPage.enableAutoLoad();


                } else {
                    StudioPage.hideLoader();
                    StudioPage.disableAutoLoad();

                }
            }
        });
    },
    filterProgrammes: function (e) {
        var charLen = StudioPage.getKeywords().length;
        if(e && e.type == "keyup" &&  charLen >= 1 && charLen < 3) return;
        StudioPage.resetPage();
        $.ajax({
            dataType: 'html',
            type: 'GET',
            data: {
                page_no: StudioPage.page,
                keywords:StudioPage.getKeywords(),
                sector:StudioPage.getSector(),
                country:StudioPage.getCountry(),
                type:'studios',
                isAjax: true
            },
            url: getCurrentPagePath(),
            success: function (data) {
                var data = data ? $(data) : '<div class="no-result-found"><h3> No Data Found.</h3></div>';
                StudioPage.$grid.empty();
                StudioPage.$grid.append(data);
                StudioPage.enableAutoLoad();
            }
        });
        StudioPage.addPage();
    },
    hideLoader: function () {
        StudioPage.$loader.fadeOut("slow");
    },
    showLoader: function () {
        StudioPage.$loader.fadeIn("slow");
    },
    enableAutoLoad: function () {
        StudioPage.autoLoad = true;
    },
    disableAutoLoad: function () {
        StudioPage.autoLoad = false;
    },
    isAtBottom: function (scrollTop) {
        return scrollTop + StudioPage.$window.height() > StudioPage.$document.height() - 700;
    },
    addPage: function () {
        StudioPage.page += 1;
    },
    resetPage: function () {
        StudioPage.page = 0;
    },
    getKeywords: function () {
        return StudioPage.$keywords.val() ? encodeURI(StudioPage.$keywords.val()) : '';
    },
    getSector: function () {
        return StudioPage.$sector_selection.val() ? encodeURI(StudioPage.$sector_selection.val()) : '';
    },
    getCountry: function () {
        return StudioPage.$country.val() ? encodeURI(StudioPage.$country.val()) : '';
    },
};

function getCurrentPagePath() {
    return location.href;
}

$(window).ready(function () {
    setTimeout(function () {
        StudioPage.init();
    }, 100)
});