/*
  JavaScript to make use of text to speech in browsers
  v0.1
  (c) 2010, http://www.text-to-speech-engine.com/
*/

/* 
  Current ID, to avoid replaying the same ID 
*/
var _tts_current="";

/* 
  tts_Speak()
  text: is the text to speak
  format: format to fetch, currently supports ogg and wav
  lang: language to speak in
  id: optional id to avoid playing same sound twice in a row (if set)
*/
function tts_Speak(text, format, lang, id, errid) {
  if (format == "ogg")
  {
    format = "ogg";
  }
  else
  {
    format = "wav";
  }
  text=text.replace(/\r/g," ");
  text=text.replace(/\n/g," ");
  text=text.replace(/\s+/g," "); // normalize whitespace
  _target = '/online/' + encodeURIComponent(text) + '.' + format;
  // make sure the same element isn't played
  // again and again because it is still selected
  if (id == undefined || id == "" || id != _tts_current) {
    _tts_current=id;
    try
    {
      if (errid != null && errid != "")
      {
        var ee = document.getElementById(errid);
	if (ee)
	{
          ee.innerHTML = "This demo currently requires HTML5 enabled browsers that support the .wav format. You can also download the sound <a href=\"" + _target + "\">here</a>.";
	}
      }
      var a=document.createElement('audio');
      a.setAttribute('src', _target); 
      // alert("playing " + text);
      var ret = a.play();
    }
    catch (e)
    {
      // alert("Got exception: " + e);
    }
  }
}

