$(document).ready(function(){
	$("input[type=radio].MamaStar").rating({
		required: true,
		callback:function(value,link){rate(this.form,this);}
	});
	$("div.UserRating input[type=submit]").hide();
});
function rate(data,wrapper)
{
	// Create new JsHttpRequest object.
	var req = new JsHttpRequest();
	action='rate';

  // Code automatically called on load finishing.
  req.onreadystatechange = function()	{
    if (req.readyState == 4)
		{
      //get errors from backend
      if(req.responseText)
      {
          $.facebox(req.responseText);
      }
			//get data from backend		
      if (req.responseJS)
      {
      	if(req.responseJS.ok)
      	{
					$(data).find("input[type=radio].MamaStar").rating("disable");
					$(data).find(".UserRating-average").html(req.responseJS.average / 1);
					$(data).find(".UserRating-votes").html(req.responseJS.votes);
					$(data).find(".UserRating-label").html('Моя оценка');
					$(data).find(".success").html(req.responseJS.success).slideDown();
      	}
      	else
      	{
					$(data)
						.find(".error")
						.html(req.responseJS.error).slideDown();
      	}
      }
    }
  }
  // Prepare request object (automatically choose GET or POST).
  req.open(null, '/js/rating.php', true);
	req.send({action:action,rate:data});
}
