<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal" 
    xmlns:ns2="com.digows.components.util.*" 
    horizontalAlign="left" backgroundGradientAlphas="[1.0, 1.0]" 
    backgroundGradientColors="[#858585, #F9F9F9]" viewSourceURL="srcview/index.html">
        
    <mx:VBox height="100%" horizontalAlign="center">
        <ns2:MemMonitor/>
    </mx:VBox>
    
    <mx:VBox width="100%" height="100%">
    
        <mx:TitleWindow id="windowModule" 
            width="100%" height="100%" 
            layout="vertical" title="Janela Module" 
            horizontalAlign="center" verticalAlign="middle">
            
            <mx:TabNavigator width="100%" height="100%" horizontalAlign="center">
            
                <mx:VBox label="Modulo AS" width="100%" height="100%">            
                    <mx:VBox id="vbModuloAS" width="100%" height="100%"/>
                    <mx:HBox width="100%">
                        <mx:Button label="Carregar" click="loadAS()"/>
                        <mx:Button label="Descarregar" click="unLoadModule(moduleLoaderAS)"/>
                    </mx:HBox>
                </mx:VBox>
                
                <mx:VBox id="vbModuloMXML" label="Modulo MXML" 
                        width="100%" height="100%" borderColor="#E2E2E2" backgroundColor="#FFFFFF">
                        
                    <mx:ModuleLoader id="moduleMXML" url="ModuleMXML.swf"  
                        width="100%" height="100%" 
                        ready="carregou()" progress="onProgress(event)"
                        error="onError(event)" unload="onUnload(event)"/>
                    <mx:Button label="Descarregar" click="unLoadModule(moduleMXML)"/>
                </mx:VBox>
                
            </mx:TabNavigator>
            
            <mx:ProgressBar id="progress" width="100%" indeterminate="true" label="Status"/>
            
        </mx:TitleWindow>
    </mx:VBox>

    <mx:Script>
        <![CDATA[
            import mx.modules.ModuleLoader;
            import mx.modules.Module;
            import mx.modules.ModuleManager;
            import mx.modules.IModuleInfo;
            import mx.events.ModuleEvent;
            import mx.controls.Alert;
            
            private var moduleLoaderAS:ModuleLoader = new ModuleLoader();
                        
            private function loadAS() : void
            {        
                //Progresso do download...
                moduleLoaderAS.addEventListener(ModuleEvent.PROGRESS,onProgress);
                
                //Ao estar carregado e pronto....
                moduleLoaderAS.addEventListener(ModuleEvent.READY,onReady);
                
                //Se ocorrer algum erro...
                moduleLoaderAS.addEventListener(ModuleEvent.ERROR,onError);
                
                moduleLoaderAS.addEventListener(ModuleEvent.UNLOAD,onUnload);
                
                moduleLoaderAS.url = "ModuleAS.swf";
                moduleLoaderAS.loadModule();
            }
            
            private function onProgress(event:ProgressEvent):void 
            {
                var numPerc:Number     = Math.round((Number(event.bytesLoaded) / Number(event.bytesTotal)) * 100);
                
                windowModule.status    = "Carregando Modulo..."+numPerc+"%";
                progress.label         = "Carregando Modulo..."+numPerc+"%";
                   progress.indeterminate = false;
            }
            
            private function onReady(event:ModuleEvent):void
            {                
                vbModuloAS.addChild(moduleLoaderAS);
                carregou();
            }
            
            private function onError(event:ModuleEvent) : void
            {
                Alert.show("Não foi possível carregar o modulo. \nDetalhes:"+event.errorText,"Erro");
            }
            
            private function onUnload(event:Event):void 
            {
               windowModule.status = "Modulo Descarregado";
               progress.label      = "Modulo Descarregado";
            }
            
            private function unLoadModule(modulo:ModuleLoader):void
            {
                modulo.url == "ModuleAS.swf" ? vbModuloAS.removeChild(moduleLoaderAS) : null;
                modulo.unloadModule();
            }
            
            private function carregou():void
            {
                windowModule.status    = "Modulo Carregado";
                progress.label         = "Modulo Carregado";
                progress.indeterminate = true;
            }

        ]]>
    </mx:Script>
    
</mx:Application>