var UT_RATING_IMG = 'http://static.networkpiece.com/tf/6_star_big.gif';
var UT_RATING_IMG_BG = 'http://static.networkpiece.com/tf/h_star_big.gif';

function UTRating(messages, ratingElementId, maxStars, objectName, ratingMessageId, starCount)
{
	this.ratingElementId = ratingElementId;
	this.maxStars = maxStars;
	this.objectName = objectName;
	this.ratingMessageId = ratingMessageId;

	this.starTimer = null;
	this.starCount=starCount;
	
	// pre-fetch image
	(new Image()).src = UT_RATING_IMG;

	function showStars(starNum, skipMessageUpdate) {
		this.clearStarTimer();
		this.greyStars();
		this.colorStars(starNum);
		if(!skipMessageUpdate)
			this.setMessage(starNum);
	}

	function setMessage(starNum) {
		$(this.ratingMessageId).innerHTML = messages[starNum];
	}

	function colorStars(starNum) {
		for (var i=0; i < starNum; i++)
			$('star_' + (i+1)).src = UT_RATING_IMG;
	}

	function greyStars() {
		for (var i=0; i < this.maxStars; i++) {
			if (i <= this.starCount)
				$('star_' + (i+1)).src = UT_RATING_IMG_BG; // UT_RATING_IMG_REMOVED;
			else
				$('star_' + (i+1)).src = UT_RATING_IMG_BG;
		}
	}

	function setStars(starNum) {
		var userId = _getCookie("h6userid");
		if(userId == '') {
			LoginAlert.show({originatingRequest: setStars, requestParameters:[starNum]});
		} else {
			var stars = '';
			for (var i=0; i < this.maxStars; i++) {
				if (i < starNum)
					stars += '<img src="'+UT_RATING_IMG+'">';
				else
					stars += '<img src="'+UT_RATING_IMG_BG+'">';
			}
			stars += '<br><b>Thanks for rating!</b>';

			$("ratingWrapper").innerHTML = stars;
			
			ajaxResponseDiv = "ratingResponse";
			$(ajaxResponseDiv).innerHTML = '<img src="/index/LoadingGraphicSmall.gif">';

			var mediaId = $('media_id').value;

			params = "media_id=" + mediaId + "&rating=" + starNum;
			ajaxpack.getAjaxRequest("/ajax/rate_media.php", params, processGetPost, "txt")
		}	
	}


	function drawStars(starNum, skipMessageUpdate) {
		this.starCount=starNum;
		this.showStars(starNum, skipMessageUpdate);
	}

	function clearStars() {
		this.starTimer = setTimeout(this.objectName + ".resetStars()", 30);
	}

	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;

}


