var width, posX, posY, ratingInput, inRatingsImage;

// Hardcode the width of the ratings else FF throws an error
var width = 74;

function changeRating(obj, inputObj)
{
	inRatingsImage = true;
	
	// Get the X & Y positions for the object
	positions = getPos(obj);
	posX = positions[0];
	posY = positions[1];
	
	// Get the input that relates to this rating
	ratingInput = document.getElementById(inputObj);
	
	obj.onmousemove = trackMouseMove;	
}

function removeRating(obj)
{
	// Reset all the variables
	inRatingsImage = false;
	posX = null;
	posY = null;
	ratingInput = null;
	obj.onmousemove = null;
	obj.src = ratingToImage('0');
}

function trackMouseMove(e)
{
	this.onmousemove = null;
	
	if(document.all)e=event;
	
	if (e.target) source = e.target;
		else if (e.srcElement) source = e.srcElement;
		if (source.nodeType == 3) // defeat Safari bug
			source = source.parentNode;

	eventX = e.clientX;
	eventY = e.clientY;
	calculateRating(this);
	
	if (inRatingsImage)
		this.onmousemove = trackMouseMove;
}

function calculateRating(obj)
{
	var rating = ( (eventX-posX) ) / ( width / 10)
	rating = Math.round(rating) / 2

	ratingInput.value = rating;
	obj.src = ratingToImage(rating);
}

function ratingToImage(rating)
{
	var src = "/images/";
	if (rating.toString().length == 1)
		src += rating + ".0.gif";
	else
		src += rating + ".gif";
	return src;
}

function getPos(theElement)
{
	var positionX = 0;
	var positionY = 0;
	while (theElement != null)
	{
		positionX += theElement.offsetLeft;
		positionY += theElement.offsetTop;
		theElement = theElement.offsetParent;
	}
	return [positionX, positionY];
}