var pbLoadingImg = document.createElement("IMG");
pbLoadingImg.src = "images/pb_loading.gif";

function vote(ele, translationId)
{
	var voteType = ele.className;
	var dataSerial = "transid=" + translationId + "&type=" + voteType;
	var oldStarImg = ele.parentNode.firstChild.src;

	//show a loading icon
	ele.parentNode.firstChild.src = "images/pb_loading.gif";

	//begin ajax
	$.ajax({
		url: "pb_vote.php",
		type: "POST",
		data: dataSerial,
		dataType: "html",
		success: function(html)
		{
			if(html == "done")
			{
				//up or down raking gets +1, and made unclickable
				ele.value = ele.value*1 + 1;
				ele.disabled = true;
				ele.style.cursor = "normal";
				ele.style.color = "#999";

				if(voteType == "up")
				{
					//make the "down" button unclickable.
					ele.nextSibling.disabled = true;
					ele.nextSibling.style.cursor = "normal";
					ele.nextSibling.style.color = "#999";

					//re-calculate the raking
					var upSum = ele.value*1;
					var downSum = ele.nextSibling.value*1;
					var rate = upSum / (upSum + downSum);
				}
				else
				{
					//in this case the "up" button should be unclickable.
					ele.previousSibling.disabled = true;
					ele.previousSibling.style.cursor = "normal";
					ele.previousSibling.style.color = "#999";

					//re-calculate the raking
					var upSum = ele.previousSibling.value*1;
					var downSum = ele.value*1;
					var rate = upSum / (upSum + downSum);
				}

				//update the star image and its mouseover tip.
				ele.parentNode.firstChild.src = getStarImage(rate);
				ele.parentNode.firstChild.title = "Ranking: " + rate*100;
			}
			else
			{
				//ajax failed, show an alert and restore the old star image.
				alert("Sorry, we cannot process your request at this moment.");
				ele.parentNode.firstChild.src = oldStarImg;
			}
		},
		error:function ()
		{
			alert("Connection failed, please try again later.");
			ele.parentNode.firstChild.src = oldStarImg;
		}
		
	});
}

function getStarImage(rate)
{
	if (rate < 0.1)
		imageFileName = "x0";
	else if (rate < 0.3)
		imageFileName = "x1";
	else if (rate < 0.5)
		imageFileName = "x2";
	else if (rate < 0.7)
		imageFileName = "x3";
	else if (rate < 0.9)
		imageFileName = "x4";
	else
		imageFileName = "x5";

	return "images/rating/" + imageFileName + ".gif";
}

