///////////////////////////////////////////////////////////////////////
// JavaScript DHTML Utilies
// (C) 1998 Netscape Communications Corporation. All Rights Reserved.
// Written by Michael Bostock (mikebos@netscape.com)
///////////////////////////////////////////////////////////////////////

var isNav4, isIE4;
if (parseInt(navigator.appVersion.charAt(0)) >= 4) {
   isNav4 = (navigator.appName == "Netscape") ? true : false;
   isIE4 = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
}

///////////////////////////////////////////////////////////////////////
// The Sound Object

function Sound() {
  this.addSound = addSound;
  this.load = writeLayer;
  this.playSound = callPlaySound;
  if(isIE4) return;
  this.loaded = false;
  this.playing = false;
  this.snds = new Array();
  this.sndsNames = new Array();
  this.layer = new Layer(200);}

function addSound(sndUrl, sndName) {
  if(isIE4) return;
  this.snds[this.snds.length] = sndUrl;
  this.sndsNames[this.sndsNames.length] = sndName;
}

function writeLayer() {
  if(isIE4) return;
  this.layer.document.open();
  this.layer.document.write("<"+"SCRIPT"+">");
  this.layer.document.write("var i = "+this.snds.length+";");
  this.layer.document.write("</"+"SCRIPT"+">");
  for(var i = 0; i < this.snds.length; i++) {
    this.layer.document.write("<EMBED ");
    this.layer.document.write("SRC=\""+this.snds[i]+"\" ");
    this.layer.document.write("HIDDEN=TRUE ");
    this.layer.document.write("AUTOSTART=FALSE>\n");
  }
  this.layer.document.write("</BODY>");
  this.layer.document.close();
}

function callPlaySound(sndName, loop) {
  if(isIE4) return;
  var sndIndex = 0;
  for(var i = 0; i < this.snds.length; i++)
    if(this.sndsNames[i] == sndName)
      sndIndex = i;
  waitPlaySound(this, sndIndex, loop);
}

function waitPlaySound(snd, sndIndex, loop) {
  if(isIE4) return;
  if(snd.playing != false || snd.layer.i != snd.snds.length ||
     !snd.layer.document.embeds[sndIndex].IsReady())
    setTimeout(waitPlaySound, 50, snd, sndIndex, loop);
  else {
    var sndEmbed = snd.layer.document.embeds[sndIndex];
    realPlaySound(snd, sndEmbed, loop);
  }
}

function realPlaySound(snd, sndEmbed, loop) {
  if(isIE4) return;
  if(snd.playing != false || sndEmbed.IsReady() != true)
    setTimeout(realPlaySound, 50, snd, sndEmbed, loop);
  else {
    snd.playing = true;
    if(loop) sndEmbed.play(true);
    else {
      sndEmbed.play(false);
      loopCheckPlaying(snd, sndEmbed);
    }
  }
}

function loopCheckPlaying(snd, sndEmbed) {
  if(isIE4) return;
  if(sndEmbed.IsPlaying()) {
    setTimeout(loopCheckPlaying, 150, snd, sndEmbed);
  } else {
    snd.playing = false;
  }
}
