//save rating on the server
function dRSaveRating(event){
	new Ajax.Request('./rating/saveRating.php', {
    	parameters: event.memo,
		onSuccess: function(xhr){
			event.findElement('.starbox').down('.indicator').update('<font color="#006600"><b>Thanks for voting</b></font>');
			new Effect.Pulsate(event.findElement('.starbox'));
			(function() { event.findElement('.starbox').down('.indicator').update(event.memo.average.toFixed(2) + ' / 7.00') }).delay(5);
			//(function() { event.findElement('.starbox').down('.indicator').update(xhr.responseText) }).delay(5);
		},
		onLoading: function(xhr){
			event.findElement('.starbox').down('.indicator').update('Updating...');
		},
		onFailure:  function(xhr){
			event.findElement('.starbox').down('.indicator').update('<font color="#990000"><b>Error while Updating!</b></font>');
		}
	});
}

// Create a Youtube style text indicator underneath the stars,
// using starbox custom events
function dRRatingChange(event) {
  var indicator = this.down('.indicator'),
  pick = (event.memo.rating || 0).round() - 1;

  // for restoring, store the indicator text on the star if it was not set before.
  this.starboxResetText = this.starboxResetText || this.down('.indicator').innerHTML;
  
  this.down('.indicator').update([
  	'Awful',
    'Poor',
	'Nothing special',
	'Once in a while',
	'Worth Listening',
	'Love it',
	'Awesome!'
  ][pick]);
}

// here we restore the indicator on mouseout, if needed
function dRRatingLeft(event) {
	this.down('.indicator').update(this.starboxResetText || '');
}

document.observe('starbox:rated', dRSaveRating); // observe the click