/*global
soundManager ngv */
/**
 *
 * playsound
 * stopsound
 * playsoundfrom
 * loadsound
 * loadsounds
 * mute
 *
 *
 */
 (function() {
    /**
    * private
    */
    soundManager.flashVersion = 9;
    soundManager.url = ngv.swfbase;
    // disable debug mode
    soundManager.defaultOptions.multiShot = false;
    soundManager.consoleOnly = true;
    /**
    * eventHandlers
    */
    var soundplayer = {
        soundManagerLoaded: false,
        init: function(e, d, r) {
            //set soundmanager variables here, I guess
            soundManager.onload = function() {
                this.soundManagerLoaded = true;
            };
            soundManager.onerror = function (){
                r.replyTo(e,"soundError");
            };
        },
        soundLoading: function(e, d, r) {
            //);
        },
        muteSound: function(e, d, r) {
            if (d.mute) {
                soundManager.mute();
            } else {
                soundManager.unmute();
            }
            r.replyTo(e, "soundMuted");
        },
        loadSounds: function(e, d, r) {
            var sp = this,i;
            //delay loading of sounds until soundmanager is finished
            //loading itself.
            if (!this.soundManagerLoaded) {
                soundManager.onload = function() {
                    r.replyTo(e, "loadSounds");
                    sp.soundManagerLoaded = true;
                };
            } else {
                this.fgsounds = d.fg;
                this.bgsounds = d.bg;
                this.base = d.base;
                for (i in d.fg) {
                    d.fg[i] = soundManager.createSound({
                        id: i,
                        url: d.base + d.fg[i],
                        volume: 50,
                        multiShot: true,
                        autoLoad: false,
                        whileloading: function() {
                            r.cause("soundLoading", {
                                soundid: this.sID,
                                bytesloaded: this.bytesLoaded,
                                bytestotal: this.bytesTotal
                            });
                        },
                        onload: function () {

                        }
                    });
                    r.cause("soundLoaded", {
                        soundid: i
                    });
                }
            }
        },
        playSound: function(e, d, r) {
            //console.log(d);
            // {soundid: name, starttime: milliseconds }
            if (this.fgsounds[d.soundid] && (d.starttime < this.fgsounds[d.soundid].duration ||  
            this.fgsounds[d.soundid].readyState===0)) {
                
                this.fgsounds[d.soundid].stop();
                this.fgsounds[d.soundid].play({
                    position: d.starttime
                });
            }
        },
        currentLoop: 0,
        currentLoopSound: "",
        loopSound: function(e, d, r) {
            var looplength = 16000;
            //sixteen seconds
            var snd = this.bgsounds[d.soundid];
            if (snd) {
                //
                if (this.currentLoop) {
                    r.clearEvery(this.currentLoop);
                    this.bgsounds[this.currentLoopSound].stop();
                }
                this.currentLoop = r.causeEveryNMilliseconds(looplength, "playSound", {
                    soundid: d.soundid
                });
                this.currentLoopSound = d.soundid;
            }
        },
        stopSound: function(e, d, r) {
            soundManager.stopAll();
            
    
        }
    };
    ngv.ed.registerListener(soundplayer);
})();

