/**************************************
 *	Handle votes
 **************************************/
window.addEvent('domready', function(){
	
	var votesContainer	= $('gamevotes');
	
	if(votesContainer){
		var stars			= votesContainer.getElements('img');
		var currentVote		= votesContainer.getElement('input[name=current_vote]');
		var totalVotes		= $('totalvotes');
		var score			= $('score');
		var gameId			= votesContainer.getElement('input[name=game_id]').value;
		
		fillStars(currentVote.value);
		
		if(currentVote.value == 0){
			stars.addEvent('mouseover', function(){
				fillStars(this.alt);
			})
			
			stars.addEvent('click', function(){
				registerScore(this.alt);
				currentVote.value = this.alt;
			})
			
			votesContainer.addEvent('mouseleave', function(){
				fillStars(currentVote.value);
			})
		}
	}

	function fillStars(selected){
		stars.each(function(star){
			if(parseInt(star.alt) <= parseInt(selected)){
				star.src = '/design/icons/star.png';
			}else{
				star.src = '/design/icons/star_off.png';
			}
		}) 
	}
	
	function registerScore(score){
		$json = new Json.Remote('/games/vote/'+gameId+'/'+score, {onComplete: updateScore});
		$json.send('json');
		currentVote.value = score;
	}
		
	function updateScore(result){
		//totalVotes.innerHTML	= result.votes;
		//score.innerHTML 		= result.score;
	}
	
})
