<?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:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.core.FlexGlobals;
			import flash.display.StageDisplayState;
			import mx.managers.SystemManager;
			import org.osmf.events.MediaPlayerStateChangeEvent;
			import org.osmf.events.TimeEvent;
			import org.osmf.media.MediaPlayerState;

			private var streamer:String;
			private var file:String;
			private var videoEventsDisabled:Boolean;
			private var previousVideoTime:Number;

			private function fullscreenListener(event:MouseEvent):void {
				try {
					switch (stage.displayState) {
						case StageDisplayState.FULL_SCREEN:
							stage.displayState = StageDisplayState.NORMAL;
							break;
						default:
							stage.displayState = StageDisplayState.FULL_SCREEN;
							break;
					}
				} catch (err:SecurityError) {
					Alert.show('Fullsceen error: ' + err);
				}
			}

			private function resetVideo():void {
				videoEventsDisabled = true;

				try {
					videoDisplay.source = "";
				} catch (any:*) {}

				setTimeout(resetVideoSource, 5000);
			}

			private function resetVideoSource():void {
				videoEventsDisabled = false;
				previousVideoTime = NaN;
				videoDisplay.source = streamer + "/" + file;
			}

			protected function stateChangeListener(event:MediaPlayerStateChangeEvent):void {
				if (videoEventsDisabled) {
					return;
				}

				if (event.state == MediaPlayerState.PLAYBACK_ERROR) {
					resetVideo();
				}
			}

			protected function timeChangeListener(event:TimeEvent):void {
				if (videoEventsDisabled) {
					return;
				}

				if (isNaN(event.time) && !isNaN(previousVideoTime)) {
					resetVideo();
				} else {
					previousVideoTime = event.time;
				}
			}

			private function init():void {
				videoDisplay.mx_internal::videoPlayer.bufferTime = 1;

				streamer = FlexGlobals.topLevelApplication.parameters.streamer;
				if(streamer == null) {
					Alert.show('Missing flashvars: streamer');
					return;
				}

				file = FlexGlobals.topLevelApplication.parameters.file;
				if(file == null) {
					Alert.show('Missing flashvars: file');
					return;
				}

				videoDisplay.addEventListener(MouseEvent.DOUBLE_CLICK, fullscreenListener);
				videoDisplay.addEventListener("MediaPlayerStateChange", stateChangeListener);
				videoDisplay.addEventListener("currentTimeChange", timeChangeListener);

				resetVideoSource();
			}
	    ]]>
	  </fx:Script>
	<s:BorderContainer x="0" y="0" width="100%" height="100%">
		<s:VideoDisplay doubleClickEnabled="true" width="100%" height="100%" id="videoDisplay"></s:VideoDisplay>
	</s:BorderContainer>
</s:Application>
