<?xml version="1.0" encoding="utf-8"?>
<!--
    Author: Richard Bezemer
    Date: October 9, 2008
    released under the Creative Commons Attribution 3.0 Unported License
    From CreativeCommons.org"
    "This license lets others distribute, remix, tweak, and build upon your work,
     even commercially, as long as they credit you for the original creation. 
     This is the most accommodating of licenses offered, in terms of what 
     others can do with your works licensed under Attribution."

-->
<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:containers="com.dougmccune.containers.*"
    layout="absolute" 
    width="400" 
    height="300"
    creationComplete="init()"  viewSourceURL="srcview/index.html">
    <mx:Script>
        <![CDATA[
            import mx.managers.CursorManager;
        import com.adobe.webapis.flickr.Photo;
        import mx.containers.Canvas;
        import mx.controls.Image;
        import mx.binding.utils.ChangeWatcher;
        
        import com.photobucket.webapi.service.PhotobucketService;
        import com.photobucket.webapi.objects.Album;
        import com.photobucket.webapi.objects.Media;
        import com.photobucket.webapi.interfaces.IAlbum;
        import com.photobucket.webapi.interfaces.IUser;
        import com.photobucket.webapi.objects.User;
        import com.photobucket.webapi.objects.Login;
        import com.photobucket.webapi.interfaces.*;
        import com.photobucket.webapi.objects.Search;
        
        private var pbLogin:Login;
        private var _pbAlbum:Album;
        
        [Embed(source="cc88x31.png")]
        [Bindable] private var ccLicenseImage:Class
         
        protected function init():void{
            //Log in to photobucket with our public and private key
            //Hidden away so they won't get abused
            pbLogin = new Login();
               pbLogin.setConsumer(PhotobucketKeys.CONSUMER, PhotobucketKeys.PRIVATE);
               
        }
        protected function pbFindAlbumsForUser(user:String):void{
            //Ask the Photobucket service to find us all albums
            // for a given photobucket username
            _pbAlbum = PhotobucketService.getInstance().albumFactory(user) as Album;
            /** 
             * This part of the API is kind of awkward. a better solution would be
             * to modify the photobucket api but I don't lik doing that for tutorials.
             * basically when you first call images it gives you back a dataprovider,
             * which works well when giving the dp to a list with a custom item rendereer
             * unfortunatly the api swaps out that dp when it assigns the first item to it
             * so we need to what when we get the imageChanged event from the api,
             * then listen for the events from the new dataprovider assigned to images.
             * confused? me to, best way to find out what I'm talking about is to 
             * download the photobucket as3 api code and set through these calls.
             **/
            _pbAlbum.images;
            ChangeWatcher.watch( _pbAlbum, "images",pbAlbumsFound);
            //Fill our combobox with all the subAlbums available
            subAlbums.dataProvider = _pbAlbum.sub_albums;
            this.subAlbums.labelField = "name";    
               
        }
        protected function pbAlbumsFound(e:Event):void {
            //Now that the dp has been replaced, listen for when the new items are added
            ChangeWatcher.watch( _pbAlbum.images, "length",pbAlbumsFound2);
        }
        protected function pbAlbumsFound2(e:Event):void {
            //limit the number of pictures displayed in the coverflow.
            CursorManager.removeBusyCursor();
            if(coverflow.numChildren<10){
                addImage(_pbAlbum.images.getItemAt(_pbAlbum.images.length-1).url);
            }
            
        }
        protected function addImage(url:String):void{
            var cvs:Canvas = new Canvas();
            cvs.width = 201;
            cvs.height = 201;
            cvs.verticalScrollPolicy = "off";
            cvs.horizontalScrollPolicy = "off";
            var img:Image = new Image();
            img.width = 200;
            img.height = 200;
            //Use the proxy from the previous post so we don't get errors
            //no crossdomain file for photobucket yet...
            img.load(PhotobucketKeys.MY_PROXY+escape(url));
            img.scaleContent = true;
            cvs.addChild(img);
            coverflow.addChild(cvs);
        }
            
        protected function userNameEntered(e:Event):void
        {
            CursorManager.setBusyCursor();
            pbFindAlbumsForUser(pbUserName.text);
        }
        protected function albumChanged(e:Event):void
        {
            //the a new combobox entry is selected, display that album to the user
            _pbAlbum = subAlbums.selectedItem as Album;
            coverflow.removeAllChildren();
            _pbAlbum.images;
            ChangeWatcher.watch( _pbAlbum, "images",pbAlbumsFound);
                        
        }
        protected function launchCCLicense(e:Event):void{
            var licenseURL:URLRequest = new URLRequest( "http://creativecommons.org/licenses/by/3.0/" );
               navigateToURL(licenseURL, "_blank");
        }
    
        ]]>
    </mx:Script>
    <mx:VBox width="400"
             height="300"
             horizontalAlign="center">
        <containers:CoverFlowContainer id="coverflow" width="400" height="200" 
                    horizontalGap="40" borderStyle="inset" backgroundColor="0x000000"
                    segments="6" reflectionEnabled="true"/>
        <mx:HBox verticalAlign="bottom">
            <mx:Form 
                
                paddingTop="2"
                paddingBottom="2"
                paddingLeft="2"
                paddingRight="2"
                >            
                <mx:FormItem label="UserName :"> 
                    <mx:TextInput id="pbUserName" enter="userNameEntered(event)"/>
                    <mx:Button 
                        label="Load Album"
                        click="userNameEntered(event)"/>
                </mx:FormItem>
                <mx:FormItem label="Albums :"> 
                    <mx:ComboBox id="subAlbums" change="albumChanged(event)"/>
                </mx:FormItem>
            </mx:Form>
            <mx:VBox>
                <mx:Image 
                    source="{ccLicenseImage}"
                    click="launchCCLicense(event)"
                    />
            </mx:VBox>
        </mx:HBox>
        
    </mx:VBox>
    
</mx:Application>