VideoManager = function () {
    this.IsFlash9 = DetectFlashVer(9, 0, 0);
    this.IsHtml5Mp4Supported = this.isHtml5Mp4Supported();
    this.IsVideoAvailable = this.IsFlash9 || this.IsHtml5Mp4Supported;
    this.VideoObjects = new Ext.util.MixedCollection();
    this.CurrentPlayingVideo = null;
};
VideoManager.prototype = {
    isHtml5Mp4Supported: function () {
        var v = document.createElement('video');
        return !!(v.canPlayType && v.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"').replace(/no/, ''));
    },
    addVideoObject: function (flvVideoObject) {
        this.VideoObjects.add(flvVideoObject.id, flvVideoObject);
    },
    removeVideoObject: function (flvVideoObject) {
        this.unRegisterPlayingVideo(flvVideoObject);
        if (this.VideoObjects.containsKey(flvVideoObject.id)) {
            this.VideoObjects.removeAtKey(flvVideoObject.id);
        }
    },
    getVideoObject: function (id) {

        if (this.VideoObjects.containsKey(id)) {
            return this.VideoObjects.getByKey(id);
        }
        else {
            return null;
        }
    },
    registerPlayingVideo: function (videoObject) {

        if (this.CurrentPlayingVideo != null && this.CurrentPlayingVideo.id != videoObject.id) {
           // Author.log("registerPlayingVideo:"+videoObject.Url +", currentPlayingVideo :"+this.CurrentPlayingVideo.Url);
            this.CurrentPlayingVideo.onCancelledByAnotherVideo(videoObject);
        }
        this.CurrentPlayingVideo = videoObject;
    },
    unRegisterPlayingVideo: function (videoObject) {
        if (this.CurrentPlayingVideo != null) {
            if (this.CurrentPlayingVideo.id == videoObject.id) {
                this.CurrentPlayingVideo = null;
            }
        }
    },
    hasPlayingVideo: function () {
        return this.CurrentPlayingVideo != null;
    },
    interruptPlayingVideoByAudio: function (audioObject) {
        if (this.CurrentPlayingVideo != null) {
            this.CurrentPlayingVideo.onCancelledByAnotherAudio(audioObject);
            this.CurrentPlayingVideo = null;
        }
    },
    stopPlayingVideo: function(){
        if (this.hasPlayingVideo()) {
            this.CurrentPlayingVideo.stop();
            this.CurrentPlayingVideo = null;
        }
    },
    onFlashVideoReady: function (id) {
        var theVideo = this.getVideoObject(id);
        if (theVideo != null) {
            theVideo.onFlashVideoReady();
        }
        else {
            Author.log("onFlashReady:VideoManager:noVideoFound! ID: " + id);
        }
    },
    onPlay: function (id, duration) {
        var theVideo = this.getVideoObject(id);

        if (theVideo != null) {
            theVideo.onPlay(duration);
        }
        else {
            Author.log("onPlay:VideoManager:noVideoFound! ID: " + id);
        }
    },
    onComplete: function (id) {
        var theVideo = this.getVideoObject(id);
        if (theVideo != null) {
            theVideo.onCompleted();
        }
        else {
            Author.log("onCompleted:VideoManager:noVideoFound! ID: " + id);
        }
    },
    onPosition: function (id, position) {
        var theVideo = this.getVideoObject(id);
        if (theVideo != null) {
            theVideo.onPosition(position);
        }
        else {
            Author.log("onPosition:VideoManager:noVideoFound! ID: " + id);
        }
    },
    onHtml5MetaDataLoaded: function (id, duration) {
        var theVideo = this.getVideoObject(id);
        if (theVideo != null) {
            theVideo.onLoadedMetaData(duration);
        }
        else {
            Author.log("onHtml5MetaDataLoaded:VideoManager:noVideoFound! ID: " + id);
        }
    },
    onError: function (id, err) {
    }
};
//Html5 Video Events
function Html5VideoPlayer_OnLoadedMetaData(evt, el, o) {
    var videoID = this.id;
    var duration = this.Html5VideoPlayer.dom.duration;
    Author.VideoManager.onHtml5MetaDataLoaded(videoID, duration);
}
function Html5VideoPlayer_OnCanPlayThrough(evt, el, o) {
    this.Duration = this.Html5VideoPlayer.dom.duration;
    // var videoID = this.Html5VideoPlayer.getAttribute('VideoObjectID');
    // var duration = (this.Html5VideoPlayer.getAttribute("duration"));
    // Author.VideoManager.onPlay(videoID, duration);
}
function Html5VideoPlayer_OnPlay(evt, el, o) {
    var duration = this.Html5VideoPlayer.dom.duration;
    Author.VideoManager.onPlay(this.id, duration);
}
function Html5VideoPlayer_OnError(evt, el, o) {
   // Author.log("Video "+this.id+" onError");
    Author.VideoManager.onError(this.id, "onError");
}
function Html5VideoPlayer_OnTimeUpdate(evt, el, o) {
   // var videoID = this.Html5VideoPlayer.getAttribute('VideoObjectID');
    var position = this.Html5VideoPlayer.dom.currentTime;
    Author.VideoManager.onPosition(this.id, position);
}
function Html5VideoPlayer_OnEnded(evt, el, o) {
    Author.VideoManager.onComplete(this.id);

}
/*
 modelEventDispatcher.dispatchEvent(new ModelEvent(ModelEvent.BUFFER, {percentage:evt.bufferPercent, id:id, client:client, version:version}));
 modelEventDispatcher.dispatchEvent(new ModelEvent(ModelEvent.ERROR, {message:evt.message, id:id, client:client, version:version}));
 modelEventDispatcher.dispatchEvent(new ModelEvent(ModelEvent.LOADED, {loaded:0, total:0, offset:0, id:id, client:client, version:version}));
 modelEventDispatcher.dispatchEvent(new ModelEvent(ModelEvent.TIME, {duration:evt.duration, position:evt.position, id:id, client:client, version:version}));
 modelEventDispatcher.dispatchEvent(new ModelEvent(ModelEvent.META, evt.metadata));
 modelEventDispatcher.dispatchEvent(new ModelEvent(ModelEvent.COMPLETED, {id:id, oldstate:_player.state, newstate:ModelStates.COMPLETED}));
 modelEventDispatcher.dispatchEvent(new ModelEvent(ModelEvent.STATE, {id:id, oldstate:evt.oldstate, newstate:evt.newstate}));
 */
function AuthorMp4Player_PlayerReady(evt) {
    var videoID = evt.id;
    Author.VideoManager.onFlashVideoReady(videoID);
}
function AuthorMp4Player_VideoCompleted(evt) {
    var videoID = evt.id;
    Author.VideoManager.onComplete(videoID);
}
function AuthorMp4Player_StateChanged(evt) {
    var videoID = evt.id;
    var oldState = evt.oldstate;
    var newState = evt.newstate;
    if (oldState == 'BUFFERING' && newState == 'PLAYING') {
        Author.VideoManager.onPlay(videoID, evt.duration);
    }
    //alert("StateChanged: \n" + print(evt));
}
function AuthorMp4Player_PositionChanged(evt) {
    var videoID = evt.id;
    Author.VideoManager.onPosition(videoID, evt.position);
}
function AuthorMp4Player_Loaded(evt) {
    var videoID = evt.id;
    //  alert("Loaded: \n" + print(evt));
}
function AuthorMp4Player_Buffered(evt) {
    var videoID = evt.id;
    // alert("Buffered: \n" + print(evt));
}
function AuthorMp4Player_Error(evt) {
    var videoID = evt.id;
    alert("AuthorMp4Player_Error: \n" + print(evt));
}
