var SpeechServer = 'http://wa.eguidedog.net';
var Speech = {
  speechQueue: new Array(),
  speechId: 0,

  englishTTS: SpeechServer + '/cgi-bin/espeak.pl?cache=1&text=',
  mandarinTTS: SpeechServer + '/cgi-bin/ekho.pl?voice=mandarin&cache=1&text=',
  fastMandarinTTS: SpeechServer +
    '/cgi-bin/ekho.pl?voice=fast_mandarin&cache=1&text=',
  hindiTTS: SpeechServer + '/cgi-bin/espeak.pl?voice=hi&cache=1&text=',
  frenchTTS: SpeechServer + '/cgi-bin/espeak.pl?voice=fr&cache=1&text=',
  germanTTS: SpeechServer + '/cgi-bin/espeak.pl?voice=de&cache=1&text=',
  cantoneseTTS: SpeechServer + '/cgi-bin/ekho.pl?voice=cantonese&cache=1&text=',
  fastCantoneseTTS: SpeechServer +
    '/cgi-bin/ekho.pl?voice=fast_cantonese&cache=1&text=',

  // push speech into queue and play by sequence
  speak: function (text, lang) {
    var tts;
    switch (lang) {
      case 'Mandarin': tts = this.mandarinTTS; break;
      case 'FastMandarin': tts = this.fastMandarinTTS; break;
      case 'Hindi': tts = this.hindiTTS; break;
      case 'French': tts = this.frenchTTS; break;
      case 'German': tts = this.germanTTS; break;
      case 'Cantonese': tts = this.cantoneseTTS; break;
      case 'FastCantonese': tts = this.fastCantoneseTTS; break;
      case 'English': // keep English last as default
      default: tts = this.englishTTS;
    }

    var id = 's' + ++this.speechId;
    this.speechQueue.push(id);
    var speechRef = this;
    soundManager.createSound({
      id: id,
      url: tts + escape(text),
      multiShot: false,
      onfinish: function() {
        speechRef.speechQueue.shift();
        if (speechRef.speechQueue.length > 0) {
          // there are some speech waiting in the queue
          soundManager.play(speechRef.speechQueue[0]);
        }
      }
    });

    if (this.speechQueue.length == 1) {
      // this is the first speech in the queue, no need to wait, play it
      soundManager.play(id);
    }
  }
};

