Ext.define('FLVVideoClass15', {
    extend:'DisplayableAction',
    constructor:function (cfgXML, o, actionSequence) {
        this.Url = "";
        this.IsFlash9 = false;
        this.IsHtml5Mp4Supported = false;
        this.CancelPreviousAudio = true;
        this.ShowController = true;
        this.IsAutoStart = false;
        this.WaitUntilComplete = false;
        this.CancelType = CancelTypes.MouseClickOrAnyKey;
        this.Duration = 0;
        this.IsVideoPlayerAvailable = false;
        this.BadVideo = false;
        this.PlayerBox = null;
        this.Html5VideoPlayer = null;
        this.FlashPlayerElement = null;
        this.ThePlayer = null;
        this.IsFlashVideoReady = false;
        this.HasCompleted = false;
        this.IsMuted = false;
        this.callParent([ cfgXML, o, actionSequence]);
        this.IsFlash9 = Author.VideoManager.IsFlash9 ;
        this.IsHtml5Mp4Supported = Author.VideoManager.IsHtml5Mp4Supported;
        this.Url = Author.resourcePath + XmlHelper.getString(cfgXML, "mp4FileName");
        this.WaitUntilComplete = XmlHelper.getBoolean(cfgXML, "waitUntilComplete");
        this.CancelPreviousAudio = XmlHelper.getBoolean(cfgXML, "cancelPreviousAudio");
        this.IsAutoStart = XmlHelper.getBoolean(cfgXML, "isAutoStart");
        this.IsMuted = XmlHelper.getBoolean(cfgXML, "isMuted");
        this.IsLoop = XmlHelper.getBoolean(cfgXML, "isLoop");
        this.ShowController = XmlHelper.getBoolean(cfgXML, "showController");
        this.CancelType = XmlHelper.getInteger(cfgXML, "cancelType");
        this.Duration = XmlHelper.getFloat(cfgXML, "duration");
        this.FlashPlayerID = this.ObjectType + "-" + this.id + "-Flash";
        this.FlashPlayerEmbedID = this.ObjectType + "-" + this.id + "-FlashEmbed";
        this.Html5PlayerID = this.ObjectType + "-" + this.id + "-Html5";
    },
    getFlashMp4Player:function () {
        var flashMp4Player = Ext.get(this.FlashPlayerID).dom;
        if (!Ext.isIE) {
            flashMp4Player = Ext.get(this.FlashPlayerEmbedID).dom;
        }
        this.ThePlayer = flashMp4Player;
        this.IsVideoPlayerAvailable = true;
        return flashMp4Player;
    },
    getHtml5Mp4Player:function () {
        this.ThePlayer = Ext.get(this.Html5PlayerID);
        this.IsVideoPlayerAvailable = true;
    },
    getFlashVar:function () {
        var flashVars = 'file=../../' + this.Url + '&id=' + this.id;
        flashVars += ('&autostart=' + this.IsAutoStart + '&showcontroller=' + this.ShowController);
        flashVars += '&mute=' + this.IsMuted;
        flashVars += '&playerready=flvReady&completed=flvFinished';
        return flashVars;
    },
    initializeDC:function (bg) {
        this.initializeDisplayComponent(bg);
        //initialize the pictureBox first
        this.PlayerBox = this.ContentContainer.createChild({
            id:this.ObjectType + "-" + this.id + "-PlayerBox",
            style:"position:absolute;background-color:#000000;width:" + this.ContentContainerWidth + "px;height:" + this.ContentContainerHeight + "px;"
        });
        if (this.IsHtml5Mp4Supported) {
            if(this.ShowController){

                this.Html5VideoPlayer = this.PlayerBox.createChild({
                    tag:"Video",
                    id:this.Html5PlayerID,
                    controls:"controls",
                    style:"position:absolute:left:0px;top:0px;width:100%;height:100%",
                    "autobuffer":"autobuffer"
                });
            }
            else
            {
                this.Html5VideoPlayer = this.PlayerBox.createChild({
                    tag:"Video",
                    id:this.Html5PlayerID,
                    style:"position:absolute:left:0px;top:0px;width:100%;height:100%",
                    "autobuffer":"autobuffer"
                });
            }
            
            this.Html5VideoPlayer.addListener("ended", Html5VideoPlayer_OnEnded, this);
            this.Html5VideoPlayer.addListener("play", Html5VideoPlayer_OnPlay, this);
            this.Html5VideoPlayer.addListener("error", Html5VideoPlayer_OnError, this);
            this.Html5VideoPlayer.addListener("timeupdate", Html5VideoPlayer_OnTimeUpdate, this);
            this.Html5VideoPlayer.addListener("canplaythrough", Html5VideoPlayer_OnCanPlayThrough, this);
            this.Html5VideoPlayer.addListener("loadedmetadata", Html5VideoPlayer_OnLoadedMetaData, this);
            this.Html5VideoPlayer.set({
                VideoObjectID:this.id
            });
            if(this.IsLoop){
                this.Html5VideoPlayer.set({
                    loop:"loop"
                });
            }
            
            this.getHtml5Mp4Player();
            this.playWithHtml5VideoPlayer();

        }else if (this.IsFlash9) {
            this.FlashPlayerElement = this.PlayerBox.createChild({
                tag:"OBJECT",
                id:this.FlashPlayerID,
                name:this.FlashPlayerID,
                ObjectType:"FlashObject",
                classid:"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000",
                codebase:"http://download.macromedia.com/pub/shockwave /cabs/flash/swflash.cab#version=9,0,115,0",
                width:this.ContentContainerWidth,
                height:this.ContentContainerHeight,
                align:"",
                children:[
                    {
                        tag:"PARAM",
                        NAME:"movie",
                        VALUE:"lib/media/videoPlayer.swf"
                    },
                    {
                        tag:"PARAM",
                        NAME:"WMODE",
                        VALUE:"opaque"
                    },
                    {
                        tag:"PARAM",
                        NAME:"AllowScriptAccess",
                        VALUE:"always"
                    },
                    {
                        tag:"PARAM",
                        NAME:"Allowfullscreen",
                        VALUE:"true"
                    },
                    {
                        tag:"PARAM",
                        NAME:"loop",
                        VALUE:IsLoop
                    },
                    {
                        tag:"PARAM",
                        NAME:"FlashVars",
                        VALUE:this.getFlashVar()
                    },
                    {
                        tag:"embed",
                        src:"lib/media/videoPlayer.swf",
                        name:this.FlashPlayerEmbedID,
                        id:this.FlashPlayerEmbedID,
                        WMODE:"opaque",
                        width:this.ContentContainerWidth,
                        height:this.ContentContainerHeight,
                        type:"application/x-shockwave-flash",
                        AllowScriptAccess:"always",
                        Allowfullscreen:"true",
                        swLiveConnect:"true",
                        FlashVars:this.getFlashVar(),
                        pluginspage:"http://www.macromedia.com/go/getflashplayer"
                    }
                ]
            });
            this.getFlashMp4Player();
        }
        else {
            this.IsVideoPlayerAvailable = false;
            alert("No Video Supported");
        }
    },
    playWithHtml5VideoPlayer:function () {
        this.Html5VideoPlayer.dom.setAttribute("src", this.Url);
        if (this.IsMuted) {
            try {
                this.Html5VideoPlayer.dom.muted = true;
            } catch (err) {
                Author.log("Can't mute html5 video.")
            }

        }
        this.Html5VideoPlayer.dom.load();

    },
    stop:function () {
        // Author.log("Video.Stop:"+this.Url);
        if (this.IsHtml5Mp4Supported) {
            //alert(print(this.Html5VideoPlayer.dom));
            this.Html5VideoPlayer.dom.pause();
        } else if (this.IsFlash9) {
            if (this.ThePlayer.stopVideo) {
                this.ThePlayer.stopVideo();
            }
            else {
                Author.log("FLVVideoClass.stop: can't find stopVideo function.");
            }
        }

        else {
            alert("Can't stop");
        }
    },
    getVideoPause:function () {
        return new AuthorPause(this, WaitModes.WaitFor, this.Duration, this.CancelType, null, null);
    },
    onCancelledByAnotherAudio:function (audioObject) {
        // showDebugInfo("onCancelledByAnotherAudio: " + audioObject.id);
        this.HasCompleted = true;
        this.stop();
    },
    onCancelledByAnotherVideo:function (videoObject) {
       // Author.log("onCancelledByAnotherVideo:" + this.Url);
        // showDebugInfo("onCancelledByAnotherVideo: " + videoObject);
        this.HasCompleted = true;
        this.stop();

    },
    onLoadedMetaData:function (duration) {
        this.Duration = duration;
        if (this.IsAutoStart) {
            this.Html5VideoPlayer.dom.play();
        }else{
            window.clearTimeout(Author.DisplayManager.ActionTimer);
            Author.DisplayManager.ActionTimer = setTimeout(Author.DisplayManager.processListActions, 20);
        }
    },
    onCanPlayThrough:function (duration) {
        this.Duration = duration;
        if (!this.WaitUntilComplete) {
            window.clearTimeout(Author.DisplayManager.ActionTimer);
            Author.DisplayManager.ActionTimer = setTimeout(Author.DisplayManager.processListActions, 20);
        }
        else {
            if (Author.DisplayManager.Pause != null) {
            }
            Author.DisplayManager.Pause = this.getVideoPause();
        }
    },
    onSkippedByUser:function () {
       // Author.log("Video skippedByUser: " + this.id);
        this.HasCompleted = true;

        if (Author.DisplayManager.Pause != null && Author.DisplayManager.Pause.Source.id == this.id) {
            window.clearTimeout(Author.DisplayManager.ActionTimer);
            Author.DisplayManager.ActionTimer = setTimeout(Author.DisplayManager.processListActions, 20);
        }
    },
    onFlashVideoReady:function () {
        //alert("[onFlashVideoReady]:this.IsProcessed:"+this.IsProcessed+", this.isFlashVideoReady: "+this.IsFlashVideoReady);
        this.IsFlashVideoReady = true;
        if (this.IsProcessed && !this.IsAutoStart && !this.WaitUntilComplete) {
            window.clearTimeout(Author.DisplayManager.ActionTimer);
            Author.DisplayManager.ActionTimer = setTimeout(Author.DisplayManager.processListActions, 20);
        }
    },
    onPlay:function (duration) {
        this.Duration = duration;
        if (this.CancelPreviousAudio) {

            if (Author.AudioManager.hasPlayingAudio()) {
                if (this.LogicalOrder.compareTo(Author.AudioManager.CurrentSoundObject.LogicalOrder) > 0) {
                    Author.AudioManager.interruptByAnotherVideo(this);
                }
            }

        }
        Author.VideoManager.registerPlayingVideo(this);

        if (this.HasCompleted) {
            return;
        }
        if (!this.WaitUntilComplete) {

            window.clearTimeout(Author.DisplayManager.ActionTimer);
            Author.DisplayManager.ActionTimer = setTimeout(Author.DisplayManager.processListActions, 20);
        }
        else {
            if (Author.DisplayManager.Pause != null) {
            }
            Author.DisplayManager.Pause = this.getVideoPause();

        }
    },
    onPosition:function (position) {
        //alert(position);
    },
    onCompleted:function () {
        if (this.IsHtml5Mp4Supported) {
            // this.Html5VideoPlayer.dom.currentTime = 0;
            this.stop();
        }
        Author.VideoManager.unRegisterPlayingVideo(this);
        this.HasCompleted = true;
        if (this.WaitUntilComplete) {
            if (Author.DisplayManager.Pause && Author.DisplayManager.Pause.Source.id == this.id) {
                Author.DisplayManager.Pause = null;
                window.clearTimeout(Author.DisplayManager.ActionTimer);
                Author.DisplayManager.ActionTimer = setTimeout(Author.DisplayManager.processListActions, 100);
            }
        }
    },
    onError:function (err) {
       // Author.log("FlvVideoClass:onError:" + err);
        window.clearTimeout(Author.DisplayManager.ActionTimer);
        Author.DisplayManager.ActionTimer = setTimeout(Author.DisplayManager.processListActions, 500);
    },
    processAction:function (bg) {
        if (this.IsChangeAction) {
            Author.DisplayManager.changeAction(this);
        }
        this.initializeDC(bg);
        this.setInitialVisibility();
        this.applyEffect();
        this.HasCompleted = false;
        Author.VideoManager.addVideoObject(this);
        //alert("[processAction]:this.IsProcessed:"+this.IsProcessed+", this.isFlashVideoReady: "+this.IsFlashVideoReady);
        this.IsProcessed = true;
        if (this.IsFlashVideoReady && !this.IsAutoStart && !this.WaitUntilComplete) {
            return {MoveOn:true, Interval:0};
        }
        else {
            return {MoveOn:true, Interval:3600 * 24 * 1000};
        }
    },
    remove:function () {
        Author.VideoManager.removeVideoObject(this);
        this.callParent();
    }
});


