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

SiteMapExample by Jamie McDaniel. December 8, 2008
Visit www.curiousfind.com/blog for more information.

This example adds functionality to the Flex tree component:
    
1. Nodes can be sorted alphabetically
2. Tree maintains its open state when its dataprovider is updated from the server

    
Copyright (c) 2008 Jamie McDaniel
 
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" layout="vertical" xmlns:components="components.*" initialize="onInitialize()" viewSourceURL="srcview/index.html">
    <mx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.managers.CursorManager;
            import mx.utils.ObjectUtil;
            import mx.utils.ArrayUtil;
            import flash.utils.Timer;
            import flash.events.TimerEvent;
            
            [Bindable]
            private var myArrayCollection:ArrayCollection;

            private var siteMapObjectFromServer:Object = 
                    {articleID: "1", title: "Home", children: [
                        {articleID: "2", title: "About Us" },
                        {articleID: "3", title: "Store Locations", children: [
                            {articleID: "9", title: "Kentucky", children: [
                                {articleID: "10", title: "Paducah" },
                                {articleID: "11", title: "Lexington" },
                                {articleID: "12", title: "Louisville" },
                            ]},
                            {articleID: "13", title: "Indiana", children: [
                                {articleID: "14", title: "Indianapolis" },
                                {articleID: "15", title: "Evansville" },
                            ]},
                            {articleID: "16", title: "Ohio", children: [
                                {articleID: "17", title: "Columbus" },
                                {articleID: "18", title: "Cincinnati" },
                            ]},                                                                            
                        ]},
                        {articleID: "4", title: "Products", children: [
                            {articleID: "5", title: "Product 3" },
                            {articleID: "6", title: "Product 2" },
                            {articleID: "19", title: "Product 1" },
                            {articleID: "20", title: "Product 4" },
                        ]},
                        {articleID: "7", title: "Services" },
                        {articleID: "8", title: "Contact Us" }                        
                    ]};
                                
            private function onInitialize():void
            {
                getDataFromServer();    
            }
            
            // this is to simulate a call to the server
            private function getDataFromServer():void
            {
                CursorManager.setBusyCursor();
                var myTimer:Timer = new Timer(500, 1);
                myTimer.addEventListener("timer", onTimer);
                myTimer.start();
            }
            
            private function onTimer(event:TimerEvent):void
            {
                CursorManager.removeBusyCursor();    
                // using the copy method to simulate new, unsorted data from the server
                myArrayCollection = new ArrayCollection(ArrayUtil.toArray(ObjectUtil.copy(siteMapObjectFromServer)));
            }

            private function onRefreshButtonClick():void
            {
                getDataFromServer();                
            }
        ]]>
    </mx:Script>
    <mx:HBox width="100%" paddingTop="5" paddingRight="5">
        <mx:Button id="btRefreshTree" label="Refresh" icon="@Embed(source='/assets/arrow_refresh.png')" right="10" bottom="10" click="onRefreshButtonClick()"/>
        <mx:CheckBox id="cbSort" label="Sort" />
        <mx:CheckBox id="cbRememberOpenState" label="Remember Open State" />
    </mx:HBox>        
    <components:SiteMap id="mySiteMap" siteMapIDField="articleID" siteMapLabelField="title" dataProvider="{myArrayCollection}" sortItems="{cbSort.selected}" rememberOpenState="{cbRememberOpenState.selected}" />    
</mx:Application>