//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

function OptionsMenu()
{
	this.__proto__ = new Menu("OptionsMenu", EMenuLayout.Vertical)
	this.parentMenu = null;
	
	this.Registered = function()
	{
		this.__proto__.Registered.call(this);
		gPause.Pause(EPauseReason.Player, true);
	}

	this.Unregistered = function()
	{
		gPause.Pause(EPauseReason.Player, false);
		this.__proto__.Unregistered.call(this);
	}

	this.SetParentMenu = function(parentMenu)
	{
		this.parentMenu = parentMenu
	}
	
	this.StartChangingOptions = function()
	{
		this.Redraw();
		this.DrawClear();
		this.menuItems.splice(1, this.menuItems.length - 1);
		gRemapButtons = [];
	}
	
	this.EndChangingOptions = function(skipDrawing, skipBackButton)
	{
		if(!skipBackButton)
		{
			this.AddMenuItem(new Button("Back",
			function()
			{
				gMenuSystem.Unregister(gOptionsMenu);
				gOptionsMenu.parentMenu.Resize();
				gMenuSystem.Register(gOptionsMenu.parentMenu);
			}),
			EMenuItemStyle.Small);
		}

		this.UpdateLayout();

		if (!skipDrawing)
		{
			for(var i = 0; i < this.menuItems.length; i++)
			{
				if(this.menuItems[i].ForceUpdate)
				{
					this.menuItems[i].ForceUpdate();
				}
			}
			this.Draw();
		}
	}
	
	this.SetVisual = function()
	{
		this.StartChangingOptions();
		
		if(gCommands)
		{
			modes = Command("displaymodes")
			if(modes.responseText)
			{
				this.AddMenuItem(new FullscreenButton(), EMenuItemStyle.Small);
				
				this.AddMenuItem(new ResolutionButton(modes.responseText), EMenuItemStyle.Medium);
			}
		}
		
		this.AddMenuItem(new QualityButton(), EMenuItemStyle.Medium);
		
		this.AddMenuItem(new AutoAdjustButton(), EMenuItemStyle.Medium);
		
		this.AddMenuItem(new ApplyBackButton(
			function()
			{
				var targetZoom = gQualityValues[gQualitySetting];
				if(gCommands)
				{
					var command = "setdisplay?fullscreen=";
					command += gOptionsFullscreen ? "1" : "0";
					command += "&width=" + gOptionsResolutions[gOptionsResolutionIndex * 2]
					command += "&height=" + gOptionsResolutions[(gOptionsResolutionIndex * 2) + 1]
					Command(command);
				}
				if(gDraw.zoom != targetZoom)
				{
					resizeCanvas(targetZoom)
				}
			}
			,
			function()
			{
				gMenuSystem.Unregister(gOptionsMenu);
				gOptionsMenu.parentMenu.Resize();
				gMenuSystem.Register(gOptionsMenu.parentMenu);
			}
		), EMenuItemStyle.Small);
		
		this.EndChangingOptions(false, true);
	}
	
	this.SetAudio = function(skipDrawing)
	{
		this.StartChangingOptions();
		this.AddMenuItem(new VolumeButton("Sound Volume",
			function(deltaVolume)
			{
				gSounds.ChangeVolume(deltaVolume);
			},
			function()
			{
				return gSounds.volume;
			}),
			EMenuItemStyle.Medium);

		this.AddMenuItem(new VolumeButton("Music Volume",
			function(deltaVolume)
			{
				gMusic.ChangeVolume(deltaVolume);
			},
			function()
			{
				return gMusic.volume;
			}),
			EMenuItemStyle.Medium);
		
		this.AddMenuItem(new TrackButton("Track",
			function(){ gMusic.PrevTrack(); gOptionsMenu.Redraw(); },
			function(){ gMusic.NextTrack(); gOptionsMenu.Redraw(); }),
			EMenuItemStyle.Medium);
		
		this.EndChangingOptions(skipDrawing);
	}
	
	this.SetInput = function()
	{
		this.StartChangingOptions();
		
		if(gInputType == EInputType.Pad)
		{
			this.AddMenuItem(new PadControls(), EMenuItemStyle.PadControls);
		}
		else
		{
			this.AddMenuItem(new RemapControlButton("Up", EKeyCode.w, EKeyCode.up, "Undo", EKeyCode.z, EKeyCode.delete), EMenuItemStyle.TextOption);
			this.AddMenuItem(new RemapControlButton("Left", EKeyCode.a, EKeyCode.left, "Restart", EKeyCode.r, EKeyCode.pagedown), EMenuItemStyle.TextOption);
			this.AddMenuItem(new RemapControlButton("Down", EKeyCode.s, EKeyCode.down, "Pause", EKeyCode.p, EKeyCode.escape), EMenuItemStyle.TextOption);
			this.AddMenuItem(new RemapControlButton("Right", EKeyCode.d, EKeyCode.right, "Level Select", EKeyCode.c, EKeyCode.home), EMenuItemStyle.TextOption);
			this.AddMenuItem(new RemapControlButton("Select", EKeyCode.return, EKeyCode.space), EMenuItemStyle.TextOption);
		}
		
		this.EndChangingOptions();
	}
	
	this.SetMisc = function()
	{
		this.StartChangingOptions();
		
		this.AddMenuItem(new PatternButton(), EMenuItemStyle.BlockOption);
		this.AddMenuItem(new ControlsDisplayButton(), EMenuItemStyle.Medium);
		
		this.EndChangingOptions();
	}
	
	this.optionsButtons = new OptionsButton();
	this.AddMenuItem(this.optionsButtons, EMenuItemStyle.Medium);
	this.SetAudio(true);
}



