var gPerformanceOverlay = new PerformanceOverlay()
var gFrameTimes = new Array();
var gAutoAdjustPerformance = true;
var autoAdjust = gStorage.getItem("options", "performance", "auto");
if(autoAdjust)
{
	gAutoAdjustPerformance = (parseInt(autoAdjust) == 1)
}

function PerformanceOverlay()
{
	this.enabled = false;
	this.lowerPerformanceCount = 0;
	this.lowerPerformanceThreshold = 30;
	this.kSkipPerformanceCheckTime = 30;
	this.skipPerformanceCheck = this.kSkipPerformanceCheckTime;
	this.reducingQuality = false;
	this.previousFocus = false;
	this.showTimer = 0;
	this.width = 0;
	this.height = 0;
	

	this.Enable = function()
	{
		this.enabled = true;
	}
	
	this.Disable = function()
	{
		this.DrawClear();
		this.enabled = false;
	}
	
	this.IgnorePerformance = function()
	{
		this.skipPerformanceCheck = this.kSkipPerformanceCheckTime;
	}
	
	this.ShouldShow = function()
	{
		return gOptionsMenu.isRegistered && gOptionsMenu.optionsButtons.currentlyLoaded == 0 || this.reducingQuality || this.showTimer > 0;
	}
	
	this.ShouldCheckPerformance = function()
	{
		return gAutoAdjustPerformance && this.previousFocus && gQualityValues.indexOf(gDraw.zoom) != gQualityValues.length - 1 && !this.ShouldShow() && this.skipPerformanceCheck == 0;
	}
	
	this.Update = function()
	{
		var ms = new Date().getTime()
		while(gFrameTimes.length > 0 && ms - gFrameTimes[0] > 1000)
		{
			gFrameTimes.splice(0, 1);
		}
		gFrameTimes.push(ms);
	
		if(this.ShouldCheckPerformance())
		{
			var fps = (this.GetFPSLastFrame() + this.GetFPS())/2;
			if(fps < 25)
			{
				var increment = 1;
				if(fps < 10)
				{
					increment = 3;
				}
				else if(fps < 20)
				{
					increment = 2;
				}
				this.lowerPerformanceCount += increment;
				
				if(this.lowerPerformanceCount > this.lowerPerformanceThreshold)
				{
					this.reducingQuality = true;
				}
			}
			else
			{
				if(this.lowerPerformanceCount > 0)
				{
					this.lowerPerformanceCount--;
				}
			}
		}
		
		if(this.reducingQuality)
		{
			var current = gQualityValues.indexOf(gDraw.zoom);
			current++;
			gQualitySetting = current;
			var newZoom = gQualityValues[current];
			resizeCanvas(newZoom);
			this.lowerPerformanceCount = 0;
			this.reducingQuality = false;
			this.showTimer = 60;
		}
		
		if(this.showTimer > 0)
		{
			this.showTimer--;
		}
		
		if(this.skipPerformanceCheck > 0)
		{
			this.skipPerformanceCheck--;
		}
		
		var currentFocus = document.hasFocus();
		if(currentFocus != this.previousFocus)
		{
			this.IgnorePerformance();
			this.previousFocus = currentFocus;
		}
		
		if(this.ShouldShow())
		{
			this.Enable();
		}
		else
		{
			this.Disable();
		}
	}

	this.DrawClear = function()
	{
		if(this.enabled)
		{
			var x = this.GetTextX();
			var y = this.GetTextY();
			var shadow = kTileSizeD2/2;
			gDraw.menu.clearRect(x - this.width - shadow, y - this.height - shadow, this.width + (2 * shadow), this.height + (2 * shadow));
			//gDraw.menu.rect(x - this.width - shadow, y - height - shadow, this.width + (2 * shadow), height + (2 * shadow));
			//gDraw.menu.fillStyle = "rgb(255, 0, 0)";
			//gDraw.menu.fill();

		}
	}
	
	this.Draw = function()
	{
		if(this.enabled)
		{
			this.width = 0;
			gDraw.menu.save();
			gDraw.menu.textAlign	 = 'right';
			gDraw.menu.textBaseline	 = 'bottom';
			gDraw.menu.shadowOffsetX = 1;
			gDraw.menu.shadowOffsetY = 1;
			gDraw.menu.shadowBlur = 2;
			gDraw.menu.shadowColor = "rgb(0, 0, 0)";
			gDraw.menu.fillStyle = "rgb(255, 255, 255)";
			
			var x = this.GetTextX();
			var y = this.GetTextY();
			
			var fontSize = this.GetFontSize();
			gDraw.menu.font	 = GetFont(fontSize);
			var text = "FPS " + this.GetFPS();
			gDraw.menu.fillText(text, x, y);
			this.width = Math.max(gDraw.menu.measureText(text).width, this.width);
			this.height = fontSize * 1.2;
			if(this.showTimer)
			{
				y -= fontSize * 1.2;
				var current = gQualityValues.indexOf(gDraw.zoom);
				var qualityText = gQualityNames[current];
				gDraw.menu.fillText(qualityText, x, y);
				this.width = Math.max(gDraw.menu.measureText(qualityText).width, this.width);
				y -= fontSize * 1.2;
				var reduceText = "Reduced quality";
				gDraw.menu.fillText(reduceText, x, y);
				this.width = Math.max(gDraw.menu.measureText(reduceText).width, this.width);
				this.height = fontSize * 3.6;
			}

			gDraw.menu.restore();
		}
	}
	
	this.GetPadding = function()
	{
		return kTileSize * 0.1;
	}
	
	this.GetTextX = function()
	{
		return gDraw.canvas.width - this.GetPadding();
	}
	
	this.GetTextY = function()
	{
		return gDraw.canvas.height - this.GetPadding();
	}
	
	this.GetFontSize = function()
	{
		return GetFontScale(26);
	}

	this.GetFPS = function()
	{
		return gFrameTimes.length;
	}
	
	this.GetFPSLastFrame = function()
	{
		var len = gFrameTimes.length;
		if(len > 2)
		{
			var msLastFrame = gFrameTimes[len - 1] - gFrameTimes[len - 2];
			return Math.round(1000/msLastFrame);
		}
		return 0;
	}
	
	this.Debug = function()
	{
		if(!this.ShouldCheckPerformance())
		{
			gDraw.Watch("Not checking Performance...");
		}
		gDraw.Watch("FPS : " + this.GetFPSLastFrame() + " " + this.GetFPS() + (this.lowerPerformanceCount > 0 ? " [" + this.lowerPerformanceCount +"]" : ""));
	}
}