(function($) {
	$(document).ready(function() {
		var getRating = function(box, url) {
			$.get(url, function(data) {
				$(box).html(data);
			});
		}
		
		getRating($('.user-rating'), $('.user-rating').attr('location'));
		getRating($('.rating'), $('.rating').attr('location'));

		// on mouseover set bullets
		$('img.rate-article')
			.livequery('mouseover', function(e) {
				// bullet where mouseover occurs and previous bullets => choose
				$(this)
					.prevAll('img')
					.andSelf()
					.attr({
						src: $(this).attr('src').replace('rating_bullet_large_grey', 'rating_bullet_large'),
						selected: true
					});
					
				// bullet after mouseover => cleanup
				$(this)
					.nextAll('img')
					.attr({
						src: $(this).attr('src').replace('rating_bullet_large', 'rating_bullet_large_grey'),
						selected: false
					});
			});

		// on click bullet post instead of using submit button
		$('img.rate-article')
			.livequery('click', function(e) {
				var parent = $(this).parent();
				var value = $(this).prevAll().andSelf().length;
				$.post($(parent).attr('action'), {value: value}, function() {
					getRating($('.user-rating'), $('.user-rating').attr('location'));
					getRating($('.rating'), $('.rating').attr('location'));
				});
			});

		$('form#form-article')
			.livequery('submit', function() {
				var value = $(this).find('img[@selected=\'true\']').length;
				$.post($(this).attr('action'), {value: value}, function() {
					getRating($('.user-rating'), $('.user-rating').attr('location'));
					getRating($('.rating'), $('.rating').attr('location'));
				});
				
				return false;
			});
	});
})(jQuery);