var UT_RATING_IMG = "/alternat-docs/images/commons/star.gif";
var UT_RATING_IMG_HALF = "/alternat-docs/images/commons/star-half.gif";
var UT_RATING_IMG_BG = "/alternat-docs/images/commons/star-bg.gif";

var onLoadFunctionList = new Array();

function preloadRatingImages() {
	MM_preloadImages(UT_RATING_IMG, UT_RATING_IMG_HALF, UT_RATING_IMG_BG);
}
function performOnLoadFunctions() {
	for (var i in onLoadFunctionList) {
		onLoadFunctionList[i]();
	}
}
function UTRating(maxStars, objectName, ratingMessageId, componentSuffix, enable) {
	this.maxStars = maxStars;
	this.objectName = objectName;
	this.ratingMessageId = ratingMessageId;
	this.componentSuffix = componentSuffix;
	this.starTimer = null;
	this.starCount = 0;
	this.rating = 0;
	this.enable = enable;
	
	// pre-fetch image
	(new Image()).src = UT_RATING_IMG;
	(new Image()).src = UT_RATING_IMG_HALF;
	function drawRatingStars() {
		if (this.rating <= this.maxStars) {
			for (var i = 1; i <= this.maxStars; i++) {
				if (i <= this.rating) {
					document.getElementById("star" + this.componentSuffix + (i)).src = UT_RATING_IMG;
				} else {
					if ((i - this.rating) >= 1) {
						document.getElementById("star" + this.componentSuffix + (i)).src = UT_RATING_IMG_BG;
					} else {
						document.getElementById("star" + this.componentSuffix + (i)).src = UT_RATING_IMG;
					}
				}
			}
		}
	}
	function showStars(starNum, skipMessageUpdate) {
		if (this.isEnableEvents()) {
			this.clearStarTimer();
			this.greyStars();
			this.colorStars(starNum);
			if (!skipMessageUpdate) {
				//this.setMessage(starNum);
			}
		}
	}
	function setMessage(starNum) {
		
		messages = new Array(
			"", 
			/*"<spring:message code="common.rating.poor.label"/>", 
			"<spring:message code="common.rating.nothing.special.label"/>", 
			"<spring:message code="common.rating.worth.watching.label"/>", 
			"<spring:message code="common.rating.pretty.cool.label"/>", 
			"<spring:message code="common.rating.awesome.label"/>")*/
			"poor", 
			"nothing.special", 
			"worth.watching", 
			"pretty.cool", 
			"awesome")
			;
			
		document.getElementById(this.ratingMessageId).innerHTML = messages[starNum];
	}
	function colorStars(starNum) {
		for (var i = 0; i < starNum; i++) {
			document.getElementById("star" + this.componentSuffix + (i + 1)).src = UT_RATING_IMG;
		}
	}
	function greyStars() {
		for (var i = 0; i < this.maxStars; i++) {
			document.getElementById("star" + this.componentSuffix + (i + 1)).src = UT_RATING_IMG_BG;
		}
	}
	function setStars(starNum) {
		this.starCount = starNum;
		this.drawStars(starNum);
	}
	function drawStars(starNum, skipMessageUpdate) {
		this.starCount = starNum;
		this.showStars(starNum, skipMessageUpdate);
	}
	function clearStars() {
		this.starTimer = setTimeout(this.objectName + ".resetStars()", 300);
	}
	function resetStars() {
		this.clearStarTimer();
		if (this.starCount) {
			this.drawStars(this.starCount);
		} else {
			this.greyStars();
		}
		//this.setMessage(0);
	}
	function clearStarTimer() {
		if (this.starTimer) {
			clearTimeout(this.starTimer);
			this.starTimer = null;
		}
	}
	function enableEvents(enable) {
		this.enable = enable;
		if (!this.isEnableEvents()) {
			this.clearStarTimer();
			for (var i = 1; i <= this.maxStars; i++) {
				star = document.getElementById("ref_star" + this.componentSuffix + (i));
				if (star != null) {
					star.oldonmouseover = star.onmouseover;
					star.onmouseover = null;
					star.oldonmouseout = star.onmouseout;
					star.onmouseout = null;
					star.oldonclick = star.onclick;
					star.onclick = null;
				}
			}
		} else {
			for (var i = 1; i <= this.maxStars; i++) {
				star = document.getElementById("ref_star" + this.componentSuffix + (i));
				if (star != null) {
					star.onmouseover = star.oldonmouseover;
					star.onmouseout = star.oldonmouseout;
					star.ononclick = star.oldonclick;
				}
			}
		}
	}
	function isEnableEvents() {
		return this.enable;
	}
	this.clearStars = clearStars;
	this.clearStarTimer = clearStarTimer;
	this.greyStars = greyStars;
	this.colorStars = colorStars;
	this.resetStars = resetStars;
	this.setStars = setStars;
	this.drawStars = drawStars;
	this.showStars = showStars;
	this.setMessage = setMessage;
	this.drawRatingStars = drawRatingStars;
	this.isEnableEvents = isEnableEvents;
	this.enableEvents = enableEvents;
}
