var sounds = new Object();
var playing_sound;

function registerSound(name, filename)
{
  sounds[name] = filename;
}

function initSounds()
{
  document.write('<span id="soundspan">');
  if (navigator.userAgent.indexOf('Opera') == -1)
    for (i in sounds)
    {
      var str = '<embed name="snd_' + i + '" id="snd_' + i + '" src="' + sounds[i] + '" loop="false" autostart="false" width="0" height="0" enablejavascript="true" />';
      str = '<applet code="AudioPlay.class" name="snd_' + i + '" id="snd_' + i + '" width="1" height="1"><param name="image" value="play.gif"><param name="audio" value="' + sounds[i] + '"></applet>';

      document.write(str);
    }    
  document.write('</span>');
}

function playSound(name) {
  playing_sound = document.getElementById('snd_' + name);
  // the embedded object won't exist if we are in Opera.
  if (playing_sound)
  {
    try 
    {
    playing_sound.Play();
    }
    catch (e)
    {
      // Just catch the error if it happens, please.
    }
  }
  else
  {
    // play sound in Opera
    playOperaSound(name);
  }
}

function stopSound() {
  if (playing_sound)
    playing_sound.Stop();
}

function playOperaSound(name) {
  document.all.soundspan.innerHTML = '<embed src="' + sounds[name] + '" hidden="true" autostart="true" loop="false" />';
  // sound has started automatically, so clear the div.
  document.all.soundspan.innerHTML = '';
}