package game.GameMain
{
	import flash.events.MouseEvent;
	import flash.media.Sound;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	import flinjin.Flinjin;
	import flinjin.graphics.Layer;
	import flinjin.graphics.Sprite;
	import flinjin.graphics.SpriteText;
	import flinjin.input.Input;
	import game.GameMenu.GameMenuItem;
	
	/**
	 * ...
	 * @author Michael Miriti <m.s.miriti@gmail.com>
	 */
	public class GameMain extends Layer
	{
		private static var _bgSound:Class;
		
		private var _background:Background;
		private var _backLights:BackgroundLight;
		private var _tileLevel:TileLevel;
		
		private var _heightCounter:SpriteText = new SpriteText('0', new TextFormat('Arial', 20, 0xffffff));
		
		[Embed(source="../../assets/hud/hud_height.png")]
		private static var _heightBmp:Class;
		
		[Embed(source="../../assets/hud/hud_menu.png")]
		private static var _gotoMenuBmp:Class;
		
		private var _gotoMenuBtn:GameMenuItem;
		
		public var _main:Main;
		
		override public function get scrollY():Number
		{
			return _background.scrollY;
		}
		
		override public function set scrollY(value:Number):void
		{
			_background.scrollY = value;
			_backLights.shiftY += value * 0.8;
		}
		
		override public function Move():void
		{
			if (alpha < 1)
			{
				alpha += 0.01;
			}
			
			_heightCounter.text = Math.floor((-(_tileLevel.Player.y - 592)/200)).toString() + '"';
			
			super.Move();
		}
		
		public function startNewLevel():void
		{
			alpha = 0;
			if ((_background != null) && (_backLights != null) && (_tileLevel != null))
			{
				deleteSprite(_background);
				deleteSprite(_backLights);
				deleteSprite(_tileLevel);
			}
			
			_background = new Background();
			_backLights = new BackgroundLight();
			_tileLevel = new TileLevel();
			_gotoMenuBtn = new GameMenuItem(new _gotoMenuBmp(), null, true, 47, 132);
			_gotoMenuBtn.addEventListener(MouseEvent.MOUSE_DOWN, onGotoMenu);
			
			addSprite(_background);
			addSprite(_backLights);
			addSprite(_tileLevel);
			addSprite(_heightCounter, 438, 14);
			addSprite(new Sprite(new _heightBmp()), 435, 40);
			addSprite(_gotoMenuBtn, 430, 510);
		}
		
		private function onGotoMenu(e:MouseEvent):void 
		{
			_main.gotoMenu();
		}
		
		public function GameMain()
		{
			super(Flinjin.sceneWidth, Flinjin.sceneHeight);
			startNewLevel();
		}
	
	}

}