var __CurrentVideoRating = 0;

function switchToCategory(id)
{
	location.href = "/category.php?id=" + id;
}

function addVideoComment(code)
{
	if ($("#Comments").attr("value").trim() == "")
	{
		alert("Моля, първо въведете коментар");
		return;
	}

	$.ajax({
		url: "/video_comment.php",
		global: false,
		type: "POST",
		data: ({
			code: escape(code),
			comment: encodeURIComponent($("#Comments").attr("value"))
		}),
		success: function(response) {
			if (response == "SUCCESS")
			{
				$("#newCommentText").html($("#Comments").attr("value").trim().stripTags());
				$("#newComment").show();
				$("#newCommentSpace").show();
				$("#noComments").css("display", "none");
			}
			else
			{
				$("#leaveComment").html('<b style="color:red">Грешка при добавяне на коментар - ' + response + '</b>');
			}
		},
		error: function(response) {
			alert("Грешка при добавяне на коментар - " + response);
		}
	});
}

function voteForVideo(videoID, id)
{
	var rating = id.substr(id.length - 1, 1);

	$.ajax({
		url: "/video_vote.php",
		global: false,
		type: "POST",
		data: ({
			vid: escape(videoID),
			rating: escape(rating)
		}),
		success: function(response) {
			if (response == "SUCCESS")
			{
				$("#spanVoteLine").html("Благодарим Ви за гласуването!");
				__CurrentVideoRating = rating;
				outVideoVote();
			}
			else
			{
				$("#spanVoteLine").html(response);
			}
		},
		error: function(response) {
			alert("Грешка при оценяване на клип - " + response);
		}
	});
}

function voteLikeForVideo(videoID, like)
{
	$.ajax({
		url: "/video_like.php",
		global: false,
		type: "POST",
		data: ({
			vid: escape(videoID),
			like: escape(like)
		}),
		success: function(response) {
			if (response.substring(0, 7) == "SUCCESS")
			{
				var arrSplit = response.split(";");
				if (like == 1)
				{
					$("#idLikeCount").html(arrSplit[1]);
				}
				else
				{
					$("#idDislikeCount").html(arrSplit[1]);
				}
			}
			else
			{
				$("#spanVoteLine").html(response);
			}
		},
		error: function(response) {
			alert("Грешка при гласуване за клип - " + response);
		}
	});
}

function showLoginForVote()
{
	$("#spanVoteLine").html('<a href="/login.php" style="color: #222222">Моля влезте</a>, за да гласувате');
}

function hoverVideoVote(rating)
{
	var imgStarFull = new Image();
	imgStarFull.src = "/images/star_full.gif";

	var imgStarEmpty = new Image();
	imgStarEmpty.src = "/images/star_empty.gif";

	for (var i = 1; i <= rating; i++)
	{
		$("#voteStar" + i).attr("src", imgStarFull.src);
	}

	for (var i = rating + 1; i <= 5; i++)
	{
		$("#voteStar" + i).attr("src", imgStarEmpty.src);
	}
}

function outVideoVote()
{
	var imgStarEmpty = new Image();
	imgStarEmpty.src = "/images/star_empty.gif";

	var imgStarHalf = new Image();
	imgStarHalf.src = "/images/star_half.gif";

	var imgStarFull = new Image();
	imgStarFull.src = "/images/star_full.gif";

	var intFullStars = Math.ceil(__CurrentVideoRating);
	var intHalfStars = __CurrentVideoRating - intFullStars == 0.5 ? 1 : 0;
	var intEmptyStars = 5 - (intFullStars + intHalfStars);

	for (var i = 1; i <= intFullStars; i++)
	{
		$("#voteStar" + i).attr("src", imgStarFull.src);
	}

	for (var i = intFullStars + 1; i <= intFullStars + intHalfStars; i++)
	{
		$("#voteStar" + i).attr("src", imgStarHalf.src);
	}

	for (var i = intFullStars + intHalfStars + 1; i <= 5; i++)
	{
		$("#voteStar" + i).attr("src", imgStarEmpty.src);
	}
}

function reportMissingVideo(code)
{
	new Ajax.Request("/report_missing.php",
	{
		method: "post",
		parameters: {
			"code": escape(code)
		},
		onSuccess: function(response) {
			if (response.responseText == "SUCCESS")
			{
				$("aReportBrokenLink").innerHTML = '<strong class="txt_15" style="color: gray">Благодарим Ви!</span>';
			}
		},
		onFailure: function(response) {
			alert("Грешка при докладване за липсващо видео");
		}
	});
}
