$(function(){
	var blogsList = $('.blogs-list');
	var navCount = blogsList.data('page-count');
	var cnt = 2;
	var loading = false;
	var link = $('#infinity-next-page');
	var href = link.attr('href');
	var delta = 900;
	var spinner = $('.spinner');

	$(window).ajaxStart(
		function(){
			spinner.css('display', 'block');
		}
	);

	$(window).scroll(function() {
		if ($('#infinity-next-page').size() && !loading) {
			if ($(window).scrollTop() + delta >= blogsList.height()+blogsList.offset().top) {
				loading = true;
				$.get(
					link.attr('href'),
					{is_ajax: 'y'},
					function(data){
						var page = $(data);
						var items = page.find('.blog-item');
						blogsList.append(items);
						blogsList.after('<script type="text/javascript" src="//yastatic.net/share/share.js" charset="utf-8"></script>');
						cnt++;
						link.attr('href', href.slice(0, href.lastIndexOf('=')+1)+cnt);
						if(cnt > navCount) {
							link.remove();
							spinner.remove();
						}
					loading = false;
				});
			}
		}
	});
});
