/**
*	@package		Belvedere
*	@subpackage		Javascript
*	@author			Ben Sekulowicz-Barclay
*	@copyright		Copyright 2009, Ben Sekulowicz-Barclay.
*	@version		1.0
*
************************************************************************************************************************ **/

var belvedere_rating = Class.create(belvedere, {	
	
	/* ****************************************************************************************************************** */
	
	onDomLoad: function() { 		
		// Find our rating items
		$$('dl.listItemMeta dd.rating').each(function(d) {
			
			// Assign the rating to the 'rel' attribute
			d.setAttribute('rel', this._getRating(d));
			
			// Empty the placeholder text
			d.removeChild(d.firstChild);
			
			// Insert the rating elements
			d.insert({top: '<span class="one ir">1</span><span class="two ir">2</span><span class="three ir">3</span><span class="four ir">4</span><span class="five ir">5</span>'});
			
			// Monitor the elements for hovering ...
			$$('dl.listItemMeta dd.rating span').each(function(s) {
				s.observe('click', this.onSpanClick.bindAsEventListener(this));
				s.observe('mouseout', this.onSpanOut.bindAsEventListener(this));
				s.observe('mouseover', this.onSpanOver.bindAsEventListener(this));
			}.bind(this));
		}.bind(this));
	},
	
	/* ****************************************************************************************************************** */
	
	onSpanClick: function(e) {
		// Get the element and its parents
		var span = Event.findElement(e, 'span');
		var item = span.up();
		var list = item.up();
		
		// Kill all of the event observers to prevent interferance
		item.select('span').each(function(s) {
			s.stopObserving();
			s.style.cursor = 'default';
		}.bind(this));
		
		// Send the AJAX request
		new Ajax.Request(base_url + 'rating/', {
			method: 'post',
			parameters: {id: list.getAttribute('rel'), rating: this._getRating(span)},
			onSuccess: function(transport) {
				
				// Get the rating and escape it ...
				var rating = transport.responseText.stripScripts().stripTags();
				
				// If we have a correct rating string
				if (['one', 'two', 'three', 'four', 'five'].include(rating)) {
					
					// Update the list with the new rating 
					item.removeClassName(item.getAttribute('rel')).removeClassName(this._getRating(span)).removeClassName(this._getRating(item));					
					item.addClassName(rating).setAttribute('rel', rating);								
				}
				
			}.bind(this)
		});	
	},
	
	/* ****************************************************************************************************************** */
	
	onSpanOut: function(e) {		
		// Get the element and its parents
		var span = Event.findElement(e, 'span');
		var item = span.up('dd');
		
		// Change the rating
		item.removeClassName(this._getRating(item));
		item.addClassName(item.getAttribute('rel'));
	},
	
	/* ****************************************************************************************************************** */
	
	onSpanOver: function(e) {		
		// Get the element and its parents
		var span = Event.findElement(e, 'span');
		var item = span.up();
		
		// Change the rating
		item.removeClassName(this._getRating(item));
		item.addClassName(this._getRating(span));
	},
	
	/* ****************************************************************************************************************** */
	
	_getRating: function(e) {
		// Get the current rating on this set
		if (e.hasClassName('five')) {
			return 'five';
		} else 	if (e.hasClassName('four')) {
			return 'four';
		} else 	if (e.hasClassName('three')) {
			return 'three';
		} else 	if (e.hasClassName('two')) {
			return 'two';
		} else 	if (e.hasClassName('one')) {
			return 'one';
		} else {
			return 'zero';			
		}
	}
	
	/* ****************************************************************************************************************** */
});

var instance_rating = new belvedere_rating();	