var voted = false;

var MouseDrag = function(e) {
	if (!voted) {
		var totalWidth = $('ratings_stars_grey').getWidth();
		var mouseX = e.client.x - this.getPosition().x;
		var mouseY = e.client.y - this.getPosition().y;
		var mousePos = mouseX + 'px, ' + mouseY + 'px';
		$('vote_amount').set('value', (Math.round((mouseX / totalWidth) * 100)));
		$('ratings_stars').setStyle('width', (mouseX + 1));
	}
}

window.addEvent('domready',function(){
	var originalWidth = $('ratings_stars').getWidth();
	var totalWidth = $('ratings_stars_grey').getWidth();
	
	$('ratings_stars_grey').addEvents({
		'click': function(e){
			if (!voted) {
				var mouseX = e.client.x - $('ratings_stars_grey').getPosition().x;
				$('vote_amount').set('value', (Math.round((mouseX / totalWidth) * 100)));
			}
		},
		'mouseenter': MouseDrag,
		'mousemove': MouseDrag,
		'mouseleave': function(){
			if (!voted) {
				$('ratings_stars').setStyle('width', originalWidth);
			}
		}
	});
	
	
	$('rate_form').addEvent('submit', function(e){
		e.stop();
		if (!voted) {
			this.set('send', {
				onComplete: function(response){
					$('rate_response').set('html', response);
					voted = true;
				}
			});
			this.send();
		}
	});
});
