package com.sarmatianHeroes {
	
	import flash.display.*;
	import flash.events.*;
	
	public class App extends MovieClip {
		
		private static var _instance:App = null;
		private var _game:Game;
        public static const SCR_W:int = 800;
        public static const SCR_H:int = 600;
		public static const APP_VER:String = "Version 0.01 - August 4, 2012";
        public static const SCR_HALF_W:int = 400;
        public static const SCR_HALF_H:int = 300;

		var _title:MovieClip;
		var _play_bt:SimpleButton;
		var _credits_bt:SimpleButton;
		var _credits:MovieClip;
		var _back_bt:SimpleButton;
		
		public function App() 
		{
	        if (_instance != null)
            {
                throw "App.as is a singleton class. Use Game.getInstance();";
            }
            _instance = this;
			if (stage)
            {
                this.init();
            }
            else
            {
                addEventListener(Event.ADDED_TO_STAGE, this.init);
            }
		}
		
        public function init(event:Event = null) : void
        {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.focus = stage;
			
			_title = new title();
			addChild(_title);
			_play_bt = _title.play_btn;
			_credits_bt = _title.credits_btn;
			_play_bt.addEventListener(MouseEvent.CLICK, this.onPlayBtn);
			_credits_bt.addEventListener(MouseEvent.CLICK, this.onCreditsBtn);
			
            removeEventListener(Event.ADDED_TO_STAGE, init);
        }// end function
		
		function onPlayBtn(event:MouseEvent) : void
		{
			_game = Game.getInstance();
            addChild(_game);
			_play_bt.removeEventListener(MouseEvent.CLICK, this.onPlayBtn);
			_credits_bt.removeEventListener(MouseEvent.CLICK, this.onCreditsBtn);
			removeChild(_title);
			_title = null;
		}
		
		function onCreditsBtn(event:MouseEvent) : void
		{
			_credits = new credits();
			addChild(_credits);
			_back_bt = _credits.Back_btn;
			_back_bt.addEventListener(MouseEvent.CLICK, this.onBackBtn);
		}
		
		function onBackBtn(event:MouseEvent) : void
		{
			removeChild(_credits);
			_back_bt.removeEventListener(MouseEvent.CLICK, this.onBackBtn);
			_credits = null;
		}
		
		public static function getInstance() : App
        {
            return _instance == null ? (new App) : (_instance);
        }
	}
	
}
