$(function(){

	var sortType;
	var cnt = 1;
	var elements = $('input[name="count-elements"]').val();
	var countElements;

	$('.pd20').on('click', '.more-projects', function() {
		var maxCnt = $('input[name="max-size"]').val();
		cnt++;
		countElements = elements * cnt;

		$.ajax({
			dataType: 'html',
			data: {
				PAGEN_1: cnt,
				sort: sortType
			},
			success: function(data) {
				if (cnt==maxCnt) {
					$('.more-projects').remove();
				}
				var res = $(data);
				var blocks = res.find('.project-block');
				$('.projects-list').append(blocks);
				blocks.slideUp(0);
				blocks.slideDown(400);
			}
		});
	});

	//Сортировка
	$('body').on('click', '.sort-asc, .sort-desc', function() {
		sortType = $(this).data('sort');

		$('.search-form').find('.sort-asc, .sort-desc').removeClass('active');
		$(this).addClass('active');

		$.ajax({
			url: location.href,
			data: {
				sort: sortType,
				newsCount: countElements
			},
			dataType: 'html',
			success: function(data) {
				var html = $(data).find('.projects-list').eq(0);
				$('.projects-list').replaceWith(html);
			}
		});
	});
});
