$(function(){
	$('.star-rating').rating();
	
	$('.auto-submit-star').rating({
		callback: function(idValue, link)
		{

			var elements = idValue.split(":");
			var productId = elements[0];
			var value = elements[1];
			var url = "/control/addReview?productId=" + productId + "&value=" + value;

			var ratingObj = $(this);

			$.getJSON(url, function(data)
			{
				//	See if we need to clear the current value of the star control
				if (data.ratingValue == 0)
				{
					var control = ratingObj.data('rating');
					if(control)
					{
						control.current = null;
						ratingObj.rating('draw');
					}
					value = 0;
				}

				//	See if there are other products on the same page. They will have the same productId as a class
				var selector = 'input.' + productId;
				var radios = $(selector);

				if (value > 0)
				{
					radios.rating('select', value - 1, false);
				}
				else
				{
					radios.each(function()
					{
						var control = $(this).data('rating');
						if(control)
						{
							if (control.current != null)
							{
								control.current = null;
								$(this).rating('draw');
							}
						}
					});
				}
			});
		}
	});
});

