//Copyright (C) Ben Parbury - All Rights Reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Written by Ben Parbury <ben@gridlockinteractive.com> 2014
var gHighscoreSystem = new HighscoreSystem();

function HighscoreSystem()
{
	this.scores = new Array();
	this.latestScore = null;
	this.count = 5;
	
	this.Save = function()
	{
		var levelName = gLevelSystem.GetLevelName();
		this.Load(levelName)
		
		var newScore = gStatsSystem.GetStat(EStatsType.GameTime);
		gConsole.Log(9, "[HSS] Adding new score " + newScore);
		this.latestScore = newScore;
		this.scores.push(newScore);
		
		this.scores.sort(function(a,b){return b-a});

		for(var i = 1; i <= this.count; i++)
		{
			gStorage.setItem("highscore", levelName, "Score" + i, this.scores[i-1] ? this.scores[i-1] : "");
		}
	}
	
	this.Load = function(levelName)
	{
		var loaded = false;
		this.scores = new Array();
		for(var i = 1; i <= this.count; i++)
		{
			var score = gStorage.getItem("highscore", levelName, "Score" + i);
			if(score)
			{
				this.scores.push(parseInt(score));
				loaded = true;
			}
			else
			{
				this.scores.push("");
			}
		}
		
		return loaded;
	}
}
