function gradeGame(grade)
{
	$.ajax({
		type: "POST",
		url: "/ocen-gre.html",
		data:
		{
			gid: $("#gid").val(),
			grade: grade
		},
		beforeSend : function()
		{
			$("input[name='grade']").attr("disabled", true);
		},
		success: function(json)
		{
			$("input[name='grade']").attr("checked", false);

			result = $.parseJSON(json);

			currentGrade = result.grade;

			if(result.error=="")
			{
				$("#gradeError").hide().html("");
				$("#gradeOk").html(result.ok).show();

				$("#gameGrade").html(result.grade);
				$("#gameGrades").html(result.grades);
			}
			else
			{
				$("#gradeOk").hide().html("");
				$("#gradeError").html(result.error).show();
			}

			$(".grade").parent("label").unbind('mouseenter mouseleave');
			$("label[for^='grade-']").removeClass("starFull").removeClass("starHalf");

			showCurrentGameGrade();
		}
	});
}

function commentGame()
{
	$.ajax({
		type: "POST",
		url: "/dodaj-komentarz.html",
		data: $("#commentForm").serialize(),
		beforeSend : function()
		{
			$("#submit").attr("disabled", true);
			$(".error").remove();
			$(".ok").remove();
			$(".limit").remove();
		},
		success: function(json)
		{
			$("#submit").attr("disabled", false);

			result = $.parseJSON(json);

			if(result.error == 1)
			{
				$.each(result.errors, function(key, value){
					$.each(value, function(key1, value1){
						$("#"+key).after('<div class="error">'+value1+'</div>');
					});
				});
				Recaptcha.reload();
			}
			else if(result.error == "limit")
			{
				$("#commentForm").before('<div class="limit">'+result.message+'</div>');
				Recaptcha.reload();
			}
			else
			{
				$("#commentForm").before('<div class="ok">'+result.message+'</div>');
				
				if($(".commentsList .empty").length>0)
				{
					$(".commentsList .empty").before('<div class="comment"><div class="header">'+$.PHPDate("Y-m-d H:i:s", new Date())+', '+$("#commentForm #author").val()+'</div><div class="message">'+$("#commentForm #message").val()+'</div></div>');
					$(".commentsList .empty").remove();					
				}
				else
				{
					$(".commentsList .comment").first().before('<div class="comment"><div class="header">'+$.PHPDate("Y-m-d H:i:s", new Date())+', '+$("#commentForm #author").val()+'</div><div class="message">'+$("#commentForm #message").val()+'</div></div>');
				}

				$("#commentForm").remove();
			}
		}
	});
}

function isFan()
{
	$.ajax({
		type: "GET",
		url: "/user/ajax-is-fan",
		success: function(json)
		{
			bResult = $.parseJSON(json);

			if(bResult)
			{
				$.cookie('hide_fb_promo', 1, { expires: 1, path: '/' });
			}
			else
			{
				$("#fbLikePromo").fadeIn();
			}
		}
	});
}

function updateUser()
{
	if($.cookie('user')!=1)
	{
		$.ajax({
			type: "GET",
			url: "/user/ajax-update",
			success: function(json)
			{
				$.cookie('user', 1, { expires: 1, path: '/' });
			}
		});
	}

}
