/* Minification failed. Returning unminified contents.
(1,1): run-time error CSS1019: Unexpected token, found '$'
(1,2): run-time error CSS1019: Unexpected token, found '('
(1,11): run-time error CSS1031: Expected selector, found ')'
(1,11): run-time error CSS1025: Expected comma or open brace, found ')'
(3,2): run-time error CSS1019: Unexpected token, found ')'
(5,10): run-time error CSS1031: Expected selector, found 'loadScores('
(5,10): run-time error CSS1025: Expected comma or open brace, found 'loadScores('
(41,10): run-time error CSS1031: Expected selector, found 'rfc3986EncodeURIComponent('
(41,10): run-time error CSS1025: Expected comma or open brace, found 'rfc3986EncodeURIComponent('
(42,60): run-time error CSS1002: Unterminated string: '()*]/g, escape);
 */
$(document).ready(function () {
	loadScores();
});

function loadScores() {
	var e = document.getElementById('gameid');
	var gameId = e.options[e.selectedIndex].value;
	var gameName = e.options[e.selectedIndex].innerHTML;
	e = document.getElementById('month');
	var month = e.options[e.selectedIndex].value;
	e = document.getElementById('year');
	month = 1 * month + 100 * e.options[e.selectedIndex].value;

	$.getJSON('/api/scores/HighScores', { game: gameId, month: month })
	.done(function (data) {
		if (data == null) { return; }

		var listbody = $('#high-scores')[0];
		listbody.innerHTML = "";

		for (var i = 0; i < data.length; i++) {
			var row = data[i];
			var h = '<tr><td class="text-center">' + row.Rank + '</td>';
			h += '<td><a href="/Games/Player/' + rfc3986EncodeURIComponent(row.UserName) + '">' + row.UserName + '</a></td>';
			h += '<td>' + localTime(row.PlayTime) + '</td>';
			h += '<td class="text-right">' + row.Score + '</td>';
			h += '</tr>';
			listbody.innerHTML += h;
		}
	});

	var e = document.getElementById('backLink');
	e.innerHTML = 'Back to ' + gameName;
	gameName = gameName.replace('Colorfall', 'Colorfall/#').replace(' ', '');
	if (gameName.indexOf('Colorfall') < 0) {
		gameName += '/';
	}
	e.href = '/Games/' + gameName;
}

function rfc3986EncodeURIComponent(str) {
	return encodeURIComponent(str).replace(/[!'()*]/g, escape);
}

function localTime(time) {
	var index = time.lastIndexOf(".");
	if (index > 0) {
		time = time.substring(0, index);
	}
	time = new Date(time + "Z");
	return time.toLocaleString();
}
