/*  CMSBox v1.0 (c) 2007
 *  Author:  Ron Miller
 *  Created: 09/01/07
 *
 *  This object is used to handle the submission of game ratings
/*--------------------------------------------------------------------------*/

rate = {	
	highlight: function(total) {
		var highlighted = 0;
		var starImages = $('rating_stars').getElementsByTagName('img');
				
		$A(starImages).each(function(img) {
			img.src = img.src.replace(/_o(n|ff)/i, "_o"+(highlighted >= total ? 'ff' : 'n'));
			highlighted++;
		});
	},
	
	revert: function(average) {
		var highlighted = 0;
		var starImages = $('rating_stars').getElementsByTagName('img');
		
		$A(starImages).each(function(img) {
			img.src = img.src.replace(/_o(n|ff)/i, "_o"+(highlighted >= average ? 'ff' : 'n'));
			highlighted++;
		});
	},

	submit: function(game_id, rating) {
		new Ajax.Request('/requests/rate/',
		{
			parameters: {
				i: game_id,
				r: rating
			},
			
			onSuccess: function(req) {
				var rateObj = eval('('+req.responseText+')');
				if(!rateObj.error) {
					rate.highlight(rating);
					$('rating_thanks').update('&nbsp;Thanks for your rating!');
					
					var starLinks = $('rating_stars').getElementsByTagName('a');					
					$A(starLinks).each(function(link) { 
						link.replace(link.innerHTML);
					});
				} else {
					alert(rateObj.message);
				}
			},
			
			onFailure: function() {
				alert("Your rating could not be processed at this time.");
			}
		});
	}	
}
