var $container = $('#game-list-container');
if (!$container.length || !$container.is(':visible')) {
$container = $('.home-games-list:visible, .game-list-grid-container:visible').first();
}
var LoadedGamesNum = $container.length ? $container.find('a').length : 0;
var loading = false;
var noMoreGames = false;
console.log('append target:', $container.attr('id'), $container.attr('class'));
if (PageType == "games" || PageType == "best" || PageType == "rated" || PageType == "updated") {
$(window).on('scroll', function() {
if (noMoreGames || loading || !$container.length) {
return;
}
var gridBottom = $container.offset().top + $container.outerHeight();
if ($(window).scrollTop() + $(window).height() > gridBottom - 900) {
if (LoadedGamesNum >= ids.split(',').length) {
noMoreGames = true;
return;
}
loading = true;
jsonajax(30);
console.log('load more fired', LoadedGamesNum, ids.split(',').length);
}
});
}
function jsonajax(e) {
if (e <= 0) return;
var url;
if (typeof cat !== 'undefined') {
url = "/ajax_loadmoregames.php?LoadedGamesNum=" + LoadedGamesNum + "&num=" + e + "&ids=" + ids + "&cat=" + cat;
} else {
url = "/ajax_loadmoregames.php?LoadedGamesNum=" + LoadedGamesNum + "&num=" + e + "&ids=" + ids + "&pagetype=" + PageType;
}
$.ajax({
url: url,
success: function(t) {
if (t == 'NoData' || $.trim(t) === '') {
noMoreGames = true;
loading = false;
return;
}
var $html = $(t);
$container.append($html);
console.log('added items:', $html.length, 'total visible:', $container.find('a:visible').length);
LoadedGamesNum = LoadedGamesNum + e;
loading = false;
},
error: function() {
loading = false;
}
});
}
});