/*
 * There was a really weird bug here.  I think it might have actually been 
 * with the FTP program, but I'm keeping these changes just in case.
 */
package meyermike
{
    import flash.display.DisplayObject;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.ProgressEvent;
    import flash.utils.getDefinitionByName;

    [ SWF( backgroundColor = '#303000', width = '450', height = '250' ) ]
    
    /**
     * ...
     * @author Mike Meyer
     */
    public class Preloader extends MovieClip
    {
        private static const LOADING_BAR_WIDTH:Number = 300;
        private static const LOADING_BAR_HEIGHT:Number = 30;
        private static const LOADING_BAR_BORDERWIDTH:Number = 2;
        private static const LOADING_BAR_MARGIN:Number = 2;
        private static const LOADING_BAR_COLOR:Number = 0xFFFFFF;
        
        private var mLoadingBarBorder:Sprite;
        private var mLoadingBarProgress:Sprite;
    
        // working around an apparent Flash bug
        private var mPreviousProgress:Number;
        
        public function Preloader()
        {
            addEventListener(Event.ENTER_FRAME, checkProgress);
            
            mPreviousProgress = 0;
            
            // show loader
            CreateGraphics();
        }

        private function progress(e:ProgressEvent):void
        {
            // update loader
        }

        private function checkProgress(e:Event):void
        {
            var p:Number = (this.loaderInfo.bytesLoaded / this.loaderInfo.bytesTotal);
            
            // weid bug?
            if ( (mPreviousProgress > p) || (p >= 1.0) )
            {
                removeEventListener(Event.ENTER_FRAME, checkProgress);
                startup();
            }
            else
            {
                mPreviousProgress = p;
                
                mLoadingBarProgress.scaleX = p;
                
                if (p >= 1.0)
                {
                    removeEventListener(Event.ENTER_FRAME, checkProgress);
                    startup();
                }
            }
        }

        private function startup():void
        {
            // hide loader
            RemoveGraphics();
            
            stop();
            var mainClass:Class = getDefinitionByName("meyermike.Main") as Class;
            addChild(new mainClass() as DisplayObject);
        }

        private function CreateGraphics():void
        {
            var barX:Number = 0.5 * (stage.stageWidth - LOADING_BAR_WIDTH);
            var barY:Number = 0.5 * (stage.stageHeight - LOADING_BAR_HEIGHT);
            
            mLoadingBarBorder = new Sprite();
            mLoadingBarBorder.graphics.lineStyle(LOADING_BAR_BORDERWIDTH, LOADING_BAR_COLOR, 1);
            mLoadingBarBorder.graphics.beginFill(LOADING_BAR_COLOR, 0.0);
            mLoadingBarBorder.graphics.drawRect(
                0,
                0,
                LOADING_BAR_WIDTH,
                LOADING_BAR_HEIGHT);
            mLoadingBarBorder.graphics.endFill();
            mLoadingBarBorder.x = barX;
            mLoadingBarBorder.y = barY;
            this.addChild(mLoadingBarBorder);
            
            mLoadingBarProgress = new Sprite();
            mLoadingBarBorder.graphics.lineStyle(LOADING_BAR_BORDERWIDTH, LOADING_BAR_COLOR, 0);
            mLoadingBarProgress.graphics.beginFill(LOADING_BAR_COLOR);
            mLoadingBarProgress.graphics.drawRect(
                0,
                0,
                LOADING_BAR_WIDTH - (2.0 * LOADING_BAR_MARGIN),
                LOADING_BAR_HEIGHT - (2.0 * LOADING_BAR_MARGIN) );
            mLoadingBarProgress.graphics.endFill();
            mLoadingBarProgress.x = barX + LOADING_BAR_MARGIN;
            mLoadingBarProgress.y = barY + LOADING_BAR_MARGIN;
            this.addChild(mLoadingBarProgress);
        }
        
        private function RemoveGraphics():void
        {
            this.removeChild(mLoadingBarBorder);
            mLoadingBarBorder = null;
            this.removeChild(mLoadingBarProgress);
            mLoadingBarProgress = null;
        }
    }

}