/*
 * dFrame Rating Script for modRating support
 * http://www.dframe.de/
 */

$(function()
{
	$(".df_rating").each(function()
	{
		var r = this;
		var oldcomment = $(".df_ratingcomment", r).text();
		var clicked = false;
		
		$("A", r).each(function()
		{
			var l = this;
			var title = $(l).attr("title");
			$(l).attr("title", "");
			
			$(this).mouseover(function()
			{
				if (clicked) return;

				$(l).prevAll("A").andSelf().find("IMG").each(function()
				{
					$(this).data("oldsrc", $(this).attr("src"));
					$(this).attr("src", "/design/star_red.png");
				});
				$(l).nextAll("A").find("IMG").each(function()
				{
					$(this).data("oldsrc", $(this).attr("src"));
					$(this).attr("src", "/design/star_shade.png");
				});
				
				var t = $(l).attr("title");
				if (title)
					$(".df_ratingcomment", r).text(title);
			});
			$(this).mouseout(function()
			{
				if (clicked) return;

				$(l).siblings("A").andSelf().find("IMG").each(function()
				{
					$(this).attr("src", $(this).data("oldsrc"));
				});
				
				$(".df_ratingcomment", r).text(oldcomment);
				
			});
			$(this).click(function()
			{
				if (clicked)
					return false;

				clicked = true;
				$.getJSON($(l).attr("href")+"/ajax", null, function(data)
				{
					if (!data || !data.success)
					{
						clicked = false;
						$(l).mouseout();
						return;
					}
					
					$(".df_ratingcomment", r).text(data.message);
				});
				return false;
			});
		});
	});
});