/*

Quicktetris published by Thomas Kjeldahl Nilsson under 
Creative Commons Attribution 3.0 license.
License terms: http://creativecommons.org/licenses/by/3.0/

*/

/*

Quicktetris published by Thomas Kjeldahl Nilsson under 
Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 license.

You are free to copy, distribute and share the work under the following conditions:

* You must attribute the work in the manner specified by the author or licensor 
  (but not in any way that suggests that they endorse you or your use of the work).
* You may not use this work for commercial purposes. 
* You may not alter, transform, or build upon this work.

*/


var Sound = {

    soundOn: true,

    SoundBank: {
        rotation: "rotation",
        landing: "landing",

        clearedOneRow: "clearedOneRow",
        clearedTwoRows: "clearedTwoRows",
        clearedThreeRows: "clearedThreeRows",
        clearedFourRows: "clearedFourRows",

        ambientMusic: "ambientMusic"
    },

    loadSounds: function() {
        soundManager.createSound(this.SoundBank.rotation, 'assets/sound/rotation.mp3');
        soundManager.createSound(this.SoundBank.landing, 'assets/sound/landing.mp3');
        soundManager.createSound(this.SoundBank.clearedOneRow, 'assets/sound/clearedOneRow.mp3');
        soundManager.createSound(this.SoundBank.clearedTwoRows, 'assets/sound/clearedTwoRows.mp3');
        soundManager.createSound(this.SoundBank.clearedThreeRows, 'assets/sound/clearedThreeRows.mp3');
        soundManager.createSound(this.SoundBank.clearedFourRows, 'assets/sound/clearedFourRows.mp3');
        soundManager.createSound(this.SoundBank.ambientMusic, 'assets/sound/ambientMusic.mp3');
    },

    playAmbientMusic: function() {
        if (this.soundOn) {
            playLooped(this.SoundBank.ambientMusic);
        }
    },

    playRotationSound: function() {
        if (this.soundOn) {
            soundManager.play(this.SoundBank.rotation);
        }
    },

    playLandingSound: function() {
        if (this.soundOn) {
            soundManager.play(this.SoundBank.landing);
        }
    },

    playLoopedLandingSound: function() {
        if (this.soundOn) {
            playLooped(this.SoundBank.landing);
        }
    },


    playClearedSound: function(level) {
        if (this.soundOn) {

            if (!level || level > 4 || level < 1) {
                return;
            }

            switch (level) {
            case 1:
                soundManager.play(this.SoundBank.clearedOneRow);
                break;
            case 2:
                soundManager.play(this.SoundBank.clearedTwoRows);
                break;
            case 3:
                soundManager.play(this.SoundBank.clearedThreeRows);
                break;
            case 4:
                soundManager.play(this.SoundBank.clearedFourRows);
                break;
            }
        }
    }

	


};


function playLooped(soundID) {
    window.setTimeout(function() {
        soundManager.play(soundID, {
            onfinish: function() {
                playLooped(soundID);
            }
        });
    },
    1);
}

soundManager.onload = function() {
    Sound.soundOn = true;
    Sound.loadSounds();
};

soundManager.onerror = function() {
    // SM2 could not start, no sound support, something broke etc. Handle gracefully.
    Sound.soundOn = true;
};


