﻿var Rate = new Class({
	Implements: [Options],
	
	options: {
		wordList : '',
		ratedNum : 0, // how many time has the item been rated
		ratesNum : 0, // the sum of the rations
		defaultRate: 0,
		disabled : false,
		maxStars : 5,
		texts : ['Összecsapott','Elmegy','Lehetne jobb is...','Jó','Remek'],
		notRatedText : 'Értékeld...',
		ratedText: 'Értékelés elmentve!'
	},
	
	initialize: function(element, options){
		this.setOptions(options);
		this.element = $(element);
		this.rate = this.options.defaultRate;
		this.disabled = this.options.disabled;
		this.stars = [];
		
		this.rateStatus = new Element('span',{'class':'rateStatus', 'html':this.options.notRatedText});
		// rating messages
		this.element.adopt(this.rateStatus);
		
		
		this.rateMe = new Element('span',{'class':'rateMe'});
		// create stars
		this.options.texts.each(function(item, index){
			// create star
			var star = new Element('a',{
							'events':{
								'mouseover' : function(event){ if(!this.disabled){ this.over(event); } }.bind(this),
								'mouseout'  : function(event){ if(!this.disabled){ this.out(event); } }.bind(this),
								'click'		: function(event){ if(!this.disabled){ this.click(event); } }.bind(this)
							}
						});
			// index starts from 1
			star.store('index',index+1);
			// sore the stare
			this.stars.include(star);
			// add star to DOM
			this.rateMe.adopt(star);
		}.bind(this));
		// do not show if the rate is 0
		this.rated = new Element('span',{'class':'rated', 'html': (this.rate ? (this.options.ratesNum / this.options.ratedNum).round(1) : '')});
		
		this.element.adopt(this.rateMe, this.rated);
		// set default rate
		this.out();		
	},
	
	over : function(event){
		var index = event.target.retrieve('index');
		// show message
		this.rateStatus.set('html',this.options.texts[index-1]);
		
		this.stars.each(function(item){
			if(item.retrieve('index') <= index){
				item.addClass('on');	
			}else{
				item.removeClass('on');
			}
		});
		
	},
	// set rate
	out : function(event){
		var index = this.rate;
		//alert(index);
		// show default rate message
		if(index == 0){
			this.rateStatus.set('html',this.options.notRatedText);
		}else{
			this.rateStatus.set('html',this.options.texts[index-1]);
		}
		this.stars.each(function(item){
			if(item.retrieve('index') <= index){
				item.addClass('on');	
			}else{
				item.removeClass('on');	
			}
		});
	},
	
	click : function(event){
		this.rate = event.target.retrieve('index');
		this.disabled = true;
		this.options.ratedNum = this.options.ratedNum*1 + 1;
		this.options.ratesNum = this.options.ratesNum*1 + this.rate;
		this.rated.set('html',(this.options.ratesNum / this.options.ratedNum).round(1));
		// save rate
		this.sendRate();
	},
	
	sendRate : function(){
		new Request({'url':'wordlist.php',
					 'onComplete': function(){
						 this.rateStatus.set('html',this.options.ratedText);
					}.bind(this)
		}).send('cmd=rate&rate='+this.rate+'&wordlist='+this.options.wordList);	
	}	
});


window.addEvent('domready', function(){
	$$('span.ratings').each(function(item){
		var wordList = item.get('wordlist');
		var ratedNum = item.get('ratedtime'); 
		var ratesNum = item.get('ratings');
		var defaultRate = (ratedNum != 0) ? (ratesNum / ratedNum).round() : 0;
		
		new Rate(item, {'wordList':wordList, 'ratedNum':ratedNum, 'ratesNum':ratesNum, 'defaultRate':defaultRate});								 
	});
});
