/**
 * @version		$Id: ratings.js 716 2010-06-25 21:43:03Z robs $
 * @package		JXtended.Comments
 * @subpackage	com_comments
 * @copyright	Copyright (C) 2008 - 2010 JXtended, LLC. All rights reserved.
 * @license		GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
 * @link		http://jxtended.com
 */

/**
 * JXtended Ratings behavior class for Comments.
 *
 * @package		JXtended.Comments
 * @subpackage	com_comments
 * @version		1.2
 */
var JXRatings = new Class({
	
	/**
	 * Method to decorate the rating forms with the ratings behavior.
	 */
	decorate: function(forms)
	{
		// Decorate the forms.
		forms.each(function(form) {
			// Get the star links.
			var links = form.getElements('ul.rating-stars a');
			
			links.each(function(link) {
				// Tie the link to the parent form.
				link.setProperty('form', form.getProperty('id'));
				
				// Set the cursor style.
				link.setStyle('cursor', 'pointer');
				
				// Attach the onClick behavior.
				link.addEvent('click', function(e) {
				
					// Stop the click event.
					e = new Event(e);
					e.stop();
					
					// Get the parent form.
					var form	= $(e.target.getProperty('form'));
					var	context	= form.getProperty('id').replace('rate-', '');
	
					// Convert the action to a JSON request.		
					var query	= 'protocol=json&tmpl=component&format=raw';
					var action	= form.getProperty('action');
					form.setProperty('action', action.contains('?') ? action+'&'+query : action+'?'+query);
					
					// Set the value of the rating.
					form.score.value = e.target.getProperty('rel');
					
					form.send({
						onComplete: function(response)
						{
							// Decode the JSON response.
							response = Json.evaluate(response);
							
							// Replace the page tokens.
							replaceTokens(response.token);
							
							// Check for errors.
							if (response.error == true) {
								alert(response.message);
							} else {
								// Get the rating counter.
								var counter = $('rating-count-'+context).getElement('span.count');
								var string	= $('rating-count-'+context).getElement('span.string');
								
								// Adjust the rating counter.
								counter.setText(response.pscore_count);
								string.setText(response.counter_text);	
	
								// Adjust the rating stars.
								stars = form.getElement('.current-rating');
								stars.setStyle('width', Math.floor(response.pscore * 100) + '%');
							}
						},
						
						onFailure: function(response)
						{	
							// Decode the JSON response.
							response = Json.evaluate(response.responseText);
							
							// Replace the page tokens.
							replaceTokens(response.token);
							
							// Alert the user about the error.
							alert(response.message);
						}
					});
				});
			});
		});
	}
});

function replaceTokens(n)
{
	$(document).getElements('input').each(function(e){
		if (e.getProperty('name') && e.getProperty('name').length == 32 && e.getProperty('value') == '1' && e.getProperty('type') == 'hidden') {
			e.setProperty('name', n);
		}
	});
}

window.addEvent('domready', function(){
	Ratings = new JXRatings();
	Ratings.decorate($$('form.addrating'));
});
