<?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/mx"
         minWidth="500" minHeight="350" creationComplete="init()">

  <fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
  </fx:Declarations>

  <fx:Script>
    <![CDATA[
      import mx.controls.Alert;
      import mx.core.FlexGlobals;
      private var streamer:String;
      private var file:String;

      private function toggleFeedListener(event:MouseEvent):void {
        if(toggleFeed.label == 'Start Feed') {
          toggleFeed.label = 'Stop Feed';
          toggleAudio.enabled = true;
          videoDisplay.play();
        } else {
          toggleFeed.label = 'Start Feed';
          toggleAudio.enabled = false;
          videoDisplay.close();
        }
      }

      private function toggleAudioHandler(event:MouseEvent):void {
        if(toggleAudio.label == 'Unmute') {
          toggleAudio.label = 'Mute';
          videoDisplay.volume = 1;
        } else {
          toggleAudio.label = 'Unmute';
          videoDisplay.volume = 0;
        }
      }

      private function initListeners():void {
        toggleFeed.addEventListener(MouseEvent.CLICK, toggleFeedListener);
        toggleAudio.addEventListener(MouseEvent.CLICK, toggleAudioHandler);
      }

      private function init():void {
        streamer = FlexGlobals.topLevelApplication.parameters.streamer;
        file = FlexGlobals.topLevelApplication.parameters.file;
        if(file == null) {
          Alert.show('Missing flashvars: file');
          return;
        }
        if(streamer == null) {
          Alert.show('Missing flashvars: streamer');
          return;
        }
        videoDisplay.source = streamer + "/" + file;
        initListeners();
      }
    ]]>
  </fx:Script>
  <s:Panel x="0" y="0" width="100%" height="100%" title="RTMP Player">
    <mx:VideoDisplay width="100%" height="100%" id="videoDisplay" autoPlay="false">
    </mx:VideoDisplay>
    <s:controlBarContent>
      <s:Button label="Start Feed" id="toggleFeed"></s:Button>
      <s:Spacer width="100%" height="100%"/>
      <s:Button label="Mute" id="toggleAudio" enabled="false"></s:Button>
    </s:controlBarContent>
  </s:Panel>
</s:Application>
