jQuery.fn.extend({
	rating   : function() 
	{ 
		return this.each( function() 
		{
			var ratingElement = $(this);
			
			var splitResult = ratingElement.attr("title").split(";");
			if (splitResult.length != 5)
			{
				ratingElement.html("Ai mai votat deja?");
				return;
			}
			
			ratingElement.empty();
	
			var rating = Math.round(parseFloat(splitResult[1]));
			
			ratingElement.attr("title", "Voturi: " + splitResult[0] + " Nota: " + Math.round(parseFloat(splitResult[1])*100)/100 );
			ratingElement.attr("jid", splitResult[2]);
			ratingElement.attr("jclass", splitResult[3]);
			
			for (var i = 0; i < 5; i++)
			{
				var rElm = $("<img/>").attr("src", i < rating ? "/static/love.gif" : "/static/love-gri.gif");
				rElm.attr("pos", i+1);
				
				rElm.mouseover( function() {
					$(this).attr("oldsrc", $(this).attr("src"));
					$(this).attr("src", "/static/love-dim.gif");
					$(this).prev().trigger("mouseover");
				});
				
				rElm.mouseout( function() {
					$(this).attr("src", $(this).attr("oldsrc"));
					$(this).prev().trigger("mouseout");
				});
				
				rElm.click( function() {
				
					var ratingElement = $(this).parent();
					ratingElement.children().hide();
					ratingElement.append( "<span class='ajax1' style='width:70px'>&nbsp;&nbsp;&nbsp;&nbsp;</span>" );
	
					$.ajax( {
						type: "GET",
						url: "?q=rate&id=" + ratingElement.attr("jid") + "&class=" + ratingElement.attr("jclass") + "&nota=" + $(this).attr("pos"),
						dataType: "HTML",
						success: 
							function(result) {
								$(ratingElement).attr("title", result);
								$(ratingElement).rating();
								if ( window.ratingAfterVote != null ) 
									ratingAfterVote();
							}
						} );
				});
				
				ratingElement.append(rElm);
			}

			return this;

		});
	}
});


