//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 gOptionsResolutionIndex = 0;
var gOptionsResolutions = null;

function gcd (a,b)
{
    if (b == 0)
	{
        return a
	}
    return gcd (b, a % b)
}

function ResolutionCenterButton(name, callback)
{
	this.__proto__ = new Button(name, callback);
	
	this.DrawText = function(selected)
	{
		var displayWidth = gOptionsResolutions[gOptionsResolutionIndex * 2]
		var displayHeight = gOptionsResolutions[(gOptionsResolutionIndex * 2) + 1]
		var displayText = displayWidth + " x " + displayHeight;
		
		var additional = this.mouseDown ? 1 : 0;
		gDraw.menu.font	 = GetFont(this.fontSize * 0.7);
		gDraw.menu.textAlign	 = 'center';
		gDraw.menu.textBaseline	 = 'bottom';
		gDraw.menu.fillTextShadow(displayText, this.x + additional, this.y + additional, this.GetTextColor(!this.locked), this.GetTextColor(this.locked));
		
		var gd = gcd(displayWidth, displayHeight);
		var aspectText = displayWidth/gd + ":" + displayHeight/gd;
		
		gDraw.menu.textBaseline	 = 'top';
		gDraw.menu.fillTextShadow(aspectText, this.x + additional, this.y + additional, this.GetTextColor(!this.locked), this.GetTextColor(this.locked));
	}
}

function ResolutionButton(resolutions)
{
	gOptionsResolutions = JSON.parse(resolutions);
	var defaultRes = gOptionsResolutions.splice(0, 2);
	var defaultWidth = defaultRes[0]
	var options = (gOptionsResolutions.length/2);
	var iniWidth = gIni.Get("display", "width", 0)
	for(var i = 0; i < options; i++)
	{
		var optionWidth = gOptionsResolutions[i * 2];
		if(iniWidth == optionWidth)
		{
			gOptionsResolutionIndex = i;
		}
	}
	
	this.__proto__ = new TripleButton("resolution",
		new Button("<", function()
		{
			gOptionsResolutionIndex--;
			if(gOptionsResolutionIndex < 0)
			{
				gOptionsResolutionIndex = (gOptionsResolutions.length/2) - 1;
			}
			gOptionsMenu.Redraw();
		}),
		new ResolutionCenterButton(name, function(){
			gOptionsResolutionIndex++;
			if(gOptionsResolutionIndex >= (gOptionsResolutions.length/2))
			{
				gOptionsResolutionIndex = 0;
			}
			gOptionsMenu.Redraw();
		}),
		new Button(">", function()
		{
			gOptionsResolutionIndex++;
			if(gOptionsResolutionIndex >= (gOptionsResolutions.length/2))
			{
				gOptionsResolutionIndex = 0;
			}
			gOptionsMenu.Redraw();
		})
	);
}
