// get rating function
function getRating(ratableKey)
{
	$.ajax({
		type: "GET",
		url: "api-js-get-rating.php",
		data: "ratableKey="+ratableKey+"&do=getrate",
		dataType: "xml",
		processData: false,
		success : function(xml) {
			var voteText;
			// apply star rating to element
			$("#"+ratableKey).css({ width: "" + $(xml).find('percent').text() + "%" });
			
			if($(xml).find('totalRatings').text() > 1)
				voteText = $(xml).find('totalRatings').text()+" votes";
			else
				voteText = $(xml).find('totalRatings').text()+" vote";
			
			document.getElementById(ratableKey+"Result").innerHTML  = $(xml).find('rate').text()+" / 5 ("+voteText+")";
		},
		error: function(result) {
			alert("some error occured, please try again later");
		}
	});
}

// link handler
//$('#ratelinks li a').click(function(){
function setRating(ratableKey,rating)
{
	$.ajax({
		type: "GET",
		url: "api-js-get-rating.php",
		data: "ratableKey="+ratableKey+"&rating="+rating+"&do=rate",
		cache: false,
		async: false,
		success: function(result) {
			// remove #ratelinks element to prevent another rate
			//$("#"+ratableKey).remove();
			// get rating after click
			getRating(ratableKey);
		},
		error: function(result) {
			alert("some error occured, please try again later");
		}
	});
	
}



