/*
Copyright (c) 2010 Mark Bartel

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the 
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, distribute with modifications, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is 
furnished to do so, subject to the following conditions: 

The above copyright notice and this permission notice shall be included 
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR 
THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name(s) of the above copyright
holders shall not be used in advertising or otherwise to promote the
sale, use or other dealings in this Software without prior written 
authorization.
*/
package
{
    import ca.markbartel.example.ApplicationDomainExampleLoader;
    
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.geom.Rectangle;
    import flash.system.ApplicationDomain;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFieldType;
    
    public class ApplicationDomainExample extends Sprite
    {
        
        public function ApplicationDomainExample()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            
            graphics.lineStyle(1, 0);
            drawLegend();

            // "top-level" ApplicationDomains
            var ad1:ApplicationDomain = new ApplicationDomain();
            var ad2:ApplicationDomain = new ApplicationDomain();

            // children of the "top-level" ApplicationDomains
            var ad11:ApplicationDomain = new ApplicationDomain(ad1);
            var ad12:ApplicationDomain = new ApplicationDomain(ad1);
            var ad13:ApplicationDomain = new ApplicationDomain(ad1);
            var ad21:ApplicationDomain = new ApplicationDomain(ad2);
            var ad22:ApplicationDomain = new ApplicationDomain(ad2);
            
            // children of the children
            var ad111:ApplicationDomain = new ApplicationDomain(ad11);
            var ad112:ApplicationDomain = new ApplicationDomain(ad11);
            var ad131:ApplicationDomain = new ApplicationDomain(ad13);
            var ad132:ApplicationDomain = new ApplicationDomain(ad13);
            var ad211:ApplicationDomain = new ApplicationDomain(ad21);
            var ad212:ApplicationDomain = new ApplicationDomain(ad21);
            var ad221:ApplicationDomain = new ApplicationDomain(ad22);
            var ad222:ApplicationDomain = new ApplicationDomain(ad22);
            
            var log:TextField = new TextField();
            log.x = 0;
            log.y = 190;
            log.width = 400;
            log.height = 50;
            addChild(log);

            var node0:ApplicationDomainExampleLoader = addNode(null, new ApplicationDomainExampleLoader(197, 5, 50, 25, null, "system", log));
            
            var node1:ApplicationDomainExampleLoader = addNode(node0, new ApplicationDomainExampleLoader( 87, 55, 50, 25, ad1, "1", log));
            var node2:ApplicationDomainExampleLoader = addNode(node0, new ApplicationDomainExampleLoader(307, 55, 50, 25, ad2, "2", log));

            var node11:ApplicationDomainExampleLoader = addNode(node1, new ApplicationDomainExampleLoader( 32, 105, 50, 25, ad11, "11", log));
            var node12:ApplicationDomainExampleLoader = addNode(node1, new ApplicationDomainExampleLoader( 87, 105, 50, 25, ad12, "12", log));
            var node13:ApplicationDomainExampleLoader = addNode(node1, new ApplicationDomainExampleLoader(142, 105, 50, 25, ad13, "13", log));
            var node21:ApplicationDomainExampleLoader = addNode(node2, new ApplicationDomainExampleLoader(252, 105, 50, 25, ad21, "21", log));
            var node22:ApplicationDomainExampleLoader = addNode(node2, new ApplicationDomainExampleLoader(362, 105, 50, 25, ad22, "22", log));

            addNode(node11, new ApplicationDomainExampleLoader(  5, 155, 50, 25, ad111, "111", log));
            addNode(node11, new ApplicationDomainExampleLoader( 60, 155, 50, 25, ad112, "112", log));
            addNode(node13, new ApplicationDomainExampleLoader(115, 155, 50, 25, ad131, "131", log));
            addNode(node13, new ApplicationDomainExampleLoader(170, 155, 50, 25, ad132, "132", log));
            addNode(node21, new ApplicationDomainExampleLoader(225, 155, 50, 25, ad211, "211", log));
            addNode(node21, new ApplicationDomainExampleLoader(280, 155, 50, 25, ad212, "212", log));
            addNode(node22, new ApplicationDomainExampleLoader(335, 155, 50, 25, ad221, "221", log));
            addNode(node22, new ApplicationDomainExampleLoader(390, 155, 50, 25, ad222, "222", log));
            
        }
        
        private function drawLegend():void
        {
            const lhs:int = 399;
            const entryLhs:int = lhs + 5;
            graphics.lineStyle(1, 0x888888);
            graphics.beginFill(0xdddddd);
            graphics.drawRect(lhs, 0, 100, 97);
            graphics.endFill();
            drawLegendEntry(0xff0000, entryLhs,  5, "ellipse-r.swf");
            drawLegendEntry(0x00ff00, entryLhs, 20, "ellipse-g.swf");
            drawLegendEntry(0x0000ff, entryLhs, 35, "ellipse-b.swf");
            drawLegendEntry(0x00ffff, entryLhs, 50, "ellipse-c.swf");
            drawLegendEntry(0xff00ff, entryLhs, 65, "ellipse-m.swf");
            drawLegendEntry(0xffff00, entryLhs, 80, "ellipse-y.swf");
        }
        
        private function drawLegendEntry(colour:int, x:int, y:int, swf:String):void
        {
            graphics.beginFill(colour);
            graphics.drawEllipse(x, y, 17, 13);
            graphics.endFill();
            var text:TextField = new TextField();
            text.autoSize = TextFieldAutoSize.LEFT;
            text.text = swf;
            text.x = x + 20;
            text.y = y + 7 - int(text.height / 2);
            addChild(text);
        }
        
        private function addNode(parentNode:ApplicationDomainExampleLoader, node:ApplicationDomainExampleLoader):ApplicationDomainExampleLoader
        {
            addChild(node);
            if (parentNode != null)
            {
                graphics.moveTo(parentNode.x + parentNode.width/2, parentNode.y + parentNode.height/2);
                graphics.lineTo(node.x + node.width/2, node.y + node.height/2);
            }
            return node;
        }
    }
}