function rate(rating) {	
	 
	$.post(voteHandler,
		   {rating: rating},
		   rateSuccessHandler
		);
}

function rateSuccessHandler(result) {
	
	 var avgRating = result.avg_rating;
	 var votes = result.votes;
	 var userRating = result.user_rating;
	 
	 if (avgRating > 0 && votes > 0) {
		 $('#avg_rating').html(avgRating); 
		 $('#votes_count').html("(based on " + votes + " votes)"); 
		 $('#user_rating').html("Your rating is: " + userRating);
	 } else {
		 $('#avg_rating').html("N/A");
		 $('#votes_count').html("(Not rated yet)");
	 }
	 
	 Cookie.set("tariff_rating_" + planId, userRating, 3600 * 24);
	 
	if (woopraEnabled) { 
		woopraTracker.postRating(userRating, result.object);		
	}
	
	
}

$(function() {
	var cookieValue = Cookie.get("tariff_rating_" + planId);

	var rated = cookieValue != null; 
	
	if (cookieValue != null) {
		$('#user_rating').html("Your rating is: " + cookieValue);
	}
	$("#rating_c").stars({
		cancelShow: false,
		oneVoteOnly: true,
		captionEl: null,
		disabled: rated,
		callback: function(rated, ui, type, value){
		
			if (rated) {
				return;
			}
			rate(value);
		}.curry(null, [rated])
	});
	
	
});	

