<?xml version="1.0" encoding="utf-8"?>
<mx:Application creationComplete="init()" addedToStage="handleAddedToStage()" xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns1="*" viewSourceURL="/assets/swf/scale-test/flex-source/index.html">
    
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.controls.Button;
            import mx.core.IFlexDisplayObject;
            import mx.core.UIComponent;
            import mx.containers.Canvas;
            import mx.events.CloseEvent;
            import mx.managers.PopUpManagerChildList;
            import mx.managers.PopUpManager;
            import mx.containers.TitleWindow;
            
            private var popup:TitleWindow;
            
            private function init():void
            {
                //Listen for when pop-ups are added via PopUpManager:
                systemManager.addEventListener(Event.ADDED, handleItemAdded);
                
                //Listen for when the slider is changed:
                scaler.slider.addEventListener(Event.CHANGE, handleScalerChange);
            }
            
            private function handleAddedToStage():void
            {
                //Listen globally for CTRL/COMMAND+- and CTRL/COMMAND+=
                //Trying to do this on creationComplete doesn't work becuase the stage is null.
                this.stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
            }
            
            private function handleKeyDown(event:KeyboardEvent):void
            {
                if(event.ctrlKey && event.keyCode == 187) //CTRL/COMMAND+=
                {
                    scaler.slider.value += .1;
                    handleScalerChange();
                }
                else if(event.ctrlKey && event.keyCode == 189) //CTRL/COMMAND+-
                {
                    scaler.slider.value -= .1;
                    handleScalerChange();
                }
            }
            
            private function handleScalerChange(event:Event = null):void
            {
                scaleX = scaleY = scaler.slider.value;
                
                //Rescale the slider clip so that it stays the same size.
                scaler.scaleX = scaler.scaleY = scaler.slider.value / Math.pow(scaler.slider.value, 2);
                
                resizePopups();
            }
            
            //Resizes any active pop-ups.
            private function resizePopups():void
            {
                for(var i:int = 0; i < systemManager.popUpChildren.numChildren; i++)
                {
                    var p:DisplayObject = DisplayObject(systemManager.popUpChildren.getChildAt(i));
                    
                    if(p is IFlexDisplayObject)
                    {    
                        p.scaleX = p.scaleY = scaleX;
                        PopUpManager.centerPopUp(IFlexDisplayObject(p));
                    }
                }
                
                //Force the stage to send a resize event which will fix the modal window background.
                //Without this, scaling the windows below 100% will result in a modal background that doesn't cover the entire application.
                stage.dispatchEvent(new Event(Event.RESIZE));
            }
            
            //Called when children are added to the systemManager.  We look for any pop-ups added via the PopUpManager and scale those.
            //We know we've found a pop-up when it is in the popUpChildren list and its parent is the systemManager.
            //Note that this will only find pop-ups that are placed on the PopUpManagerChildList.POPUP child list.
            private function handleItemAdded(event:Event):void
            {
                var d:DisplayObject = DisplayObject(event.target.parent);
                if(systemManager.popUpChildren.contains(d) && d.parent == systemManager)
                {
                    d.scaleX = d.scaleY = scaleX;
                }
            }
            
            private function scaleTo(sf:Number):void
            {
                scaleX = scaleY = sf;
            }
            
            //Called when you click on the 'PopUp' button.
            private function togglePopUp(event:Event = null):void
            {
                if(popup)
                {
                    PopUpManager.removePopUp(popup);
                    popup = null;
                }
                else
                {
                    //Create the popup:
                    popup = new TitleWindow();
                    popup.showCloseButton = true;
                    popup.width = 300;
                    popup.height = 200;
                    var b:Button = new Button();
                    b.label = "HELLO WORLD!";
                    popup.addChild(b);
                    popup.addEventListener(CloseEvent.CLOSE, togglePopUp, false, 0, true);
                    
                    //You MUST add pop-ups to the PopUpManagerChildList.POPUP child list!
                    PopUpManager.addPopUp(popup, this, true, PopUpManagerChildList.POPUP);
                    PopUpManager.centerPopUp(popup);
                }
            }
            

        ]]>
    </mx:Script>
    <mx:HDividedBox top="10" bottom="10" left="10" right="10">
        <mx:VBox height="100%">
            <mx:Panel height="200" layout="absolute" width="100%" id="panel">
                <mx:Button label="PopUp" click="{togglePopUp()}" horizontalCenter="0" verticalCenter="0"/>
            </mx:Panel>
        </mx:VBox>
        <mx:TextArea width="100%" height="100%">
            <mx:text><![CDATA[Vestibulum non molestie lacus. Nulla sem lectus, volutpat sed vulputate quis, rhoncus dapibus urna. Donec erat leo, facilisis non vestibulum vitae, interdum quis tellus. Sed vitae lectus sed justo placerat porttitor. Donec sapien arcu, rutrum id commodo nec, pulvinar quis ligula. Fusce eleifend consequat nunc, vel ullamcorper sem aliquam eget. Vivamus sit amet elit vitae turpis malesuada accumsan nec at purus. Duis et felis lorem. Morbi in velit magna. Duis consectetur velit nec libero placerat sit amet scelerisque nunc egestas. Suspendisse potenti. Fusce tincidunt tortor nibh. Nulla facilisi. Aliquam vitae sem justo, et hendrerit elit. Curabitur sit amet massa augue, adipiscing auctor ipsum. Cras pharetra velit vitae nisl egestas sit amet adipiscing dolor suscipit. Nulla facilisi.

Nullam cursus consequat eros. Quisque mauris dui, tincidunt et adipiscing vel, congue sed enim. Pellentesque ac iaculis urna. Sed mi ante, sollicitudin in volutpat ac, pellentesque et leo. Phasellus consequat, dolor at adipiscing accumsan, augue ligula bibendum nibh, pretium commodo ante diam vitae justo. Sed rhoncus dignissim dolor, nec pretium risus adipiscing a. Integer gravida euismod urna, non tincidunt mi viverra quis. Nunc ultrices ullamcorper ornare. Vestibulum fringilla vestibulum lacus ac cursus. Cras semper, nisl sit amet lacinia porta, mi est laoreet ipsum, in convallis nulla velit nec dui. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nulla non est quis lectus iaculis facilisis. Phasellus id nulla lacus. Ut quam augue, vestibulum eget fermentum quis, porttitor vehicula enim. Nulla non lacinia urna. Donec sem diam, iaculis et eleifend ornare, pellentesque eget nisi. Duis eleifend hendrerit augue. Donec turpis elit, consectetur sed porta in, pellentesque porta ipsum. Cras orci leo, porta eget consequat sit amet, iaculis et libero.

Sed lectus enim, mollis eget ullamcorper ac, accumsan fringilla turpis. Maecenas ullamcorper enim in sem luctus feugiat. Nulla vitae varius nunc. Nullam eleifend lacus quis ante luctus varius. Duis turpis libero, fermentum at cursus et, sodales nec elit. Ut ullamcorper, sapien eu euismod lacinia, tortor sapien fringilla odio, sit amet rhoncus lectus nulla ut turpis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Duis tempor porta dui non commodo. Nulla facilisi. Sed quis nisi a massa fringilla tempor. ]]></mx:text>
        </mx:TextArea>
    </mx:HDividedBox>
    <ns1:Scaler right="0" top="0" id="scaler">
    </ns1:Scaler>
    
</mx:Application>