var MOORating = new Class({
	Implements: [Options, Log],
	options: {
		mode: 'normal', // one of (percent, fractional, fixed)
		votes: 0,
		stars: 5,
		width: 84,
		votesLabel: 'Votos:',
		onUpdate : $empty
	},
	initialize: function(wrapper, options){
		this.setOptions(options);
		this.wrapper = wrapper || document.id(wrapper) || null ;
		this.stars = document.id(wrapper.id+"-stars");
		this.value = document.id(wrapper.id+"-value");
		this.votes = document.id(wrapper.id+"-votes");
		
		this.bindEvents();
	},
	bindEvents:function(){
			var self = this;
			this.revert =true;
			this.submited = false;
			if(this.options.mode != 'fixed'){
				if(this.stars)
				this.stars.addEvents({
					'mousemove': function(event){
						if(!self.submited){
						 	var w = event.client.x - this.getPosition().x;
						 	this.getFirst('span').setStyle('width', w);
							var x = (w/self.options.width) * self.options.stars;
							if(self.options.mode == 'percent'){
								var v = Math.round(w/this.options.width*100);
								if(v <101 && self.value) self.value.set('text', Math.round(w/this.options.width*100)+'%');
							}else if(self.options.mode == 'fractional'){
								if((x<=self.options.stars || x >=0) && self.value) self.value.set('text',self.formatNumber(x,2));
							}
							else{//normal
								if(self.value)
								self.value.set('text', Math.ceil(x));
								//self.value.innerHTML= Math.ceil(x);
								w = Math.ceil(x)*84/5;
								this.getFirst().setStyle('width', w);
							}
						}
					},
					
					'click': function(){
							self.updateRating(self.wrapper.id,self.value.get('text'));
							this.getFirst().title = parseFloat(self.value.get('text'));
							self.revert = false;
							self.submited = true;
							self.votes.set('text', self.options.votesLabel+" "+(1+self.options.votes));
					},
					'mouseleave': function(){
							if(self.revert && !self.submited){
								var v = self.formatNumber(this.getFirst('span').title,2);
								if(self.options.mode == 'percent'){
									var w = v/100 * self.options.width;
									self.value.set('text', v +'%');
								}else{
									var w = v * (self.options.width/self.options.stars);
									self.value.set('text', v);
								}
								this.getFirst('span').setStyle('width', w);
							}
					}
				});
			}
	},
	setData:function(value, votes){
		if(this.options.mode == 'percent'){
			var w = value/100 * this.options.width;
			this.value.set('text', v +'%');
		}else{
			var w = value * (this.options.width/this.options.stars);
			this.value.set('text', value);
		}
		this.stars.getFirst('span').setStyle('width', w);
		this.start.getFirst('span').title = value;
		this.votes.set('text', votes);
	},
	formatNumber:function (myNum, numOfDec) { 
		var decimal = 1;
		for(i=1; i<=numOfDec;i++)
			decimal = decimal *10
		var myFormattedNum = (Math.round(myNum * decimal)/decimal).toFixed(numOfDec)
		return myFormattedNum;
	},
	updateRating: function (id, rating) {
		if(this.options.onUpdate){
			this.options.onUpdate(id, rating, this);
		}
	} 
});

