GenericRating = new Class({

	/**
	 * OPTIONS
	 *
	 * Required
	 * displayOnly {boolean}: Whether the display is modifiable.
	 *
	 * Optional
	 * rating {number}: The rating to initialize to.
	 *
	 * Callbacks
	 * onComplete {function}: Called when a rating is selected.
	 */

	initialize: function(id, options) {
		this.options = options || {};
		this.rating = this.options.rating || 0;
		this.imagename = this.options.imagename || 'star';
		//alert(this.imagename);
		var item = jQuery('#' + id);
		this.stars = item.find('img');
		var self = this;

		if (!this.options.displayOnly) {
			item.bind('mouseout', {self: this}, this.onHoverOut);

			this.stars.each(function(index) {
				this.index = index + 1;
				jQuery(this).bind('click', {self: self}, self.onClick)
					.bind('mouseover', {self: self}, self.onHoverOver);
			})
		}

		this.display(this.rating, 'rating');
	},

	display: function(rating, mode, imagename) {
		var self = this;

		//prendo o il valore del parametro o la proprieta' dell'oggetto
		rating = rating == null ? this.rating : rating;
		imagename = imagename == null ? this.imagename : imagename;

		var whole = Math.floor(rating);
		var fraction = rating - whole;

		this.stars.each(function(index) {
			image = this;
			exp= new RegExp("\\b"+imagename+"_.*\\.");
			//alert(exp);
			if (index < whole) {
				
				if (mode == 'hover') {
					image.src = image.src.replace(exp, imagename+'_hover.');
				}
				else {
					image.src = image.src.replace(exp, imagename+'_on.');
				}
			}
			else {
				if (fraction < 0.25) {
					image.src = image.src.replace(exp, imagename+'_off.');
				}
				else if (fraction < 0.50) {
					image.src = image.src.replace(exp, imagename+'_on_quarter.');
				}
				else if (fraction < 0.75) {
					image.src = image.src.replace(exp, imagename+'_on_half.');
				}
				else if (fraction < 1.00) {
					image.src = image.src.replace(exp, imagename+'_on_threequarters.');
				}
				fraction = 0;
				
			}
		});
	},

	onHoverOver: function(event) {
		event.data.self.display(this.index, 'hover');
	},

	onHoverOut: function(event) {
		event.data.self.display();
	},

	onClick: function(event) {
		var target = this;
		var newRating = target.index;
		var self = event.data.self;

		self.rating = newRating;

		if (self.options.onComplete) {
			self.options.onComplete(newRating);
		}

		self.display(newRating);
	}
});