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

	import flash.media.Video;
	import flash.media.SoundTransform;
	import flash.utils.Timer;
	
	import FlashMediaElement;
	import HtmlMediaEvent;
	
	public class VideoElement extends Sprite implements IMediaElement 
	{
		private var _currentUrl:String = "";
		private var _autoplay:Boolean = true;
		
        private var _connection:NetConnection;
        private var _stream:NetStream;
        private var _video:Video;
		private var _element:FlashMediaElement;
		private var _soundTransform;
		private var _oldVolume:Number = 1;
		
		// event values
		private var _duration:Number = 0;
		private var _framerate:Number;
		private var _isPaused:Boolean = true;
		private var _isEnded:Boolean = false;
		private var _isLoaded:Boolean = false;
		private var _volume:Number = 1;
		private var _isMuted:Boolean = false;	
		
		private var _bytesLoaded:Number = 0;
		private var _bytesTotal:Number = 0;		
		private var _bufferedTime:Number = 0;		
		
		private var _videoWidth:Number = -1;
		private var _videoHeight:Number = -1;
		
		private var _timer:Timer;
		
		private var _isRTMP:Boolean = false;
		private var _playRTMPasap:Boolean = false;
		

		public function get video():Video {
			return _video;
		}
		
		public function get videoHeight():Number {
			return _videoHeight;
		}
		
		public function get videoWidth():Number {
			return _videoWidth;
		}
		
		
		public function duration():Number {		
			return _duration;
		}
		
		public function currentTime():Number {		
			return _stream.time;
		}			
		
		
		
		public function VideoElement(element:FlashMediaElement, autoplay:Boolean, timerRate:Number) 
		{
			_element = element;
			_autoplay = autoplay;
			
			_video = new Video();
			addChild(_video);
			
            _connection = new NetConnection();
            _connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
            _connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            
            //will be called in load()
            //_connection.connect(null);	
			
			_timer = new Timer(timerRate);
			_timer.addEventListener("timer", timerHandler);
			
		}
		
		private function timerHandler(e:TimerEvent) {

			_bytesLoaded = _stream.bytesLoaded;
			_bytesTotal = _stream.bytesTotal;
			
			sendEvent(HtmlMediaEvent.TIMEUPDATE);

			trace("bytes", _bytesLoaded, _bytesTotal);

			if (_bytesLoaded < _bytesTotal)
				sendEvent(HtmlMediaEvent.PROGRESS);			
		}

		// internal events
        private function netStatusHandler(event:NetStatusEvent):void {
			trace("netStatus", event.info.code);

			switch (event.info.code) {

				case "NetStream.Buffer.Empty":
                case "NetStream.Buffer.Full":
					_bytesLoaded = _stream.bytesLoaded;
					_bytesTotal = _stream.bytesTotal;

					sendEvent(HtmlMediaEvent.PROGRESS);	
                    break;

