/**
	* this function is copied from capital javascript sources for testing reasons and
	* is modified to work in local enviroments.
	*
	* NOT FOR PRODUCTIVE USE.
**/	
	
var rating = Class.create({
	greyImg: '../source/css/img/immokompass/widgets/rating/stern_1.gif',
	emptyImg: '../source/css/img/immokompass/widgets/rating/stern_1.gif',
	filledImg: '../source/css/img/immokompass/widgets/rating/stern_3.gif',
	maxRating: 5,
	initialRating: 0,
	reviewer: 0,
	ratings: null, 
	editable: false,
	infoContainer: null,
	actionContainer: null,
	assetid: null,
	clickUrl: null,

	initialize: function(actionContainer, editable, reviewer, stars, assetid, clickUrl)
	{
		// var ratingtext = reviewer == 1 ? reviewer+' Bewertung' : reviewer+' Bewertungen';
		var ratingtext = ''; // Bewertungsanzahl auf Wunsch der Redaktion ausgeblendet
		//var infostr = '<span class="ratingtext">'+ratingtext+'</span></a>&nbsp;&nbsp;<span class="ratingpics"></span>';
		var actionstr = '<span class="ratingpics"></span>';
		
		/* init values */
		this.actionContainer = $(actionContainer);
		this.reviewer = reviewer;
		this.initialRating = stars;
		this.assetid = assetid;
		this.clickUrl = clickUrl;
		
		/* update container */
		$(this.actionContainer).update(actionstr);

		editable = true;//this.checkEdit(assetid, editable);	
	
		this.initStars(this.actionContainer.down('.ratingpics'), this.initialRating, editable);
		
		$(this.actionContainer).show();
	},
	
	initStars: function(container, stars, editable)
	{
		container.select('img').each(
			function(element){
				element.remove();
			});
		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) {
		/* the rating element */
		var ratingElement = Event.element(event);
		/* the rating value */
		var ratingValue = ratingElement.star;
		/* the asset id */
		var assetId = this.assetid;
		
		/**
		* UPDATE RATING VALUE THROUGH AJAX HERE.
		* execute the following functions on successful AJAX REQUEST:
		**/
		
		/* set rating on click */
		this.initStars(this.actionContainer.down('.ratingpics'), ratingValue, true);
		/* set new rating */
		this.initialRating = ratingValue;
	},
	
	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;
			}
		}
	}

});