var RATING_IMG = '/graphics/blue_star.gif';
var RATING_IMG_BG = '/graphics/blank_blue_star.gif';




function Rating(ratingElementId, maxStars, objectName, formName, ratingMessageId, entryId)
{
	this.ratingElementId = ratingElementId;
	this.maxStars = maxStars;
	this.objectName = objectName;
	this.formName = formName;
	this.ratingMessageId = ratingMessageId
	this.entryId = entryId

	this.starTimer = null;
	this.starCount = 0;


	// pre-fetch image
	(new Image()).src = RATING_IMG;

	function showStars(starNum, skipMessageUpdate) {
		this.clearStarTimer();
		this.greyStars();
		this.colorStars(starNum);
		if(!skipMessageUpdate)
			this.setMessage(starNum);
	}

	function setMessage(starNum) {
		if ( isNaN(starNum) ) {
			starNum = 0;
		}
		messages = new Array("Rate this entry", "Absolute crap", "A bit boring", "Not bad", "Liked it", "Brilliant!");
		document.getElementById(this.ratingMessageId).innerHTML = messages[starNum];
	}

	function colorStars(starNum) {
		for (var i=0; i < starNum; i++)
			document.getElementById('star_'  + this.entryId + "_" + (i+1)).src = RATING_IMG;
	}

	function greyStars() {
		for (var i=0; i < this.maxStars; i++)
			if (i <= this.starCount)
				document.getElementById('star_' + this.entryId + "_"  + (i+1)).src = RATING_IMG_BG;
			else
				document.getElementById('star_' + this.entryId + "_"  + (i+1)).src = RATING_IMG_BG;
	}

	function setStars(starNum, entryId) {
		this.starCount = starNum;
		this.drawStars(starNum);
		this.entryId = entryId;
		document.forms[this.formName]['value'].value = this.starCount;
		document.forms[this.formName]['entry_id'].value = this.entryId;
		var ratingElementId = this.entryId;
		document.forms[this.formName]['value'].value = this.starCount;
		document.forms[this.formName]['entry_id'].value = this.entryId;
		document.forms[this.formName].submit();
	}


	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;

}
