var timer = null;
var timerRated = null;
var rated = false;

function displayRatingStars( productId, productType, productScore, rating ) {
	
	if(! rated) {
		if(timer != null)
			window.clearTimeout(timer);
		
		// Creates the stars
		createStars (rating, "rating");
						
		var rateId = "R" + productId + "" + productType;
		var libCookies = new lib_cookies();
		var wasRated = libCookies.getCookieValue(rateId);
		
		// Creates the text
		if( ! wasRated) {
			document.getElementById("scoring_text").innerHTML = rating + " Sterne <br /> Wertung abgeben";
		} else {
			document.getElementById("scoring_text").innerHTML = "Sie haben dieses Produkt <br /> bereits bewertet";
		}
	}
		
}

function displayScoringStars( productId, productType, productScore ) {
	
	if(! rated) {
		// Creates the stars
		createStars (productScore, "full");
		
		// Creates the text
		if( productScore > 0) {
			document.getElementById("scoring_text").innerHTML = "Kundenbewertung <br /> " + productScore + " von 5 Sternen";
		} else {
			document.getElementById("scoring_text").innerHTML = "Noch keine Bewertung <br /> vorhanden";
		}
	}
	
}

function rateProduct( productId, productType, productScore, rating ) {
	
	
	var rateId = "R" + productId + "" + productType;
	var libCookies = new lib_cookies();
	var wasRated = libCookies.getCookieValue(rateId);
	
	if(! wasRated) {
		
		rated = true;
		libCookies.setCookieValue(rateId, true);
		
		tx_amarokcatalog_pi1rateProduct(productId, productType, rating);
		
		for( var i=1; i <= 5; i++) {
			document.getElementById("scoringStar_" + i).removeAttribute('onmouseover');
			document.getElementById("scoringStar_" + i).removeAttribute('onmouseout');
			document.getElementById("scoringStar_" + i).removeAttribute('onclick');
		}
		
		// Creates the text
		document.getElementById("scoring_text").innerHTML = "<span style=\"color:#00BF00;\">Vielen Dank <br /> f&uuml;r Ihre Bewertung</span>";
		
		timerRated = window.setTimeout( "tx_amarokcatalog_pi1resetScoring( " + productId + ", '" + productType + "'); rated=false;" , 3000);
	}
}





//
// HELPER FUNCTIONS
//

function createStars (score, starName) {
	
	var starsHTML = '';
	
	for(var i=1; i <= 5; i++) {
			
		var starSrc = (i <= score) ? "star_" + starName + ".gif" : "star_empty.gif";
		document.getElementById("scoringStar_" + i).src = "fileadmin/templates/amarok/images/catalog/" + starSrc;
	}
	
	return starsHTML;
	
}
