function initalize_content_rating() {
	links = $$("a.rateLink");
	links.each(function(link) {
		link.onclick = function() {
			var item_id = null;
			var rating = null;
			this.id.scan(/rate_(\d+)_with_(\d)/i, function(match) {
				item_id = match[1];
				rating = match[2];
			});
			var url = '/content/rate/'+item_id+'/'+rating+'.json';
			new Ajax.Request(url, {method: 'get', asynchronous: true, requestHeaders: {Accept: 'text/x-json'},
				onSuccess: function(transport, json) {
					var json = transport.responseText.evalJSON(true);
					if (json.status == 'OK') {
						$('vote_count_for_' + item_id).innerHTML = parseInt(json.item.ratings_count) != 1 ? json.item.ratings_count + ' stemmer' : json.item.ratings_count + ' stemme';
						$('current_rating_for_' + item_id).style.width = (100 - (parseInt(json.item.average_rating) * 25)) + '%';
					}
					display_message_for_content(json.message, item_id);
					window.setTimeout(fade_to_rating_for_content, 3 * 1000, item_id);
				},
				onFailure: function() {
					display_message_for_content('Fejl: Kunne ikke kommunikere med serveren...', item_id);
					window.setTimeout(fade_to_rating_for_content, 3 * 1000, item_id);
				}
				});
			return false;
		}
	});
}

function display_message_for_content(message, item_id) {
	$('rating_message_for_' + item_id).innerHTML = message;
	new Effect.Parallel([
		new Effect.Fade('vote_count_for_' + item_id, {sync: true}),
		new Effect.Appear('rating_message_for_' + item_id, {sync: true})
	], {duration: 0.8, queue: {position:'front', scope: 'rating_scope'}}
	);
}

function fade_to_rating_for_content(item_id) {
	new Effect.Parallel([
		new Effect.Fade('rating_message_for_' + item_id, {sync: true}),
		new Effect.Appear('vote_count_for_' + item_id, {sync: true})
	], {duration: 0.8, queue: {position:'end', scope: 'rating_scope'}}
	);
}

document.observe('dom:loaded', initalize_content_rating);