rating = {
	greyImg: '/img/stern1.gif',
	emptyImg: '/img/stern2.gif',
	filledImg: '/img/stern3.gif',
	maxRating: 5,
	initialRating: 0,
	reviewer: 0,
	ratings: null,
	editable: false,
	infoContainer: null,
	actionContainer: null,
	assetid: null,
	clickUrl: null,

	initialize: function(infoContainer, actionContainer, editable, reviewer, stars, assetid, clickUrl)
	{
		var ratingtext = reviewer == 1 ? reviewer+' Bewertung' : reviewer+' Bewertungen';
		var infostr = '<a href="#bewertung"><span class="ratingtext">'+ratingtext+'</span></a>&nbsp;&nbsp;<span class="ratingpics"></span>';
		var actionstr = '<span class="ratingtext">'+ratingtext+'</span>&nbsp;&nbsp;<span class="ratingpics"></span>';
		this.infoContainer = $(infoContainer).update(infostr);
		this.actionContainer = $(actionContainer).update(actionstr);
		this.reviewer = reviewer;
		this.initialRating = stars;
		this.assetid = assetid;
		this.clickUrl = clickUrl;

		editable = this.checkEdit(assetid, editable);
		this.initStars(this.infoContainer.down('.ratingpics'), this.initialRating, false);
		this.initStars(this.actionContainer.down('.ratingpics'), this.initialRating, editable);
		this.infoContainer.show();
		this.actionContainer.show();
	},



	checkEdit: function(assetid, editable) {
		var cookies = new CookieJar({});
		this.ratings = cookies.get('FTDRating');
		if (!(this.ratings instanceof Object) || typeof this.ratings[assetid] == 'undefined') {
			new Ajax.Request(this.clickUrl, {
				asynchronous: false,
				parameters: $H({aid: this.assetid}),
				onSuccess: (function(transport, json)
				{
					this.initialRating = json.rating;
					this.reviewer = json.votes;
				}).bind(this)
			});
		}
		this.ratings = cookies.get('FTDRating');
		editable = (typeof this.ratings[assetid] != 'undefined' && !this.ratings[assetid].stored && editable);
		return editable;
	},


	initStars: function(container, stars, editable)
	{
		for (var i = 0; i < this.maxRating; i++) {
			var img = document.createElement('img');
			img.star = i+1;
			if (editable) {
				img.style.cursor = 'pointer';
				img.onclick = Prototype.emptyFunction;
				Event.observe(img, 'click', this.clickEvent.bindAsEventListener(this));
				Event.observe(img, 'mouseover', this.showEvent.bindAsEventListener(this));
			}
			container.appendChild(img);
		}
		Event.observe(container, 'mouseout', this.showEvent.bindAsEventListener(this));
		this.show(container, stars);
	},

	clickEvent: function(event) {
		var img = Event.element(event);
		new Ajax.Request(this.clickUrl, {
			parameters: $H({aid: this.assetid, rank: img.star}),
			onSuccess: (function(transport, json)
			{
				this.initialize(this.infoContainer, this.actionContainer, false, json.votes, json.rating, null, null);
			}).bind(this),
			onFailure: (function(transport, json)
			{
				this.initialize(this.infoContainer, this.actionContainer, false, this.reviewer, this.initialRating, null, null);
			}).bind(this)
		});

	},

	showEvent: function(event) {
		var stars = this.initialRating;
		if (event.type == 'mouseover') {
			var img = Event.element(event);
			stars = img.star;
		}
		this.show(Event.findElement(event, 'span'), stars);
	},


	show: function(container, stars)
	{
		var initial = Math.round(this.initialRating)
		stars = Math.round(stars);
		for (var i = 1; i < this.maxRating+1; i++) {
			img = container.childNodes[i-1];
			if (i > stars) {
				img.alt = '';
				if (i > initial) {
					img.src = this.emptyImg;
				} else {
					img.src = this.greyImg;
				}
			} else {
				img.alt = '*';
				img.src = this.filledImg;
			}
		}
	}

}
