<?xml version="1.0" encoding="utf-8"?>
<mx:ApolloApplication
    xmlns:mx="http://www.adobe.com/2006/mxml"
    creationComplete="creationComplete()"
    backgroundColor="#FFFFFF"
    layout="absolute">
    
    <mx:Script>
        <![CDATA[
            
            import flash.filesystem.*;
            
            [Bindable]
            private var xml:XML = <wishlist></wishlist>;
            private var file:File = File.appStorageDirectory.resolve("wishlist.xml");
            private var fileStream:FileStream = new FileStream();
            
            
            private function creationComplete ():void {
                if (!this.file.exists || this.file.size == 0){
                    this.save();
                }
                this.load();
            }
            
            private function add ():void {
                var item:XML =
                    <item>
                        <title>{this.html.javaScriptDocument.title}</title>
                        <url>{this.html.location}</url>
                    </item>
                this.xml.appendChild(item);
            }
            
            private function goto (event:Event):void {
                this.html.location = this.xml.item[event.target.selectedIndex].url;
            }
            
            private function save ():void {
                this.createFile();
                // write file:
                this.fileStream.open(this.file, FileMode.WRITE);
                this.fileStream.writeUTFBytes('<?xml version="1.0" encoding="utf-8"?>\n'+this.xml.toXMLString());
                this.fileStream.close();
            }
            
            private function load ():void {
                // read file:
                this.fileStream.open(this.file, FileMode.READ);
                this.xml = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
                this.fileStream.close();
            }
            
            private function createFile ():void {
                if (this.file.exists){
                    return;
                }
                var tempFile:File = File.createTempFile();
                tempFile.copyTo(this.file, true);
            }
            
            
        ]]>
    </mx:Script>
    
    <mx:HDividedBox width="100%" height="100%" backgroundColor="#EEEEEE">
        <mx:Canvas height="100%" minWidth="200" backgroundColor="#FFFFFF">
            <mx:VBox width="100%">
                <mx:HBox width="100%" horizontalGap="5">
                    <mx:Button width="100%" label="Add this site to wishlist" click="add()" />
                    <mx:Button width="100%" label="Save wishlist" click="save()" />
                </mx:HBox>
                <mx:List width="100%" height="200" dataProvider="{this.xml.item}" change="goto(event)">
                    <mx:itemRenderer>
                        <mx:Component>
                            <mx:Label text="{data.title}" />
                        </mx:Component>
                    </mx:itemRenderer>
                </mx:List>
            </mx:VBox>
        </mx:Canvas>
        <mx:HTML id="html" width="100%" height="100%" location="http://www.google.com" />
    </mx:HDividedBox>
    
</mx:ApolloApplication>