// Winamop voting script.

// How many times to flash the vote on select:
var flashes = 3;
// Where the server side script is:
var scriptUrl = '/cgi-bin/winamopvote.pl';
// What to display when waiting for the server to respond:
var pleaseWaitMessage = 'Just a mo...';
// What to display when nobody has voted:
var nobodyVotedMessage = 'Nobody has voted yet!';
// Star 'on' filename
var starOnFileName = '/star1.gif';
// Star 'off' filename
var starOffFileName = '/star2.gif';

// Voting messages, in order of value (0 - 5), with 0 being no current vote.
var voteMessages = [
	"",
	"Rubbish",
	"Hmm...",
	"OK I Suppose",
	"Good",
	"Absolutely Great!"
];



// Don't change these.
var starOff = new Image();
var starOn = new Image();
var xhr = (window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"));
var vote = 0;
var article = '';

function loadStars()
{
	starOff.src = starOnFileName;
	starOn.src = starOffFileName;
	article = location.pathname;
	article = article.substring(article.lastIndexOf('/') + 1, article.lastIndexOf('.'));
	if(!submitVote(0))
	{
		vote = 0;
		highlight(0);
		setMessage('');
	}
	highlight(vote);
}

function highlight(x)
{
	if(vote == 0)
		document.getElementById('vote').innerHTML = voteMessages[x];
	else
	{
		document.getElementById('vote').innerHTML = "You voted: " + voteMessages[vote];
		x = vote;
	}
	for(var i = 1; i <= 5; i++)
	{
		document.getElementById(i).src = (i <= x ? starOn.src : starOff.src);
	}
}

function setStar(x)
{
   if (vote == 0 && x > 0)
   {
		flash(x);
		vote = x;
		if(!submitVote(x))
		{
			// An error occurred, clear the vote.
			vote = 0;
			highlight(0);
			setMessage('Error!');
		}
		else
		{
			highlight(vote);
		}
   }
}

var flashCount=0;
function flash()
{
	if(flashCount == 0)
		flashCount = flashes * 2 + 1;
	for(var i = 1; i <= vote; i++)
	{
		document.getElementById(i).src = (flashCount % 2 == 0 ? starOff.src : starOn.src);
	}
	flashCount--;
	if(flashCount > 0)
		setTimeout(flash, 200);
}

function setMessage(message)
{
	document.getElementById('vote').innerHTML = message;
}

function displayAverageVote(sumVotes, countVotes)
{
	if(countVotes == 0)
		document.getElementById('rating').innerHTML = nobodyVotedMessage;
	else
		document.getElementById('rating').innerHTML = "Rating: " + (sumVotes / countVotes).toFixed(1) + " (" + countVotes + " vote" + (countVotes > 1 ? "s" : "") + ")";
}

function submitVote(x)
{
	setMessage(pleaseWaitMessage);
	try
	{
		xhr.open('GET', scriptUrl + '?article=' + article + (x > 0 ? '&vote=' + x : ''), false);
		xhr.send();
	}
	catch(err)
	{
		return false;
	}
	if(xhr.status == 200)
	{
		var sumVotes = 0;
		var countVotes = 0;
		var lines = xhr.responseText.split(/[\r\n]+/);
		for(var i in lines)
		{
			var pair = lines[i].split(/\s*=\s*/, 2);
			//if(pair[0] == 'message')
			//	setMessage(pair[1]);
			if(pair[0] == 'voted')
				vote = parseInt(pair[1]);
			else if(pair[0] == 'total')
				sumVotes = parseInt(pair[1]);
			else if(pair[0] == 'count')
				countVotes = parseInt(pair[1]);
		}
		displayAverageVote(sumVotes, countVotes);
		return true;
	}
	return false;
}

loadStars();

