<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                layout="absolute"
                addedToStage="init();"
                frameRate="31"
                horizontalScrollPolicy="off"
                verticalScrollPolicy="off"
                usePreloader="true"
                backgroundGradientColors="[0xCCCCCC, 0xFFFFFF]" viewSourceURL="srcview/index.html">
    <mx:Panel x="294" y="10" width="432" height="322" layout="absolute" title="This panel is not sticky" id="_propPanel">
        <mx:TextInput x="150" y="10" width="80" id="_winwTF" text="0" editable="false"/>
        <mx:TextInput x="150" y="70" width="80" id="_swfwTF" text="0"/>
        <mx:TextInput x="150" y="100" width="80" id="_swfhTF" text="0"/>
        <mx:TextInput x="150" y="40" width="80" id="_winhTF" text="0" editable="false"/>
        <mx:Label x="20" y="12" text="browser window width"/>
        <mx:Label x="80" y="72" text="SWF width"/>
        <mx:Label x="73" y="102" text="SWF height"/>
        <mx:Label x="13" y="42" text="browser window height"/>
        <mx:CheckBox x="238" y="70" label="is in px" id="_swfwCbx"/>
        <mx:CheckBox x="238" y="100" label="is in px" id="_swfhCbx"/>
        <mx:CheckBox x="150" y="131" label="always" id="_awCbx"/>
        <mx:CheckBox x="150" y="161" label="always" id="_ahCbx"/>
        <mx:CheckBox x="150" y="191" label="always" id="_aCbx"/>
        <mx:Button x="312" y="70" label="set" width="90" id="_swfwSetBtn"/>
        <mx:Button x="312" y="100" label="set" width="90" id="_swfhSetBtn"/>
        <mx:Button x="219" y="131" label="autosize width" width="183" id="_awBtn" fontWeight="normal"/>
        <mx:Button x="219" y="161" label="autosize height" width="183" id="_ahBtn" fontWeight="normal"/>
        <mx:Button x="219" y="191" label="autosize width &amp; height" width="183" id="_aBtn" fontWeight="normal"/>
        <mx:Button x="219" y="221" label="show me some magic :)" width="183" id="_magicBtn"/>
        <mx:Button x="219" y="251" label="about SWFSize" width="183.25" id="_aboutBtn"/>
    </mx:Panel>
    <mx:Panel x="10" y="10" width="275" height="300" layout="absolute" title="This panel is sticky" id="_infoPanel">
        <mx:SWFLoader y="10" source="assets/SWFSizeLogo.swf" horizontalCenter="0"/>
        <mx:TextInput x="160" y="136" width="80" id="_leftxTF" text="0" editable="false"/>
        <mx:TextInput x="160" y="196" width="80" text="0" id="_topyTF" editable="false"/>
        <mx:TextInput x="160" y="226" width="80" id="_botyTF" text="0" editable="false"/>
        <mx:TextInput x="160" y="166" width="80" id="_rightxTF" text="0" editable="false"/>
        <mx:Label x="32" y="138" text="browser window leftX" textAlign="right"/>
        <mx:Label x="32" y="198" text="browser window topY" textAlign="right"/>
        <mx:Label x="10" y="228" text="browser window bottomY" textAlign="right"/>
        <mx:Label x="25" y="168" text="browser window rightX" textAlign="right"/>
    </mx:Panel>
    <mx:Canvas x="10" y="378" width="290" height="200" id="_dude">
        <mx:SWFLoader y="40" source="assets/Dude.swf" horizontalCenter="0"/>
        <mx:Button x="200" y="10" label="^ back ^" width="80" id="_magicBackBtn"/>
    </mx:Canvas>
    <mx:Canvas x="326" y="378" width="485" height="200" id="_about">
        <mx:Text x="0" y="25" width="485" text="So, what are the benefits of using SWFSize?&#xd;1) Cross-browser, lightweight library&#xd;2) Works with SWFObject&#xd;3) Allows you to change the width/height of your swf container at run-time&#xd;4) Allows you to use the browser's native scrollbars (mac users would appreciate that)&#xd;5) You can always get the &quot;visible&quot; browser coordinates inside your swf&#xd;6) Allows you to take control over the native scrollbar &#xd;7) Emmm...? &#xd;&#xd;Want more? Then you may extend the lib if you wish so, cause the project&#xd;is open-source. Or you can mail me and request a new feature - i would be&#xd; glad to help. "/>
        <mx:Button x="405" y="0" label="^ back ^" width="80" id="_aboutBackBtn"/>
    </mx:Canvas>
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.controls.SWFLoader;
            import mx.controls.Image;
            import mx.core.BitmapAsset;
            
            // SWFSize api
            import com.chargedweb.swfsize.SWFSize;
            import com.chargedweb.swfsize.SWFSizeEvent;
    
            // TweenLite api
            import gs.TweenLite;
            import gs.easing.*;

            // fp api
            import mx.controls.Button;
            import mx.controls.CheckBox;
            import flash.display.StageAlign;
            import flash.display.StageScaleMode;
            import flash.events.MouseEvent;
            import flash.events.Event;
            
            // assets {start}
            [Embed(source="../html-template/assets/Lines.swf", symbol="LineTop")]
            private var LineTop:Class;
            
            [Embed(source="../html-template/assets/Lines.swf", symbol="LineBot")]
            private var LineBot:Class;
            
            [Embed(source="../html-template/assets/Lines.swf", symbol="LineLeft")]
            private var LineLeft:Class;
            
            [Embed(source="../html-template/assets/Lines.swf", symbol="LineRight")]
            private var LineRight:Class;
            
            private var bgT:Sprite;
            private var bgB:Sprite;
            private var bgL:Sprite;
            private var bgR:Sprite;
            // assets {end}
            
            // SWFSize ref
            private var swfSizer:SWFSize;
            
            /**
             * Initializes the SWFSize demo app
             */
            public function init():void
            {
                _dude.visible = false;
                _dude.alpha = 0;
                _dude.y = 1600;
            
                bgT = new LineTop();
                rawChildren.addChild(bgT);
                
                bgB = new LineBot();
                rawChildren.addChild(bgB);
                
                bgL = new LineLeft();
                rawChildren.addChild(bgL);
                
                bgR = new LineRight();
                rawChildren.addChild(bgR);
                
                _swfwSetBtn.addEventListener(MouseEvent.CLICK, onSetWClick);
                _swfhSetBtn.addEventListener(MouseEvent.CLICK, onSetHClick);
                _awBtn.addEventListener(MouseEvent.CLICK, onAutoSizeWClick);
                _ahBtn.addEventListener(MouseEvent.CLICK, onAutoSizeHClick);
                _aBtn.addEventListener(MouseEvent.CLICK, onAutoSizeClick);
                _magicBtn.addEventListener(MouseEvent.CLICK, onMagicClick);
                _magicBackBtn.addEventListener(MouseEvent.CLICK, onMagicBackClick);
                _aboutBtn.addEventListener(MouseEvent.CLICK, onAboutClick);
                _aboutBackBtn.addEventListener(MouseEvent.CLICK, onAboutBackClick);
                
                SWFSize.SWF_ID = stage.loaderInfo.parameters.swfsizeId;
                swfSizer = SWFSize.getInstance();
                swfSizer.addEventListener(SWFSizeEvent.INIT, onWindowInit);
                swfSizer.addEventListener(SWFSizeEvent.SCROLL, onWindowScroll);
                swfSizer.addEventListener(SWFSizeEvent.RESIZE, onWindowResize);
                
                onStageResize(null);
            }
            
            ////////////////////////////////
            //
            //    PRIVATE
            //
            ////////////////////////////////
            
            /**
              * Get and show all metrics of the SWFSize
              * @return    nothing
              */
            private function getAllMetrics():void
            {
                // retrieve the current leftX value
                _leftxTF.text = String(swfSizer.leftX);
            
                // retrieve the current rightX value
                _rightxTF.text = String(swfSizer.rightX);
            
                // retrieve the current topY value
                _topyTF.text = String(swfSizer.topY);
            
                // retrieve the current bottomY value
                _botyTF.text = String(swfSizer.bottomY);
            
                // retrieve the current width value of the swf container
                var result:Object = swfSizer.getSWFWidth();
                _swfwTF.text = String(result.value);
                _swfwCbx.selected = result.absolute;
            
                // retrieve the current height value of the swf container
                result = swfSizer.getSWFHeight();
                _swfhTF.text = String(result.value);
                _swfhCbx.selected = result.absolute;
            
                // get the browser window width
                _winwTF.text = String(swfSizer.windowWidth);
            
                // get the browser window height 
                _winhTF.text = String(swfSizer.windowHeight);
            }
            
            /**
             * Update the positions of the sticky items
             * @return    nothing
              */
            private function updateStickyItemsPos():void
            {
                // make dude's x pos at center
                _dude.x = swfSizer.leftX + int((swfSizer.windowWidth - _dude.width) / 2);
            
                // make it always stick to the top-left corner
                //_infoPanel.x = swfSizer.leftX;
                TweenLite.to(_infoPanel, 1, {x: swfSizer.leftX, y: swfSizer.topY});
            
                // make it always stick to the top (center)
                bgT.x = swfSizer.leftX + int((swfSizer.windowWidth - bgT.width) / 2);
                bgT.y = swfSizer.topY;
                
                // make it always stick to the left (center)
                bgL.x = swfSizer.leftX;
                bgL.y = swfSizer.topY + int((swfSizer.windowHeight - bgL.height) / 2);
                
                // make it always stick to the right (center)
                bgR.x = swfSizer.rightX - bgR.width;
                bgR.y = swfSizer.topY + int((swfSizer.windowHeight - bgR.height) / 2);

                // make it always stick to the bottom (center)
                bgB.x = swfSizer.leftX + int((swfSizer.windowWidth - bgB.width) / 2);
                bgB.y = swfSizer.bottomY - bgB.height;
            }
        
            /**
             * Stage resize handler | just resizing the background
              * @param    event:Event    @default null
              */
            private function onStageResize(event:Event = null):void
            {
                _about.x = int((stage.stageWidth - _about.width)/2);
                _about.y = int(stage.stageHeight - _about.height - 15);
            }
            
            /**
             * On window init handler i.e. body.onload
              * @param    event:SWFSizeEvent
              */
            private function onWindowInit(event:SWFSizeEvent):void
            {
                getAllMetrics();
                updateStickyItemsPos();
            }
        
            /**
              * On window scroll handler
              * @param    event:SWFSizeEvent
              */
            private function onWindowScroll(event:SWFSizeEvent):void
            {
                getAllMetrics();
                updateStickyItemsPos();
            }
            
            /**
             * On window resize handler
              * @param    event:SWFSizeEvent
              */
            private function onWindowResize(event:SWFSizeEvent):void
            {
                getAllMetrics();
                updateStickyItemsPos();
                onStageResize();
            }
            
            /**
             * Show the about panel
             * @param    event:MouseEvent
              */
            private function onAboutClick(event:MouseEvent):void
            {
                TweenLite.to(swfSizer, 3, {scrollY: stage.stageHeight - swfSizer.windowHeight + 20 , ease:Bounce.easeOut});
            }
            
            /**
              * Move to top
              * @param    event:MouseEvent
              */
            private function onAboutBackClick(event:MouseEvent):void
            {
                TweenLite.to(swfSizer, 1, {scrollY: 0});
            }
        
            /**
              * The magic handler :)
             * @param    event:MouseEvent
             */
            private function onMagicClick(event:MouseEvent):void
            {    
                _dude.visible = true;
                _dude.alpha = 0;
                TweenLite.to(_dude, 1, {alpha: 1});
                TweenLite.to(swfSizer, 3, {scrollY: _dude.y - 200, ease:Back.easeOut});
            }
        
            /**
             * The 'more' magic handler :)
              * @param    event:MouseEvent
              */
            private function onMagicBackClick(event:MouseEvent):void
            {
                _dude.visible = true;
                _dude.alpha = 1;
                TweenLite.to(_dude, 1, {alpha: 0, onComplete: onDudeFadeOut});
                TweenLite.to(swfSizer, 2, {scrollY: 0, ease:Back.easeOut});
            }
        
            /**
              * Just to hide the dude
              * @return nothing
              */
            private function onDudeFadeOut():void
            {
                _dude.visible = false;
            }
        
            /**
             * On set width click handler
             * @param    event:MouseEvent
              */
            private function onSetWClick(event:MouseEvent):void
            {
                var value:Number = Number(_swfwTF.text);
            
                // set the width of the swf container according to the values
                // of the textfield and the checkbox
                swfSizer.setSWFWidth(value, _swfwCbx.selected);
            }
        
            /**
             * On set height click handler
             * @param    event:MouseEvent
              */
            private function onSetHClick(event:MouseEvent):void
            {
                var value:Number = Number(_swfhTF.text);
            
                // set the height of the swf container according to the values
                // of the textfield and the checkbox
                swfSizer.setSWFHeight(value, _swfhCbx.selected);
            }
            
            /**
              * On autoSize width click handler
              * @param    event:MouseEvent    @default null
              */
            private function onAutoSizeWClick(event:MouseEvent = null):void
            {
                // if the 'always' checkbox selected, set the autoSizeWidth with 'always' param
                // i.e. make the width elastic
                swfSizer.autoSizeWidth(_awCbx.selected);
                getAllMetrics();
            }
        
            /**
              * On autoSize height click handler
              * @param    event:MouseEvent    @default null
              */
            private function onAutoSizeHClick(event:MouseEvent = null):void
            {
                // if the 'always' checkbox selected, set the autoSizeHeight with 'always' param
                // i.e. make the height elastic
                swfSizer.autoSizeHeight(_ahCbx.selected);
                getAllMetrics();
            }
        
            /**
              * On autoSize click handler
              * @param    event:MouseEvent    @default null
              */
            private function onAutoSizeClick(event:MouseEvent = null):void
            {
                // if the 'always' checkbox selected, set the autoSize with 'always' param
                // i.e. make the width and height elastic
                swfSizer.autoSize(_aCbx.selected);
                getAllMetrics();
            }
        ]]>
    </mx:Script>
</mx:Application>