/**
 * Star rating widget
 */  
kff.widgets.StarRating = function(element, options)
{
	this.options = $.extend({
		average: 0,
		starWidth: 19,
		starCount: 5,
		resolution: 5,
		click: null
	}, options);
	this.$element = $(element);
};

kff.widgets.StarRating.prototype.activate = function()
{
	var that = this;
	var offset = { left: 0 };
	var width = this.options.starWidth * this.options.starCount;
	var value = null;

	this.$innerElement = this.$element.find('span');
	this.$element.width(width);
	this.drawAverage();

	this.$element.bind('mouseenter', function(event)
	{
		offset = that.$element.offset();
		that.$element.addClass('hover');
		return false;
	});
	this.$element.bind('mousemove', function(event)
	{
		var ratio = (event.pageX - offset.left) / width;
		value = Math.ceil(that.options.resolution * ratio);
		that.$innerElement.css('width', (value / that.options.starCount * (that.options.starCount / that.options.resolution) * 100) + '%');
		event.preventDefault();
		return false;
	});
	this.$element.bind('mouseleave', function(event)
	{
		that.drawAverage();
		that.$element.removeClass('hover');
		return false;
	});
	this.$element.bind('click', function(event)
	{
		if(typeof(that.options.click) == 'function') that.options.click(that, value);
		event.preventDefault();
		that.$element.removeClass('hover');
		return false;
	});
}

kff.widgets.StarRating.prototype.deactivate = function()
{
	this.$element.unbind();
}

kff.widgets.StarRating.prototype.disable = function()
{
	this.$element.unbind();
}

kff.widgets.StarRating.prototype.setAverage = function(average)
{
	this.options.average = average;
}

kff.widgets.StarRating.prototype.drawAverage = function()
{
	this.$innerElement.css('width', (this.options.average * 100) + '%');
}
