var STAR_RT_IMG = metvMediaUrl + 'images/'+currentThemeName+'/star_full.gif';
var STAR_RT_IMG_OFF = metvMediaUrl + 'images/'+currentThemeName+'/star_empty.gif';
var STAR_RT_IMG_HALF = metvMediaUrl + 'images/'+currentThemeName+'/star_half.gif';

var STAR_RT_IMG_NAV = metvMediaUrl + 'images/'+currentThemeName+'/star_full_nav.gif';
var STAR_RT_IMG_OFF_NAV = metvMediaUrl + 'images/'+currentThemeName+'/star_empty_nav.gif';
var STAR_RT_IMG_HALF_NAV = metvMediaUrl + 'images/'+currentThemeName+'/star_half_nav.gif';


function postChannelRating(channelId, rating)
{
	$.get(metvServicesUrl + "channels/rate.xml", { rating: rating } );
	$("#channelMessage").text("Rating Sent!");
}

function postVideoRating(videoId, rating) { $.get(metvServicesUrl + "videos/rate.xml", { rating: rating, id: videoId }); }

function RatingMD(elementId, stars, id, contentId)
{
	this.elementId = elementId;
	this.stars = stars;
	this.id = id;
	this.rating = 0;
	this.timer = null;
	this.postHandler = null;
	this.contentId = contentId;
	this.rated = false;

	this.setRating = setRating;
	this.clearRating = clearRating;
	this.drawRating = drawRating;
	this.showRating = showRating;
	this.clearTimer = clearTimer;
	this.resetRating = resetRating;

	function setRating(rating, submit)
	{
		this.rating = rating;
		this.showRating(rating);
		if (submit)
		{
			for (var i=1; i <= this.stars; i++)
			{
				var img = document.getElementById("star_" + this.id + "_" + i);
				this.rated = true;
			}				
			if (this.postHandler)
			{
				this.postHandler(this.contentId, this.rating);
			}
		}					
	}

	function showRating(rating)
	{
		if (!this.rated)
		{
			this.clearTimer();
			this.drawRating(rating);
		}
	}

	function drawRating(rating)
	{
		for (var i=1; i <= this.stars; i++)
		{
			var img;
			if (this.id != 'channel')
			{
				if (rating >= i) {
					img = STAR_RT_IMG;
				} else if (rating < i && rating > (i-1)) {
					img = STAR_RT_IMG_HALF;
				} else {
					img = STAR_RT_IMG_OFF;
				}
			}
			else
			{
				if (rating >= i) {
					img = STAR_RT_IMG_NAV;
				} else if (rating < i && rating > (i-1)) {
					img = STAR_RT_IMG_HALF_NAV;
				} else {
					img = STAR_RT_IMG_OFF_NAV;
				}
			}
			document.getElementById("star_" + this.id + "_" + i).src = img;
			
		}
	}

	function clearRating() { this.timer = setTimeout(this.elementId + ".resetRating()", 300); }
	function resetRating() { this.drawRating(this.rating); }
	function clearTimer() {
		if (this.timer) {
			clearTimeout(this.timer);
			this.timer = null;
		}
	}
}
