/**  play.js         written by Scott Honey
  tested ok on June 15, 1998 10:00pm
	A library of functions that control output for the Singing Syllbary program.
	There must be a frame called "SOUNDFRAME" to output the sounds, and
	a netscape 3.0 or higher to run this, otherwise it is very compatible.
	There is some linking to the array.js file.


FUNCTIONS:

	outputSound(STRING)
		sends the location of the sound to the browser so it can play. 

	playTonedSound(STRING)
		takes a toned sound (one like "ai3" ) and determines what sex to play it using the system defaults, and then sends it on it's way out of the system.

	playFullSound(STRING)
		Takes a sound that has both tone and sex prespecified and sends it on it's way out of the system.

	playShortSound(STRING)
		Takes a toneless, sexless sound and sends it on it's way using the system defaults.

	playAllSounds(STRING)
		takes a short sound, and plays all tonal variations in the system default sex.

	playBunch(ARRAY)
		takes a list of sounds and plays them all.  They can be of at any form of completion ("ai1" "ai" "ai1f")
	playSound(STRING)
		analyzises the sound to see what catagory it falls in (how much information is provided, like "ai" "ai1" "ai1f") and then sends it the right way...
	
	object multiPlay(ARRAY)
		creates an object that goes through and plays each sound of the array it is passed.  Works with multiPlaySound to do this.   Setting Soundlength at the top of this file will determine how long it waits before playing the next sound.

	multiPlaySound()
		a helper function for the object multiPlay(ARRAY)

	playMovie(STRING)
		launches a window and plays a movie in it...  It uses cookies for the titles of the movies -- this could be improved, but javascript is bad for persistant state and the workaround was taking too much time.


*/


//These variables are for passing state to the movie windows.
var moviemovie="";
var moviemovie1="";
var moviemovie2="";
var movienotes="";




//no need for a playSexedSound, because that information will come AFTER the sound and tone, and thus there is no way of determining it.  Also, the prgoram doesn't really make a big deal about male/female differences, whereas it does care about tones and the actual sounds...

//if the test_play.js.html isn't working, that might be because you are running it from the js directory.  To fix this, change the line below to Soundpath="../sounds/" and it should work - Remember to change it back!


/* outputSound(STRING)
	sends the location of the sound to the browser so it can play. */
function outputSound(soundString) {
	  //set the path of the sound and filetype.
  soundString = Soundpath + soundString + Soundextension
	 // clear should stop old sounds from piling up...

	//checking for umulated Us and turning them into uus...
  if (soundString.indexOf("ü")!=-1){
	pos = soundString.indexOf("ü");
	soundString = soundString.substring(0,pos)+"uu" +soundString.substring(pos+1,soundString.length);
	}
  SOUNDFRAME.document.clear();
  SOUNDFRAME.document.write("<body>");
  SOUNDFRAME.document.write("<embed SRC=\""+soundString+"\" autoplay=true><br>");
  SOUNDFRAME.document.write("</body>");
  SOUNDFRAME.document.close();
}


/* playTonedSound(STRING)
	takes a toned sound (one like "ai3" ) and determines what sex to play it using the system defaults, and then sends it on it's way out of the system.  */
function playTonedSound(sound){
    if (Defaultsex=="female"){
      sound = sound +"f";
    }
    else {
      if (Defaultsex=="male"){
	sound = sound +"m";
      }
    }

    outputSound(sound);
}


/* playFullSound(STRING)
	Takes a sound that has both tone and sex prespecified and sends it on it's way out of the system.*/
function playFullSound(sound){
  outputSound(sound);
}

/* playShortSound(STRING)
	Takes a toneless, sexless sound and sends it on it's way using the system defaults.*/
function playShortSound(sound) {
  sound = sound + Defaulttone+ "";
  playTonedSound(sound);
}

/* playAllSounds(STRING)
	takes a short sound, and plays all tonal variations in the system default sex.*/
function playAllSounds(sound){
  soundArray= new Array();
  soundArray[0]=sound+"1";
  soundArray[1]=sound+"2";
  soundArray[2]=sound+"3";
  soundArray[3]=sound+"4";
  soundArray[4]=sound+"5";
  playBunch(soundArray);
}

/* playBunch(ARRAY)
	takes a list of sounds and plays them all.  They can be of at any form of completion ("ai1" "ai" "ai1f")*/
function playBunch(soundList){
  // creating multiplay object
  mp = new multiPlay(soundList);
  mp.play()
}


/* playSound(STRING)
	analyzises the sound to see what catagory it falls in (how much information is provided, like "ai" "ai1" "ai1f") and then sends it the right way... */
function playSound(sound){
 sound = sound + "";
   if ((sound.charAt(sound.length-1)=='1') || 
	(sound.charAt(sound.length-1)=='2') || 
	(sound.charAt(sound.length-1)=='3') ||
	(sound.charAt(sound.length-1)=='4') ||
	(sound.charAt(sound.length-1)=='5')) {
       playTonedSound(sound);}
    else {
      if (((sound.charAt(sound.length-2)=='1') || 
	   (sound.charAt(sound.length-2)=='2') || 
	   (sound.charAt(sound.length-2)=='3') ||
	   (sound.charAt(sound.length-2)=='4') ||
	   (sound.charAt(sound.length-2)=='5')) &&
          ((sound.charAt(sound.length-1)=='m') ||
           (sound.charAt(sound.length-1)=='f'))) {
         playFullSound(sound);
        }
      else {
         playShortSound(sound);
    }
  }
}


/* object multiPlay(ARRAY)
	creates an object that goes through and plays each sound of the array it is passed.  Works with multiPlaySound to do this.   Setting Soundlength at the top of this file will determine how long it waits before playing the next sound.*/ 
function multiPlay(soundList) {
    this.current = 0;
    this.list = soundList;
    this.listLength = soundList.length;
    this.wait = Soundlength; 
    this.play = multiPlaySound;  //links to multiPlaySound
}

/* multiPlaySound()
	a helper function for the object multiPlay(ARRAY)*/
function multiPlaySound() {
  if (this.current<this.listLength) {
    playSound(this.list[this.current ]);
//    alert (this.list[this.current] + " is this position, " +  this.current + "is the counter" + this.list + " is the damn array");
	this.current++
    //recursive call:: not obvious, but mp.play() points to this function.
    waiter = setTimeout('mp.play()', this.wait); 
  }
  else {
    this.current=0
  };
}	


/* playMovie(STRING)
	launches a window and plays a movie in it...  It uses cookies for the titles of the movies -- this could be improved, but javascript is bad for persistant state and the workaround was taking too much time.*/
function playMovie(sound) {
  moviemovie=sound;
 	//1 and 2 are different zoom levels of the movie.
  moviemovie1=Moviepath + sound+"1"+Movieextension;
  moviemovie2=Moviepath + sound+"2"+Movieextension;
  movienotes=Notespath + sound + Notesextension; 
  movieWindow=window.open("movie.html","_blank","Height=350,WIDTH=190");
}
 

