function rate(rating) {	
	
	 new Ajax.Request(voteHandler, {parameters: "rating=" + rating,
		 							onSuccess: rateSuccessHandler});
}

function rateSuccessHandler(request) {
		
	
	 var result = request.responseJSON;
	 var avgRating = result.avg_rating;
	 var votes = result.votes;
	 var userRating = result.user_rating;
	 
	 if (avgRating > 0 && votes > 0) {
		 $('avg_rating').innerHTML = avgRating; 
		 $('votes_count').innerHTML = "(based on " + votes + " votes)"; 
		 $('user_rating').innerHTML = "Your rating is: " + userRating;
	 } else {
		 $('avg_rating').innerHTML = "N/A";
		 $('votes_count').innerHTML = "(Not rated yet)";
	 }
	 
	 Cookie.set("phone_rating_" + productId, userRating, 3600 * 24);
	 
	if (woopraEnabled) { 
		woopraTracker.postRating(userRating, result.object);		
	}
}

Event.observe(window, "load", function() {
	var cookieValue = Cookie.get("phone_rating_" + productId);

	var rated = cookieValue != null; 
	
	if (cookieValue != null) {
		$('user_rating').innerHTML = "Your rating is: " + cookieValue;
	}
	var ratingObj = new Control.Rating('rating_c',{  
	 value: productRating,  
	 rated: rated,  
	 min: 1,  
	 max: 10,
	 afterChange: rate});  
	
});	


