(function($)
{
	$.fn.displayRating = function(ops)
	{
		var ops = jQuery.extend(
		{
			increment         : 1,
			maxValue          : 5,
			attribute         : 'rating',
			starClass         : 'star disabled',
			starLeftClass     : 'star-left',
			starRightClass    : 'star-right',
			starSelectedClass : 'selected'
		}, ops || {});

		return this.each(function()
		{
			var rating = parseFloat($(this).attr(ops.attribute));
			var idx    = 0;

			if (rating >= 0)
			{
				for (var i=0; i<=5; i++)
				{
					if (i > 0)
					{
						var $star = $("<div class='"+ops.starClass+"'></div>").append("<a>"+i+"</a>").appendTo(this);
						if (ops.increment == .5)
						{
							$star.addClass((idx % 2) ? ops.starLeftClass : ops.starRightClass);
						}

						if (i <= rating)
						{
							$star.addClass(ops.starSelectedClass);
						}
					}

					i = i-1+.5;
					idx++;
				}
			}
		});
	}

	$.fn.rating = function(cback, ops)
	{
		if (cback == null || cback == undefined)
			return;

		var ops = jQuery.extend(
		{
			callback          : cback,
			increment         : 1,
			curValue          : 0,
			maxValue          : 5,
			dblClick          : true,
			starClass         : 'star',
			starLeftClass     : 'star-left',
			starRightClass    : 'star-right',
			starHoverClass    : 'hover',
			starSelectedClass : 'selected'
		}, ops || {});

		return this.each(function()
		{
			var cont = jQuery(this);
			var idx  = 0;

			jQuery.extend(cont,
			{
				avgRating : ops.curValue,
				callback  : ops.callback
			});

			ops.increment = (ops.increment < .75) ? .5 : 1;
			for (var i=0; i<=ops.maxValue; i++)
			{
				if (i > 0)
				{
					var $star = $("<div class='"+ops.starClass+"'></div>").append("<a title='Rate it: "+i+"/"+ops.maxValue+"'>"+i+"</a>").appendTo(cont);
					if (ops.increment == .5)
					{
						$star.addClass((idx % 2) ? ops.starLeftClass : ops.starRightClass);
					}
				}

				i = i-1+ops.increment;
				idx++;
			}
			
			var stars = jQuery(cont).children('.'+ops.starClass);
			stars.mouseover(function()
			{
				event.clear();
				event.fill(this);
			}).mouseout(function()
			{
				event.clear();
				event.reset();
			}).focus(function()
			{
				event.clear();
				event.fill(this);
			}).blur(function()
			{
				event.clear();
				event.reset();
			}).click(function(e)
			{
				ops.curValue = (stars.index(this)*ops.increment)+ops.increment;
				ops.callback.apply(cont, [ops, e]);

				e.preventDefault();
				return false;
			}).dblclick(function(e)
			{
				if (ops.dblClick)
				{
					ops.curValue = 0;
					ops.callback.apply(cont, [ops, e]);

					event.clear();
				}

				e.preventDefault();
				return false;
			});

			var event =
			{
				fill : function(el)
				{
					stars.children('a').css('width', '100%').end().slice(0, stars.index(el)+1).addClass(ops.starHoverClass).end();
				},

				clear: function()
				{
					stars.filter('.'+ops.starSelectedClass).removeClass(ops.starSelectedClass).end().filter('.'+ops.starHoverClass).removeClass(ops.starHoverClass).end();
				},

				reset: function()
				{
					stars.slice(0, ops.curValue/ops.increment).addClass(ops.starSelectedClass).end();
				}
			}

			event.reset();
		});
	}
})(jQuery)