var StarRating = 
{
	isInit: false,
	greyImg: null,
	coloredImg: null,
	
	init: function(greyImg, coloredImg)
	{
		if(this.isInit)
			return;
		
		this.isInit = true;
		
		this.greyImg = greyImg;
		this.coloredImg = coloredImg;
	},
	
	create: function(id, rating, votes)
	{
		if(!this.isInit)
			return;
			
		var id = id;
			
		document.writeln("<div id=\"StarRating_" + id + "\"></div>");
		document.writeln("<span id=\"TextRating_" + id + "\">" + rating + "/5</span>");
		document.writeln("<span id=\"RatingVotes_" + id + "\">" + votes + " " + (votes == 1 ? "Vote" : "Votes") + "</span>");
		 
		var Container = $("StarRating_" + id);
		Container.setStyle({
			float			:'left',
			height			: '25px', 
			width			: '125px',
			cursor			: 'pointer',
			position		: 'relative',
			backgroundImage	: 'url(' + this.greyImg + ')'
		});
		
		var TextRating = $("TextRating_" + id);
		TextRating.setStyle({
			float		: 'left',
			padding		: '10px 6px 10px 3px',
			fontSize	: '12px',
			color		: '#999999'
		});
		
		var RatingVotes = $("RatingVotes_" + id);
		RatingVotes.setStyle({
			float		: 'left',
			padding		: '10px 0px',
			fontSize	: '12px',
			color		: '#777777'
		});
		
		var Rating = new Element('div');
		Rating.setStyle({
			height			: '25px', 
			width			: Math.round(rating*25) + 'px',
			backgroundImage	: 'url(' + this.coloredImg + ')'
		});
		
		var Rater = new Element('div');
		Rater.setStyle({
			height			: '25px', 
			width			: '25px',
			position		: 'absolute',
			top				: '0px',
			left			: '0px',
			opacity			: .5,
			backgroundImage	: 'url(' + this.coloredImg + ')'
		});
		
		Container.appendChild(Rating);
		Container.appendChild(Rater);
		
		Rater.hide();
		
		Event.observe(Container, 'mouseenter', function(event)
		{
			Rating.hide();
			Rater.show();
		});
		
		Event.observe(Container, 'mouseleave', function(event)
		{
			Rating.show();
			Rater.hide();
		});
		
		Event.observe(Container, 'mousemove', function(event)
		{
			Rater.setStyle({ width: Math.ceil((Event.pointerX(event) - Container.viewportOffset().left)/25)*25 + 'px' });
		});
		
		Event.observe(Container, 'click', function(event)
		{
			var rate = Math.ceil(parseInt(Rater.getStyle('width'))/25);
			
			new Ajax.Request( $F('AjaxDir') + '/rate.php',
			{
				method: 'post',
				
				parameters:
				{
					PostID: id,
					Rating: rate
				},
				
				onComplete: function(val)
				{
					resp = val.responseText.evalJSON();
					
					if(!!resp.Error)
					{
						switch(resp.Error)
						{
							case "INVALID_RATING": alert("This is an invalid rating."); break;
							case "INVALID_POST_ID": alert("This is an invalid post ID."); break;
							case "ALREADY_RATED": alert("You've already rated this post."); break;
						}
					}
					else
					{
						Rating.setStyle({width: parseFloat(resp.Rating)*25 + "px"});	
						TextRating.update(resp.Rating + "/5");
						RatingVotes.update(resp.Votes + " " + (resp.Votes == 1 ? "Vote" : "Votes"));
						alert("Your rating has been recorded, thank you for you input!");
					}
				}
			});
		});
	}
}