/* ---------------------
Menu Stuff

We're going to have a menu entity for each type of menu (start, pause, maybe options)

a menu coordinator/director
	builds the menu items
	controls which items are selected via keyboard
	
	maybe specify a background at some point
	
menu item
	designates next action (load level, another/sub menu, new game, continue game, etc)
	mouse click also activates?
	
	grayed out when unselectable
	when selected, blink
	
	eventually need a toggleable menu item
----------------------*/

//We're going to test with levels to begin with

var StartMenuEntity = me.ObjectEntity.extend({
	init:function(x,y,settings){
		//gameData.save('darknessModeUnlocked','true');
				
		//Options
		/*
			Continue (if saved game is detected) - M1 Level 01 - THe hIVeetc
			New Game [Normal] (if saved game is detected, prompt with yes no screen)
			New Game [Darkness] (if beat game is detected, prompt with yes no screen)
			Level Select (goes to mission select screen)
			Options (goes to option screen)
			Quit to Desktop
		
		*/
		
		this.parent(x,y,settings);
		//this.script = script[settings.script];
		//this.scriptLength = this.script.length;
		//this.nextLevel = settings.nextLevel;
		
		//
		this.updateWhenPaused = true;
		this.floating = true;
		this.transitioning = false;
		
		this.checkForCheat = false;
		
		//FullScreen Hack Fix		
		this.resizeFromFullScreen = false;
		this.resizeFromFullScreenCount = 0;
		this.lastWinWidth = 0;
		
		if(this.renderable){
			//this.renderable.alpha = 0.8;
			//this.renderable.addAnimation('static',[0,1,2,3],1);
			//this.renderable.setCurrentAnimation('static');
		}
		
		//Set correct Key Bindings
		this.setMenuKeyBindings();
		
		this.exitTo = '';
		
		if(settings.menu == 'start'){
			this.startMenu();
			this.checkForCheat = true;
		}else if(settings.menu == 'newGameDarknessConfirmation'){
			this.newGameDarknessConfirmation();
			this.exitTo = 'start_menu';
		}else if(settings.menu == 'newGameNormalConfirmation'){
			this.newGameNormalConfirmation();
			this.exitTo = 'start_menu';
		}else if(settings.menu == 'newGameNoviceConfirmation'){
			this.newGameNoviceConfirmation();
			this.exitTo = 'start_menu';
		}else if(settings.menu == 'newGameTrueNoviceConfirmation'){
			this.newGameTrueNoviceConfirmation();
			this.exitTo = 'start_menu';
		}else if(settings.menu == 'newGameGhostConfirmation'){
			this.newGameGhostConfirmation();
			this.exitTo = 'start_menu';
		}else if(settings.menu == 'optionsMenu'){
			this.resizeFromFullScreen = true;
			try{					
				var win = gui.Window.get();
				this.lastWinWidth = win.width;		
			}catch (e){
				
			}
			
			this.optionsMenu();
			this.exitTo = 'start_menu';
		}else if(settings.menu == 'optionsAudioMenu'){
			this.optionsAudioMenu();
			this.exitTo = 'options_menu';
		}else if(settings.menu == 'optionsWebGLMenu'){
			this.optionsWebGLMenu();
			this.exitTo = 'options_menu';
		}else if(settings.menu == 'optionsControlsMenu'){
			this.optionsControlsMenu();
			this.exitTo = 'options_menu';
		}else if(settings.menu == 'optionsScreenShakeMenu'){
			this.optionsScreenShakeMenu();
			this.exitTo = 'options_menu';
		}else if(settings.menu == 'optionsCheckPointMenu'){
			this.optionsCheckPointMenu();
			this.exitTo = 'options_menu';
		}else if(settings.menu == 'optionsWindowedMenu'){
			this.optionsWindowedMenu();
			this.exitTo = 'options_menu';
		}else if(settings.menu == 'mission_01'){
			this.levelSelectMenu('m_01');
			this.exitTo = 'missionSelect';
		}else if(settings.menu == 'mission_02'){
			this.levelSelectMenu('m_02');
			this.exitTo = 'missionSelect';
		}else if(settings.menu == 'mission_03'){
			this.levelSelectMenu('m_03');
			this.exitTo = 'missionSelect';
		}else if(settings.menu == 'mission_04'){
			this.levelSelectMenu('m_04');
			this.exitTo = 'missionSelect';
		}else if(settings.menu == 'mission_05'){
			this.levelSelectMenu('m_05');
			this.exitTo = 'missionSelect';
		}else if(settings.menu == 'mission_vr'){
			this.levelSelectMenu('m_vr');
			this.exitTo = 'missionSelect';
		}else if(settings.menu == 'cutscenes'){
			this.cutsceneSelectMenu();
			this.exitTo = 'start_menu';
		}else if(settings.menu == 'levelScoreScreen'){
			this.levelScoreScreen();
		}
		
		this.cheatCounter = 0;
		this.cheatTime = 60;
		
		//CHEATERS!
		this.cheatString = '11223434';
		this.cheatInput = '';
		
		//leveltransition = new LevelTransitionEntity(-160, -120, {image:'spiraltransition3', spritewidth:640, spriteheight: 480, transitionType: 'in' });
		var leveltransition = me.entityPool.newInstanceOf("leveltransition",-160, -120, {image:'spiraltransition3', spritewidth:640, spriteheight: 480, transitionType: 'in' });
		me.game.add(leveltransition,300);
		//me.game.sort();
	},
	update:function(){
		if(this.renderable){
			//this.renderable.alpha = 1;
			
			//this.renderable.setCurrentAnimation('static');
		}
		
		//Resize Hack
		if(this.resizeFromFullScreen && me.gamestat.getItemValue('resizeFromFullScreen') == 'yes'){
			
			//Resize Hack
			var scaleBy = gameData.load('resolutionSetting');
			
			try{
				//Resize it!
				var win = gui.Window.get();
				
				this.resizeFromFullScreenCount++;
				var desiredWidth = (320*scaleBy+windowOffSetWidth);
				var currentWidth = gui.Window.get().width;
				
				if(this.resizeFromFullScreenCount >= 60){
					this.resizeFromFullScreenCount = 0;
					
					if(currentWidth != desiredWidth){
						
						win.setResizable(true);
						win.resizeTo(320*scaleBy+windowOffSetWidth,240*scaleBy+windowOffSetHeight);
						
						//Center it!
						var newXPos = (window.screen.width/2) - (win.width/2);
						var newYPos = (window.screen.height/2) - (win.height/2);					
						win.moveTo(newXPos, newYPos);
						
						win.setResizable(false);
						//We're *probably* at the right size
						me.gamestat.setValue('resizeFromFullScreen', 'no');
						this.resizeFromFullScreen = false;
						
					}else{
						//We're at the right size
						me.gamestat.setValue('resizeFromFullScreen', 'no');
						this.resizeFromFullScreen = false;
					}
					
				}				
				
			}catch (e){
					
			}				
			
		}else{
			this.resizeFromFullScreen = false;
		}
		
		//Check for change		
		//Watch for Up/Down and space/enter
		if(me.input.isKeyPressed('enter')){
			//Either go to submenu or load a level.
			
			var selectedOption = me.game.getEntityByProp('optionName',this.menuOptions[this.currentSelection]);
			for(var i = 0; i < selectedOption.length; i++){
				if(selectedOption[i].action == 'loadLevel'){
					
					if(selectedOption[i].unlocked){
						me.audio.play('title', false, null, 0.5);
						this.loadLevel(selectedOption[i].levelName);
					}else{
						//er sfx
						me.audio.play('menudeny');
					}
				}else if(selectedOption[i].action == 'loadCutscene'){
					if(selectedOption[i].unlocked){
						me.audio.play('title', false, null, 0.5);
						this.loadCutscene(selectedOption[i].levelName);
					}else{
						//er sfx
						me.audio.play('menudeny');
					}
				}else if(selectedOption[i].action == 'continue'){
					me.audio.play('title', false, null, 0.5);
					this.continueGame();
				}else if(selectedOption[i].action == 'newGameNormal'){
					me.audio.play('title', false, null, 0.5);
					this.newGameNormal();
				}else if(selectedOption[i].action == 'newGameTrueNovice'){
					me.audio.play('title', false, null, 0.5);
					this.newGameTrueNovice();
				}else if(selectedOption[i].action == 'newGameNovice'){
					me.audio.play('title', false, null, 0.5);
					this.newGameNovice();
				}else if(selectedOption[i].action == 'newGameDarkness'){
					me.audio.play('title', false, null, 0.5);
					this.newGameDarkness();
				}else if(selectedOption[i].action == 'newGameGhost'){
					me.audio.play('title', false, null, 0.5);
					this.newGameGhost();
				}else if(selectedOption[i].action == 'missionSelect'){
					me.audio.play('menuselect');
					this.missionSelect();
				}else if(selectedOption[i].action == 'options'){
					me.audio.play('menuselect');
					this.options();
				}else if(selectedOption[i].action == 'quitToDesktop'){
					me.audio.play('computer_disconnect');
					this.quitToDesktop();
				}else if(selectedOption[i].action == 'newGameNormalYes'){
					me.audio.play('title', false, null, 0.5);
					this.newGameNormalYes();
				}else if(selectedOption[i].action == 'newGameNoviceYes'){
					me.audio.play('title', false, null, 0.5);
					this.newGameNoviceYes();
				}else if(selectedOption[i].action == 'newGameTrueNoviceYes'){
					me.audio.play('title', false, null, 0.5);
					this.newGameTrueNoviceYes();
				}else if(selectedOption[i].action == 'newGameGhostYes'){
					me.audio.play('title', false, null, 0.5);
					this.newGameGhostYes();
				}else if(selectedOption[i].action == 'newGameNormalNo'){
					me.audio.play('uncloak');
					this.newGameNormalNo();
				}else if(selectedOption[i].action == 'newGameNoviceNo'){
					me.audio.play('uncloak');
					this.newGameNoviceNo();
				}else if(selectedOption[i].action == 'newGameTrueNoviceNo'){
					me.audio.play('uncloak');
					this.newGameTrueNoviceNo();
				}else if(selectedOption[i].action == 'newGameGhostNo'){
					me.audio.play('uncloak');
					this.newGameGhostNo();
				}else if(selectedOption[i].action == 'newGameDarknessYes'){
					me.audio.play('title', false, null, 0.5);
					this.newGameDarknessYes();
				}else if(selectedOption[i].action == 'newGameDarknessNo'){
					me.audio.play('uncloak');
					this.newGameDarknessNo();
				}else if(selectedOption[i].action == 'procedeToNextLevel'){
					me.audio.play('title', false, null, 0.5);				
					this.procedeToNextLevel(selectedOption[i].levelName);
				}else if(selectedOption[i].action == 'replayLevel'){
					me.audio.play('title', false, null, 0.5);
					this.replayLevel(selectedOption[i].levelName);
				}else if(selectedOption[i].action == 'quitToMissionMenu'){
					me.audio.play('uncloak');
					this.quitToMissionMenu(selectedOption[i].levelName);
				}else if(selectedOption[i].action == 'quitToStartMenu'){
					me.audio.play('uncloak');
					this.quitToStartMenu();
				}else if(selectedOption[i].action == 'loadOption'){
					me.audio.play('menuselect');
					this.loadOption(selectedOption[i].levelName);					
				}else if(selectedOption[i].action == 'optionBackNo'){
					me.audio.play('uncloak');
					this.exitToOptionMenu();
				}else if(selectedOption[i].action == 'optionAudioYes'){
					me.audio.play('menuselect');
					this.optionAudioYes();					
				}else if(selectedOption[i].action == 'optionWebGLYes'){
					me.audio.play('menuselect');
					this.optionWebGLYes();					
				}else if(selectedOption[i].action == 'optionScreenShakeYes'){
					me.audio.play('menuselect');
					this.optionScreenShakeYes();					
				}else if(selectedOption[i].action == 'optionCheckPointYes'){
					me.audio.play('menuselect');
					this.optionCheckPointYes();					
				}else if(selectedOption[i].action == 'optionResolutionChange'){
					me.audio.play('menuselect');
					this.optionResolutionChange(selectedOption[i].ressetting);
					//this.optionWindowedYes();
				}else if(selectedOption[i].action == 'cutscenes'){
					me.audio.play('menuselect');
					this.transitionToLevel('cutscene_select');
					//this.optionWindowedYes();
				}else{
					me.audio.play('uncloak');
				}
			}
				
		}else if(me.input.isKeyPressed('up')){
			//navigate to next position in array
			if(this.currentSelection !== 0){
				
				me.audio.play('menucursor');
				
				var selectedOption = me.game.getEntityByProp('optionName',this.menuOptions[this.currentSelection]);
				for(var i = 0; i < selectedOption.length; i++){
					selectedOption[i].selected = false;					
				}
				
				this.currentSelection -= 1;
				
				var selectedOption = me.game.getEntityByProp('optionName',this.menuOptions[this.currentSelection]);
				for(var i = 0; i < selectedOption.length; i++){
					selectedOption[i].selected = true;							
				}
			}
			
			this.cheatInput += '1';
			this.cheatCounter = 0;
		}else if(me.input.isKeyPressed('down')){
			
			//navigate to previous position in array
			if(this.currentSelection != this.menuOptionsLength){
			
				//me.audio.play('switchtrigger');
				me.audio.play('menucursor');
				
				var selectedOption = me.game.getEntityByProp('optionName',this.menuOptions[this.currentSelection]);
				for(var i = 0; i < selectedOption.length; i++){
					//console.log(trapDevices[i]);
					selectedOption[i].selected = false;							
				}
				
				//Go up
				this.currentSelection += 1;
				//console.log(this.menuOptions[this.currentSelection]);
				
				
				var selectedOption = me.game.getEntityByProp('optionName',this.menuOptions[this.currentSelection]);
				for(var i = 0; i < selectedOption.length; i++){
					selectedOption[i].selected = true;							
				}				
			}
			
			this.cheatInput += '2';
			this.cheatCounter = 0;
		}
		
		if(me.input.isKeyPressed('left')){
			this.cheatInput += '3';
			this.cheatCounter = 0;
		}
		
		if(me.input.isKeyPressed('right')){
			this.cheatInput += '4';
			this.cheatCounter = 0;
		}
		
		if(me.input.isKeyPressed('pause')){
			//Pause, bleh, this is escape key
			//if level select menu
			if(this.exitTo == 'missionSelect'){
				this.exitLevelSelectMenu();
			}else if(this.exitTo == 'start_menu'){
				this.exitToStartMenu();
			}else if(this.exitTo == 'options_menu'){
				this.exitToOptionMenu();
			}
		}
		
		/*
		if(me.input.isKeyPressed('pause')){
			if(me.state.isPaused()){
				this.unPause();
			}
		} */
		
		if(this.checkForCheat && me.gamestat.getItemValue('cheatEnabled') !== 1){
			if(this.cheatInput.length > this.cheatString.length){
				this.cheatInput = this.cheatInput.substr((this.cheatInput.length - this.cheatString.length));
			}

			if(this.cheatInput == this.cheatString){
				me.gamestat.setValue('cheatEnabled',1);
				//devMode = true;
				me.audio.play('title', false, null, 0.5);
				//console.log('Cheats Enabled! ' + me.gamestat.getItemValue('cheatEnabled'));
				
			}
			
			if(this.cheatCounter <= this.cheatTime){
				this.cheatCounter++;
			}else{
				this.cheatCounter = 0;
				this.cheatInput = '';
			} 
		}
	
	},	
	loadCutscene: function(levelName){
		//Time Attack because it gets reset right for normal games
		me.gamestat.setValue('currentMode','cutSceneSelect');
		this.fixKeyBindings();
		this.transitionToLevel(levelName);
	},
	loadLevel: function(levelName){ // Used for Level Select/Time Attack Mode
		
		me.gamestat.setValue('currentMode','timeAttack');
		me.gamestat.setValue('currentTimeAttackLevel',levelName);
		me.gamestat.setValue('currentTimeAttackLevelTime',0);
		me.gamestat.setValue('currentTimeAttackLevelCaught',0);
		
		if(me.audio.getCurrentTrack()){
			//Might not need this
			//me.audio.stop(me.audio.getCurrentTrack());
		}
		if(me.state.isPaused()){
			this.unPause();
		}
		this.fixKeyBindings();
		//me.levelDirector.loadLevel(levelName);
		
		//Get the mission name, load the right cutscene
		var currentMission = levelName.split('_')[0] + '_' + levelName.split('_')[1] + '_level_load';
		
		//check to see if mission 5, level 8 or greater, append '_b'
		if(levelName.split('_')[1] == '05' && levelName.split('_')[3] >= 8){
			currentMission += "_b";
		}
		
		this.transitionToLevel(currentMission);
	},
	exitLevelSelectMenu:function(){
		//me.levelDirector.loadLevel('mission_select');
		me.audio.play('uncloak');
		this.transitionToLevel('mission_select');
	},
	exitToOptionMenu:function(){
		//me.levelDirector.loadLevel('mission_select');
		me.audio.play('uncloak');
		this.transitionToLevel('options_menu');
	},	
	exitToStartMenu:function(){
		//me.levelDirector.loadLevel('mission_select');
		me.audio.play('uncloak');
		this.transitionToLevel('start_menu');
	},
	loadOption:function(option){
		this.transitionToLevel(option);
	},
	optionAudioYes:function(){
		//Check to see if audio is already enabled, if it 
		if(audioIsMuted == true){
			me.audio.unmuteAll();
			audioIsMuted = false;
			gameData.save('audioIsMuted','no');
		}else{
			me.audio.muteAll();
			audioIsMuted = true;
			gameData.save('audioIsMuted','yes');
		}
			
		this.transitionToLevel('options_menu');
	},
	optionWebGLYes:function(){
		//Check to see if webgl is already enabled, if it 
		if(gameData.load('webGLDisabled') == 'yes'){
			gameData.save('webGLDisabled','no');
			fakeCRT();
		}else{
			gameData.save('webGLDisabled','yes');
			clearWebGL();
		}
		
		gameData.save('webGLUserPrefSet','yes');
		/*
		
		try{
			var win = gui.Window.get();
			win.reloadDev();			
		}catch (e){
			alert('nope');
		}
			*/
		this.transitionToLevel('options_menu');
	},
	optionScreenShakeYes:function(){
		//Check to see if webgl is already enabled, if it 
		if(gameData.load('screenshakeDisabled') == 'yes'){
			gameData.save('screenshakeDisabled','no');
			me.gamestat.setValue('screenshakeDisabled','no');
		}else{
			gameData.save('screenshakeDisabled','yes');
			me.gamestat.setValue('screenshakeDisabled','yes');
		}		
		this.transitionToLevel('options_menu');
	},
	optionCheckPointYes:function(){
		//Check to see if webgl is already enabled, if it 
		if(gameData.load('levelSelectCPDisabled') == 'yes'){
			gameData.save('levelSelectCPDisabled','no');
			me.gamestat.setValue('levelSelectCPDisabled','no');
		}else{
			gameData.save('levelSelectCPDisabled','yes');
			me.gamestat.setValue('levelSelectCPDisabled','yes');
		}		
		this.transitionToLevel('options_menu');
	},
	optionWindowedYes:function(){
		//Check to see if windowed is already enabled, if it 
		if(gameData.load('windowed') == 'yes'){
			gameData.save('windowed','no');
		}else{
			gameData.save('windowed','yes');
		}
		/* unfortunately doesn't work in 0.8.x
		try{
			var win = gui.Window.get();
			win.reloadDev();			
		}catch (e){
			alert('nope');
		} */
		
		this.transitionToLevel('options_menu');
	},
	optionResolutionChange:function(ressetting){
		
		if(gameData.load('resolutionSetting') !== ressetting){ //If it's the same setting, don't do anything
			
			//console.log('resolution: ' + ressetting);		
				
			var scaleBy = 2;
			var defaultWidth = 320;
			var defaultHeight = 240;
			
			
			try{					
				var win = gui.Window.get();
			}catch (e){
				
			}
				
			
			if(ressetting == 'fullscreen'){ 
				//enter full screen
				//Update Node-webkit's frame width and height			
				try{					
					win.setResizable(true);
					win.enterFullscreen();					
					win.resizeTo(window.screen.width,window.screen.height);
				}catch (e){
					
				}
								
				//Take care of our page elements
				scaleBy = (window.screen.height / defaultHeight);
				var resizeWidth = scaleBy * defaultWidth;
				
				//document.getElementById('info').style.width = resizeWidth +'px';
				document.getElementById('jsapp').style.width = resizeWidth +'px';
				
				
			}else{ //Windowed Mode
				scaleBy = ressetting;
				//
				
				if(gameData.load('resolutionSetting') == 'fullscreen'){
					me.gamestat.setValue('resizeFromFullScreen', 'yes');
					//exit out of fullscreen					
					try{
						win.setResizable(true);
						win.leaveFullscreen();
						win.setResizable(false);
					}catch (e){
						
					}
				}else{
					try{
						win.setResizable(true);
						win.resizeTo(defaultWidth*scaleBy+windowOffSetWidth,defaultHeight*scaleBy+windowOffSetHeight);
						var newXPos = (window.screen.width/2) - (win.width/2);
						var newYPos = (window.screen.height/2) - (win.height/2);					
						win.moveTo(newXPos, newYPos);
						win.setResizable(false);
					}catch (e){
						
					}
					
				}	
				
				document.getElementById('jsapp').style.width = defaultWidth*scaleBy +'px';				
			}
			
			//check for device pixelDepth
			//console.log('scale by: ' + scaleBy);
			var resizeRatio = scaleBy * window.devicePixelRatio;
			me.video.updateDisplaySize(resizeRatio,resizeRatio);			
						
			//save setting
			gameData.save('resolutionSetting',ressetting); 
		
		}
		
		this.transitionToLevel('options_menu');	
		
	},
	continueGame: function(){
		//Get & Set Current LEVEL
		//Get & Set Current Mode
		//Load times and other stats
		if(gameData.load('currentMode') == 'darkness'){
			me.gamestat.setValue('currentMode', 'darkness');
		}else if(gameData.load('currentMode') == 'novice'){ //Novice is now Operative difficulty
			me.gamestat.setValue('currentMode', 'novice');
		}else if(gameData.load('currentMode') == 'trueNovice'){
			me.gamestat.setValue('currentMode', 'trueNovice');
		}else if(gameData.load('currentMode') == 'ghost'){
			me.gamestat.setValue('currentMode', 'ghost');
		}else{
			me.gamestat.setValue('currentMode', 'normal'); //normal mode is [MASTER]
		}
		
		console.log('Load current time: ' + gameData.load('current_level_time'));
		
		//Need to verify that these stats aren't null/NaN, in which case we 0
		/*
		if(isNaN(gameData.load('total_time')) || gameData.load('total_time') === null || gameData.load('total_time') == 0){
			gameData.save('total_time',0);
		}
		
		if(isNaN(gameData.load('total_caught')) || gameData.load('total_caught') === null || gameData.load('total_caught') == 0){
			gameData.save('total_caught',0);
		}*/
		
		if(isNaN(gameData.load('current_mission_time')) || gameData.load('current_mission_time') === null || gameData.load('current_mission_time') == 0){
			gameData.save('current_mission_time',0);
		}
		
		if(isNaN(gameData.load('current_mission_caught')) || gameData.load('current_mission_caught') === null || gameData.load('current_mission_caught') == 0){
			gameData.save('current_mission_caught',0);
		}
		
		if(isNaN(gameData.load('current_level_time')) || gameData.load('current_level_time') === null || gameData.load('current_level_time') == 0){
			gameData.save('current_level_time',0);
		}
		
		if(isNaN(gameData.load('current_level_caught')) || gameData.load('current_level_caught') === null || gameData.load('current_level_caught') == 0){
			gameData.save('current_level_caught',0);
		}
		
		if(isNaN(gameData.load('current_game_caught')) || gameData.load('current_game_caught') === null || gameData.load('current_game_caught') == 0){
			gameData.save('current_game_caught',0);
		}
		
		if(isNaN(gameData.load('current_game_time')) || gameData.load('current_game_time') === null || gameData.load('current_game_time') == 0){
			gameData.save('current_game_time',0);
		}
		
		console.log('Load current time: ' + gameData.load('current_level_time'));
		
		me.gamestat.setValue('current_mission_time',parseInt(gameData.load('current_mission_time')));
		me.gamestat.setValue('current_mission_caught',parseInt(gameData.load('current_mission_caught')));
		
		me.gamestat.setValue('current_level_time',parseInt(gameData.load('current_level_time')));
		me.gamestat.setValue('current_level_caught',parseInt(gameData.load('current_level_caught')));	
		me.gamestat.setValue('current_game_time',parseInt(gameData.load('current_game_time')));
		me.gamestat.setValue('current_game_caught',parseInt(gameData.load('current_game_caught')));			
			
		this.fixKeyBindings();
		//load level
		//me.levelDirector.loadLevel(gameData.load('currentLevel'));
		//this.transitionToLevel(gameData.load('currentLevel'));
		
		//We are going to transition to the Mission Screen first
		me.gamestat.setValue('loadingSaveGame',true);
		var currentLevel = gameData.load('currentLevel');
		var currentMission = currentLevel.split('_')[0] + '_' + currentLevel.split('_')[1] + '_mission_screen';
		
		this.transitionToLevel(currentMission);
	},
	newGameNormal: function(){
		//Get & Set Current LEVEL
		//Get & Set Current Mode
		//Clear all stats
		
		//load level
		if(!gameData.load('currentLevel')){
			//clear all stats here
			this.newGameResetStats();
			
			//set mode to normal
			gameData.save('currentMode','normal');
		
			me.gamestat.setValue('currentMode', 'normal');
			//load into Mission 1 Mission Screen
			this.fixKeyBindings();
			//me.levelDirector.loadLevel('m_01_mission_screen');
			this.transitionToLevel('m_01_mission_screen');
		}else{
			//me.levelDirector.loadLevel('new_game_normal');
			this.transitionToLevel('new_game_normal');
		}
	},
	newGameNovice: function(){
		//Get & Set Current LEVEL
		//Get & Set Current Mode
		//Clear all stats
		
		//load level
		if(!gameData.load('currentLevel')){
			//clear all stats here
			this.newGameResetStats();
			
			//set mode to novice
			gameData.save('currentMode','novice');
		
			me.gamestat.setValue('currentMode', 'novice');
			//load into Mission 1 Mission Screen
			this.fixKeyBindings();
			//me.levelDirector.loadLevel('m_01_mission_screen');
			this.transitionToLevel('m_01_mission_screen');
		}else{
			//me.levelDirector.loadLevel('new_game_normal');
			this.transitionToLevel('new_game_novice');
		}
	},
	newGameTrueNovice: function(){
		//Get & Set Current LEVEL
		//Get & Set Current Mode
		//Clear all stats
		
		//load level
		if(!gameData.load('currentLevel')){
			//clear all stats here
			this.newGameResetStats();
			
			//set mode to novice
			gameData.save('currentMode','trueNovice');
		
			me.gamestat.setValue('currentMode', 'trueNovice');
			//load into Mission 1 Mission Screen
			this.fixKeyBindings();
			//me.levelDirector.loadLevel('m_01_mission_screen');
			this.transitionToLevel('m_01_mission_screen');
		}else{
			//me.levelDirector.loadLevel('new_game_normal');
			this.transitionToLevel('new_game_true_novice');
		}
	},
	newGameGhost: function(){
		//Get & Set Current LEVEL
		//Get & Set Current Mode
		//Clear all stats
		
		//load level
		if(!gameData.load('currentLevel')){
			//clear all stats here
			this.newGameResetStats();
			
			//set mode to novice
			gameData.save('currentMode','ghost');
		
			me.gamestat.setValue('currentMode', 'ghost');
			//load into Mission 1 Mission Screen
			this.fixKeyBindings();
			//me.levelDirector.loadLevel('m_01_mission_screen');
			this.transitionToLevel('m_01_mission_screen');
		}else{
			//me.levelDirector.loadLevel('new_game_normal');
			this.transitionToLevel('new_game_ghost');
		}
	},
	newGameNormalNo: function(){
		//me.levelDirector.loadLevel('start_menu');
		this.transitionToLevel('start_menu');
	},
	newGameNoviceNo: function(){
		//me.levelDirector.loadLevel('start_menu');
		this.transitionToLevel('start_menu');
	},
	newGameTrueNoviceNo: function(){
		//me.levelDirector.loadLevel('start_menu');
		this.transitionToLevel('start_menu');
	},
	newGameGhostNo: function(){
		//me.levelDirector.loadLevel('start_menu');
		this.transitionToLevel('start_menu');
	},
	newGameNormalYes: function(){
		//clear all stats here
		this.newGameResetStats();
		
		//set mode to normal
		gameData.save('currentMode','normal');
		
		me.gamestat.setValue('currentMode', 'normal');
		
		//load into Mission 1 Mission Screen
		this.fixKeyBindings();
		//me.levelDirector.loadLevel('m_01_mission_screen');
		this.transitionToLevel('m_01_mission_screen');
	},
	newGameNoviceYes: function(){
		//clear all stats here
		this.newGameResetStats();
		
		//set mode to normal
		gameData.save('currentMode','novice');		
		me.gamestat.setValue('currentMode', 'novice');
		
		//gameData.save('currentDifficulty','novice');		
		//me.gamestat.setValue('currentDifficulty', 'novice');
		
		//load into Mission 1 Mission Screen
		this.fixKeyBindings();
		//me.levelDirector.loadLevel('m_01_mission_screen');
		this.transitionToLevel('m_01_mission_screen');
	},

	newGameTrueNoviceYes: function(){
		//clear all stats here
		this.newGameResetStats();
		
		//set mode to normal
		gameData.save('currentMode','trueNovice');		
		me.gamestat.setValue('currentMode', 'trueNovice');
		
		//gameData.save('currentDifficulty','novice');		
		//me.gamestat.setValue('currentDifficulty', 'novice');
		
		//load into Mission 1 Mission Screen
		this.fixKeyBindings();
		//me.levelDirector.loadLevel('m_01_mission_screen');
		this.transitionToLevel('m_01_mission_screen');
	},
	newGameGhostYes: function(){
		//clear all stats here
		this.newGameResetStats();
		
		//set mode to normal
		gameData.save('currentMode','ghost');		
		me.gamestat.setValue('currentMode', 'ghost');
		
		//gameData.save('currentDifficulty','novice');		
		//me.gamestat.setValue('currentDifficulty', 'novice');
		
		//load into Mission 1 Mission Screen
		this.fixKeyBindings();
		//me.levelDirector.loadLevel('m_01_mission_screen');
		this.transitionToLevel('m_01_mission_screen');
	},
	newGameDarkness: function(){
		//Get & Set Current LEVEL
		//Get & Set Current Mode
				
		//load level
		//me.levelDirector.loadLevel('new_game_darkness');
		this.transitionToLevel('new_game_darkness');
	},
	newGameDarknessNo: function(){
		//me.levelDirector.loadLevel('start_menu');
		this.transitionToLevel('start_menu');
	},
	newGameDarknessYes: function(){
		//clear all stats here
		this.newGameResetStats();
		
		//set mode to darkness
		//load into Mission 1 Mission Screen
		gameData.save('currentMode','darkness');
		
		me.gamestat.setValue('currentMode', 'darkness');
		
		this.fixKeyBindings();
		//me.levelDirector.loadLevel('m_01_mission_screen');
		this.transitionToLevel('m_01_mission_screen');
	},
	newGameResetStats: function(){
		
		gameData.save('vr_01_complete','');
		gameData.save('vr_02_complete','');
		gameData.save('vr_03_complete','');
		gameData.save('vr_04_complete','');
		gameData.save('vr_05_complete','');
		gameData.save('vr_06_complete','');
		gameData.save('vr_07_complete','');
		gameData.save('vr_08_complete','');

		gameData.save('vr_complete','');
		
		gameData.save('current_mission_time',0);
		gameData.save('current_mission_caught',0);
		gameData.save('current_level_time',0);
		gameData.save('current_level_caught',0);
		gameData.save('current_game_time',0);
		gameData.save('current_game_caught',0);
		
		gameData.save('currentLevel','m_01_lvl_01');
		
		me.gamestat.setValue('current_mission_time',0);
		me.gamestat.setValue('current_mission_caught',0);
		me.gamestat.setValue('current_level_time',0);
		me.gamestat.setValue('current_level_caught',0);	
		me.gamestat.setValue('current_game_time',0);
		me.gamestat.setValue('current_game_caught',0);	
		
		me.gamestat.setValue('current_mission_time_temp',0);
		me.gamestat.setValue('current_mission_caught_temp',0);	

		gameData.save('mixedMode',false);
		
	},
	missionSelect: function(){
		//Load Mission Select Menu Screen
		//me.levelDirector.loadLevel('mission_select');
		this.transitionToLevel('mission_select');
	},
	options: function(){
		//Load Options Menu Screen
		this.transitionToLevel('options_menu');
	},
	quitToDesktop: function(){
		//Use Node function to exit game
		//gui.App.quit();
		try{
			gui.App.quit();
		}catch (e){
			
		}
	},
	procedeToNextLevel: function(nextLevel){
			
		this.fixKeyBindings();
		me.gamestat.setValue('currentTimeAttackLevel',nextLevel);
		//me.levelDirector.loadLevel(nextLevel);
		this.transitionToLevel(nextLevel);
	},
	replayLevel: function(nextLevel){
		this.fixKeyBindings();
		me.gamestat.setValue('currentTimeAttackLevel',nextLevel);
		//me.levelDirector.loadLevel(nextLevel);
		this.transitionToLevel(nextLevel);
	},
	quitToMissionMenu: function(nextLevel){
		this.fixKeyBindings();
		//me.gamestat.setValue('currentTimeAttackLevel',nextLevel);
		//me.levelDirector.loadLevel(nextLevel);
		this.transitionToLevel(nextLevel);
	},
	quitToStartMenu: function(){ //This is actually title screen not start menu
		this.transitionToLevel('title_screen');
	},
	transitionToLevel:function(levelName){
		//leveltransition = new LevelTransitionEntity(-160, -120, {image:'spiraltransition3', spritewidth:640, spriteheight: 480, transitionType: 'out' });
		var leveltransition = me.entityPool.newInstanceOf("leveltransition",-160, -120, {image:'spiraltransition3', spritewidth:640, spriteheight: 480, transitionType: 'out' });		
			me.game.add(leveltransition,300);
			//me.game.sort();
					
			me.game.viewport.fadeIn('#000000',500, function(){ me.levelDirector.loadLevel(levelName); });
	},
	fixKeyBindings: function(){
		//enable the keyboard
		/*
		me.input.bindKey(me.input.KEY.A, "left");
		me.input.bindKey(me.input.KEY.D, "right");
		me.input.bindKey(me.input.KEY.W, "jump", true);
		me.input.bindKey(me.input.KEY.LEFT, "left");
		me.input.bindKey(me.input.KEY.RIGHT, "right");
		me.input.bindKey(me.input.KEY.UP, "jump", true);
		me.input.bindKey(me.input.KEY.SPACE, "jump", true);
		me.input.bindKey(me.input.KEY.X, "jump", true);
		me.input.bindKey(me.input.KEY.SHIFT, "stealth", true);
		me.input.bindKey(me.input.KEY.S, "stealth", true);
		me.input.bindKey(me.input.KEY.DOWN, "stealth", true);
		*/
		InputControls.setForGameplay();
	},
	setMenuKeyBindings: function(){
		//Key Bindings 
		/*
		me.input.bindKey(me.input.KEY.SPACE, "enter", true);
		me.input.bindKey(me.input.KEY.ENTER, "enter", true);
		
		//me.input.bindKey(me.input.KEY.ESC, "cancel", true);
		//me.input.bindKey(me.input.KEY.ENTER, "enter", true);
		
		me.input.bindKey(me.input.KEY.DOWN, "down", true);
		me.input.bindKey(me.input.KEY.S, "down", true);
		
		me.input.bindKey(me.input.KEY.UP, "up", true);
		me.input.bindKey(me.input.KEY.W, "up", true);
		*/
		InputControls.setForMenu();
	},
	unPause: function(){
		me.audio.play('computer_boot');
		this.fixKeyBindings();
		me.state.resume();
		var options = me.game.getEntityByProp('type','MENUOPTION');
		for(var i = 0; i < options.length; i++){
			me.game.remove(options[i],true);
		}
		me.game.remove(this);
	},
	startMenu: function(){
		//Build Menu System
		//Array that contains currently selected item and the next action to take
		//Nah going to be lazy and just do it live...
		
		if(gameData.load('currentLevel')){
			this.currentSelection = 0;
		}else{
			this.currentSelection = 1; //Setting this to 1 to default to [OPERATIVE] mode
		}
		
		this.posY = 64;
		this.posX = 32;
		this.ySpacing = 16;
		
		
		//var menuTitle = new CutSceneTextEntity(0, 0, {msg:'Menu',textPosY:20, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
		var menuTitle = me.entityPool.newInstanceOf("cutscenetext",0, 0, {msg:'Menu',textPosY:20, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
			me.game.add(menuTitle,100);
			//me.game.sort();
			
			
		//These are set as the optionName for each option, so we can find them and update them later on
		//this.menuOptions = ['mission1','mission2','mission3','mission4','mission5'];
		this.menuOptions = [];
		
		
		//CONTINUE
		//Check for Save Game
		if(gameData.load('currentLevel')){
			//Get MODE
			var current_mode = '';
			if(gameData.load('currentMode') == 'darkness'){
				me.gamestat.setValue('currentMode', 'darkness');
				var current_mode = 'BLIND MASTER';
			}else if(gameData.load('currentMode') == 'trueNovice'){
				me.gamestat.setValue('currentMode', 'trueNovice');
				var current_mode = 'NOVICE';
			}else if(gameData.load('currentMode') == 'novice'){
				me.gamestat.setValue('currentMode', 'novice');
				var current_mode = 'OPERATIVE';
			}else if(gameData.load('currentMode') == 'ghost'){
				me.gamestat.setValue('currentMode', 'ghost');
				var current_mode = 'NARRATIVE';
			}else{
				me.gamestat.setValue('currentMode', 'normal');
				var current_mode = 'MASTER';
			}			
			
			//Get Level Name
			
			var currentLevel = gameData.load('currentLevel'); // Get title of current level
			
			var misssionLevelNumber = currentLevel.split('_')[1] + '.' + currentLevel.split('_')[3] + ' ';
			
			var canonicalName = '';
			
			//Get Level Titles from this array
			for(var level = 0; level < levels.length; level++){
				if(levels[level].map == currentLevel){
					if(levels[level].name != ''){
						//canonicalName = misssionLevelNumber + levels[level].name;
						canonicalName = levels[level].name;
						//canonicalName = misssionLevelNumber.trim();
						
					}else{
						//canonicalName = misssionLevelNumber + '[no name]';
						canonicalName = '[no name]';
						//canonicalName = misssionLevelNumber.trim();
					}
				}
			}
			
		//	var option_1 = new MenuOptionEntity(0, 0, {optionName:'continue', msg:'CONTINUE [' + misssionLevelNumber.trim() + ']|' + canonicalName,action:'continue',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
			//var option_1 = new MenuOptionEntity(0, 0, {optionName:'continue', msg:'CONTINUE [' + misssionLevelNumber.trim() + '-' + current_mode + ']',action:'continue',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
			var option_1 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'continue', msg:'CONTINUE [' + misssionLevelNumber.trim() + '-' + current_mode + ']',action:'continue',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
			me.game.add(option_1,200);
			//me.game.sort();
			this.posY+=this.ySpacing;
			this.menuOptions.push('continue');
			var newGameSelected = false;
		}else{
			var newGameSelected = true;
		}
		
		
		//NEW GAME [NOVICE]
		//The is after normal
		//Hmmm maybe actually before normal mode
		
		//var option_21 = new MenuOptionEntity(0, 0, {optionName:'newGameNovice',msg:'NEW GAME [NOVICE]',action:'newGameNovice',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:newGameSelected});
		var option_22 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'newGameTrueNovice',msg:'NEW GAME [NOVICE]',action:'newGameTrueNovice',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_22,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('newGameTrueNovice');
		
		//NEW GAME [NOVICE]
		//The is after normal
		//Hmmm maybe actually before normal mode
		
		//var option_21 = new MenuOptionEntity(0, 0, {optionName:'newGameNovice',msg:'NEW GAME [NOVICE]',action:'newGameNovice',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:newGameSelected});
		var option_21 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'newGameNovice',msg:'NEW GAME [OPERATIVE]',action:'newGameNovice',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:newGameSelected});
				me.game.add(option_21,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('newGameNovice');
				
		//NEW GAME [NORMAL]
		//if there is no save game, this is Selected
		
		//var option_2 = new MenuOptionEntity(0, 0, {optionName:'newGameNormal',msg:'NEW GAME [NORMAL]',action:'newGameNormal',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_2 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'newGameNormal',msg:'NEW GAME [MASTER]',action:'newGameNormal',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_2,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('newGameNormal');

		//var option_2 = new MenuOptionEntity(0, 0, {optionName:'newGameNormal',msg:'NEW GAME [NORMAL]',action:'newGameNormal',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_25 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'newGameGhost',msg:'NEW GAME [NARRATIVE]',action:'newGameGhost',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_25,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('newGameGhost');
		
		
		//NEW GAME [DARKNESS MODE]
		if(gameData.load('darknessModeUnlocked')){
			//var option_3 = new MenuOptionEntity(0, 0, {optionName:'newGameDarkness',msg:'NEW GAME [BLIND MASTER]',action:'newGameDarkness',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
			var option_3 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'newGameDarkness',msg:'NEW GAME [BLIND MASTER]',action:'newGameDarkness',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_3,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('newGameDarkness');
		}
		
		//LEVEL SELECT
		//var option_4 = new MenuOptionEntity(0, 0, {optionName:'missionSelect',msg:'LEVEL SELECT',action:'missionSelect',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_4 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'missionSelect',msg:'LEVEL SELECT',action:'missionSelect',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_4,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('missionSelect');
		
		
		//CUTSCENE SELECT
		//var option_4 = new MenuOptionEntity(0, 0, {optionName:'missionSelect',msg:'LEVEL SELECT',action:'missionSelect',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_4_5 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'cutscenes',msg:'CUTSCENE SELECT',action:'cutscenes',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_4_5,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('cutscenes');
		
		//OPTIONS
		//var option_5 = new MenuOptionEntity(0, 0, {optionName:'options',msg:'OPTIONS',action:'options',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_5 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'options',msg:'OPTIONS',action:'options',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_5,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('options');
				
		
		//QUIT TO DESKTOP
		//var option_6 = new MenuOptionEntity(0, 0, {optionName:'quitToDesktop',msg:'QUIT TO DESKTOP',action:'quitToDesktop',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_6 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'quitToDesktop',msg:'QUIT TO DESKTOP',action:'quitToDesktop',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_6,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('quitToDesktop');
				
		this.menuOptionsLength = this.menuOptions.length - 1;
	},
	newGameNormalConfirmation:function(){	
		this.currentSelection = 0;
		this.posY = 64;
		this.posX = 32;
		this.ySpacing = 16;
		
		//var confirmationPrompt = new CutSceneTextEntity(0, 0, {msg:'Overwrite Previous Progress?',textPosY:20, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
		var confirmationPrompt = me.entityPool.newInstanceOf("cutscenetext",0, 0, {msg:'Overwrite Previous Progress?',textPosY:20, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
			me.game.add(confirmationPrompt,100);
			//me.game.sort();
			//this.posY+=(this.ySpacing*2);
					
		//These are set as the optionName for each option, so we can find them and update them later on
		//this.menuOptions = ['mission1','mission2','mission3','mission4','mission5'];
		this.menuOptions = [];
		
		//var option_1 = new MenuOptionEntity(0, 0, {optionName:'newGameNormalYes',msg:'YES',action:'newGameNormalYes',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
		var option_1 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'newGameNormalYes',msg:'YES',action:'newGameNormalYes',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
				me.game.add(option_1,200);
				me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('newGameNormalYes');
				
		//var option_2 = new MenuOptionEntity(0, 0, {optionName:'newGameNormalNo',msg:'NO',action:'newGameNormalNo',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_2 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'newGameNormalNo',msg:'NO',action:'newGameNormalNo',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
			me.game.add(option_2,200);
			//me.game.sort();
			this.posY+=this.ySpacing;
			this.menuOptions.push('newGameNormalNo');
		
		this.menuOptionsLength = this.menuOptions.length - 1;
		
	},
	newGameNoviceConfirmation:function(){	
		this.currentSelection = 0;
		this.posY = 64;
		this.posX = 32;
		this.ySpacing = 16;
		
		//var confirmationPrompt = new CutSceneTextEntity(0, 0, {msg:'Overwrite Previous Progress?',textPosY:20, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
		var confirmationPrompt = me.entityPool.newInstanceOf("cutscenetext",0, 0, {msg:'Overwrite Previous Progress?',textPosY:20, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
			me.game.add(confirmationPrompt,100);
			//me.game.sort();
			//this.posY+=(this.ySpacing*2);
					
		//These are set as the optionName for each option, so we can find them and update them later on
		//this.menuOptions = ['mission1','mission2','mission3','mission4','mission5'];
		this.menuOptions = [];
		
		//var option_1 = new MenuOptionEntity(0, 0, {optionName:'newGameNoviceYes',msg:'YES',action:'newGameNoviceYes',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
		var option_1 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'newGameNoviceYes',msg:'YES',action:'newGameNoviceYes',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
				me.game.add(option_1,200);
				me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('newGameNoviceYes');
				
		//var option_2 = new MenuOptionEntity(0, 0, {optionName:'newGameNoviceNo',msg:'NO',action:'newGameNoviceNo',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_2 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'newGameNoviceNo',msg:'NO',action:'newGameNoviceNo',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
			me.game.add(option_2,200);
			me.game.sort();
			this.posY+=this.ySpacing;
			this.menuOptions.push('newGameNoviceNo');
		
		this.menuOptionsLength = this.menuOptions.length - 1;
		
	},

	newGameTrueNoviceConfirmation:function(){	
		this.currentSelection = 0;
		this.posY = 64;
		this.posX = 32;
		this.ySpacing = 16;
		
		//var confirmationPrompt = new CutSceneTextEntity(0, 0, {msg:'Overwrite Previous Progress?',textPosY:20, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
		var confirmationPrompt = me.entityPool.newInstanceOf("cutscenetext",0, 0, {msg:'Overwrite Previous Progress?',textPosY:20, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
			me.game.add(confirmationPrompt,100);
			//me.game.sort();
			//this.posY+=(this.ySpacing*2);
					
		//These are set as the optionName for each option, so we can find them and update them later on
		//this.menuOptions = ['mission1','mission2','mission3','mission4','mission5'];
		this.menuOptions = [];
		
		//var option_1 = new MenuOptionEntity(0, 0, {optionName:'newGameNoviceYes',msg:'YES',action:'newGameNoviceYes',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
		var option_1 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'newGameTrueNoviceYes',msg:'YES',action:'newGameTrueNoviceYes',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
				me.game.add(option_1,200);
				me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('newGameTrueNoviceYes');
				
		//var option_2 = new MenuOptionEntity(0, 0, {optionName:'newGameNoviceNo',msg:'NO',action:'newGameNoviceNo',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_2 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'newGameTrueNoviceNo',msg:'NO',action:'newGameTrueNoviceNo',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
			me.game.add(option_2,200);
			me.game.sort();
			this.posY+=this.ySpacing;
			this.menuOptions.push('newGameTrueNoviceNo');
		
		this.menuOptionsLength = this.menuOptions.length - 1;
		
	},

	newGameGhostConfirmation:function(){	
		this.currentSelection = 0;
		this.posY = 64;
		this.posX = 32;
		this.ySpacing = 16;
		
		//var confirmationPrompt = new CutSceneTextEntity(0, 0, {msg:'Overwrite Previous Progress?',textPosY:20, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
		var confirmationPrompt = me.entityPool.newInstanceOf("cutscenetext",0, 0, {msg:'Overwrite Previous Progress?',textPosY:20, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
			me.game.add(confirmationPrompt,100);
			//me.game.sort();
			//this.posY+=(this.ySpacing*2);
					
		//These are set as the optionName for each option, so we can find them and update them later on
		//this.menuOptions = ['mission1','mission2','mission3','mission4','mission5'];
		this.menuOptions = [];
		
		//var option_1 = new MenuOptionEntity(0, 0, {optionName:'newGameNoviceYes',msg:'YES',action:'newGameNoviceYes',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
		var option_1 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'newGameGhostYes',msg:'YES',action:'newGameGhostYes',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
				me.game.add(option_1,200);
				me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('newGameGhostYes');
				
		//var option_2 = new MenuOptionEntity(0, 0, {optionName:'newGameNoviceNo',msg:'NO',action:'newGameNoviceNo',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_2 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'newGameGhostNo',msg:'NO',action:'newGameGhostNo',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
			me.game.add(option_2,200);
			me.game.sort();
			this.posY+=this.ySpacing;
			this.menuOptions.push('newGameGhostNo');
		
		this.menuOptionsLength = this.menuOptions.length - 1;
		
	},
	newGameDarknessConfirmation:function(){
		this.currentSelection = 0;
		this.posY = 64;
		this.posX = 32;
		this.ySpacing = 16;
		
		//var confirmationPrompt = new CutSceneTextEntity(0, 0, {msg:'Overwrite Previous Progress?',textPosY:this.posY, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
		var confirmationPrompt = me.entityPool.newInstanceOf("cutscenetext",0, 0, {msg:'Overwrite Previous Progress?',textPosY:this.posY, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
			me.game.add(confirmationPrompt,100);
			me.game.sort();
			this.posY+=(this.ySpacing*2);
					
		//These are set as the optionName for each option, so we can find them and update them later on
		//this.menuOptions = ['mission1','mission2','mission3','mission4','mission5'];
		this.menuOptions = [];
		
		//var option_1 = new MenuOptionEntity(0, 0, {optionName:'newGameDarknessYes',msg:'YES',action:'newGameDarknessYes',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
		var option_1 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'newGameDarknessYes',msg:'YES',action:'newGameDarknessYes',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
				me.game.add(option_1,200);
				me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('newGameDarknessYes');
				
		//var option_2 = new MenuOptionEntity(0, 0, {optionName:'newGameDarknessNo',msg:'NO',action:'newGameDarknessNo',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_2 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'newGameDarknessNo',msg:'NO',action:'newGameDarknessNo',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
			me.game.add(option_2,200);
			me.game.sort();
			this.posY+=this.ySpacing;
			this.menuOptions.push('newGameDarknessNo');
		
		this.menuOptionsLength = this.menuOptions.length - 1;
	},
	cutsceneSelectMenu:function(){
		
		this.currentSelection = 0;
		this.posY = 32;
		this.posX = 32;
		this.ySpacing = 16;
				
		//var missionName = new CutSceneTextEntity(0, 0, {msg:'Mission ' + mission.split('_')[1],textPosY:20, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
		var menuName = me.entityPool.newInstanceOf("cutscenetext",0, 0, {msg:'Cutscenes',textPosY:20, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
			me.game.add(menuName,200);
			me.game.sort();
			//this.posY+=(this.ySpacing*2);
			this.posY+=this.ySpacing;
			
		this.menuOptions = [];
		
		//Some variables
		var cutscene_count = 10;
		var unlocked;
		var levelMap;
		var cutsceneMap;
		var canonicalName;
		
		//Loop through cutscene_list 
		for(var cs = 0; cs < cutscene_list.length; cs++){
			if(cs == 0){
				var defaultSelected = true;
			}else{
				var defaultSelected = false;
			}
			
			cutsceneMap = cutscene_list[cs].cutscene;
			levelMap = cutscene_list[cs].levelBeat;
			unlocked = false;
			canonicalName = '';
			
			//Check to see if it's locked
			//need to check gameData.load('darknessModeUnlocked') for m_05_cut_scene_02 and credits
			if( (levelMap=='complete' && gameData.load('darknessModeUnlocked')) || gameData.load(levelMap+'_unlocked') || me.gamestat.getItemValue('cheatEnabled') == 1){
				unlocked = true;
				canonicalName = cutscene_list[cs].name;
			}else{
				canonicalName = '???';
			}
			
			console.log(cutsceneMap+' '+canonicalName+' '+defaultSelected);
			//var option = new MenuOptionEntity(0, 0, {optionName:levelMap,msg:canonicalName,action:'loadLevel',levelName:levelMap,textPosY:this.posY, textPosX:this.posX, drawImediate:true, unlocked:unlocked,selected:defaultSelected});
			var option = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:cutsceneMap,msg:canonicalName,action:'loadCutscene',levelName:cutsceneMap,textPosY:this.posY, textPosX:this.posX, drawImediate:true, unlocked:unlocked,selected:defaultSelected});
				me.game.add(option,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push(cutsceneMap);
				var option = '';			
		}
		
		this.menuOptionsLength = this.menuOptions.length - 1;		
		
	},
	levelSelectMenu:function(mission){
		this.currentSelection = 0;
		this.posY = 32;
		this.posX = 8; //32
		this.ySpacing = 16;
		
		//var missionName = new CutSceneTextEntity(0, 0, {msg:'Mission ' + mission.split('_')[1],textPosY:20, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
		var missionName = me.entityPool.newInstanceOf("cutscenetext",0, 0, {msg:'Mission ' + mission.split('_')[1],textPosY:20, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
			me.game.add(missionName,200);
			me.game.sort();
			this.posY+=(this.ySpacing*2);
			
		this.menuOptions = [];
		
		if(mission == 'm_vr'){
			var levelCount = 8;
		}else{
			var levelCount = 10;
		}
		
		var canonicalName;
		var levelNumber;
		var textSpacing;
		var unlocked;
		/*
		var option = new MenuOptionEntity(0, 0, {optionName:none,msg:'LEVEL              BEST',action:'none',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option,200);
				me.game.sort();
				this.posY+=this.ySpacing;
				var option = '';
		*/
		//mission levels
		for(i = 1; i <= levelCount;i++){
			if(i == 1){
				var defaultSelected = true;
			}else{
				var defaultSelected = false;
			}
			
			if(i == 10){
				var levelMap = mission + '_lvl_10';
				levelNumber = String(i) + '. ' ;
			}else{
				var levelMap = mission + '_lvl_0' + String(i);
				levelNumber = '0' + String(i) + '. ' ;
			}			
			
			unlocked = false;
			//Check to see if it's locked
			if(gameData.load(levelMap+'_unlocked') || me.gamestat.getItemValue('cheatEnabled') == 1){
				unlocked = true;
				
				//if it's not locked, show canonical name
			
				textSpacing = 0;
				canonicalName = '';
				//console.log(levelMap);
				//Get Level Titles from this array
				for(var level = 0; level < levels.length; level++){
					if(levels[level].map == levelMap){
						if(levels[level].name != ''){
							canonicalName = levelNumber + levels[level].name;
						}else{
							canonicalName = levelNumber + '[no name]';
						}
					}
				}
			}else{
				canonicalName = levelNumber + '???';
			}
			
			
			//Get Times/Deaths
			textSpacing = 26 - canonicalName.length; //20
			
			for(var tS = 0; tS < textSpacing; tS++){
				canonicalName += ' ';
			}
			//canonicalName += '00:00|010';
			
			//Get Time/Caught
			var bestTime = gameData.load(levelMap+'_time');
			var bestCaught = gameData.load(levelMap+'_caught');
		
			if(bestTime != null){
				var bestTimeH = parseInt( (bestTime / 3600000) % 24);
				//var bestTimeM = parseInt( (bestTime / 60000) % 60);
				var bestTimeM = parseInt( (bestTime / 60000)); //If we want mintues and no hours
				var bestTimeS = parseInt( (bestTime/1000) % 60);
				var bestTimeMs = Math.round((parseInt((bestTime%1000))/10));
				
				//var bestTimeSTotal = bestTime;
				//bestTimeH < 10 ? "0" + bestTimeH : bestTimeH) + ":" +
				var bestTimeFormated = (bestTimeM < 10 ? "0" + bestTimeM : bestTimeM) + ":" + (bestTimeS  < 10 ? "0" + bestTimeS : bestTimeS) + "." + (bestTimeMs  < 10 ? "0" + bestTimeMs : bestTimeMs);
				
				canonicalName += bestTimeFormated+' ['+bestCaught + ']';
			}else{
				canonicalName += '[NO DATA]';
			}
			
			console.log(levelNumber + ' unlocked? ' + unlocked);
			
			//var option = new MenuOptionEntity(0, 0, {optionName:levelMap,msg:canonicalName,action:'loadLevel',levelName:levelMap,textPosY:this.posY, textPosX:this.posX, drawImediate:true, unlocked:unlocked,selected:defaultSelected});
			var option = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:levelMap,msg:canonicalName,action:'loadLevel',levelName:levelMap,textPosY:this.posY, textPosX:this.posX, drawImediate:true, unlocked:unlocked,selected:defaultSelected});
				me.game.add(option,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push(levelMap);
				var option = '';
		}
		
		this.menuOptionsLength = this.menuOptions.length - 1;
	},
	levelScoreScreen: function(){
		//Get Level name from gamestat
		//find best score, compare
		//display best score and current score
		//options: next level, replay, quit to menu
		this.replayDefaultSelected = false;
		this.currentSelection = 0;
		this.posY = 64;
		this.posX = 32;
		this.ySpacing = 16;
		
		
		var nextLevel = me.gamestat.getItemValue('nextTimeAttackLevel');
		var currentLevel = me.gamestat.getItemValue('currentTimeAttackLevel');
		
		var currentMission = 'level_select_m_' + currentLevel.split('_')[1];
		
		this.menuOptions = [];
		
		//var time = me.gamestat.getItemValue('currentTimeAttackLevelTime');
		var caughtCount = me.gamestat.getItemValue('currentTimeAttackLevelCaught');
		caughtCount = (caughtCount) ? caughtCount : 0;
		var levelTime = Math.round(me.gamestat.getItemValue('currentTimeAttackLevelTime') / .06); //.06 instead of 60, gives me 1/10th ms
		
		var levelTimeH = parseInt( (levelTime / 3600000) % 24);
		//var levelTimeM = parseInt( (levelTime / 60000) % 60);
		var levelTimeM = parseInt( (levelTime / 60000)); //If we want mintues and no hours
		var levelTimeS = parseInt( (levelTime/1000) % 60);
		var levelTimeMs = Math.round((parseInt((levelTime%1000))/10));
	
		
		console.log('MS: ' + parseInt((levelTime%1000)/10));
		//var levelTimeSTotal = levelTime;
		//levelTimeH < 10 ? "0" + levelTimeH : levelTimeH) + ":" +
		var levelTimeFormated = (levelTimeM < 10 ? "0" + levelTimeM : levelTimeM) + ":" + (levelTimeS  < 10 ? "0" + levelTimeS : levelTimeS) + "." + (levelTimeMs  < 10 ? "0" + levelTimeMs : levelTimeMs);
		            
		console.log(levelTime);
		var bestTime = gameData.load(currentLevel+'_time');
		var bestCaught = gameData.load(currentLevel+'_caught');
		bestCaught = (bestCaught) ? bestCaught : 0;
		//compare best and current times
		//if(bestTime == null || Date.parse('1986-04-14 '+levelTime) < Date.parse('1986-04-14 '+bestTime) ){
		console.log(bestTime);
		if(isNaN(bestTime) || bestTime === null || bestTime === 0 || levelTime < bestTime ){
			gameData.save(currentLevel+'_time',levelTime);
			gameData.save(currentLevel+'_caught',caughtCount);
			bestTime = levelTime;
			bestCaught = caughtCount;
			var bestTimeFormated  = levelTimeFormated;
		}else{
			//var bestTime = Math.round(bestTime / .06); //.06 instead of 60, gives me 1/10th ms
		
			var bestTimeH = parseInt( (bestTime / 3600000) % 24);
			//var bestTimeM = parseInt( (bestTime / 60000) % 60);
			var bestTimeM = parseInt( (bestTime / 60000)); //If we want mintues and no hours
			var bestTimeS = parseInt( (bestTime/1000) % 60);
			var bestTimeMs = Math.round((parseInt((bestTime%1000))/10));
			
			//var bestTimeSTotal = bestTime;
			//bestTimeH < 10 ? "0" + bestTimeH : bestTimeH) + ":" +
			var bestTimeFormated = (bestTimeM < 10 ? "0" + bestTimeM : bestTimeM) + ":" + (bestTimeS  < 10 ? "0" + bestTimeS : bestTimeS) + "." + (bestTimeMs  < 10 ? "0" + bestTimeMs : bestTimeMs);
			
		}
		
		me.gamestat.setValue('currentTimeAttackLevelTime',0);
		me.gamestat.setValue('currentTimeAttackLevelCaught',0);
		
		var canonicalName = '';
		var levelNumber = currentLevel.split('_')[1] + '.' + currentLevel.split('_')[3] + ' ';
		//Get Level Title from this array
		for(var level = 0; level < levels.length; level++){
			if(levels[level].map == currentLevel){
				if(levels[level].name != ''){
					canonicalName = levelNumber + levels[level].name;
				}else{
					canonicalName = levelNumber + '[no name]';
				}
			}
		}
		
		//var levelCanonicalName = new CutSceneTextEntity(0, 0, {msg:canonicalName,textPosY:this.posY, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
		var levelCanonicalName = me.entityPool.newInstanceOf("cutscenetext",0, 0, {msg:canonicalName,textPosY:this.posY, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
			me.game.add(levelCanonicalName,100);
			//me.game.sort();
			this.posY+=(this.ySpacing*2);
			
		//var thisTime = new CutSceneTextEntity(0, 0, {msg:'Time: ' + levelTimeFormated + '      Caught: ' + caughtCount,textPosY:this.posY, textPosX:this.posX, blink:false,  drawImediate:true});
		var thisTime = me.entityPool.newInstanceOf("cutscenetext",0, 0, {msg:'Time: ' + levelTimeFormated + '      Caught: ' + caughtCount,textPosY:this.posY, textPosX:this.posX, blink:false,  drawImediate:true});
			me.game.add(thisTime,100);
			me.game.sort();
			this.posY+=(this.ySpacing);
		
		//var bestTime = new CutSceneTextEntity(0, 0, {msg:'Best: '+bestTimeFormated + '      Caught: ' + bestCaught,textPosY:this.posY, textPosX:this.posX, blink:false,  drawImediate:true});
		var bestTime = me.entityPool.newInstanceOf("cutscenetext",0, 0, {msg:'Best: '+bestTimeFormated + '      Caught: ' + bestCaught,textPosY:this.posY, textPosX:this.posX, blink:false,  drawImediate:true});
			me.game.add(bestTime,100);
			me.game.sort();
			this.posY+=(this.ySpacing*2);
		
		if(nextLevel != 'vr' && nextLevel != 'return'){
			//var option_1 = new MenuOptionEntity(0, 0, {optionName:'procedeToNextLevel',msg:'PROCEDE TO NEXT LEVEL',action:'procedeToNextLevel',levelName:nextLevel,textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
			var option_1 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'procedeToNextLevel',msg:'PROCEED TO NEXT LEVEL',action:'procedeToNextLevel',levelName:nextLevel,textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
				me.game.add(option_1,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('procedeToNextLevel');
		}else{
			this.replayDefaultSelected = true;
		}
		
		//var option_2 = new MenuOptionEntity(0, 0, {optionName:'replayLevel',msg:'REPLAY LEVEL',action:'replayLevel',levelName:currentLevel,textPosY:this.posY, textPosX:this.posX, drawImediate:true,selected:this.replayDefaultSelected});
		var option_2 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'replayLevel',msg:'REPLAY LEVEL',action:'replayLevel',levelName:currentLevel,textPosY:this.posY, textPosX:this.posX, drawImediate:true,selected:this.replayDefaultSelected});
			me.game.add(option_2,200);
			me.game.sort();
			this.posY+=this.ySpacing;
			this.menuOptions.push('replayLevel');
			
		//var option_3 = new MenuOptionEntity(0, 0, {optionName:'quitToMissionMenu',msg:'QUIT TO LEVEL SELECT',action:'quitToMissionMenu',levelName:currentMission,textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_3 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'quitToMissionMenu',msg:'QUIT TO LEVEL SELECT',action:'quitToMissionMenu',levelName:currentMission,textPosY:this.posY, textPosX:this.posX, drawImediate:true});
			me.game.add(option_3,200);
			//me.game.sort();
			this.posY+=this.ySpacing;
			this.menuOptions.push('quitToMissionMenu');
		
		this.menuOptionsLength = this.menuOptions.length - 1;
	},
	optionsMenu: function(){
		
		this.currentSelection = 0;
		this.posY = 20;
		this.posX = 32;
		this.ySpacing = 16;
		this.menuOptions = [];
		
		//var confirmationPrompt = new CutSceneTextEntity(0, 0, {msg:'OPTIONS',textPosY:this.posY, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
		var confirmationPrompt = me.entityPool.newInstanceOf("cutscenetext",0, 0, {msg:'OPTIONS',textPosY:this.posY, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
			me.game.add(confirmationPrompt,100);
			//me.game.sort();
			this.posY+=(this.ySpacing*3);
		

		//var option_2 = new MenuOptionEntity(0, 0, {optionName:'optionWindowed',msg:windowedMSG,action:'loadOption',levelName:'options_windowed_menu',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
		var option_12 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'optionControls',msg:'CONTROLS',action:'loadOption',levelName:'options_controls_menu',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
				me.game.add(option_12,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('optionControls');
				
		
		if(gameData.load('windowed') == 'yes'){	
			var windowedMSG = 'FULLSCREEN [DISABLED]';
		}else{
			var windowedMSG = 'FULLSCREEN [ENABLED]';
		}
		
		var windowedMSG = 'SET RESOLUTION';
		
		//var option_2 = new MenuOptionEntity(0, 0, {optionName:'optionWindowed',msg:windowedMSG,action:'loadOption',levelName:'options_windowed_menu',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
		var option_2 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'optionWindowed',msg:windowedMSG,action:'loadOption',levelName:'options_windowed_menu',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_2,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('optionWindowed');
				
		
		if(gameData.load('audioIsMuted') == 'yes'){	
			var audioMSG = 'AUDIO [DISABLED]';
		}else{
			var audioMSG = 'AUDIO [ENABLED]';
		}
		
		//var option_1 = new MenuOptionEntity(0, 0, {optionName:'optionAudio',msg:audioMSG,action:'loadOption',levelName:'options_audio_menu',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_1 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'optionAudio',msg:audioMSG,action:'loadOption',levelName:'options_audio_menu',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_1,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('optionAudio');
		
		
		if(gameData.load('webGLDisabled') == 'yes'){	
			var webGLMSG = 'WEBGL RENDERING [DISABLED]';
		}else{
			var webGLMSG = 'WEBGL RENDERING [ENABLED]';
		}
		
		//var option_3 = new MenuOptionEntity(0, 0, {optionName:'optionWebGL',msg:webGLMSG,action:'loadOption',levelName:'options_webgl_menu',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_3 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'optionWebGL',msg:webGLMSG,action:'loadOption',levelName:'options_webgl_menu',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_3,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('optionWebGL');
				
		if(gameData.load('screenshakeDisabled') == 'yes'){	
			var webGLMSG = 'SCREENSHAKE [DISABLED]';
		}else{
			var webGLMSG = 'SCREENSHAKE [ENABLED]';
		}
		
		//var option_4 = new MenuOptionEntity(0, 0, {optionName:'optionScreenShake',msg:webGLMSG,action:'loadOption',levelName:'options_screenshake_menu',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_4 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'optionScreenShake',msg:webGLMSG,action:'loadOption',levelName:'options_screenshake_menu',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_4,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('optionScreenShake');
			
		this.menuOptionsLength = this.menuOptions.length - 1;
		
		if(gameData.load('levelSelectCPDisabled') == 'yes'){	
			var webGLMSG = 'LEVEL SELECT CHECKPOINTS [DISABLED]';
		}else{
			var webGLMSG = 'LEVEL SELECT CHECKPOINTS [ENABLED]';
		}
		
		//var option_4 = new MenuOptionEntity(0, 0, {optionName:'optionScreenShake',msg:webGLMSG,action:'loadOption',levelName:'options_screenshake_menu',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_4 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'optionCheckPoint',msg:webGLMSG,action:'loadOption',levelName:'options_checkpoint_menu',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_4,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('optionCheckPoint');
			
		this.menuOptionsLength = this.menuOptions.length - 1;
	},
	optionsAudioMenu: function(){
		this.currentSelection = 0;
		this.posY = 64;
		this.posX = 32;
		this.ySpacing = 16;
		
		if(gameData.load('audioIsMuted') == 'yes'){	
			var confirmationText = 'Enable Audio?';
		}else{
			var confirmationText = 'Disable Audio?';
		}
			
		
		//var confirmationPrompt = new CutSceneTextEntity(0, 0, {msg:confirmationText,textPosY:this.posY, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
		var confirmationPrompt = me.entityPool.newInstanceOf("cutscenetext",0, 0, {msg:confirmationText,textPosY:this.posY, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
			me.game.add(confirmationPrompt,100);
			//me.game.sort();
			this.posY+=(this.ySpacing*2);
					
		//These are set as the optionName for each option, so we can find them and update them later on
		//this.menuOptions = ['mission1','mission2','mission3','mission4','mission5'];
		this.menuOptions = [];
		
		//var option_1 = new MenuOptionEntity(0, 0, {optionName:'optionAudioYes',msg:'YES',action:'optionAudioYes',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
		var option_1 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'optionAudioYes',msg:'YES',action:'optionAudioYes',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
				me.game.add(option_1,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('optionAudioYes');
				
		//var option_2 = new MenuOptionEntity(0, 0, {optionName:'optionBackNo',msg:'NO',action:'optionBackNo',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_2 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'optionBackNo',msg:'NO',action:'optionBackNo',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
			me.game.add(option_2,200);
			//me.game.sort();
			this.posY+=this.ySpacing;
			this.menuOptions.push('optionBackNo');
		
		this.menuOptionsLength = this.menuOptions.length - 1;
	},
	optionsWebGLMenu: function(){
		this.currentSelection = 0;
		this.posY = 64;
		this.posX = 32;
		this.ySpacing = 16;
		
		if(gameData.load('webGLDisabled') == 'yes'){	
			var confirmationText = 'Enable WebGL?*';
		}else{
			var confirmationText = 'Disable WebGL?*';
		}
		
		
		
		
		//var confirmationPrompt = new CutSceneTextEntity(0, 0, {msg:confirmationText,textPosY:this.posY, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
		var confirmationPrompt = me.entityPool.newInstanceOf("cutscenetext",0, 0, {msg:confirmationText,textPosY:this.posY, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
			me.game.add(confirmationPrompt,100);
			//me.game.sort();
			this.posY+=(this.ySpacing*2);
					
		//These are set as the optionName for each option, so we can find them and update them later on
		//this.menuOptions = ['mission1','mission2','mission3','mission4','mission5'];
		this.menuOptions = [];
		
		//var option_1 = new MenuOptionEntity(0, 0, {optionName:'optionWebGLYes',msg:'YES',action:'optionWebGLYes',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
		var option_1 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'optionWebGLYes',msg:'YES',action:'optionWebGLYes',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
				me.game.add(option_1,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('optionWebGLYes');
				
		//var option_2 = new MenuOptionEntity(0, 0, {optionName:'optionBackNo',msg:'NO',action:'optionBackNo',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_2 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'optionBackNo',msg:'NO',action:'optionBackNo',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
			me.game.add(option_2,200);
			//me.game.sort();
			this.posY+=this.ySpacing;
			this.menuOptions.push('optionBackNo');
		
		this.menuOptionsLength = this.menuOptions.length - 1;
		
		/*
		this.posY+=this.ySpacing;
		var restartMSG = new CutSceneTextEntity(0, 0, {msg:'*Restart Required',textPosY:this.posY, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
			me.game.add(restartMSG,100);
			me.game.sort();
		*/
	},
	optionsControlsMenu: function(){
		this.currentSelection = 0;
		this.posY = 216;
		this.posX = 32;
		this.ySpacing = 16;
		
		
		//These are set as the optionName for each option, so we can find them and update them later on
		//this.menuOptions = ['mission1','mission2','mission3','mission4','mission5'];
		this.menuOptions = [];		
				
		//var option_2 = new MenuOptionEntity(0, 0, {optionName:'optionBackNo',msg:'NO',action:'optionBackNo',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_2 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'optionBackNo',msg:'BACK',action:'optionBackNo',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
			me.game.add(option_2,200);
			//me.game.sort();
			this.posY+=this.ySpacing;
			this.menuOptions.push('optionBackNo');
		
		this.menuOptionsLength = this.menuOptions.length - 1;
		
	},
	optionsScreenShakeMenu: function(){
		this.currentSelection = 0;
		this.posY = 64;
		this.posX = 32;
		this.ySpacing = 16;
		
		if(gameData.load('screenshakeDisabled') == 'yes'){	
			var confirmationText = 'Enable Screen Shake?';
		}else{
			var confirmationText = 'Disable Screen Shake?';
		}
		
		
		//var confirmationPrompt = new CutSceneTextEntity(0, 0, {msg:confirmationText,textPosY:this.posY, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
		var confirmationPrompt = me.entityPool.newInstanceOf("cutscenetext",0, 0, {msg:confirmationText,textPosY:this.posY, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
			me.game.add(confirmationPrompt,100);
			//me.game.sort();
			this.posY+=(this.ySpacing*2);
					
		//These are set as the optionName for each option, so we can find them and update them later on
		//this.menuOptions = ['mission1','mission2','mission3','mission4','mission5'];
		this.menuOptions = [];
		
		//var option_1 = new MenuOptionEntity(0, 0, {optionName:'optionScreenShakeYes',msg:'YES',action:'optionScreenShakeYes',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
		var option_1 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'optionScreenShakeYes',msg:'YES',action:'optionScreenShakeYes',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
				me.game.add(option_1,200);
				me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('optionScreenShakeYes');
				
		//var option_2 = new MenuOptionEntity(0, 0, {optionName:'optionBackNo',msg:'NO',action:'optionBackNo',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_2 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'optionBackNo',msg:'NO',action:'optionBackNo',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
			me.game.add(option_2,200);
			me.game.sort();
			this.posY+=this.ySpacing;
			this.menuOptions.push('optionBackNo');
		
		this.menuOptionsLength = this.menuOptions.length - 1;
		
	},
	optionsCheckPointMenu: function(){
		this.currentSelection = 0;
		this.posY = 64;
		this.posX = 32;
		this.ySpacing = 16;
		
		if(gameData.load('levelSelectCPDisabled') == 'yes'){	
			var confirmationText = 'Enable Checkpoints in Level Select?';
		}else{
			var confirmationText = 'Disable Checkpoints in Level Select?';
		}
		
		
		//var confirmationPrompt = new CutSceneTextEntity(0, 0, {msg:confirmationText,textPosY:this.posY, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
		var confirmationPrompt = me.entityPool.newInstanceOf("cutscenetext",0, 0, {msg:confirmationText,textPosY:this.posY, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
			me.game.add(confirmationPrompt,100);
			//me.game.sort();
			this.posY+=(this.ySpacing*2);
					
		//These are set as the optionName for each option, so we can find them and update them later on
		//this.menuOptions = ['mission1','mission2','mission3','mission4','mission5'];
		this.menuOptions = [];
		
		//var option_1 = new MenuOptionEntity(0, 0, {optionName:'optionScreenShakeYes',msg:'YES',action:'optionScreenShakeYes',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
		var option_1 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'optionCheckPointYes',msg:'YES',action:'optionCheckPointYes',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
				me.game.add(option_1,200);
				me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('optionCheckPointYes');
				
		//var option_2 = new MenuOptionEntity(0, 0, {optionName:'optionBackNo',msg:'NO',action:'optionBackNo',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_2 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'optionBackNo',msg:'NO',action:'optionBackNo',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
			me.game.add(option_2,200);
			me.game.sort();
			this.posY+=this.ySpacing;
			this.menuOptions.push('optionBackNo');
		
		this.menuOptionsLength = this.menuOptions.length - 1;
		
	},
	optionsWindowedMenu: function(){
		this.currentSelection = 0;
		this.posY = 64;
		this.posX = 32;
		this.ySpacing = 16;
		
		
		//get Window Height and Width, limit options accordingly
		var height_limit = window.screen.height;
		
		if(gameData.load('windowed') == 'yes'){	
			var confirmationText = 'Enable FullScreen?*';
		}else{
			var confirmationText = 'Disable FullScreen?*';
		}		
		
		var confirmationText = 'Set Resolution:';
		
		//var confirmationPrompt = new CutSceneTextEntity(0, 0, {msg:confirmationText,textPosY:this.posY, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
		var confirmationPrompt = me.entityPool.newInstanceOf("cutscenetext",0, 0, {msg:confirmationText,textPosY:this.posY, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
			me.game.add(confirmationPrompt,100);
			//me.game.sort();
			this.posY+=(this.ySpacing*2);
					
		//These are set as the optionName for each option, so we can find them and update them later on
		//this.menuOptions = ['mission1','mission2','mission3','mission4','mission5'];
		this.menuOptions = [];
		
		//var option_fs = new MenuOptionEntity(0, 0, {optionName:'optionResolutionChangefs',msg:'Fullscreen',action:'optionResolutionChange',ressetting:'fullscreen',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
		var option_fs = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'optionResolutionChangefs',msg:'Fullscreen',action:'optionResolutionChange',ressetting:'fullscreen',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
				me.game.add(option_fs,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('optionResolutionChangefs');
		
		//var option_1 = new MenuOptionEntity(0, 0, {optionName:'optionResolutionChange1',msg:'320x240',action:'optionResolutionChange',ressetting:1,textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_1 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'optionResolutionChange1',msg:'320x240',action:'optionResolutionChange',ressetting:1,textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_1,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('optionResolutionChange1');
				
		//var option_2 = new MenuOptionEntity(0, 0, {optionName:'optionResolutionChange2',msg:'640x480',action:'optionResolutionChange',ressetting:2,textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_2 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'optionResolutionChange2',msg:'640x480',action:'optionResolutionChange',ressetting:2,textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_2,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('optionResolutionChange2');
				
		//var option_3 = new MenuOptionEntity(0, 0, {optionName:'optionResolutionChange3',msg:'960x720',action:'optionResolutionChange',ressetting:3,textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_3 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'optionResolutionChange3',msg:'960x720',action:'optionResolutionChange',ressetting:3,textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_3,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('optionResolutionChange3');
				
		if(height_limit >= 960){ 		
		//var option_4 = new MenuOptionEntity(0, 0, {optionName:'optionResolutionChange4',msg:'1280x960',action:'optionResolutionChange',ressetting:4,textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_4 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'optionResolutionChange4',msg:'1280x960',action:'optionResolutionChange',ressetting:4,textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_4,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('optionResolutionChange4');
		}		
		
		if(height_limit >= 1200){ 				
		//var option_5 = new MenuOptionEntity(0, 0, {optionName:'optionResolutionChange5',msg:'1600x1200',action:'optionResolutionChange',ressetting:5,textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_5 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'optionResolutionChange5',msg:'1600x1200',action:'optionResolutionChange',ressetting:5,textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_5,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('optionResolutionChange5');
		}

		if(height_limit >= 1400){ 				
		//var option_6 = new MenuOptionEntity(0, 0, {optionName:'optionResolutionChange6',msg:'1920x1440',action:'optionResolutionChange',ressetting:6,textPosY:this.posY, textPosX:this.posX, drawImediate:true});
		var option_6 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'optionResolutionChange6',msg:'1920x1440',action:'optionResolutionChange',ressetting:6,textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_6,200);
				//me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('optionResolutionChange6');
		}		
			
		/*
		var option_1 = new MenuOptionEntity(0, 0, {optionName:'optionWindowedYes',msg:'YES',action:'optionWindowedYes',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
				me.game.add(option_1,200);
				me.game.sort();
				this.posY+=this.ySpacing;
				this.menuOptions.push('optionWindowedYes');
				
		var option_2 = new MenuOptionEntity(0, 0, {optionName:'optionBackNo',msg:'NO',action:'optionBackNo',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
			me.game.add(option_2,200);
			me.game.sort();
			this.posY+=this.ySpacing;
			this.menuOptions.push('optionBackNo');
			*/
		this.menuOptionsLength = this.menuOptions.length - 1;
		
		/*
		this.posY+=this.ySpacing;
		var restartMSG = new CutSceneTextEntity(0, 0, {msg:'*Restart Required',textPosY:this.posY, textPosX:this.posX, blink:false,  drawImediate:true, fontColor:'red'});
			me.game.add(restartMSG,100);
			me.game.sort();
		*/
	}
});




var MissionSelectEntity = me.ObjectEntity.extend({
	init:function(x,y,settings){
		
		
		/*
			Continue (if saved game is detected) - M1 Level 01 - THe hIVeetc
			New Game [Normal] (if saved game is detected, prompt with yes no screen)
			New Game [Darkness] (if beat game is detected, prompt with yes no screen)
			Level Select (goes to mission select screen)
			Options (goes to option screen)
			Quit to Desktop
		
		*/
		
		this.parent(x,y,settings);
		//this.script = script[settings.script];
		//this.scriptLength = this.script.length;
		//this.nextLevel = settings.nextLevel;
		
		//
		this.updateWhenPaused = true;
		this.floating = true;
		this.transitioning = false;		
		
		if(this.renderable){
			//this.renderable.alpha = 0.8;
			//this.renderable.addAnimation('static',[0,1,2,3],1);
			//this.renderable.setCurrentAnimation('static');
		}
		
		//Set correct Key Bindings
		this.setMenuKeyBindings();
		
		/*
		if(settings.menu == 'start'){
			this.startMenu();
		}else if(settings.menu == 'newGameDarknessConfirmation'){
			this.newGameDarknessConfirmation();
		}else if(settings.menu == 'newGameNormalConfirmation'){
			this.newGameNormalConfirmation();
		}
		*/
		//leveltransition = new LevelTransitionEntity(-160, -120, {image:'spiraltransition3', spritewidth:640, spriteheight: 480, transitionType: 'in' });
		var leveltransition = me.entityPool.newInstanceOf("leveltransition",-160, -120, {image:'spiraltransition3', spritewidth:640, spriteheight: 480, transitionType: 'in' });
		me.game.add(leveltransition,300);
		//me.game.sort();
		
		this.missionMenu();
		
	},
	update:function(){
		if(this.renderable){
			//this.renderable.alpha = 1;
			
			//this.renderable.setCurrentAnimation('static');
		}
		
		
		//Check for change		
		//Watch for Up/Down and space/enter
		if(me.input.isKeyPressed('enter')){
			//Either go to submenu or load a level.
			
			var selectedOption = me.game.getEntityByProp('optionName',this.menuOptions[this.currentSelection]);
			for(var i = 0; i < selectedOption.length; i++){
				if(selectedOption[i].unlocked){
					me.audio.play('menuselect');
					if(selectedOption[i].action == 'm_01'){
						this.loadLevel('level_select_m_01');
					}else if(selectedOption[i].action == 'm_02'){
						this.loadLevel('level_select_m_02');
					}else if(selectedOption[i].action == 'm_03'){
						this.loadLevel('level_select_m_03');
					}else if(selectedOption[i].action == 'm_04'){
						this.loadLevel('level_select_m_04');
					}else if(selectedOption[i].action == 'm_05'){
						this.loadLevel('level_select_m_05');
					}else if(selectedOption[i].action == 'm_vr'){
						this.loadLevel('level_select_m_vr');
					}
				}else{
					me.audio.play('menudeny');
				}
			}	
		}

		if(me.input.isKeyPressed('up')){
			//navigate to next position in array
			if(this.currentSelection !== 0 && this.currentSelection !== 1){
				
				//me.audio.play('switchtrigger');
				me.audio.play('menucursor');
				
				var selectedOption = me.game.getEntityByProp('optionName',this.menuOptions[this.currentSelection]);
				for(var i = 0; i < selectedOption.length; i++){
					selectedOption[i].selected = false;					
				}
				
				//value%2 == 0
				
				//this.currentSelection -= 1;
				this.currentSelection -= 2;
				
				var selectedOption = me.game.getEntityByProp('optionName',this.menuOptions[this.currentSelection]);
				for(var i = 0; i < selectedOption.length; i++){
					selectedOption[i].selected = true;							
				}
				
				//console.log(this.currentSelection);
			}
		}

		if(me.input.isKeyPressed('down')){
			//navigate to previous position in array
			if(this.currentSelection != this.menuOptionsLength && this.currentSelection != this.menuOptionsLength-1){
			
				//me.audio.play('switchtrigger');
				me.audio.play('menucursor');
				
				var selectedOption = me.game.getEntityByProp('optionName',this.menuOptions[this.currentSelection]);
				for(var i = 0; i < selectedOption.length; i++){
					//console.log(trapDevices[i]);
					selectedOption[i].selected = false;							
				}
				
				//Go down
				//this.currentSelection += 1;
				this.currentSelection += 2;
				//console.log(this.menuOptions[this.currentSelection]);
				
				
				var selectedOption = me.game.getEntityByProp('optionName',this.menuOptions[this.currentSelection]);
				for(var i = 0; i < selectedOption.length; i++){
					selectedOption[i].selected = true;							
				}			

				//console.log(this.currentSelection);
				
			}
			
		}

		if(me.input.isKeyPressed('left')){
			//if current option is not even
			if(this.currentSelection%2 !== 0 && this.currentSelection !== 0){
				//me.audio.play('switchtrigger');
				me.audio.play('menucursor');
			
				var selectedOption = me.game.getEntityByProp('optionName',this.menuOptions[this.currentSelection]);
				for(var i = 0; i < selectedOption.length; i++){
					//console.log(trapDevices[i]);
					selectedOption[i].selected = false;							
				}
				this.currentSelection -= 1;
				
				var selectedOption = me.game.getEntityByProp('optionName',this.menuOptions[this.currentSelection]);
				for(var i = 0; i < selectedOption.length; i++){
					selectedOption[i].selected = true;							
				}
				
				//console.log(this.currentSelection);
			}
		}

		if(me.input.isKeyPressed('right')){
		
			//if current option is not odd
			
			if(this.currentSelection%2 == 0 && this.currentSelection != this.menuOptionsLength){
				//me.audio.play('switchtrigger');
				me.audio.play('menucursor');
			
				var selectedOption = me.game.getEntityByProp('optionName',this.menuOptions[this.currentSelection]);
				for(var i = 0; i < selectedOption.length; i++){
					//console.log(trapDevices[i]);
					selectedOption[i].selected = false;							
				}
				this.currentSelection += 1;
				
				var selectedOption = me.game.getEntityByProp('optionName',this.menuOptions[this.currentSelection]);
				for(var i = 0; i < selectedOption.length; i++){
					selectedOption[i].selected = true;							
				}
				
				//console.log(this.currentSelection);
			}
		}
		
		if(me.input.isKeyPressed('pause')){
			//Pause, bleh, this is escape key
			me.audio.play('uncloak');
			this.exitMissionMenu();
		}
	
		
		/*
		if(me.input.isKeyPressed('pause')){
			if(me.state.isPaused()){
				this.unPause();
			}
		} */
	
	},
	exitMissionMenu:function(){
		//me.levelDirector.loadLevel('start_menu');
		this.transitionToLevel('start_menu');
	},
	loadLevel: function(levelName){
		
		if(me.state.isPaused()){
			this.unPause();
		}
		this.fixKeyBindings();
		//me.levelDirector.loadLevel(levelName);
		this.transitionToLevel(levelName);
	},
	transitionToLevel:function(levelName){
		//leveltransition = new LevelTransitionEntity(-160, -120, {image:'spiraltransition3', spritewidth:640, spriteheight: 480, transitionType: 'out' });
		var leveltransition = me.entityPool.newInstanceOf("leveltransition",-160, -120, {image:'spiraltransition3', spritewidth:640, spriteheight: 480, transitionType: 'out' });
			me.game.add(leveltransition,300);
			me.game.sort();
					
			me.game.viewport.fadeIn('#000000',500, function(){ me.levelDirector.loadLevel(levelName); });
	},
	fixKeyBindings: function(){
		//enable the keyboard
		/*
		me.input.bindKey(me.input.KEY.A, "left");
		me.input.bindKey(me.input.KEY.D, "right");
		me.input.bindKey(me.input.KEY.W, "jump", true);
		me.input.bindKey(me.input.KEY.LEFT, "left");
		me.input.bindKey(me.input.KEY.RIGHT, "right");
		me.input.bindKey(me.input.KEY.UP, "jump", true);
		me.input.bindKey(me.input.KEY.SPACE, "jump", true);
		me.input.bindKey(me.input.KEY.X, "jump", true);
		me.input.bindKey(me.input.KEY.SHIFT, "stealth", true);
		me.input.bindKey(me.input.KEY.S, "stealth", true);
		me.input.bindKey(me.input.KEY.DOWN, "stealth", true);
		*/
		InputControls.setForGameplay();
	},
	setMenuKeyBindings: function(){
		//Key Bindings 
		/*
		me.input.bindKey(me.input.KEY.SPACE, "enter", true);
		me.input.bindKey(me.input.KEY.ENTER, "enter", true);
		
		//me.input.bindKey(me.input.KEY.ESC, "cancel", true);
		//me.input.bindKey(me.input.KEY.ENTER, "enter", true);
		
		me.input.bindKey(me.input.KEY.DOWN, "down", true);
		me.input.bindKey(me.input.KEY.S, "down", true);
		
		me.input.bindKey(me.input.KEY.UP, "up", true);
		me.input.bindKey(me.input.KEY.W, "up", true);
		*/
		InputControls.setForMenu();
	},
	unPause: function(){
		me.audio.play('computer_boot');
		this.fixKeyBindings();
		me.state.resume();
		var options = me.game.getEntityByProp('type','MENUOPTION');
		for(var i = 0; i < options.length; i++){
			me.game.remove(options[i],true);
		}
		me.game.remove(this);
	},
	missionMenu: function(){
		//Build Menu System
		//Array that contains currently selected item and the next action to take
		//Nah going to be lazy and just do it live...
		
		this.currentSelection = 0;
		this.posY = 48;
		this.posX = 64;
		this.ySpacing = 16;
		this.xSpacing = 16;
		
		this.posY2 = 104;
		this.posX2 = 200;
		
		//var missionSelectTitle = new CutSceneTextEntity(0, 0, {msg:'Mission Select',textPosY:20, textPosX:32, blink:false,  drawImediate:true, fontColor:'red'});
		var missionSelectTitle = me.entityPool.newInstanceOf("cutscenetext",0, 0, {msg:'Mission Select',textPosY:20, textPosX:32, blink:false,  drawImediate:true, fontColor:'red'});
			me.game.add(missionSelectTitle,200);
			//me.game.sort();
			
			
		//These are set as the optionName for each option, so we can find them and update them later on
		//this.menuOptions = ['mission1','mission2','mission3','mission4','mission5'];
		this.menuOptions = [];
		
		//Mission 1
		//This one is always unlocked ok
		//48
		//var mission_01 = new MissionSelectOptionEntity(64, 64, {image:'m_select_01_button',spritewidth:120,optionName:'m_01',action:'m_01',unlocked:true,selected:true});
		var mission_01 = me.entityPool.newInstanceOf("missionselectoption",64, 64, {image:'m_select_01_button',spritewidth:120,optionName:'m_01',action:'m_01',unlocked:true,selected:true});
			me.game.add(mission_01,200);
			this.menuOptions.push('m_01');
		
		//Mission 2
		if(gameData.load('m_02_unlocked') || me.gamestat.getItemValue('cheatEnabled') == 1){
			var m_02_action = 'm_02'; 
			var m_02_unlocked = true;
		}else{
			var m_02_unlocked = false;
			var m_02_action = 'locked';
		}
		
		//var mission_02 = new MissionSelectOptionEntity(192, 64, {image:'m_select_02_button',spritewidth:120,optionName:'m_02',action:m_02_action,unlocked:m_02_unlocked});
		var mission_02 = me.entityPool.newInstanceOf("missionselectoption",192, 64, {image:'m_select_02_button',spritewidth:120,optionName:'m_02',action:m_02_action,unlocked:m_02_unlocked});
			me.game.add(mission_02,200);
			this.menuOptions.push('m_02');
			
		//Mission 3
		if(gameData.load('m_03_unlocked') || me.gamestat.getItemValue('cheatEnabled')){
			var m_03_action = 'm_03'; 
			var m_03_unlocked = true;
		}else{
			var m_03_unlocked = false;
			var m_03_action = 'locked';
		}
		
		//96
		//var mission_03 = new MissionSelectOptionEntity(64, 112, {image:'m_select_03_button',spritewidth:120,optionName:'m_03',action:m_03_action,unlocked:m_03_unlocked});
		var mission_03 = me.entityPool.newInstanceOf("missionselectoption",64, 112, {image:'m_select_03_button',spritewidth:120,optionName:'m_03',action:m_03_action,unlocked:m_03_unlocked});
			me.game.add(mission_03,200);
			this.menuOptions.push('m_03');	
			
			
		//Mission 4
		if(gameData.load('m_04_unlocked') || me.gamestat.getItemValue('cheatEnabled')){
			var m_04_action = 'm_04'; 
			var m_04_unlocked = true;
		}else{
			var m_04_unlocked = false;
			var m_04_action = 'locked';
		}
		
		//var mission_04 = new MissionSelectOptionEntity(192, 112, {image:'m_select_04_button',spritewidth:120,optionName:'m_04',action:m_04_action,unlocked:m_04_unlocked});
		var mission_04 = me.entityPool.newInstanceOf("missionselectoption",192, 112, {image:'m_select_04_button',spritewidth:120,optionName:'m_04',action:m_04_action,unlocked:m_04_unlocked});
			me.game.add(mission_04,200);
			this.menuOptions.push('m_04');
				
		//Mission 5
		if(gameData.load('m_05_unlocked') || me.gamestat.getItemValue('cheatEnabled')){
			var m_05_action = 'm_05'; 
			var m_05_unlocked = true;
		}else{
			var m_05_unlocked = false;
			var m_05_action = 'locked';
		}
			
		//144
		//var mission_05 = new MissionSelectOptionEntity(64, 160, {image:'m_select_05_button',spritewidth:120,optionName:'m_05',action:m_05_action,unlocked:m_05_unlocked});
		var mission_05 = me.entityPool.newInstanceOf("missionselectoption",64, 160, {image:'m_select_05_button',spritewidth:120,optionName:'m_05',action:m_05_action,unlocked:m_05_unlocked});
			me.game.add(mission_05,200);
			this.menuOptions.push('m_05');
				
		//Mission VR
		if(gameData.load('m_vr_unlocked') || me.gamestat.getItemValue('cheatEnabled')){
			var m_vr_action = 'm_vr'; 
			var m_vr_unlocked = true;
		}else{
			var m_vr_unlocked = false;
			var m_vr_action = 'locked';
		}			
		
		//var mission_vr = new MissionSelectOptionEntity(192, 160, {image:'m_select_vr_button',spritewidth:120,optionName:'m_vr',action:m_vr_action,unlocked:m_vr_unlocked});
		var mission_vr = me.entityPool.newInstanceOf("missionselectoption",192, 160, {image:'m_select_vr_button',spritewidth:120,optionName:'m_vr',action:m_vr_action,unlocked:m_vr_unlocked});
			me.game.add(mission_vr,200);
			this.menuOptions.push('m_vr');
			
		this.menuOptionsLength = this.menuOptions.length - 1;
	}
});


/* ---------------------
Pause Menu Entity

2 Pause States - Story Mode & Time Attack Mode

----------------------*/

var PauseMenuEntity = me.ObjectEntity.extend({
	init:function(x,y,settings){
		
		this.parent(x,y,settings);
		//this.script = script[settings.script];		
		//this.scriptLength = this.script.length;
		//this.nextLevel = settings.nextLevel;
		
		//
		this.updateWhenPaused = true;
		this.floating = true;
		this.transitioning = false;		
		
		if(this.renderable){
			this.renderable.alpha = 0.8;
			//this.renderable.addAnimation('static',[0,1,2,3],1);
			//this.renderable.setCurrentAnimation('static');
		}
		//Key Bindings
		this.setMenuKeyBindings();
		
		
		//Build Menu System
		//Array that contains currently selected item and the next action to take
		//Nah going to be lazy and just do it live...
		
		this.currentSelection = 0;
		
		//console.log("Game Data: " + gameData.load('currentMode'));
		//console.log("Game Stat: " + me.gamestat.getItemValue('currentMode'));
		
		if(devMode){
			
			//These are set as the optionName for each option, so we can find them and update them later on
			this.menuOptions = ['mission1','mission2','mission3','mission4','mission5','quitToTitle'];
			this.menuOptionsLength = this.menuOptions.length - 1;
			
			//Mission 1
			//var option_1 = new MenuOptionEntity(0, 0, {optionName:'mission1', msg:'MISSION 1',action:'loadLevel',levelName:'m_01_mission_screen',textPosY:32, textPosX:0, drawImediate:true, selected:true});
			var option_1 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'mission1', msg:'MISSION 1',action:'loadLevel',levelName:'m_01_mission_screen',textPosY:32, textPosX:0, drawImediate:true, selected:true});
					me.game.add(option_1,200);
					//me.game.sort();
						
			//Mission 2
			//var option_2 = new MenuOptionEntity(0, 0, {optionName:'mission2',msg:'MISSION 2',action:'loadLevel',levelName:'m_02_mission_screen',textPosY:48, textPosX:0, drawImediate:true});
			var option_2 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'mission2',msg:'MISSION 2',action:'loadLevel',levelName:'m_02_mission_screen',textPosY:48, textPosX:0, drawImediate:true});
					me.game.add(option_2,200);
					//me.game.sort();
			
			//Mission 3
			//var option_3 = new MenuOptionEntity(0, 0, {optionName:'mission3',msg:'MISSION 3',action:'loadLevel',levelName:'m_03_lvl_01',textPosY:64, textPosX:0, drawImediate:true});
			var option_3 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'mission3',msg:'MISSION 3',action:'loadLevel',levelName:'m_03_lvl_01',textPosY:64, textPosX:0, drawImediate:true});
					me.game.add(option_3,200);
					//me.game.sort();
					
			//Mission 4
			//var option_4 = new MenuOptionEntity(0, 0, {optionName:'mission4',msg:'MISSION 4',action:'loadLevel',levelName:'m_04_lvl_01',textPosY:80, textPosX:0, drawImediate:true});
			var option_4 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'mission4',msg:'MISSION 4',action:'loadLevel',levelName:'m_04_lvl_01',textPosY:80, textPosX:0, drawImediate:true});
					me.game.add(option_4,200);
					//me.game.sort();
			
			//Mission 5
			//var option_5 = new MenuOptionEntity(0, 0, {optionName:'mission5',msg:'MISSION 5',action:'loadLevel',levelName:'m_05_lvl_01',textPosY:96, textPosX:0, drawImediate:true});
			var option_5 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'mission5',msg:'MISSION 5',action:'loadLevel',levelName:'m_05_lvl_01',textPosY:96, textPosX:0, drawImediate:true});
					me.game.add(option_5,200);
					//me.game.sort();
			
			//var option_quit = new MenuOptionEntity(0, 0, {optionName:'quitToTitle',msg:'QUIT TO TITLE',action:'quitToStartMenu',textPosY:112, textPosX:0, drawImediate:true});
			var option_quit = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'quitToTitle',msg:'QUIT TO TITLE',action:'quitToStartMenu',textPosY:112, textPosX:0, drawImediate:true});
				me.game.add(option_quit,200);
				//me.game.sort();
				
				
				
		}else{
			
			this.posY = 32;
			this.posX = 32;
			this.ySpacing = 16;
			
			var currentLevel = me.levelDirector.getCurrentLevelId();
			console.log(currentLevel);
			var canonicalName = '';
			var levelNumber = currentLevel.split('_')[1] + '.' + currentLevel.split('_')[3] + ' ';
			//Get Level Title from this array
			for(var level = 0; level < levels.length; level++){
				if(levels[level].map == currentLevel){
					if(levels[level].name != ''){
						canonicalName = levelNumber + levels[level].name;
					}else{
						canonicalName = levelNumber + '[no name]';
					}
				}
			}
			
			console.log(canonicalName);
			
			//var levelCanonicalName = new CutSceneTextEntity(0, 0, {msg:canonicalName,textPosY:this.posY, textPosX:this.posX, blink:false, drawImediate:true, pauseText:true});
			var levelCanonicalName = me.entityPool.newInstanceOf("cutscenetext",0, 0, {msg:canonicalName,textPosY:this.posY, textPosX:this.posX, blink:false, drawImediate:true, pauseText:true});	
				me.game.add(levelCanonicalName,300);
				//me.game.sort();
				this.posY+=(this.ySpacing*2);
			
			if(me.gamestat.getItemValue('currentMode') == 'timeAttack'){				
				this.menuOptions = ['resume','quitToLevelSelect','quitToTitle','quitToDesktop'];
			}else{
				if(me.gamestat.getItemValue('currentMode') == 'trueNovice'){
					this.menuOptions = ['resume','restartLevel','setModeOperative','setModeNarrative','quitToTitle','quitToDesktop'];	
				}else if(me.gamestat.getItemValue('currentMode') == 'novice'){
					this.menuOptions = ['resume','restartLevel','setModeNovice','setModeNarrative','quitToTitle','quitToDesktop'];	
				}else if(me.gamestat.getItemValue('currentMode') == 'ghost'){
					this.menuOptions = ['resume','restartLevel','setModeNovice','setModeOperative','quitToTitle','quitToDesktop'];	
				}
			}
			this.menuOptionsLength = this.menuOptions.length - 1;
			//Resume
			
			//var option_resume = new MenuOptionEntity(0, 0, {optionName:'resume', msg:'RESUME',action:'resume',levelName:'',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
			var option_resume = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'resume', msg:'RESUME',action:'resume',levelName:'',textPosY:this.posY, textPosX:this.posX, drawImediate:true, selected:true});
				me.game.add(option_resume,200);
				//me.game.sort();
				this.posY+=(this.ySpacing);
			
			if(me.gamestat.getItemValue('currentMode') == 'timeAttack'){
				//var option_0 = new MenuOptionEntity(0, 0, {optionName:'quitToLevelSelect',msg:'RETURN TO LEVEL SELECT',action:'quitToLevelSelect',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				var option_0 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'quitToLevelSelect',msg:'RETURN TO LEVEL SELECT',action:'quitToLevelSelect',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
					me.game.add(option_0,200);
					//me.game.sort();
					this.posY+=(this.ySpacing);
			}else{
				//var option_0 = new MenuOptionEntity(0, 0, {optionName:'restartLevel',msg:'RESTART LEVEL',action:'restartLevel',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				var option_0 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'restartLevel',msg:'RESTART LEVEL',action:'restartLevel',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
					me.game.add(option_0,200);
					//me.game.sort();
					this.posY+=(this.ySpacing);
			}
			
			if(me.gamestat.getItemValue('currentMode') == 'novice' || me.gamestat.getItemValue('currentMode') == 'ghost'){
				//var option_1 = new MenuOptionEntity(0, 0, {optionName:'quitToTitle',msg:'QUIT TO TITLE',action:'quitToStartMenu',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				var option_1_1 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'setModeNovice',msg:'CHANGE TO [NOVICE] MODE',action:'setModeNovice',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_1_1,200);
				//me.game.sort();
				this.posY+=(this.ySpacing);
			}
			
			if(me.gamestat.getItemValue('currentMode') == 'trueNovice' || me.gamestat.getItemValue('currentMode') == 'ghost'){
				//var option_1 = new MenuOptionEntity(0, 0, {optionName:'quitToTitle',msg:'QUIT TO TITLE',action:'quitToStartMenu',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				var option_1_2 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'setModeOperative',msg:'CHANGE TO [OPERATIVE] MODE',action:'setModeOperative',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_1_2,200);
				//me.game.sort();
				this.posY+=(this.ySpacing);	
			}
			
			if(me.gamestat.getItemValue('currentMode') == 'trueNovice' || me.gamestat.getItemValue('currentMode') == 'novice'){
				//var option_1 = new MenuOptionEntity(0, 0, {optionName:'quitToTitle',msg:'QUIT TO TITLE',action:'quitToStartMenu',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				var option_1_3 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'setModeNarrative',msg:'CHANGE TO [NARRATIVE] MODE',action:'setModeNarrative',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_1_3,200);
				//me.game.sort();
				this.posY+=(this.ySpacing);
			}
			
			//var option_1 = new MenuOptionEntity(0, 0, {optionName:'quitToTitle',msg:'QUIT TO TITLE',action:'quitToStartMenu',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
			var option_1 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'quitToTitle',msg:'QUIT TO TITLE',action:'quitToStartMenu',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_1,200);
				//me.game.sort();
				this.posY+=(this.ySpacing);
				
			//var option_2 = new MenuOptionEntity(0, 0, {optionName:'quitToDesktop',msg:'QUIT TO DESKTOP',action:'quitToDesktop',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
			var option_2 = me.entityPool.newInstanceOf("menuoption",0, 0, {optionName:'quitToDesktop',msg:'QUIT TO DESKTOP',action:'quitToDesktop',textPosY:this.posY, textPosX:this.posX, drawImediate:true});
				me.game.add(option_2,200);
				//me.game.sort();
				this.posY+=(this.ySpacing);
		}
	},
	update:function(){
		if(this.renderable){
			//this.renderable.alpha = 1;
			
			//this.renderable.setCurrentAnimation('static');
		}
		
		
		//Check for change		
		//Watch for Up/Down and space/enter
		if(me.input.isKeyPressed('enter')){
			//Either go to submenu or load a level.
			
			var selectedOption = me.game.getEntityByProp('optionName',this.menuOptions[this.currentSelection]);
			for(var i = 0; i < selectedOption.length; i++){
				if(selectedOption[i].action == 'loadLevel'){
					this.loadLevel(selectedOption[i].levelName);
				}else if(selectedOption[i].action == 'resume'){					
					this.unPause();
				}else if(selectedOption[i].action == 'quitToLevelSelect'){
					me.audio.play('title', false, null, 0.5);
					this.quitToLevelSelect();
				}else if(selectedOption[i].action == 'restartLevel'){
					me.audio.play('title', false, null, 0.5);
					this.restartLevel();
				}else if(selectedOption[i].action == 'setModeNovice'){
					me.audio.play('title', false, null, 0.5);
					this.setModeNovice();
				}else if(selectedOption[i].action == 'setModeOperative'){
					me.audio.play('title', false, null, 0.5);
					this.setModeOperative();
				}else if(selectedOption[i].action == 'setModeNarrative'){
					me.audio.play('title', false, null, 0.5);
					this.setModeNarrative();
				}else if(selectedOption[i].action == 'quitToStartMenu'){
					me.audio.play('title', false, null, 0.5);
					this.quitToStartMenu();
				}else if(selectedOption[i].action == 'quitToDesktop'){
					me.audio.play('title', false, null, 0.5);
					this.quitToDesktop();
				}
			}
				
		}else if(me.input.isKeyPressed('up')){
			//navigate to next position in array
			if(this.currentSelection !== 0){
				
			 //me.audio.play('switchtrigger');
			 me.audio.play('menucursor');
				
				var selectedOption = me.game.getEntityByProp('optionName',this.menuOptions[this.currentSelection]);
				for(var i = 0; i < selectedOption.length; i++){
					selectedOption[i].selected = false;					
				}
				
				this.currentSelection -= 1;
				
				var selectedOption = me.game.getEntityByProp('optionName',this.menuOptions[this.currentSelection]);
				for(var i = 0; i < selectedOption.length; i++){
					selectedOption[i].selected = true;							
				}
			}
		}else if(me.input.isKeyPressed('down')){
			//navigate to previous position in array
			if(this.currentSelection != this.menuOptionsLength){
			
				//me.audio.play('switchtrigger');
				me.audio.play('menucursor');
				
				var selectedOption = me.game.getEntityByProp('optionName',this.menuOptions[this.currentSelection]);
				for(var i = 0; i < selectedOption.length; i++){
					//console.log(trapDevices[i]);
					selectedOption[i].selected = false;							
				}
				
				//Go up
				this.currentSelection += 1;
				//console.log(this.menuOptions[this.currentSelection]);
				
				
				var selectedOption = me.game.getEntityByProp('optionName',this.menuOptions[this.currentSelection]);
				for(var i = 0; i < selectedOption.length; i++){
					selectedOption[i].selected = true;							
				}				
			}
		}
		
		if(me.input.isKeyPressed('pause')){
			if(me.state.isPaused()){
				this.unPause();
			}
		}
	
	},
	loadLevel: function(levelName){
		if(me.audio.getCurrentTrack()){
			me.audio.stop(me.audio.getCurrentTrack());
		}
		if(me.state.isPaused()){
			this.unPause();
		}
		this.fixKeyBindings();
		InputControls.setForGameplay();
		//me.levelDirector.loadLevel(levelName);
		this.transitionToLevel(levelName);
	},
	quitToLevelSelect: function(){		
		this.transitionToLevel('level_select_m_'+me.levelDirector.getCurrentLevelId().split('_')[1]);
	},
	restartLevel: function(){		
		//Clear Checkpoint Data
		me.gamestat.setValue('checkpointDataTemp','');
		me.gamestat.setValue('checkpointData','');
		me.gamestat.setValue('checkpointActivated','');
		me.gamestat.setValue('checkpointCurrent','');

		this.fixKeyBindings();
		me.state.resume(true);
		
		me.levelDirector.reloadLevel();
		//this.transitionToLevel('level_select_m_'+me.levelDirector.getCurrentLevelId().split('_')[1]);
	},
	setModeNovice: function(){		
		//Clear Checkpoint Data
		/*
		me.gamestat.setValue('checkpointDataTemp','');
		me.gamestat.setValue('checkpointData','');
		me.gamestat.setValue('checkpointActivated','');
		me.gamestat.setValue('checkpointCurrent','');
		*/
		gameData.save('currentMode', 'trueNovice');
		me.gamestat.setValue('currentMode', 'trueNovice');
		
		gameData.save('mixedMode',true);

		this.fixKeyBindings();
		me.state.resume(true);
		
		me.levelDirector.reloadLevel();
		//this.transitionToLevel('level_select_m_'+me.levelDirector.getCurrentLevelId().split('_')[1]);
	},
	setModeOperative: function(){		
		//Clear Checkpoint Data
		/*
		me.gamestat.setValue('checkpointDataTemp','');
		me.gamestat.setValue('checkpointData','');
		me.gamestat.setValue('checkpointActivated','');
		me.gamestat.setValue('checkpointCurrent','');
		*/
		gameData.save('currentMode', 'novice');
		me.gamestat.setValue('currentMode', 'novice');
		
		gameData.save('mixedMode',true);

		this.fixKeyBindings();
		me.state.resume(true);
		
		me.levelDirector.reloadLevel();
		//this.transitionToLevel('level_select_m_'+me.levelDirector.getCurrentLevelId().split('_')[1]);
	},
	setModeNarrative: function(){		
		//Clear Checkpoint Data
		/*
		me.gamestat.setValue('checkpointDataTemp','');
		me.gamestat.setValue('checkpointData','');
		me.gamestat.setValue('checkpointActivated','');
		me.gamestat.setValue('checkpointCurrent','');
		*/
		gameData.save('currentMode', 'ghost');
		me.gamestat.setValue('currentMode', 'ghost');
		
		gameData.save('mixedMode',true);

		this.fixKeyBindings();
		me.state.resume(true);
		
		me.levelDirector.reloadLevel();
		//this.transitionToLevel('level_select_m_'+me.levelDirector.getCurrentLevelId().split('_')[1]);
	},
	quitToStartMenu: function(){
		this.transitionToLevel('title_screen');
	},
	quitToDesktop: function(){
		//Node-webkit code here
		//gui.App.quit();
		
		try{
			gui.App.quit();
		}catch (e){
			
		}
	},
	transitionToLevel:function(levelName){
		
		//Clear Checkpoint Data
		me.gamestat.setValue('checkpointDataTemp','');
		me.gamestat.setValue('checkpointData','');
		me.gamestat.setValue('checkpointActivated','');
		me.gamestat.setValue('checkpointCurrent','');
		
		this.fixKeyBindings();
		me.state.resume();
		if(me.audio.getCurrentTrack()){
			me.audio.stop(me.audio.getCurrentTrack());
		}		
		//leveltransition = new LevelTransitionEntity(spy.pos.x - 320, spy.pos.y - 240, {image:'spiraltransition3', spritewidth:640, spriteheight: 480, transitionType: 'out', updateWhenPaused:true });
		var leveltransition = me.entityPool.newInstanceOf("leveltransition",spy.pos.x - 320, spy.pos.y - 240, {image:'spiraltransition3', spritewidth:640, spriteheight: 480, transitionType: 'out', updateWhenPaused:true });
			me.game.add(leveltransition,300);
			//me.game.sort();
					
			me.game.viewport.fadeIn('#000000',500, function(){ me.levelDirector.loadLevel(levelName); });
	},
	fixKeyBindings: function(){
		//enable the keyboard
		/*
		me.input.bindKey(me.input.KEY.A, "left");
		me.input.bindKey(me.input.KEY.D, "right");
		me.input.bindKey(me.input.KEY.W, "jump", true);
		me.input.bindKey(me.input.KEY.LEFT, "left");
		me.input.bindKey(me.input.KEY.RIGHT, "right");
		me.input.bindKey(me.input.KEY.UP, "jump", true);
		me.input.bindKey(me.input.KEY.SPACE, "jump", true);
		me.input.bindKey(me.input.KEY.X, "jump", true);
		me.input.bindKey(me.input.KEY.SHIFT, "stealth", true);
		me.input.bindKey(me.input.KEY.S, "stealth", true);
		me.input.bindKey(me.input.KEY.DOWN, "stealth", true);
		*/
		
		if(spy.isCaught){
			InputControls.setForGameplay();
			InputControls.setForRetry();
		}else{
			InputControls.setForGameplay();
		}
		
	},
	setMenuKeyBindings: function(){
		//Key Bindings 
		/*
		me.input.bindKey(me.input.KEY.SPACE, "enter", true);
		me.input.bindKey(me.input.KEY.ENTER, "enter", true);
		
		//me.input.bindKey(me.input.KEY.ESC, "cancel", true);
		//me.input.bindKey(me.input.KEY.ENTER, "enter", true);
		
		me.input.bindKey(me.input.KEY.DOWN, "down", true);
		me.input.bindKey(me.input.KEY.S, "down", true);
		
		me.input.bindKey(me.input.KEY.UP, "up", true);
		me.input.bindKey(me.input.KEY.W, "up", true);
		*/
		InputControls.setForMenu();
	},
	unPause: function(){
		me.audio.play('computer_boot');
		this.fixKeyBindings();
		
		var options = me.game.getEntityByProp('type','MENUOPTION');
		for(var i = 0; i < options.length; i++){
			me.game.remove(options[i],true);
		}
		
		var pauseText = me.game.getEntityByProp('pauseText',true);
		for(var i = 0; i < pauseText.length; i++){
			me.game.remove(pauseText[i],true);
		}
		me.state.resume(true);
		me.game.remove(this);
	}
});

/* ---------------------
Menu Option Entity

This eventually needs to be clickable

----------------------*/

var MenuOptionEntity = me.ObjectEntity.extend({
	init: function(x,y,settings){
		settings.height = 240;
		settings.width = 320;
		//settings.image = 'transparent';
		this.alwaysUpdate = true;
		this.parent(x,y,settings);
		this.type = 'MENUOPTION';
		this.updateWhenPaused = true;
		
		this.unlocked = (settings.unlocked === false) ? settings.unlocked : true;	//Default to True
		
		//Load the font
		(settings.drawImediate) ? this.drawImediate = settings.drawImediate : this.drawImediate = false;
		
		(settings.optionName) ? this.optionName = settings.optionName : this.optionName = false;
		
		(settings.blink) ? this.blink = settings.blink : this.blink = false;
		
		(settings.action) ? this.action = settings.action : this.action = false;
		(settings.levelName) ? this.levelName = settings.levelName : this.levelName = false;
		
		(settings.ressetting) ? this.ressetting = settings.ressetting : this.ressetting = false;
		
		(settings.selected) ? this.selected = settings.selected : this.selected = false;
		this.blinkCounter = 0;
		
		(settings.textPos) ? this.textPos = settings.textPos : this.textPos = 'left';
		
		(settings.textPosX) ? this.posX = settings.textPosX : this.posX = 40;
		
		//164,180,196
		(settings.textPosY) ? this.posY = settings.textPosY : this.posY = 180;
		if(this.unlocked){
			this.font = new me.BitmapFont(TextFont,8);			
		}else{
			this.font = new me.BitmapFont(TextFontGray,8);
			//this.font = new me.BitmapFont('8x8PressStartP2Blue',8);
		}
		this.msg = settings.msg;
		this.originalMsg = settings.msg;
		this.msgSplit = this.msg.split('');
		this.msgIndex = 0;
		this.msgFull = this.msg;
		this.msg = '';
		this.msg2 = '';
		this.msg3 = '';
		this.msg4 = '';
		this.msg5 = '';
		this.timer = 0;
		this.msgComplete = false;
		this.msgLine = 1;
		this.floating = true;
		this.textAlpha = 1;
		this.alphaUp = true;
		this.defaultAlpha = (settings.defaultAlpha) ? settings.defaultAlpha : 1;
		
		console.log(this.optionName);
	},
	update:function(){
		if(this.drawImediate){
			if(this.msgFull.indexOf('|') > -1){
				this.msgSplit = this.msgFull.split('|');
				this.msg = this.msgSplit[0];
				this.msg2 = this.msgSplit[1];
			}else{
				this.msg = this.msgFull;
			}
			
			
			this.msgComplete = true;
		}else{
			this.timer++;
			//console.log(this.msg);
			if(this.timer > 2){
				if(this.msgIndex < this.msgSplit.length){
					if(this.msgSplit[this.msgIndex] == '|'){
						this.msgLine++;
						console.log(this.msgLine);
					}else{
						switch(this.msgLine){
							case 1:
								this.msg += this.msgSplit[this.msgIndex];
								break;
							case 2:
								this.msg2 += this.msgSplit[this.msgIndex];
								break;
							case 3:
								this.msg3 += this.msgSplit[this.msgIndex];
								break;
							case 4:
								this.msg4 += this.msgSplit[this.msgIndex];
								break;
							case 5:
								this.msg5 += this.msgSplit[this.msgIndex];
								break;
						}
						me.audio.play("text");
					}
					this.msgIndex++;
				}else{
					this.msgComplete = true;
				}
				this.timer = 0;
			}	
		
		}
		
		if(this.msgComplete && this.selected){
			/*
			if(this.blinkCounter < 12){
				this.blinkCounter += 1*me.timer.tick;
			}else{
				this.blinkCounter = 0;
					//console.log('test');
				if(this.textAlpha == 0){
					this.textAlpha = this.defaultAlpha;
				}else{
					this.textAlpha = 0;
				
				}
			} */
			
			if(this.blinkCounter < 15){
				this.blinkCounter += 1*me.timer.tick;
				if(this.alphaUp){
					this.textAlpha -= .05;
				}else{
					this.textAlpha += .05;
				}
			}else{
				this.blinkCounter = 0;
				this.alphaUp = !this.alphaUp;
					//console.log('test');
					/*
				if(this.textAlpha == 0){
					this.textAlpha = this.defaultAlpha;
				}else{
					this.textAlpha = 0;					
					//this.renderable.alpha = .3;
				
				}
				*/
			}
			
		}else if(!this.selected){
			this.textAlpha = this.defaultAlpha;
			this.blinkCounter = 0;
			this.alphaUp = true;
		}
		
	},
	draw: function(context){
		//Need some logic for new		
		
		
		//if(this.textAlpha){
		
		context.globalAlpha = this.textAlpha;
		
		this.font.draw(context,
			this.msg,this.posX,
			this.posY);
		
		
		this.msgWidth2 = this.font.measureText(context,this.msg2).width;
			this.font.draw(context,
				this.msg2,this.posX,
				this.posY + 16);
		
		context.globalAlpha = 1;
		/*
		this.msgWidth3 = this.font.measureText(context,this.msg3).width;
			this.font.draw(context,
				this.msg3,this.posX,
				this.posY + 32);
		
		this.msgWidth4 = this.font.measureText(context,this.msg4).width;
			this.font.draw(context,
				this.msg4,this.posX,
				this.posY + 48);
				*/
		//}
		
	}
});

/* ---------------------
Mission Select Option Entity

This eventually needs to be clickable

----------------------*/

var MissionSelectOptionEntity = me.ObjectEntity.extend({
	init: function(x,y,settings){
		settings.height = 40;
		settings.width = 120;
		
		this.alwaysUpdate = true;
		this.parent(x,y,settings);
		this.type = 'MENUOPTION';
		this.updateWhenPaused = true;
		this.floating = true;
		
		(settings.selected) ? this.selected = settings.selected : this.selected = false;		
		(settings.optionName) ? this.optionName = settings.optionName : this.optionName = false;		
		(settings.action) ? this.action = settings.action : this.action = false;
		
		this.unlocked = (settings.unlocked) ? settings.unlocked : false;	
		
		this.renderable.addAnimation('selected', [1]);
		this.renderable.addAnimation('unselected', [0]);
		this.renderable.addAnimation('locked', [2]);
		this.renderable.addAnimation('lockedSelected', [3]);
		
		if(this.unlocked){
			if(this.selected){
				this.renderable.setCurrentAnimation('selected');
			}else{
				this.renderable.setCurrentAnimation('unselected');				
			}
		}else{
			this.renderable.setCurrentAnimation('locked');
		}
		
		console.log(this.optionName + ' ' + x + ','+ y);
	},
	update:function(){
		if(this.unlocked){
			if(this.selected){
				this.renderable.setCurrentAnimation('selected');
			}else{
				this.renderable.setCurrentAnimation('unselected');				
			}
		}else{
			if(this.selected){
				this.renderable.setCurrentAnimation('lockedSelected');
			}else{
				this.renderable.setCurrentAnimation('locked');				
			}
			
		}
		
	}
});