<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    horizontalAlign="center" 
    verticalAlign="middle" 
    width="100%" 
    height="100%" 
    frameRate="30"
    applicationComplete="initApp()"
    xmlns:xray="com.blitzagency.xray.logger.*"
    layout="absolute" 
    xmlns:ns1="*" viewSourceURL="srcview/index.html">
    <mx:Style source="styles.css" />
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import com.blitzagency.util.LSOUserPreferences;
            import com.blitzagency.xray.logger.XrayLog;
            import com.blitzagency.xray.logger.XrayLogger;
            import mx.core.ScrollPolicy; 
            import mx.collections.ArrayCollection;

            import com.blitzagency.xray.ui.OutputTools;
            import mx.controls.List;
            
            private var nc:NetConnection;
            private var ns:NetStream;
            
            private var log:XrayLog;

            [Bindable]
            public var videoList:ArrayCollection;
            
            public function initApp():void
            {                
                Security.allowDomain("*");
                // setup Xray's logger and create the log instance
                log = new XrayLog();
                XrayLogger.getInstance().registerStage(stage);
                
                // add listener for stage resize
                addEventListener("resize", handleResize);
                
                // Local SharedObject
                LSOUserPreferences.load("FLVPlayerPrefs");
                
                // set the path in the text field to oflaDemo by default
                givenPath.text = "rtmp://localhost/oflaDemo";
                
                //  create the netConnection
                nc = new NetConnection();
                //  set it's client/focus to this
                nc.client = this;
                
                // add listeners for netstatus and security issues
                nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
                nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
                
                // set the encoding to AMF0 - still waiting for AMF3 to be implemented on Red5
                nc.objectEncoding = ObjectEncoding.AMF0;
            }
            
            public function doConnection():void
            {
                // do the connection - called by the Connect Button's click event
                nc.connect(givenPath.text);
            }
            
            public function onBWDone():void
            {
                // have to have this for an RTMP connection
            }
            
            private function netStatusHandler(event:NetStatusEvent):void 
            {
                log.debug("netStatus", event.info.code);
                if(event.info.code == "NetConnection.Connect.Success")
                {
                    // we can't use the connection until we get this flag back
                    // register it with the previewWindow
                    log.debug("connected", nc.connected);
                    previewWindow.registerConnection(nc);
                    // get the list of videos
                    catchVideos();
                }
            }
            
            public function catchVideos():void
            {
                // call server-side method
                // create a responder and set it to getMediaList
                var nc_responder:Responder = new Responder(getMediaList, null);
                // call the server side method to get list of FLV's
                nc.call("demoService.getListOfAvailableFLVs", nc_responder);
            }
            
            public function getMediaList (list:Object):void 
            {
                // this is the result of the server side getListOfAvailableFLVs
                var mediaList:Array = new Array();
                for(var items:String in list) 
                {
                    log.debug("items", list[items]);
                    mediaList.push({label:items, size:list[items].size, dateModified:list[items].lastModified});
                }
                
                // videoList is bindable and the datagrid is set to use this for it's dataprovider
                // wrap it in an ArrayCollection first
                videoList = new ArrayCollection(mediaList);
            }
            
            private function securityErrorHandler(e:SecurityErrorEvent):void
            {
                log.debug("nc error", e.text);
            }
            
            private function handleResize(e:Event):void
            {
                // this helps with the redraw of the datagrid
                videoListContainer.invalidateDisplayList();
            }
            
            public function changePreview(event:Event):void
            {
                // when the datagrid is clicked, pass the selected item on to the previewWindow
                previewWindow.setPath(videoListContainer.selectedItem.label);
            }
        ]]>
    </mx:Script>
    <mx:Button label="Connect" click="doConnection()" toolTip="Connect to Red5" width="65" right="5" top="5">
    </mx:Button>
        <ns1:PreviewWindow id="previewWindow" right="5" bottom="110" top="30" left="5">    
        </ns1:PreviewWindow>
        <mx:Panel right="5" bottom="5" layout="absolute" title="Current Videos" horizontalScrollPolicy="off" verticalScrollPolicy="off" left="5" height="100">
            <mx:DataGrid id="videoListContainer" height="100%" width="100%" dataProvider="{videoList}" change="changePreview(event)">
                <mx:columns>
                    <mx:DataGridColumn dataField="label" headerText="File"/>
                    <mx:DataGridColumn dataField="size" headerText="Size"/>
                    <mx:DataGridColumn dataField="dateModified" headerText="Modified Date"/>
                </mx:columns>
            </mx:DataGrid>
        </mx:Panel>
    <mx:TextInput right="78" left="5" id="givenPath" top="5"/>
</mx:Application>