<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="absolute">
    
    <mx:Script>
        <![CDATA[
            import mx.rpc.events.ResultEvent;
            
            public function getPipe():void
            {
                currentState = "searching";
                pipesService.send();
            }
            
            public function pipeRetrieved(event:ResultEvent):void
            {
                currentState='resultsRetrieved'
            }
        ]]>
    </mx:Script>
    
    <mx:states>
        <mx:State name="resultsRetrieved">
            <mx:AddChild position="lastChild">
                <!-- We're expecting RSS, so we bind to channel.item, essentially an XMLList.  -->
                <mx:List 
                    dataProvider="{pipesService.lastResult.channel.item}" 
                    left="10" 
                    right="10" 
                    top="64" 
                    bottom="10">
                    <mx:itemRenderer>
                        <mx:Component>
                            <mx:HBox 
                                width="100%" 
                                height="100%"
                                toolTip="{data.link}" 
                                mouseOver="linkButton.visible = true;" 
                                mouseOut="linkButton.visible = false;">
                                <mx:LinkButton id="linkButton" 
                                    visible="false" 
                                    icon="@Embed('assets/external_link.png')" 
                                    click="navigateToURL(new URLRequest(data.link))"/>
                                <mx:Label
                                    width="100%" 
                                    height="100%"
                                    htmlText="{data.title}" 
                                    doubleClickEnabled="true" 
                                    doubleClick="navigateToURL(new URLRequest(data.link))"/>
                            </mx:HBox>
                        </mx:Component>
                    </mx:itemRenderer>  
                </mx:List>
            </mx:AddChild>
        </mx:State>
        <mx:State name="searching">
            <mx:AddChild position="lastChild">
                <mx:Image 
                    horizontalCenter="0" 
                    verticalCenter="0" 
                    source="assets/cog.swf" 
                    alpha="0.6"/>
            </mx:AddChild>
            <mx:SetProperty 
                target="{criteriaTextInput}" 
                name="enabled" 
                value="false"/>
            <mx:SetProperty 
                target="{searchButton}" 
                name="enabled" 
                value="false"/>
        </mx:State>
    </mx:states>
    
    <mx:Label 
        text="Aggregated News Alerts" 
        fontWeight="bold" 
        top="10" 
        left="10"/>
        
    <mx:TextInput id="criteriaTextInput"
        text="Yahoo" 
        right="55" 
        top="6"
        enter="getPipe()" />
        
    <mx:Label 
        right="223"
        top="8"
        text="Find Stories About:" />
        
    <mx:Button id="searchButton" 
        label="Go" 
        right="10" 
        top="6"
        click="getPipe()"/>
    
    <!-- 
        Here's the service that connects to the Pipes RSS feed
        Notice I've replaced ampersands with the entity "&amp;" to prevent issues with MXML. 
    -->
    <mx:HTTPService id="pipesService" 
        resultFormat="e4x" 
        url="http://pipes.yahooapis.com/pipes/pipe.run?_id=fELaGmGz2xGtBTC3qe5lkA&amp;_render=rss&amp;textinput1={criteriaTextInput.text}"
        result="pipeRetrieved(event)"/>

</mx:Application>