var option_screen;
var show_option_screen = false;


function open_option_screen() {
	option_screen = new option();
	show_option_screen = true;
	interrupt_ui = true;
	hide_drawImage_hack = true;
}


function close_option_screen() {
	show_option_screen = false;
	interrupt_ui = false;
	hide_drawImage_hack = false;
}


function menu() {
	this.selection = 0;
	this.header = "menu";
	this.prompt = "prompt goes here";
	this.menu_option = [
	{name: "option 0", function() {}},
	{name: "option 1", function() {}},
	{name: "option 2", function() {}}
	]

	this.draw = function() {
		var margin = 8;
		var lines = 2 + this.menu_option.length;
		var config_width = width - 2 * margin;
		var config_height = lines * 8 + 2 * 8;
		var selection = this.selection;
		render_panel_with_transition(config_width, config_height, 1);

		var message_lines = drawStringWrapped(this.prompt, 16, prompt_height, 24);
		lines += message_lines;	

		var x = width / 2 - config_width / 2 + 24;
		var y = height / 2 - config_height / 2 + 8;
		var index = 0;
		drawString(this.header, x, y);
		y += 8;			
		// drawString("------------------", x, y);
		//drawString("-", x, y);
		y += 8;
		for (var i2 = 0; i2 < this.menu_option.length; i2 ++) {
			var string = this.menu_option[i2].name;
			drawString(string, x, y);

			// selection indicator
			if (index == selection) {
				if (cycle_colors)
					drawIndexedSpriteToRGB(spriteImageIndexed, 6, newPalette, offscreenImageData, Math.floor(x - 4 - 12), Math.floor(y - 4));
				else
					drawString("-", x - 7, y);
			}
			y += 8;
			index ++;
		}
		y += 8;

		if (this.prompt.length > 0) {
			drawFilledRectangle(0, height, width + hudWidth, height + 32, 0, 0, 0);
			drawString(this.prompt, 16, prompt_height);
		}
		
		// drawString("key pressed: " + this.last_code.toString(), x, y);
		// y += 8;

		//var line_count = drawStringWrapped(this.message, x, y - 8, 20);
		//y += 8 * line_count;
	}

	this.notify_keypress = function(code) {
		// use array of callbacks

		if (code == 38) {
			// move up
			this.selection = (this.selection - 1 + this.menu_option.length) % this.menu_option.length;
		}
		if (code == 40) {
			// move up
			this.selection = (this.selection + 1 + this.menu_option.length) % this.menu_option.length;
		}

		if (code == 13) {
			// select option (use callback)
			if (this.menu_option.length > this.selection) {
				if (this.menu_option[this.selection].callback != undefined)
					this.menu_option[this.selection].callback(); // pass in parameter?
			}
		}
	}
}


function option() {
	var ui = new menu();
	ui.header = "options";
	ui.prompt = "arrows, enter to select";
	ui.refresh = function() { // refreshes all menu options
		ui.menu_option = [
		{name: "sound" + boolean_indicator(sound_enabled), callback: toggle_sound},
		{name: "player 1 controls", callback: open_control_config_0},
		{name: "player 2 controls", callback: open_control_config_1},
		{name: "reduce flicker" + boolean_indicator(antiflicker), callback: toggle_antiflicker},
		{name: "disable cycling" + boolean_indicator(!cycle_colors), callback: toggle_color_cycling},
		{name: "solid warnings" + boolean_indicator(solid_warning_areas), callback: toggle_solid_warning_areas},
		{name: "exit", callback: close_option_screen}
		]
	}

	ui.refresh();
	return ui;
}


function open_accessibility_screen() {
	close_option_screen();
	// set player here
	// open_accessibility_screen();
}


function open_control_config_0() {
	close_option_screen();
	// set player here
	open_key_config(0);
}


function open_control_config_1() {
	close_option_screen();
	// set player here
	open_key_config(1);
}


// returns a text indicator for whether a boolean is on or not
function boolean_indicator(value) {
	var string = " (";
	string += value ? "X" : " ";
	string += ")";

	string = ": " + (value ? "on" : "off");
	return string;
}


function toggle_antiflicker() {
	antiflicker = !antiflicker;
	apply_options();
}


function toggle_color_cycling() {
	cycle_colors = !cycle_colors;
	apply_options();
}


function toggle_solid_warning_areas() {
	solid_warning_areas = !solid_warning_areas;
	apply_options();
}


function toggle_sound() {
	sound_enabled = !sound_enabled;

	// is this right?
	if (sound_enabled == false) {
		introSound.pause();
		cutSceneSound.pause();
		gameOverSound.pause();
		levelSound2.pause();
		levelSound1.pause();
		// what should happen to turn sound off properly?
	}
	if (sound_enabled == true) {
		// what should happen to re-enable sound?
		introSound.play();
	}

	apply_options();
}


function apply_options() {
	// save
	save_options();
	// now refresh the options ui to reflect this
	option_screen.refresh();
}