window.addEvent('domready', function(){
	setUpRatingStars();
	
	comment_formHTML = $('comment_form').get('html');
	
	$('comment_form').addEvent('submit', function(e){
		e.stop();
		if ($('user_id').value == '') {
			ajaxUrlPopup('/login/ajaxLogin/', e);
			return false;
		}
		else {
			var comment;
			var rating;
			this.set('send', {
				onRequest: function(){
					comment = $$('textarea[name=comment]').get('value');
					rating = $('rating').get('value');
					$('comment_form').setStyle('opacity', .5);
				},
				onComplete: function(response){
					$('comment_form').set('html', comment_formHTML);
					setUpRatingStars();
					$('comment_form').setStyles({
						'opacity': 1
					});
					if (response != '0') {
						$('comment_injection_point').set('html', response + $('comment_injection_point').get('html'));
						$('comment_injection_point').setStyle('opacity', 0).chain($('comment_injection_point').tween('opacity', 1));
					}
				}
			});
			this.send();
		}
	});
	
	$$('form[class=like]').addEvent('submit', function(e){
		e.stop();
		thisForm = this;
		this.set('send', {
			onRequest: function(){
				$$('input[commentid=' + thisForm.get('commentid') + ']').set('disabled', true);
			},
			onComplete: function(response){
				$$('div[commentid=' + thisForm.get('commentid') + ']').set('html', response);
			}
		});
		this.send();
	});
});

function setUpRatingStars(){
	var clicked = 0;
	$$('div.star').addEvent('mouseover', function(){
		$$('div.star').removeClass('hovered');
		var value = this.get('alt');
		for(var i = 1; i <= value; ++i){
			var string = 'div[alt='+ i +']';
			$$(string).addClass('hovered');
		}
		this.addEvent('click', function(){
			clicked = value;
			$('rating').set('value', value);
		});
	});
	$$('div.star').addEvent('mouseout', function(){									
		$$('div.star').removeClass('hovered');
		for (var i = 1; i <= clicked; ++i) {
			var string = 'div[alt=' + i + ']';
			$$(string).addClass('hovered');
		}
	});
}