/**
 * @author Antonio G Greco
 */
 
Event.observe(window, 'load', function() {
//Event.observe(document, "dom:loaded", function(){
	if ($$('.rate')[0]) {
		var rating_a = new Rating({container: '.rate'});
	}
});

Rating = Class.create({
	
	mainContainer: null,
	loader: null,
	hoverText: null,
	unorderedList: null,
	form:          null,
	
	createOLElement: function(num){
		this.orderedList = new Element('ol', {'class':'score-'+num});
		liElement = new Element('li');
		pElement = new Element('p');
		this.orderedList.insert(liElement);
		liElement.insert(pElement);
		this.mainContainer.insert(this.orderedList);
	},

	initialize: function(opt){
		if (opt.container == null) throw ('Rating(): a rating container needs to be specified.');
		this.mainContainer = $($$(opt.container)[0]);
		if (!this.mainContainer.down('ol.set-rating')) return;
		this.applyRating();
	},
	
	applyRating:function(){
		this.form = this.mainContainer.down('.rating-range');
		var rated = this.hasRated();
		this.mainContainer.down('ol.set-rating').select('a').each(function(item){
			if (!rated) {
				item.observe('mouseover', this.hoverOver.bindAsEventListener(this, item));
				item.observe('mouseout', this.hoverOut.bindAsEventListener(this, item));
				item.observe('click', this.sendRating.bindAsEventListener(this, item));
			}
			else {item.hide();}
		}.bind(this));
	},
	
	hoverOver:function(event, item){
		item.up('ol').addClassName(item.className);
	},
	
	hoverOut: function(event, item){
		item.up('ol').removeClassName(item.className);
	},
    
	sendRating: function(event, item){
		Event.stop(event);
		var idNum = item.className.split('-');
		var score = idNum[1].sub('dot', '.');
		this.loading(score);
		this.form.request({
			parameters: {score:score},
			onComplete:function(response){
				this.loaded(response);
			}.bind(this),
			onFailure: function(response){
				this.error(response);
			}.bind(this)
		});
	},
	
	loading: function(num){
		this.mainContainer.down('ol.set-rating').remove();
		this.mainContainer.down('p.rateTextInline').innerHTML = 'Please wait...';
		this.createOLElement(num);
		this.loader = new Element('p', {'class':'loader'});
		this.mainContainer.insert(this.loader);
	},
	
	loaded:function(response){
		this.mainContainer.removeChild(this.loader);
		this.mainContainer.down('p.rating-response').show();
		this.mainContainer.down('p.rateTextInline').hide();
		this.removeRef();	
	},
	
	error: function(response){
		this.mainContainer.removeChild(this.unorderedList);
		this.mainContainer.removeChild(this.loader);
		this.removeRef();
	},
	
	removeRef: function(){
		delete this.mainContainer;
		delete this.hoverText;
		delete this.loader;
		delete this.unorderedList;
		this.mainContainer = null;
		this.loader = null;
		this.unorderedList = null;
	},

	/* Make an array from the cookie, check our form value is in the array
	 */
	hasRated: function() {
		var cookieVal = JAMLite.utils.readCookie('ratingsRated');
		var rated = []; 
		if(!cookieVal) return;
		cookieVal.scan(/\d+-\w+/, function(crumb) {rated.push(crumb[0])});
		currentRating = $F(this.form.elements['object_id']) + '-' + $F(this.form.elements['object_type']);
		return rated.include(currentRating);
	}

});
