var imgExt = '.gif';
var UT_RATING_IMG = '/images/Star_Voted'+imgExt;
var UT_RATING_IMG_HOVER = '/images/Star_Voted'+imgExt;
var UT_RATING_IMG_HALF = '/Images/starhalf'+imgExt;
var UT_RATING_IMG_BG = '/images/Star_Unvoted'+imgExt;
var UT_RATING_IMG_REMOVED = '/images/Star_Unvoted'+imgExt;

function UTRating(ratingElementId, maxStars, objectName, formName, ratingMessageId, componentSuffix, messages)
{
	this.ratingElementId = ratingElementId;
	this.maxStars = maxStars;
	this.objectName = objectName;
	this.formName = formName;
	this.ratingMessageId = ratingMessageId
	this.componentSuffix = componentSuffix
	this.messages = messages;

	this.starTimer = null;
	this.starCount = 0;

	// pre-fetch image
	(new Image()).src = UT_RATING_IMG;
	(new Image()).src = UT_RATING_IMG_HALF;

	function showStars(starNum, skipMessageUpdate) {
		this.clearStarTimer();
		this.greyStars();
		this.colorStars(starNum);		
		if(!skipMessageUpdate)
			this.setMessage(starNum, messages);
	}

	function setMessage(starNum) {
		$(this.ratingMessageId).innerHTML = this.messages[starNum];
	}

	function colorStars(starNum) {
	    for (var i = 0; i < starNum; i++) {

	        if ($('#star_' + this.componentSuffix + "_" + (i + 1))) $('#star_' + this.componentSuffix + "_" + (i + 1)).attr("src", UT_RATING_IMG);
		}
	}

	function greyStars() {
		for (var i=0; i < this.maxStars; i++)
			if (i <= this.starCount) {
				$('#star_' + this.componentSuffix + "_"  + (i+1)).attr("src", UT_RATING_IMG_BG);
			}
			else
			{
			    $('#star_' + this.componentSuffix + "_" + (i + 1)).attr("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;
		}
	}

	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;

}



