var $grid = $('.grid').masonry({
	columnWidth: '.grid-sizer',
	itemSelector: '.grid-item-wrapper',
	percentPosition: true
});

$grid.imagesLoaded().progress( function() {$grid.masonry('layout');});

$(document).on('click', '.btn-load-more', function (e) {
	var $this = $(this),
		limit = $this.data('limit'),
		offset = $this.data('offset'),
		channel = $this.data('channel'),
		type = $this.data('type'),
		category = $this.data('category') || '',
		author = $this.data('author') || '';
	$this.prop('disabled', true);
	$this.html('Loading <span class="loading"></span>');
	
	let load_url = '/ajax/get_more_' + type + '/' + channel + (category != '' ? '/category/' + category : '') + (author != '' ? '/author/' + author : '') + '/' + offset + '/' + limit;

	$.ajax({
		url: load_url,
		method: 'GET'
	}).done(function (r) {
		if (r == 'none') {
			$this.prop('disabled', true);
			$this.text('No more');
		} else {
			$this.removeAttr('disabled');
			$this.html('Load more');
			$this.data('offset', offset + limit);

			var items = $(r);

			if (type == 'grid') {
				$grid.masonry().append(items).masonry('appended', items);
				setTimeout(function () {
					$grid.imagesLoaded().progress( function() {$grid.masonry('layout');});
				}, 300);
			} else {items.insertBefore($this);}
		}
	});
}).ready(function () {$grid.masonry('layout');});