AudioManager = function () {
    this.IsAudioAvailable = false;
    this.IsFlash9 = false;
    this.IsHtml5Mp3Supported = false;
    this.CurrentSoundObject = null;
    this.FlashMp3Player = null;
    this.HTML5Player = null;
    this.IsFlash9 = DetectFlashVer(9, 0, 0);
    this.IsHtml5Mp3Supported = this.isHtml5Mp3Supported();
    this.IOSPlayedOnce = false;

    if (this.IsHtml5Mp3Supported) {
        this.getHtml5Player();
    } else if (this.IsFlash9) {
        this.getFlashMp3Player();
    }
    else {
        this.IsAudioAvailable = false;
    }
};
AudioManager.prototype = {
    isHtml5Mp3Supported: function () {
        var a = document.createElement('audio');
        return !!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/, ''));
    },
    getFlashMp3Player: function () {
        var mp3Player = Ext.get("audioPlayerHTA").dom;
        if (!Ext.isIE) {
            mp3Player = Ext.get("audioPlayerEmbed").dom;
        }
        this.FlashMp3Player = mp3Player;
        this.IsAudioAvailable = true;
    },
    hasPlayingAudio: function () {

        return this.CurrentSoundObject != null;
    },
    interruptByAnotherVideo: function (videoObject) {
        this.CurrentSoundObject.onCancelledByAnotherVideo(videoObject);
        this.stop();
        this.CurrentSoundObject = null;
    },
    stopPlayingAudio: function () {
        if (this.CurrentSoundObject != null) {
            this.stop();
        }
    },
    play: function (soundObject) {
        if (this.CurrentSoundObject != null) {
            this.CurrentSoundObject.onCancelledByAnotherAudio(soundObject);
            this.stop();
        }
        if (Author.VideoManager.hasPlayingVideo()) {
            Author.VideoManager.interruptPlayingVideoByAudio(soundObject);
        }

        if (this.IsHtml5Mp3Supported) {
            this.playWithHtml5Player(soundObject);
        }
        else if (this.IsFlash9) {
            this.playWithFlashMp3Player(soundObject);
        }
        else {
            alert("Can't play");
        }


    },
    stop: function () {

        if (this.IsHtml5Mp3Supported) {
            this.stopHtml5Player();
        } else if (this.IsFlash9) {
            this.stopFlashMp3Player();
        }
        else {
            alert("Can't stop");
        }
    },
    playWithHtml5Player: function (soundObject) {
        this.CurrentSoundObject = soundObject;
        this.HTML5Player.set({
            SoundObjectID: soundObject.id
        });

        var audioPromise = this.HTML5Player.dom.setAttribute("src", soundObject.Url);
        if(audioPromise !== undefined)
        {
            audioPromise.catch( function (e){
                //This code runs if src fails to set in modern browsers
                //No need to do anyting
            }
            )
        }
        this.HTML5Player.dom.load();
        var playPromise = this.HTML5Player.dom.play();
        if(playPromise !== undefined)
        {
            playPromise.catch(function (e) {
                //This code runs if the first audio fails to play.
                //This will only happen if you disable the play button and have auto play audio in first screen
                //Leaving console log in as it could be useful for Author Users but will never run once published.
                console.log("Audio not playing because autoplay is not supported on this device");
            })
        }
    },
    stopHtml5Player: function () {
        if (Author.isIOS()) {
            //this.HTML5Player.applyStyles("position:absolute;width:200px;height:40px;left:-300px;top:-50px;z-index:10000;");
        }
        this.CurrentSoundObject = null;
        this.HTML5Player.set({
            SoundObjectID: null
        });
        this.HTML5Player.dom.pause();
    },
    playWithFlashMp3Player: function (soundObject) {
        if (this.FlashMp3Player == null) {
            Author.log("flash mp3 player not initalized");
            return;
        }
        this.CurrentSoundObject = soundObject;
        this.FlashMp3Player.playMp3(soundObject.Url, soundObject.id);
    },
    stopFlashMp3Player: function () {
        if (this.FlashMp3Player == null) {
            Author.log("flash mp3 player not initalized");
        }
        this.CurrentSoundObject = null;
        this.FlashMp3Player.stopMp3();
    },
    getHtml5Player: function () {
        this.HTML5Player = Ext.get("html5audio");
        this.IsAudioAvailable = true;
        this.HTML5Player.addListener("ended", Html5Mp3Player_OnEnded, this);
        this.HTML5Player.addListener("play", Html5Mp3Player_OnPlay, this);
        this.HTML5Player.addListener("error", Html5Mp3Player_OnError, this);
        this.HTML5Player.addListener("timeupdate", Html5Mp3Player_OnTimeUpdate, this);
        this.HTML5Player.addListener("canplaythrough", Html5Mp3Player_OnCanPlayThrough, this);
        this.HTML5Player.addListener("loadedmetadata", Html5Mp3Player_OnLoadedMetaData, this);
    }
};
//Html5 audio player event handlers
function Html5Mp3Player_OnLoadedMetaData(evt, el, o) {
    var sndID = this.HTML5Player.getAttribute('SoundObjectID');
    var duration = (this.HTML5Player.getAttribute("duration"));
    if (Author.AudioManager.CurrentSoundObject&&Author.AudioManager.CurrentSoundObject.id == sndID) {
        Author.AudioManager.CurrentSoundObject.onLoadedMetaData(duration);
    }
}
function Html5Mp3Player_OnCanPlayThrough(evt, el, o) {
    var sndID = this.HTML5Player.getAttribute('SoundObjectID');
    var duration = (this.HTML5Player.getAttribute("duration"));
    if (Author.AudioManager.CurrentSoundObject&&Author.AudioManager.CurrentSoundObject.id == sndID) {
        Author.AudioManager.CurrentSoundObject.onCanPlayThrough(duration);
    }
}
function Html5Mp3Player_OnPlay(evt, el, o) {
    if (Author.isIOS()) {
        Author.AudioManager.IOSPlayedOnce = true;
        var iosLabel = Ext.get("iosInfoLabel");
        iosLabel.dom.innerHTML = "";
        //  Author.AudioManager.HTML5Player.applyStyles("position:absolute;width:200px;height:40px;left:-300px;top:-50px;z-index:10000;");
    }
}
function Html5Mp3Player_OnError(evt, el, o) {
    var sndID = this.HTML5Player.getAttribute('SoundObjectID');
    if (Author.AudioManager.CurrentSoundObject&&Author.AudioManager.CurrentSoundObject.id == sndID) {
        Author.AudioManager.CurrentSoundObject.onError("Html5 audio error.");
    }
}
function Html5Mp3Player_OnTimeUpdate(evt, el, o) {
    var sndID = this.HTML5Player.getAttribute('SoundObjectID');
    if (sndID) {
        var position = this.HTML5Player.getAttribute('currentTime');
        if (Author.AudioManager.CurrentSoundObject&&Author.AudioManager.CurrentSoundObject.id == sndID) {
            Author.AudioManager.CurrentSoundObject.onPosition(position);
        }
    }
}
function Html5Mp3Player_OnEnded(evt, el, o) {
    var sndID = this.HTML5Player.getAttribute('SoundObjectID');
    if (Author.AudioManager.CurrentSoundObject&&Author.AudioManager.CurrentSoundObject.id == sndID) {
        Author.AudioManager.CurrentSoundObject.onCompleted();
    }
}
//Flash mp3 player event handlers
function AuthorMp3Player_OnPlay(snd) {
    if (Author.AudioManager.CurrentSoundObject&&Author.AudioManager.CurrentSoundObject.id == snd.ID) {
        Author.AudioManager.CurrentSoundObject.onPlay(snd.Length);
    }
}
function AuthorMp3Player_OnError(des) {
    if (Author.AudioManager.CurrentSoundObject&&Author.AudioManager.CurrentSoundObject.id == sndID) {
        Author.AudioManager.CurrentSoundObject.onError(des.Description);
    }
}
function AuthorMp3Player_OnPosition(snd) {
    if (Author.AudioManager.CurrentSoundObject&&Author.AudioManager.CurrentSoundObject.id == snd.ID) {
        Author.AudioManager.CurrentSoundObject.onPosition(snd.Position);
    }
}
function AuthorMp3Player_OnAudioCompleted(snd) {
    if (Author.AudioManager.CurrentSoundObject&&Author.AudioManager.CurrentSoundObject.id == snd.ID) {
        Author.AudioManager.CurrentSoundObject.onCompleted();
    }
}




