
function Rating(elementName, currentRating) {
	this.elementName 	= elementName;
	this.currentRating 	= currentRating;
	
	/**
	 * Initialisiert das Rating
	 */
	this.init = function() {
		var span = document.getElementById(this.elementName + 'Span');
		
		if (span) {
			// Sterne hinzufügen
			for (var i = 1; i <= 5; i++) {
				var star = document.createElement('img');
				star.src = SITEURL + 'images/icon/noRatingSmall.png';
				star.alt = '';
				star.rating = this;
				star.name = i;
				
				star.onmouseover = function() { 
					this.style.cursor = 'pointer'; 
					this.rating.showRating(parseInt(this.name)); 
				};
				
				star.onclick = function() { 
					this.rating.submitRating(parseInt(this.name)); 
				};
				
				span.appendChild(star);
				
				span.rating = this;
				span.onmouseout = function() { 
					this.rating.showCurrentRating(); 
				};
			
				// Sichtbar machen
				span.className = '';
			}
		}
		
		if (this.currentRating > 0) {
			this.showCurrentRating();
		}
	}
	
	/**
	 * Zeigt das Rating des aktuellen Besuchers
	 */
	this.showCurrentRating = function() {
		this.showRating(this.currentRating);
	}
	
	/**
	 * Zeigt das ausgewählte Rating
	 */
	this.showRating = function(rating) {
		var span = document.getElementById(this.elementName + 'Span');
		
		if (span) {
			for (var i = 1; i <= rating; i++) {
				if (span.childNodes[i - 1]) {
					span.childNodes[i - 1].src = SITEURL + 'images/icon/ratingSmall.png';
				}
			}
			
			for (var i = rating + 1; i <= 5; i++) {
				if (span.childNodes[i - 1]) {
					span.childNodes[i - 1].src = SITEURL + 'images/icon/noRatingSmall.png';
				}
			}
		}
	}
	
	/**
	 * Übermittelt das gewählte Rating
	 */
	this.submitRating = function(rating) {
		this.currentRating = rating;
		
		jQuery('#'+elementName).attr('value', rating);
		document.getElementById('RatingForm').submit();
	}
	
	this.init();
}