/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

var sfx = {};

function initSFX() {
    if (localStorage.getItem("alertsound") == null) {
        localStorage.setItem("alertsound", "sonar");
    }
    if (localStorage.getItem("alertvolume") == null) {
        localStorage.setItem("alertvolume", 100);
    }

    var alertNoiseName = localStorage.getItem("alertsound");
    var alertVolume = localStorage.getItem("alertvolume");

    sfx = {
        "alert": new Audio("assets/audio/alert." + alertNoiseName + ".mp3"),
        "ok": new Audio("assets/audio/ok.mp3"),
        "error": new Audio("assets/audio/error.mp3")
    };

    setVolume("alert", alertVolume);
}

/**
 * Play a sound.
 * @param string sound Name of the sound to play (alert, ok, error)
 * @returns {undefined}
 */
function playSound(sound) {
    sfx[sound].play();
}

/**
 * Set sound volume
 * @param string sound The name of the sound to set volume of
 * @param number volume Number in range 0 to 100
 */
function setVolume(sound, volume) {
    sfx[sound].volume = volume / 100.0;
}

initSFX();