﻿var StarRater=new Class({
	Implements:[Options,Events],
	
	options:{
		maxStars:5,
		onRate:$empty
	},
	initialize:function (element,options) {
		this.setOptions(options);
		this.element=element;

		this.element.getElements("a").setProperty("hideFocus","hideFocus").addEvent("click",function (e) {
			this.element.addClass("loading");
		
			//e.target is the <a> element which carries in its className the rate (as in star-###)
			e.stop();
			var rate=/\bstar\-(\d+)\b/.test(e.target.className) ? +RegExp.$1 : 0;
			e.target.blur();
			// fire onRate event which gets a rate and callback function (onAfterRate) which gets the new rate
			this.fireEvent("onRate",[rate,this.onAfterRate.bind(this)]);
		}.bind(this));
	},
	onAfterRate:function (newRate) {
		this.element.addClass("disabled");
		this.element.getElement(".current-rating")
			.setStyles({width:(Math.round(newRate)/this.options.maxStars*100)+"%"});
		this.element.removeClass("loading");
	}
});
