package
{
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author Michael Miriti
	 */
	public class MainMenu
	{
		public var callback:Function;
		private var _blink_shown:Boolean = true;
		private var _blink_timeout:Number = 0;
		private var _down:Boolean = false;
		private var _title:String = "Symbol City Incident";
		
		public function MainMenu()
		{
			
			revailPAKdie();			
			
			Main.i.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			Main.i.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			Main.i.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			Main.i.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		}
		
		private function onMouseDown(e:MouseEvent):void 
		{
			onKeyDown(null);
		}
		
		private function onMouseUp(e:MouseEvent):void 
		{
			onKeyUp(null);
		}
		
		private function onKeyUp(e:KeyboardEvent):void
		{
			Sounds.getI().sndSelect.play();
			callback();
			Main.i.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			Main.i.removeEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			Main.i.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			Main.i.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		
		private function revailPAKdie():void
		{
			Main.i.setString(29 + (_down ? 1 : 0), 11 + (_down ? 1 : 0), "Press any key to play", 0, 7);
		}
		
		private function hidePAKdie():void
		{
			Main.i.fillBlock(29, 11, 22, 1, 15, 7, 7);
		}
		
		private function onKeyDown(e:KeyboardEvent):void
		{
			_down = true;
		}
		
		public function update(delta:Number):void
		{
			Main.i.fillBlock(19, 8, 43, 10, 15, 9, 9);
			
			
			if (_down)
			{
				Main.i.window(21, 10, 40, 5, 8, 7, true, 0, 0, 15);
			}
			else
			{
				Main.i.window(0, 0, 80, 25, 14, 9);
				Main.i.window(20, 9, 40, 5, 8, 7, true, 0, 0, 15);
			}
			
			if (_blink_timeout < 500)
			{
				revailPAKdie();
			}
			else
			{
				hidePAKdie();
			}
			if (_blink_timeout >= 1000)
				_blink_timeout = 0;
			_blink_timeout += delta;
			
			Main.i.setString((80 - _title.length) / 2, 5, _title, 14, 9);
			Main.i.setString((80 - "game programm".length) / 2, 6, "game programm", 14, 9);
			Main.i.setString(2, 23, "Michael \"KEFIR\" Miriti special for IGDC.ru #84 08/2012", 7, 9);
		}
	
	}

}