                 order at the nominal playback rate), the client SHOULD NOT
                 choose a segment which starts less than three target durations from
                 the end of the Playlist file */
-                var maxLivePosition : Number = Math.max(0, _levels[_level].duration - 3 * _levels[_level].averageduration);
+                var maxLivePosition : Number = Math.max(0, _levels[level].duration - 3 * _levels[level].averageduration);
                 if (position == -1) {
                     // seek 3 fragments from end
                     seek_position = maxLivePosition;

            }
        };

        private function _updateLevel(buffer : Number) : int {
            var level : int;
            if (_manifest_just_loaded) {
                level = _hls.startlevel;
            } else if (_fragment_first_loaded == false) {
                level = _hls.seeklevel;
            } else if (_bIOError == true) {
                /* in case IO Error has been raised, stick to same level */
                level = _level;
                /* in case fragment was loaded for PTS analysis, stick to same level */
            } else if (_pts_just_loaded == true) {
                _pts_just_loaded = false;
                level = _level;
                /* in case we are switching levels (waiting for playlist to reload) or seeking , stick to same level */
            } else if (_switchlevel == true) {
                level = _level;
            } else if (_manual_level == -1 && _levels.length > 1 ) {
                if (_metrics_previous) {
                    var last_segment_duration : Number = (_frag_previous ? 1000 * _frag_previous.duration : 0);
                    level = _autoLevelManager.getnextlevel(_level, buffer, last_segment_duration, _metrics_previous.processing_duration, _metrics_previous.bandwidth);
                } else {
                    level = _autoLevelManager.getnextlevel(_level, buffer, 0, 0, 0);
                }
            } else if (_manual_level == -1 && _levels.length == 1 ) {
                level = 0;
            } else {
                level = _manual_level;
            }
            if (level != _level || _manifest_just_loaded) {
                _level = level;
                _switchlevel = true;
                _hls.dispatchEvent(new HLSEvent(HLSEvent.LEVEL_SWITCH, _level));
            }
            return level;
        }

        private function _loadfirstfragment(position : Number) : int {
            CONFIG::LOGGING {
                Log.debug("loadfirstfragment(" + position + ")");
            }
            _need_reload = false;
            _switchlevel = true;
            _updateLevel(0);

            // check if we received playlist for new level. if live playlist, ensure that new playlist has been refreshed
            if ((_levels[_level].fragments.length == 0) || (_hls.type == HLSTypes.LIVE && _last_updated_level != _level)) {
                // playlist not yet received
                CONFIG::LOGGING {
                    Log.debug("loadfirstfragment : playlist not received for level:" + _level);
                }
                return 1;
            }

                        return;
                    }
                }
                var level : int;
                if (_fragment_first_loaded == false) {
                    _need_reload = false;
                    // determine first fragment load level, check if we are in fresh start or seek condition
                    if (_manifest_just_loaded) {
                        level = _hls.startlevel;
                    } else {
                        level = _hls.seeklevel;
                    }
                    if (level != _level || _manifest_just_loaded) {
                        _level = level;
                        _hls.dispatchEvent(new HLSEvent(HLSEvent.LEVEL_SWITCH, _level));
                    }
                    _switchlevel = true;
                    // just after seek, load first fragment
                    loadstatus = _loadfirstfragment(_seek_pos, level);
                    // check if we need to load next fragment, check if buffer is full
                } else if (HLSSettings.maxBufferLength == 0 || _hls.stream.bufferLength < HLSSettings.maxBufferLength) {
                    if (_bIOError == true) {
                        /* in case IO Error has been raised, stick to same level */
                        level = _level;
                        /* in case fragment was loaded for PTS analysis, stick to same level */
                    } else if (_pts_just_loaded == true) {
                        _pts_just_loaded = false;
                        level = _level;
                        /* in case we are switching levels (waiting for playlist to reload) or seeking , stick to same level */
                    } else if (_switchlevel == true) {
                        level = _level;
                    } else if (_manual_level == -1 && _levels.length > 1 ) {
                        if (_metrics_previous) {
                            var last_segment_duration : Number = (_frag_previous ? 1000 * _frag_previous.duration : 0);
                            level = _autoLevelManager.getnextlevel(_level, _hls.stream.bufferLength, last_segment_duration, _metrics_previous.processing_duration, _metrics_previous.bandwidth);
                        } else {
                            level = _autoLevelManager.getnextlevel(_level, _hls.stream.bufferLength, 0, 0, 0);
                        }
                    } else if (_manual_level == -1 && _levels.length == 1 ) {
                        level = 0;
                    } else {
                        level = _manual_level;
                    }
                    if (level != _level) {
                        _level = level;
                        _switchlevel = true;
                        _hls.dispatchEvent(new HLSEvent(HLSEvent.LEVEL_SWITCH, _level));
                    }
                    _need_reload = false;
                    loadstatus = _loadnextfragment(_level, _frag_previous);
                } else {
                    // no need to load any new fragment, buffer is full already
                    return;
