instructionController=function() {
	var level_indicators = [ ];

	return {
		initialize : function() {
			$('#instructions').show();
			set_page("objective");
			$(window).resize(center_title);
			center_title();
			load_variable_values();
			load_all_icons();
			load_agent_names();
			load_location_names();
			load_special_action_names();
			initialize_global_status_elements();
			countdownController.setCountdown(
				"instruction_submit_timer",
				variables.turn_length,
				function(id) { }
			);
			$('#objective_button').click(function() {
				set_page("objective");
			} );
			$('#gameplay_button').click(function() {
				set_page("gameplay_instructions");
			} );
			$('#map_button').click(function() {
				set_page("map_and_icons");
			} );
			$('#agent_button').click(function() {
				set_page("agent_instruction");
			} );
			$('#parameter_button').click(function() {
				set_page("parameters");
			} );
			$('#strategy_button').click(function() {
				set_page("strategy");
			} );
			$('#start_button').click(function() {
				$('#start_button').hide();
				$('#waiting').show();
				setTimeout(function() {
					submit("instruction_done")
				}, 1000);
			} );

			$('#show_instructions').click(this.show_instructions);
			$('#hide_instructions').click(this.hide_instructions);

                        $('#waiting_room').html('<a href="' +
				WAITING_ROOM_URL +
				'">Return to Waiting Room</a>');
		},

		show_instructions : function() {
			$('#instructions').show();
			$('#show_instructions').hide();
			$('#hide_instructions').show();
		},

		hide_instructions : function() {
			$('#instructions').hide();
			$('#show_instructions').show();
			$('#hide_instructions').hide();
			$('#instruction_headings').hide();
			$('#waiting').hide();
		},

		end_game : function() {
			$('#nav_buttons').hide();
			$('#instructions').show();
			$('#instruction_headings').show();
			set_page("result_page");
		}
	}

	function center_title() {
		var instruction_width = $("#instructions").width();

		[ "#title_box", "#start_game" ].forEach(function(element_id) {
			var element_width = $(element_id).width();
			var diff, pixels, margin_left;

			diff = instruction_width - element_width;
			pixels = Math.floor(diff / 2);
			margin_left = String(pixels) + "px";
			$(element_id).css("margin-left", margin_left);
		} );
	}

	function hide_all_instruction_pages() {
		$('#objective').hide();
		$('#gameplay_instructions').hide();
		$('#map_and_icons').hide();
		$('#agent_instruction').hide();
		$('#parameters').hide();
		$('#strategy').hide();
	}

	function set_page(page) {
		hide_all_instruction_pages();
		$('#'+page).show();
	}

	function load_variable_values() {
		[ "aid_decrement",
		  "aid_impact",
		  "aid_level",
		  "bank_balance",
		  "blue_team_strength_target",
		  "bomber_building_ied_wait",
		  "cp_blown_to_bits_cost",
		  "cp_blown_to_bits_wait",
		  "cp_engaging_wait",
		  "cp_fuel_capacity",
		  "cp_out_of_fuel_wait",
		  "cp_refueling_wait",
		  "convoy_blown_to_bits_cost",
		  "convoy_blown_to_bits_wait",
		  "convoy_delivering_wait",
		  "convoy_fuel_capacity",
		  "convoy_loading_wait",
		  "convoy_out_of_fuel_wait",
		  "convoy_refueling_wait",
		  "digger_captured_cost",
		  "digger_captured_wait",
		  "digger_planting_wait",
		  "financier_captured_cost",
		  "financier_captured_wait",
		  "ied_cost",
		  "smuggler_captured_cost",
		  "smuggler_captured_wait",
		  "turn_length",
		  "uav_fuel_capacity",
		  "uav_out_of_fuel_wait",
		  "uav_refueling_wait",
		  "village_sentiment_target",
		].forEach(function(variable) {
			$(variable).text(variables[variable]);
		} );
	}

	function load_all_icons() {
		for (var agent_type in agents) {
			var agent = agents[agent_type];
			load_icon(agent_type + '_img', agent.file);
		}
		load_icon("bomb_img", "bmico.png");
		load_icon("explosion_img", "Boom2.png");
		load_icon("map_img", "IED_Background2.png");
	}

	function load_icon(element, img_file) {
		var img_html = '<img src="' + getFile(img_file) + '" />';
		$(element).html(img_html);
	}

	function load_agent_names() {
		for (var agent_type in agents) {
			var agent = agents[agent_type];
			var name_html;

			name_html = '<span class="' +
				agent.side +
				'_instruction">' +
				agent.display_name +
				'</span>';

			$(agent_type + '_name').html(name_html);
		}
	}

	function load_location_names() {
		for (var location_name in locations) {
			var location = locations[location_name];
			var name_html;

			name_html = '<span>' +
				location.display_name +
				'</span>';

			$(location_name + '_name').html(name_html);
		}
	}

	function load_special_action_names() {
		for (var action_type in special_actions) {
			var action = special_actions[action_type];
			var action_html;

			action_html = '<span>' +
				action.display_name +
				'</span>';

			$(action_type + '_name').html(action_html);
		}
	}

	function initialize_global_status_elements() {
		var indicator;
		var level = 2 * Number(variables.aid_impact);
		villages.forEach(function(village) {
			var id = village + "_sentiment_instr";
			var text = locations[village].display_name +
				" Sentiment";
			indicator = new PercentRangeIndicator(id, text, 0,
				Number(variables.village_sentiment_target));
			indicator.val(level);
			level -= Number(variables.aid_impact);
			level_indicators.push(indicator);
		} );
		indicator = new PercentRangeIndicator("bf_strength_instr",
			"Blue Force Strength",
			Number(variables.blue_team_strength_target),
			100, true);
		indicator.val(91);
		level_indicators.push(indicator);
	}

} ();
