var page_ratingSelectors = new Array();
function ratingSelector(id, text, userScore, averageScore, totalVotes)
{
	//debugger;
	this.id = id;
	this.DOMobject = document.getElementById(id);
	this.items = new Array();
	this.score = userScore;
	this.averageScore = averageScore;
	this.totalVotes = totalVotes;
	this.text = text;
	
	page_ratingSelectors[id] = this;
	
	// Initialise Ratings Selector
	// find rating items within this selector
	var score = 0;
	
	// find rating items parent span
	var itemsParent;
	for (var i=0; i < this.DOMobject.childNodes.length; i++)
	{
		if (this.DOMobject.childNodes[i].className == "items")
		{
			// we've found it
			var itemsParent = this.DOMobject.childNodes[i];
		}
	}
	
	for (var i=0; i < itemsParent.childNodes.length; i++)
	{
		var node = itemsParent.childNodes[i];
		if (node.className == "rating_item")
		{
			score++;
			// Add this rating item to the selector object
			var defaultImage = node.childNodes[0].src;
			var rating = new ratingSelector_item(this, score, node);
			this.items[score] = rating;
			
			// Add AJAX functionality to this node;
			node.onmouseover = new Function("ratingSelector_item_mouseOver('"+id+"','"+score+"')");
			node.onfocus = node.onmouseover;
			
		}
	}
	
	this.maxScore = score;
	this.onClickAnimationTimeout = null;
	this.onClickAnimationFlashToggle = 0;
	this.onClickAnimationFlashCount = 0;
	this.onClickAnimationFlashSpeed = 50;
	this.onClickAnimationFlashMaxCount = 20;
	
	 this.DOMobject.onmouseout = new Function("ratingSelector_mouseOut('"+id+"')");
	//this.DOMobject.onblur = new Function("ratingSelector_mouseOut('"+id+"')");
	
	//this.items[1].DOMobject.onkeypress = new Function("return ratingSelector_item_keypress('"+id+"','1');"); //assign keypress to first item
	//this.items[score].DOMobject.onkeypress = new Function("return ratingSelector_item_keypress('"+id+"','"+score+"');"); //assign keypress to last item
	
	this.items[1].DOMobject.onblur = Function("ratingSelector_mouseOut('"+id+"')");
	this.items[score].DOMobject.onblur = Function("ratingSelector_mouseOut('"+id+"')");
	
	this.render();
}
ratingSelector.prototype.render = ratingSelector_render;
ratingSelector.prototype.onClickAnimation = ratingSelector_onClickAnimation;



function ratingSelector_item(ratingSelector, score, DOMobject)
{
	//debugger;
	this.ratingSelector = ratingSelector;
	this.score = score;
	this.DOMobject = DOMobject;
	this.Image = DOMobject.childNodes[0];

	if (this.Image.src.indexOf("star_on") >= 0)
		this.state = "on";
	else
		this.state = "off";
		
	this.hover = 0;
}

function ratingSelector_setRating(score)
{
	this.score = score;
	this.render();
}

function ratingSelector_render()
{
	//debugger;
	for (var i=1; i <= this.maxScore; i++)
	{
		var item = this.items[i];
		if (item.hover)
		{
			item.Image.src = "http://www.sstaffs.gov.uk/images/star_hover.gif";
		}
		else
		{
			if (item.state == "on")
				item.Image.src = "http://www.sstaffs.gov.uk/images/star_on.gif";
			else
				item.Image.src = "http://www.sstaffs.gov.uk/images/star_off.gif";
		}
		
		var alt = item.ratingSelector.text+" - "+i+" / "+item.ratingSelector.maxScore;
		if (item.ratingSelector.score > 0)
		{
			alt += "\r\nYour current rating = " + item.ratingSelector.score
				+ "\r\nAverage rating = " + item.ratingSelector.averageScore + " (" + item.ratingSelector.totalVotes + " votes)";
		}
		item.DOMobject.title = alt;
		item.Image.alt = alt;
	}
}

function ratingSelector_item_mouseOver(id, score)
{
	var selector = page_ratingSelectors[id];
	
	selector.hoverItem = score;
	
	for (var i=1; i <= selector.maxScore; i++)
	{
		if (i <= score)
			selector.items[i].hover = 1;
		else
			selector.items[i].hover = 0;
	}
	
	selector.render();

}

function ratingSelector_mouseOut(id)
{
	var selector = page_ratingSelectors[id];
	
	for (var i=1; i <= selector.maxScore; i++)
	{
		selector.items[i].hover = 0;
	}
	
	selector.render();
}

function ratingSelector_item_click(id, score)
{
	
	
}

function ratingSelector_item_keypress(id, score)
{

}

function ratingSelector_onClickAnimation(id)
{
	

}

function ratingSelector_updateServer()
{
	
}

function ratingSelector_serverResponseHandler()
{

}
