
function voice_talk(text) {

	stop();

	// get all voices that browser offers
	var available_voices = window.speechSynthesis.getVoices();

	// this will hold an spanish voice
	var spanish_voice = '';

	// find voice by language locale "en-US"
	// if not then select the first voice
	for(var i=0; i<available_voices.length; i++) {
		if(available_voices[i].lang === 'es-ES') {
			spanish_voice = available_voices[i];
			break;
		}
	}
	if(spanish_voice === '')
		spanish_voice = available_voices[0];

	// new SpeechSynthesisUtterance object
	var utter = new SpeechSynthesisUtterance();
	utter.rate = 0.75;
	utter.pitch = 2;
	utter.text = text;
	utter.voice = spanish_voice;

	// speak
	window.speechSynthesis.speak(utter);


	
}


function voice_start(opciones) {
    window.SpeechRecognition = window.webkitSpeechRecognition || window.SpeechRecognition;
    let finalTranscript = '';
    let recognition = new window.SpeechRecognition();
    recognition.interimResults = true;
	recognition.lang = "es-ES"
    recognition.maxAlternatives = 10;
    recognition.continuous = true;
    recognition.onresult = (event) => {
      let interimTranscript = '';
      for (let i = event.resultIndex, len = event.results.length; i < len; i++) {
        let transcript = event.results[i][0].transcript;
		
		if (opciones.includes(transcript)) {
	
			if (transcript.includes("parte")) {
			  window.location.href = "/forkilla/triaAvatar";
			}else if(transcript.includes("María")){
			  window.location.href = "/forkilla/parte/?triaAvatar=María";
			}else if(transcript.includes("siguiente") && opciones.includes("1")){
			  window.location.href = "/forkilla/parte1";
			}else if(transcript.includes("siguiente") && opciones.includes("2")){
			  window.location.href = "/forkilla/parte11";
			}else if(transcript.includes("siguiente") && opciones.includes("3")){
			  window.location.href = "/forkilla/parte2";
			}else if(transcript.includes("atrás") && opciones.includes("0")){
			  window.location.href = "/forkilla";
			}else if(transcript.includes("atrás") && opciones.includes("1")){
			  window.location.href = "/forkilla/triaAvatar";
			}else if(transcript.includes("atrás") && opciones.includes("2")){
			  window.location.href = "/forkilla/parte";
			}else if(transcript.includes("atrás") && opciones.includes("3")){
			  window.location.href = "/forkilla/parte1";
			}else if(transcript.includes("atrás") && opciones.includes("4")){
			  window.location.href = "/forkilla/parte11";
			}else if(transcript.includes("hola")){
			  pause();
			}else{
			  window.location.href = "/forkilla/bateria";			  
			}
	    }
			
		
		recognition.start();
        if (event.results[i].isFinal) {
          finalTranscript += transcript;
        } else {
          interimTranscript += transcript;
        }
      }
      document.querySelector('body').innerHTML = finalTranscript + '<i style="color:#ddd;">' + interimTranscript + '</>';
    }
    recognition.start();
}

function speak() {
	var text = 'Hola, esto es una prueba. La frase es un poco larga.'
  var msg = new SpeechSynthesisUtterance(text);
  	var es_voice = '';
var voices = window.speechSynthesis.getVoices();
    for(i = 0; i < voices.length ; i++) {
      if(voices[i].lang === 'es-ES') {
        es_voice = voices[i];
	    break;
      }
    }

  msg.volume = 1; // 0 to 1
  msg.rate = 1; // 0.1 to 10
  msg.pitch = 0.75; //0 to 2
  window.speechSynthesis.speak(msg);
}

function pause() {
  window.speechSynthesis.pause();
  //if (window.speechSynthesis.speaking) {
  //  if (window.speechSynthesis.paused) {
  //    window.speechSynthesis.resume();
  //    //button2.innerText = "Pause";
  //  } else {
  //    window.speechSynthesis.pause();
  //    //button2.innerText = "Resume";
  //  }
  //}
}


function resume() {
  window.speechSynthesis.resume();
  //  if (window.speechSynthesis.paused) {
  //    window.speechSynthesis.resume();
  //    //button2.innerText = "Pause";
  //  }
}

function stop() {
  window.speechSynthesis.cancel();
}

(function()
{
   var page = document.querySelector("#bottomButtonWithMorePage"),
       drawer = page.querySelector("#moreoptionsDrawer"),
       handler = page.querySelector(".ui-more");

   page.addEventListener("pagebeforeshow", function() 
   {
      if (tau.support.shape.circle) 
      {
         tau.helper.DrawerMoreStyle.create(drawer, 
         {
            handler: ".drawer-handler",

         });
      }
   });
})();


function avatar_image(avatar) {
  var num = -1;
  if(avatar==1){
    num = 1;
    return "/static/forkilla/Avatar1.PNG";
  }else if(avatar==2){
    num = 2;
    return "/static/forkilla/Avatar2.PNG";
  }else if(avatar==1){
    num = 3;
    return "/static/forkilla/Avatar3.PNG";
  }else{
    if(num != -1){
	  return "/static/forkilla/Avatar" + num + ".PNG";
	}else{
	return -1;
	}
  }
  window.speechSynthesis.cancel();
}