var start = 0;
var count = 20;

function loadList(operation)
{
	var showingNonNumbered = false;
	if(operation == "")
		showingNonNumbered = true;
	if(start < 0 && !showingNonNumbered)
	{
		start = 0;
	}
	if(start > 11100)
	{
		start = 11100;
	}
	$.get("getmedals.php", { 'op' : operation, 'start' : start, 'count' : count },
		function(data) {
			$("#medallist").html(data.html);
			$("#gentime").text(data.time);
			if(!showingNonNumbered)
			{
				window.location.hash = start + 1;
				$("input#gotoNum").val(start+1);
			}
		}, "json");
}

function doLoadPrev() {
	if(start != 0)
	{
		count = 20;
		start = start - 20;
		loadList("withnum");
	}
	return false;
}

function doLoadNext() {
	count = 20;
	start = start + 20;
	loadList("withnum");
	return false;
}

$(function() {
	if(window.location.hash != "")
	{
		start = parseInt(window.location.hash.substring(1)) - 1;
	}
	loadList();
});

$(function() {
	$("a#loadPrevTop").click(doLoadPrev);
	$("a#loadPrev").click(doLoadPrev);
	$("a#loadNextTop").click(doLoadNext);
	$("a#loadNext").click(doLoadNext);
	$("a#showAll").click(function() {
		count = 12000;
		start = 0;
		$("#medallist").text("Loading...");
		loadList("withnum");
	});
	$("a#showUnnumbered").click(function() {
		count = 20;
		// set -20 so that next time Next is clicked, 0 is start
		start = -20;
		loadList("");
	});
	$("button#gotoBtn").click(function() {
		count = 20;
		start = parseInt($("input#gotoNum").val()) - 1;
		loadList("withnum");
	});
});
