<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="absolute" creationComplete="init()" xmlns:local="*" currentState="Login" 
    xmlns:fms="pfms.fms.components.connect.*" 
    xmlns:simpleSharedObjectEditor="pfms.samples.simpleSharedObjectEditor.*"
    xmlns:sampleApplication2="pfms.samples.sample2.*" 
    xmlns:sampleApplication3="pfms.samples.sample3.*" 
    xmlns:sharedBall="pfms.samples.sharedBall.*" viewSourceURL="srcview/index.html" xmlns:passingClasses="pfms.samples.passingClasses.*" xmlns:textChat="pfms.samples.textChat.*" xmlns:plaintextChat="pfms.samples.plaintextChat.*" xmlns:minimalSample="pfms.samples.minimalSample.*" xmlns:peopleList="pfms.samples.peopleList.*" xmlns:simpleLiveStream="pfms.samples.simpleLiveStream.*" xmlns:controlledLiveStream="pfms.samples.controlledLiveStream.*" xmlns:simpleVideoConference="pfms.samples.simpleVideoConference.*" xmlns:sharedText="pfms.samples.sharedText.*" xmlns:scatchPad="pfms.samples.scatchPad.*">
    <mx:Style source="css/main.css" />
    <mx:states>
        <mx:State name="Login">
            <mx:AddChild position="lastChild">
                <mx:VBox horizontalCenter="0" verticalCenter="-20" >
                    <fms:SimpleConnectForm id="simpleConnectForm" connectionRequest="connect(event)" >
                    </fms:SimpleConnectForm>
                    <mx:CheckBox id="rejectConnectionComboBox" label="Force server to reject connection attempts" />
                    <mx:CheckBox id="AMF3Encoding" label="Encode in AMF3" change="changeEncoding()"/>
                </mx:VBox >
            </mx:AddChild>        
        </mx:State>
        <mx:State name="Explore" enterState="userNameLabel.text = 'Welcome: ' + user.userName">
            <mx:AddChild position="lastChild">
                <mx:VBox width="100%" height="100%">
                    <mx:ApplicationControlBar width="100%" height="40">
                        <mx:Label text="Flex 2 FMS Explorer" styleName="applicationControlBarControl" />
                        <mx:Spacer width="100%"/>
                        <mx:Label id="userNameLabel" styleName="applicationControlBarControl"/>
                        <mx:Spacer width="100%"/>
                        <mx:Button  id="logoutButton" label="Logout" fontSize="12" styleName="applicationControlBarControl" click="disconnect(event)" fillAlphas="[0.19, 0.0]"/>
                    </mx:ApplicationControlBar>
                    <mx:HBox width="100%" height="100%">
                        <mx:VBox backgroundColor="666666" backgroundAlpha=".2"  width="200" height="100%" paddingTop="6" paddingRight="6" paddingLeft="6">
                            <mx:ToggleButtonBar verticalGap="2" direction="vertical" dataProvider="{stack}" width="100%"/>
                        </mx:VBox>
                        
                        <mx:VBox  width="100%" height="100%">
                            <mx:ViewStack id="stack" change="changeViewItem(event)" width="100%" height="100%">
                                <peopleList:PeopleList/>
                                <simpleLiveStream:SimpleLiveStream/>
                                <controlledLiveStream:ControlledLiveStream/>
                                <simpleVideoConference:SimpleVideoConference/>
                                <simpleSharedObjectEditor:SimpleSharedObjectEditor/>
                                <sharedBall:SharedBallPanel/>
                                <scatchPad:ScratchPad/>
                                <plaintextChat:PlainTextChat/>
                                <sharedText:SharedText/>
                                <minimalSample:MinimalSample/>
                            </mx:ViewStack>
                        </mx:VBox>
                    </mx:HBox>
                </mx:VBox>
            </mx:AddChild>
        </mx:State>
    </mx:states>

    
