$(function(){
	var loading = '<p align="center"><img src="/media/images/loadingAnimation.gif" alt="loading"/></p>';
	$('.prevPageBlock').click(function(){
		var actualPage = $(this).next().text();
		if (actualPage > 1){
			var ajaxAnswer = false;
			var newPage = parseInt(actualPage)-1;
			$(this).next().text(newPage);
			var blockID = $(this).parent().attr("rel");
			$('#'+blockID+'Children').fadeOut('fast',function(){
				$(this).remove();
				if (!ajaxAnswer){
					$('#'+blockID+'ChildrenContainer').html(loading);
				}
			});
			$('#'+blockID+'ChildrenContainer').animate({
				height: "30px"
			}, 500  );
			$.post("/_"+actualLang+"/mainpage/getblockchildren/"+blockID+"?ajax=true", {
				page: newPage
			},
			function(data){
				ajaxAnswer = true;
				$('#'+blockID+'ChildrenContainer').html(data);
				var newHeight = $('#'+blockID+'ChildrenContainer').children(':first').height() ;
				$('#'+blockID+'ChildrenContainer').animate({
					height: newHeight+"px"
				}, 500 , function(){
					$('#'+blockID+'ChildrenContainer').children(':first').fadeIn('fast');
				} );
			});
		}
	});
	$('.nextPageBlock').click(function(){
		var actualPage = $(this).prev().text();
		var pageLimit = $(this).prev().attr("rel");
		if (actualPage < pageLimit){
			var ajaxAnswer = false;
			var newPage = parseInt(actualPage)+1;
			$(this).prev().text(newPage);
			var blockID = $(this).parent().attr("rel");
			var originalHeight = $('#'+blockID+'Children').height() ;
			$('#'+blockID+'Children').fadeOut('500',function(){
				$(this).remove();
				if (!ajaxAnswer){
					$('#'+blockID+'ChildrenContainer').html(loading);
				}
			});
			$('#'+blockID+'ChildrenContainer').animate({
				height: "30px"
			}, 500  );
			$.post("/_"+actualLang+"/mainpage/getblockchildren/"+blockID+"?ajax=true", {
				page: newPage
			},
			function(data){
				ajaxAnswer = true;
				$('#'+blockID+'ChildrenContainer').html(data);
				var newHeight = $('#'+blockID+'ChildrenContainer').children(':first').height();
				$('#'+blockID+'ChildrenContainer').animate({
					height: newHeight+"px"
				}, 500 , function(){
					$('#'+blockID+'ChildrenContainer').children(':first').fadeIn('fast');
				} );
			});
		}
	});
});
