-﻿
-package htmlelements
+﻿package htmlelements
 {
 	import flash.display.Sprite;
 	import flash.events.*;

							",videoWidth:" + _videoWidth +
							",videoHeight:" + _videoHeight +							
							"}";
			
			_element.sendEvent(eventName, values);			
		}		
	}
}
		

                case "NetStream.Play.StreamNotFound":
                    trace("Unable to locate video");
                    break;

				// STREAM
                case "NetStream.Play.Start":					
					_isPaused = false;
					sendEvent(HtmlMediaEvent.PLAY);
					sendEvent(HtmlMediaEvent.PLAYING);					
					_timer.start();
                    break;

                case "NetStream.Seek.Notify":
					sendEvent(HtmlMediaEvent.SEEKED);
                    break;					

				case "NetStream.Pause.Notify":
					_isPaused = true;
					sendEvent(HtmlMediaEvent.PAUSE);
                    break;			

				case "NetStream.Play.Stop":
					_isPaused = false;
					_timer.stop();
					sendEvent(HtmlMediaEvent.ENDED);
                    break;						

            }
        }		

        private function connectStream():void {
			trace("connectStream");
            _stream = new NetStream(_connection);
            
			_stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); // same event as connection			
            _stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);

			var customClient:Object = new Object();
			customClient.onMetaData = onMetaDataHandler;
			_stream.client = customClient;

            _video.attachNetStream(_stream);
			
			_isConnected = true;
			
			if (_playWhenConnected && !_hasStartedPlaying) {
				play();
				_playWhenConnected = false;
			}
				
        }		

        private function securityErrorHandler(event:SecurityErrorEvent):void {
            trace("securityErrorHandler: " + event);
        }

		private function asyncErrorHandler(event:AsyncErrorEvent):void {
            // ignore AsyncErrorEvent events.
        }


		private function onMetaDataHandler(info:Object):void {
			_duration = info.duration;
			_framerate = info.framerate;
			_videoWidth = info.width;
			_videoHeight = info.height;

			// set size?			

			sendEvent(HtmlMediaEvent.LOADEDMETADATA);
		}




		// interface members
		public function setSrc(url:String):void {
			if (_isConnected && _stream) {
				// stop and restart
				_stream.pause();
			}
			
			_isRTMP = !!_currentUrl.match(/^rtmp(s|t|e|te)?\:\/\//);
			_currentUrl = url;
			_isConnected = false;
			_hasStartedPlaying = false;
		}

		public function load():void {		
			// as far as I know there is no way to start downloading a file without playing it (preload="auto" in HTML5)
			// so this "load" just begins a Flash connection
			if (_isConnected && _stream) {
				_stream.pause();
			}
			_isConnected = false;
			
			if (_isRTMP) {
				_connection.connect(_currentUrl.replace(/\/[^\/]+$/,"/"));
			} else {
				_connection.connect(null);
			}
			// in a few moments the "NetConnection.Connect.Success" event will fire
			// and call createConnection which finishes the "load" sequence
			
			
		}

		public function play():void {	
		
			if (!_hasStartedPlaying && !_isConnected) {
				_playWhenConnected = true;
				load();
				return;
			}

			if (_hasStartedPlaying) {
				if (_isPaused) {
					_stream.resume();
					_timer.start();
					_isPaused = false;
					sendEvent(HtmlMediaEvent.PLAY);
					sendEvent(HtmlMediaEvent.PLAYING);
				}
			} else {
				
				if (_isRTMP) {
					_stream.play(_currentUrl.split("/").pop());
				} else {
