
				$(function(){
					$("#rat").children().not("select, #rating_title").hide();

					// Create caption element
					var $caption = $('<div id="caption" />');
					var $avg = $('<div id="avg" />');
					//var file_id = $("#file_id").val();

					// Create stars
					$("#rat").stars({
						inputType: "select",
						oneVoteOnly: true,
						captionEl: $caption,
						callback: function(ui, type, value)
						{
							// Display message to the user at the begining of request
							$("#messages").text("Сохраняем...").fadeIn(30);
							
							// Send request to the server using POST method
							$.post("/include/stars.php", {rate: value, file_id: $("#fileid").val()}, function(json)
							{
								// Change widget's title
								$("#rating_title").text("Рейтинг: ");
								
								// Select stars from "Average rating" control to match the returned average rating value
								ui.select(Math.round(json.average));
								
								// Update widget's caption
								//$caption.text(" (" + json.votes + " голосов; " + json.average + ")");
								$caption.text(" (" + json.votes + " голосов)");
								
								// Display confirmation message to the user
								//$("#messages").text("Оценка сохранена (" + value + "). Спасибо!").stop().css("opacity", 1).fadeIn(30);
								$("#messages").text("Сохранено").stop().css("opacity", 1).fadeIn(30);
								
								// Hide confirmation message after 2 sec...
								setTimeout(function(){
									$("#messages").fadeOut(1000)
								}, 2000);

							}, "json");
						}
					});

					// Since the <option value="3"> was selected by default, we must remove this selection from Stars.
					//$("#rat").stars("selectID", -1);
					
					// Update widget's caption
					if ( $("#votes").val() != '' ) {
						//$caption.text(" (" + $("#votes").val() + " голосов)");
						$avg.text( $("#average").val() );
					}

					$avg.appendTo("#rat");

					// Append caption element !after! the Stars
					$caption.appendTo("#rat");

					// Create element to use for confirmation messages
					$('<div id="messages"/>').appendTo("#rat");
				});

