// The audioplayer variable is used as a reference
// to the current AudioPlayer applet.

var audioplayer;
var lastFileName = "";
var sound = "off";
var stack = 0;

// Assigns a reference to the AudioPlayer applet
// to audioplayer.
function initAudioPlayer() {
 audioplayer = document.applets["AudioPlayer"];
}

// Load and play the specified sound file.
function playSound(fileName) {
 // Prevent JS errors caused when painting page if mouse is on an audio
 if (sound == "off") return;
 // Prevent stack overflow
 if (stack > 2) return;
 if (audioplayer == null) initAudioPlayer();
 if (audioplayer == null) {
  alert("AudioPlayer cannot load.  Check that Java is installed.");
  return;
 }
 // Prevent stuttering when mouse hovers
 if (lastFileName == fileName) return;
 lastFileName = fileName;
 stack ++;
 audioplayer.playAudioClip(fileName);
 stack --;
 for (i=0; i<2000; i++) {};
}

// Load and play the specified sound file in a
// continuous loop.
function loopSound(fileName) {
 if (audioplayer == null) initAudioPlayer();
 if (audioplayer == null) {
  alert("AudioPlayer cannot load.  Check that Java is installed.");
  return;
 }
 // Prevent stuttering when mouse hovers
 if (lastFileName == fileName) return;
 lastFileName = fileName;
 audioplayer.loopAudioClip(fileName);
}

// Stop the playing of the sound file.
function stopSound(fileName) {
 lastFileName = "";  // reset stuttering control
 if (audioplayer == null) return;
 audioplayer.stopAudioClip();
}