<mx:Script>
    <![CDATA[

    import mx.managers.CursorManager;    
    import mx.controls.Alert;
    import mx.events.IndexChangedEvent;
    import flash.events.NetStatusEvent;
    import flash.net.*;
    import pfms.fms.FMSConnection;
    import pfms.fms.User;
    import pfms.fms.components.connect.ConnectionRequest;
    import pfms.samples.ISample;
    
    public var fmsConnection:FMSConnection;
    public var user:User;
    private var _flex2FMSExplorerReference:Object = this;
    
    // the _connectionClient object will be assigned to the netconnection.client
    // in order to have remote method calls called on it. 
    // The _connectionClient object is dynamic so methods can be added to it
    // at any time. 
    //
    // It starts off with one remote method defined on it: 
    //     initClientApplication 
    // It's initClientApplication method just calls the flex2FMSExplorer's 
    // initClientMethod.
    //
    // Any component can get an reference to this object this way:
    // var client:Object = netConnection.client.
    // 
    private var _connectionClient:Object = {
        initClientApplication: function(userInfo:Object):void{
            _flex2FMSExplorerReference.initClientApplication(userInfo);
        }
    };
    
    public function changeEncoding():void{
        if (AMF3Encoding.selected){
            NetConnection.defaultObjectEncoding = ObjectEncoding.AMF3;
            SharedObject.defaultObjectEncoding = ObjectEncoding.AMF3;
        }
        else {
            NetConnection.defaultObjectEncoding = ObjectEncoding.AMF0;
            SharedObject.defaultObjectEncoding = ObjectEncoding.AMF0;
        }
    }
        
    public function init():void{ 
        //_flex2FMSExplorerReference = this;
        NetConnection.defaultObjectEncoding = ObjectEncoding.AMF0;
        SharedObject.defaultObjectEncoding = ObjectEncoding.AMF0;
    }
    
    public function connectResult(event:NetStatusEvent):void{
        trace("connectResult>");
    }
    
    public function initClientApplication(userInfo:Object):void{
        trace("initClientApplication>");
        user = new User(userInfo);
        simpleConnectForm.enabled = true;
        CursorManager.removeBusyCursor();
        currentState = "Explore";
        var currentSampleApp:ISample = stack.selectedChild as ISample;
        currentSampleApp.user = user;
        currentSampleApp.netConnection = fmsConnection;
    }

    public function connectFail(event:NetStatusEvent):void{
        var errorMsg:String = "Can't connect to FMS!\n" + event.info.description;
        if (event.info.application){
            errorMsg += "\n" + event.info.application.msg;
        }
        Alert.show(errorMsg, "Connection Error!");
        simpleConnectForm.enabled = true;
        CursorManager.removeBusyCursor();
    }
    
    public function connect(event:ConnectionRequest):void{
        // Since we might channge encoding get rid of the last fmsConnection
        // object if we have one:
        if (fmsConnection){
            fmsConnection.removeEventListener("connectResult",    connectResult);
            fmsConnection.removeEventListener("connectFail",      connectFail);
            fmsConnection.removeEventListener("disconnectResult", disconnectResult);
        }
        
        fmsConnection = new FMSConnection();
        fmsConnection.addEventListener("connectResult",    connectResult);
        fmsConnection.addEventListener("connectFail",      connectFail);
        fmsConnection.addEventListener("disconnectResult", disconnectResult);
        fmsConnection.client = _connectionClient;
        simpleConnectForm.enabled = false;
        CursorManager.setBusyCursor();
        if (rejectConnectionComboBox.selected){
            event.credentials.rejectConnection = true;
        }
        fmsConnection.connect(event.uri, event.credentials);
    }
    
    public function disconnectResult(event:NetStatusEvent):void{
        if (stack){
            var currentSampleApp:ISample = stack.selectedChild  as ISample;
            currentSampleApp.disconnect();
        }
        fmsConnection.close();
        currentState = "Login";
        Alert.show("Lost network connection...");
    }
    
    private function disconnect(event:MouseEvent):void{
        if (stack){
            var currentSampleApp:ISample = stack.selectedChild as ISample;
            currentSampleApp.disconnect();
        }    
        fmsConnection.close();
        currentState = "Login";
    }
    
    private function changeViewItem(event:IndexChangedEvent):void{
        var lastSampleApp:ISample = stack.getChildAt(event.oldIndex) as ISample;
        var nextSampleApp:ISample = stack.getChildAt(event.newIndex) as ISample;
        lastSampleApp.disconnect();
        nextSampleApp.user = user;
        nextSampleApp.netConnection = fmsConnection;
    }
    
        
    ]]>
</mx:Script>
</mx:Application>