<?xml version="1.0" encoding="utf-8"?>
<!--

    Copyright (c) 2007 Josh Tynjala

    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, 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
    AUTHORS OR 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.

-->
<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:business="com.joshtynjala.search.business.*"
    xmlns:controller="com.joshtynjala.search.controller.*"
    layout="absolute" viewSourceURL="srcview/index.html">
    
    <!-- declare the business services and the controller -->
    <business:Services id="yahooServices"/>
    <controller:SearchController id="controller"/>
    
    <!-- since we have one basic view, I'm just going to put it directly
        in the main application. More complex apps should put the views in
        the "views" directory. -->
    
    <mx:Label text="Yahoo! Search Cairngorm Example" fontSize="16" color="#ffffff" x="24" y="25"/>
    <mx:Label x="24" y="46" text="by Josh Tynjala"/>
    
    <mx:HBox x="24" y="72">
        <mx:Label text="Search:"/>
        <mx:TextInput id="query" width="300"/>
    </mx:HBox>
    <mx:Button label="Web" click="searchWeb(true)" x="383" y="72"/>
    <mx:Button label="Images" click="searchImages(true)" x="437" y="72"/>
    
    <!-- these states correpond to the states defined in the model -->
    
    <mx:states>
    
        <mx:State name="searching">
            <mx:AddChild>
                <mx:ProgressBar indeterminate="true" x="179" y="126"/>
            </mx:AddChild>
        </mx:State>
    
        <mx:State name="webResults">
            <mx:AddChild>
                <mx:List dataProvider="{model.results}" x="24" y="102" width="483" height="250" variableRowHeight="true" selectable="false">
                    <mx:itemRenderer>
                        <mx:Component>
                            <mx:Text htmlText="{formatData(data)}">
                                <mx:Script>
                                    <![CDATA[
                                        
                                    private function formatData(data:Object):String
                                    {
                                        return '<p><font size="13" color="#0000ff"><u><a href="' + data.url + '">' + data.name + '</a></u></font></p><p>' + data.summary + '</p>';
                                    }
                                    
                                    ]]>
                                </mx:Script>
                            </mx:Text>
                        </mx:Component>
                    </mx:itemRenderer>
                </mx:List>
            </mx:AddChild>
            
            <mx:AddChild>
                <mx:HBox x="370" y="354">
                    <mx:Button label="Previous" enabled="{page > 0}" click="searchWeb(false, true)"/>
                    <mx:Button label="Next" click="searchWeb()"/>
                </mx:HBox>
            </mx:AddChild>
        </mx:State>
        
        <mx:State name="imageResults">
            <mx:AddChild>
                <mx:TileList x="24" y="102" width="483" height="250" dataProvider="{model.results}" rowHeight="100" columnWidth="100">
                    <mx:itemRenderer>
                        <mx:Component>
                            <mx:Image source="{data.url}"/>
                        </mx:Component>
                    </mx:itemRenderer>
                </mx:TileList>
            </mx:AddChild>
            <mx:AddChild>
                <mx:HBox x="370" y="354">
                    <mx:Button label="Previous" enabled="{page > 0}" click="searchImages(false, true)"/>
                    <mx:Button label="Next" click="searchImages()"/>
                </mx:HBox>
            </mx:AddChild>
        </mx:State>
        
        <mx:State name="error">
            <mx:AddChild>
                <mx:Label text="Search failed. Please try again." x="173" y="115" color="#660000" fontWeight="bold" fontSize="14"/>
            </mx:AddChild>
        </mx:State>
        
    </mx:states>
    
    <!-- connect the view states to the application states -->
    <mx:Binding source="{model.searchState}" destination="updateSearchState" />
    
    <mx:Script>
        <![CDATA[
            import com.yahoo.webapis.search.WebSearchResult;
            import com.adobe.cairngorm.control.CairngormEventDispatcher;
            import com.joshtynjala.search.model.ModelLocator;
            import com.joshtynjala.search.events.SearchEvent;
            
            private var model:ModelLocator = ModelLocator.getInstance();
            
            [Bindable]
            private var page:int = -1;
            
            public function set updateSearchState(value:String):void
            {
                switch(value)
                {
                    case ModelLocator.STATE_WEB_RESULTS:
                        this.currentState = "webResults";
                        break;
                    case ModelLocator.STATE_IMAGE_RESULTS:
                        this.currentState = "imageResults"
                        break;
                    case ModelLocator.STATE_SEARCHING:
                        this.currentState = "searching";
                        break;
                    case ModelLocator.STATE_ERROR:
                        this.currentState = "error";
                        break;
                    default:
                        this.currentState = "";
                }
            }
            
            private function searchWeb(reset:Boolean = false, backwards:Boolean = false):void
            {
                if(reset) page = 0;
                else if(backwards) page--;
                else page++
                
                var event:SearchEvent = new SearchEvent(SearchEvent.SEARCH_WEB, this.query.text, page);
                CairngormEventDispatcher.getInstance().dispatchEvent(event);
            }
            
            private function searchImages(reset:Boolean = false, backwards:Boolean = false):void
            {
                if(reset) page = 0;
                else if(backwards) page--;
                else page++
                
                var event:SearchEvent = new SearchEvent(SearchEvent.SEARCH_IMAGES, this.query.text, page);
                CairngormEventDispatcher.getInstance().dispatchEvent(event);
            }
        ]]>
    </mx:Script>
    
</mx:Application>