<?xml version="1.0" encoding="utf-8"?>
<s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="640" minHeight="480" viewSourceURL="srcview/index.html">
    
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    
    <fx:Script>
        <![CDATA[
            import ca.service.ZendAMFService;
            import ca.vo.Track;
            
            import flash.events.MouseEvent;
            
            import mx.collections.ArrayCollection;
            import mx.controls.Alert;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            
            protected function button1_clickHandler( event : MouseEvent ) : void
            {
                // Get an instance of the service
                var service : ZendAMFService = new ZendAMFService(); 
                
                // Add some handlers for result and fault
                service.addEventListener( ResultEvent.RESULT, resultHandler );
                service.addEventListener( FaultEvent.FAULT, faultHandler );
                
                // Call the function on the server
                service.getTrack( 10 ); // The trackId doesnt matter for this tutorial
            }
            
            
            protected function button2_clickHandler( event : MouseEvent ) : void
            {
                // Get an instance of the service
                var service : ZendAMFService = new ZendAMFService();
                
                // Add some handlers for result and fault
                service.addEventListener( ResultEvent.RESULT, resultHandler );
                service.addEventListener( FaultEvent.FAULT, faultHandler );
                
                // Call the function on the server
                service.getTrackList( 10 ); // The albumId doesnt matter for this tutorial
            }
            
            
            protected function resultHandler( event : ResultEvent ) : void
            {
                // Is this return from getTrackList
                if( event.result is ArrayCollection )
                {
                    resultList.dataProvider = event.result as ArrayCollection;    
                }
                
                // Is this return from getTrack
                if( event.result is Track )
                {
                    resultList.dataProvider = new ArrayCollection([event.result]); // Convert the single track to an array    
                }
            }
            
            
            protected function faultHandler( event : FaultEvent ) : void
            {
                Alert.show( event.message.toString() );
            }
            
        ]]>
    </fx:Script>
    <s:Label top="20" left="20" text="This is a simple example showing ZendAMF in action" />
    
    <s:HGroup top="40" left="20">
        <s:Button 
            label="GetTrack" 
            click="button1_clickHandler(event)" />
        
        <s:Button 
            label="GetTrackList" 
            click="button2_clickHandler(event)" />
        
        <s:Button 
            label="Clear" 
            click="resultList.dataProvider = null" />    
    </s:HGroup>
    
    
    <s:List 
        id="resultList"
        labelField="trackName"
        width="400" 
        top="70"
        left="20"/>
    
</s:Application>