package 
{
	import engine.Keys;
	import engine.Label;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.utils.getTimer;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author Fiodar Paulavets
	 */
	[Frame(factoryClass="Preloader")]
	public class Main extends Sprite 
	{
		[Embed(source = "../lib/fonts/nobr1.otf", fontName = 'Nobr1', mimeType = 'application/x-font-truetype', advancedAntiAliasing = 'true', embedAsCFF = 'false')] private const embedFontCaptions: Class;
		[Embed(source = "../lib/fonts/neucha.otf", fontName = 'Neucha', mimeType = 'application/x-font-truetype', advancedAntiAliasing = 'true', embedAsCFF = 'false')] private const embedFontText: Class;
		
		private var _deltaTime: Number, _maxDeltaTime: Number, _lastTick: Number;
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
			SWFProfiler.init(stage, this);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			Keys.ProccessKeys(stage);
			
			Sounds.manager = new Sounds();
			Sounds.manager.playSound("prop");
			
			Game.game = new Game(stage.stageWidth, stage.stageHeight);
			addChild(Game.game);
			
			_lastTick = 0;
			_maxDeltaTime = 1 / stage.frameRate;
			addEventListener(Event.ENTER_FRAME, enterFrame);
		}
		
		private function enterFrame(e:Event):void 
		{
			_deltaTime = (getTimer() - _lastTick) / 1000;
			_deltaTime = (_deltaTime > _maxDeltaTime) ? _maxDeltaTime : _deltaTime;
			//_deltaTime = _maxDeltaTime;
			
			Game.game.Update(_deltaTime);
			
			_lastTick = getTimer();
		}
		
	}
	
}