/* Copyright 2003-2008 Emergent Music LLC  All rights reserved.
$Id$
*/

FLYFI.AudioPlayer = function() {    // class
    var self = this;
    
    self.MSEC_DELAYBEFOREPLAY = 500; // delay after calling SoundManager before playing to ensure it 'hears' us

    self.bytesLoaded = 0;
    self.bytesTotal = 0;
    self.position = 0;
    self.duration = 0;
    
    
    self.play = function(trackDict) {
        if (FLYFI.isLoadedInSoundManager(trackDict.trackID)) {
            var sound = soundManager.sounds[trackDict.trackID];
            self.bytesLoaded = sound.bytesLoaded;
            self.bytesTotal = sound.bytesTotal;
            self.position = 0;
            self.duration = 0;
        } else {
            self.bytesLoaded = 0;
            self.bytesTotal = 0;
            self.position = 0;
            self.duration = 0;
            soundManager.safe_createSound({
                id: trackDict.trackID, 
                url: trackDict.play_url,
                whileloading: function() {
                    if (FLYFI.playingTrack.trackDict && (trackDict.trackID != FLYFI.playingTrack.trackDict.trackID)) {
                        return; // user changed tracks
                    }
                    FLYFI.audioPlayer.bytesLoaded = this.bytesLoaded;
                    if (!FLYFI.audioPlayer.bytesTotal && this.bytesTotal && this.bytesTotal > 0) {
                        FLYFI.audioPlayer.bytesTotal = this.bytesTotal;
                    }
                },
                whileplaying: function() {
                    if (FLYFI.playingTrack.trackDict && (trackDict.trackID != FLYFI.playingTrack.trackDict.trackID)) {
                        return; // user changed tracks
                    }
                    FLYFI.audioPlayer.position = this.position;
                    FLYFI.audioPlayer.duration = this.loaded ? this.duration : this.durationEstimate;
                    if (!FLYFI.audioPlayer.bytesTotal && this.bytesTotal && this.bytesTotal > 0) {
                        FLYFI.audioPlayer.bytesTotal = this.bytesTotal;
                    }
                },
                onfinish: function() { 
                    FLYFI.playingTrack.notifyFinished(FLYFI.playingTrack.trackDict.trackID);
                },
                onstop: function() { 
                    if (!this.loaded) {
                        this.unload(self.sID);
                    }
                }
            });
        }
        setTimeout(FLYFI.onTimeout_PlayAudio, self.MSEC_DELAYBEFOREPLAY); // need a delay here or the soundManager doesn't play
    };
    
    self.pause = function(trackID) {
        soundManager.pause(trackID);
    };
    
    self.stop = function() {
        soundManager.stopAll();
    };
    
    self.resume = function(trackID) {
        soundManager.resume(trackID);
    };
    
    self.setPlayerStatus = function() {
        if (!FLYFI.playingTrack.haveAudioTrack()) {
            return;
        }
       
        FLYFI.playingTrack.bytesLoaded = self.bytesLoaded;
        FLYFI.playingTrack.bytesTotal = self.bytesTotal;
        
        FLYFI.playingTrack.position = self.position;
        FLYFI.playingTrack.duration = self.duration;
    };

    self.setPosition = function(msec) {
        soundManager.setPosition(FLYFI.playingTrack.trackDict.trackID, msec);
        setTimeout(FLYFI.onTimeout_PlayAudio, self.MSEC_DELAYBEFOREPLAY); // need a delay here or the soundManager doesn't play
    };
    

};
FLYFI.audioPlayer = new FLYFI.AudioPlayer();

FLYFI.onTimeout_PlayAudio = function() {    // command to play the audio
    if (FLYFI.playingTrack && FLYFI.playingTrack.trackDict) { // if change rapidly, may miss it
        soundManager.safe_play(FLYFI.playingTrack.trackDict.trackID, {});
        if (FLYFI.playingTrack.playerControls) {
            FLYFI.playingTrack.playerControls.onSoundPlay();
        }
    }
};
    
// ensure the soundManager gets initialized

soundManager.safe_createSound = function(oOptions) {
    // Create the given track only when soundManager has finished loading.
    if (this._didInit) {
        this.createSound(oOptions);
    } else {
        this.safe_createSound_args = { oOptions: oOptions };
    }
};
    
soundManager.safe_play = function(sID, oOptions) {
    // Play the given track only when soundManager has finished loading.
    if (this._didInit) {
        this.play(sID, oOptions);
    } else {
        this.safe_play_args = { sID: sID, oOptions: oOptions };
    }
};
    
soundManager.onload = function() {
    if (soundManager.safe_createSound_args) {
        soundManager.createSound(soundManager.safe_createSound_args.oOptions);
    }
    if (soundManager.safe_play_args) {
        soundManager.play(soundManager.safe_play_args.sID, soundManager.safe_play_args.oOptions);
    }
};

FLYFI.isLoadedInSoundManager = function(trackID){
    var l = soundManager.soundIDs.length;
    for (var i=0; i < l; ++i) {
        if (soundManager.soundIDs[i] == trackID) {
            return soundManager.sounds[trackID].loaded;
        }
    }
    return false;
};


