<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="absolute"
    themeColor="HaloSilver"
    backgroundGradientAlphas="[1.0, 1.0]" 
    backgroundGradientColors="[#FFFFFF, #FFFFFF]" 
    viewSourceURL="srcview/index.html">
     
    <mx:Script>
        <![CDATA[
            import flash.net.navigateToURL;
            
            [Bindable]
            public var shuffleTime:Date;
            
            import mx.core.Container;
            
            public function addCard( cardId:String ):void
            {
                var cardContainer:Container = new Container();
                var cardClip:MovieClip = MovieClip( loadedSWF.content ).getInstance( cardId )
                cardClip.width = 50;
                cardClip.height = 75;
                cardClip.x = 2;
                cardClip.y = 2;
                cardClip.filters = [ new DropShadowFilter( 2 , 45 , 0 , .5 ) ];
                cardContainer.rawChildren.addChild( cardClip );
                cardContainer.width = 54;
                cardContainer.height = 79;
                cards.addChild( cardContainer );
            }
            
            public function shuffleServiceResult( data:Array ):void
            {
                var rawDate:String = data.shift();
                shuffleTime = new Date( Number( rawDate ) );
                for( var i:String in data )
                {
                    addCard( data[i] );
                }        
                shufflebu.enabled = true;
                decks.enabled = true;
                jokers.enabled = true;
            }
            
            public function shuffleDecks():void
            {
                shufflebu.enabled = false;
                decks.enabled = false;
                jokers.enabled = false;
                cards.removeAllChildren();
                shuffleService.send();
            }
            
        ]]>
    </mx:Script> 
    
    <mx:HTTPService 
        id="shuffleService" 
        resultFormat="text"
        result="shuffleServiceResult( String( event.result ).split('|') )"
        url="{ 'http://onflex.org/api/games/cards/shuffle/1/' + decks.value + '/' + jokers.value  }"/>
    
    <mx:Button 
        id="shufflebu"
        enabled="false"
        click="shuffleDecks()" 
        label="Load Cards from Shuffle API" 
        top="10" left="213" width="198"/>
    
    <mx:Button 
        enabled="true"
        click="flash.net.navigateToURL( new URLRequest( shuffleService.url ) , '_new' )" 
        label="Open Shuffle API URL" 
        top="10" left="423" width="161"/>
    
    <mx:SWFLoader 
        id="loadedSWF" 
        source="cards.swf"
        width="0" height="0" visible="false"
        complete="shufflebu.enabled = true" 
        />
        
    <mx:Tile 
        id="cards"
        horizontalGap="2"
        verticalGap="2" 
        top="38" left="10" right="10" bottom="10"/>
    <mx:NumericStepper x="57" y="10" minimum="1" maximum="10" stepSize="1" enabled="true" width="43" id="decks" value="1" toolTip="# of Decks"/>
    <mx:NumericStepper x="157" y="10" minimum="0" maximum="4" stepSize="1" enabled="true" width="43" id="jokers" value="0" toolTip="# of Jokers per Deck"/>
    <mx:Label x="16" y="12" text="Decks" toolTip="# of Decks"/>
    <mx:Label x="112" y="12" text="Jokers" toolTip="# of Jokers per Deck"/>
    
</mx:Application>