package 
{
    import com.constants.AssetURL;
    import com.view.fonts.Futura;
    import com.view.fonts.MinionPro;
    import com.view.swfassets.TightTextClip;
    
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.system.ApplicationDomain;
    import flash.system.LoaderContext;
    import flash.text.Font;

    public class DynamicFont extends Sprite
    {
        private var tightTextClip : TightTextClip;
        
        private var fontAssetLoader : Loader;
        private var isFontAssetLoadComplete : Boolean;
        
        private var swfAssetLoader : Loader;
        private var isSWFAssetLoadComplete : Boolean;
        
        /**
         * Initialize loaders and load all assets
         * 
         */        
        public function DynamicFont()
        {
            isFontAssetLoadComplete = false;
            fontAssetLoader = new Loader();
            fontAssetLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, onFontAssetsLoadComplete );
            fontAssetLoader.load( new URLRequest( AssetURL.FONT_ASSET_URL ),
                                  new LoaderContext( false, ApplicationDomain.currentDomain ) );
            
            isSWFAssetLoadComplete = false;
            swfAssetLoader = new Loader();
            swfAssetLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, onSWFAssetLoadComplete );
            swfAssetLoader.load( new URLRequest( AssetURL.SWF_ASSET_URL ),
                                 new LoaderContext( false, ApplicationDomain.currentDomain ) );
        }
        
        /**
         * Called when all assets have been successfully loaded,
         * finishes initialization by adding the loaded content
         * to the display list
         * 
         */        
        private function init() : void
        {
            //declare and add the TightTextClip to the stage
            tightTextClip = new TightTextClip();
            addChild( tightTextClip );
        }
        
        /**
         * Called when the font assets have been loaded
         * 
         * @param evt
         * 
         */        
        private function onFontAssetsLoadComplete( evt : Event ) : void
        {
            Font.registerFont( Futura );
            Font.registerFont( MinionPro );
            
            isFontAssetLoadComplete = true;
            
            onAllLoadComplete();
        }
        
        /**
         * Called when the swf assets have been loaded
         * 
         * @param evt
         * 
         */        
        private function onSWFAssetLoadComplete( evt : Event ) : void
        {
            isSWFAssetLoadComplete = true;
            
            onAllLoadComplete();
        }
        
        /**
         * Checks to see that all assets have been loaded before
         * calling the init function
         * 
         */        
        private function onAllLoadComplete() : void
        {
            if( isFontAssetLoadComplete && isSWFAssetLoadComplete )
            {
                init();
            }
        }
        
    }
}