
function onloadFunction() {
  var t,
  timeout = 30000;
  firstTime = 0;
  logoWrap = document.getElementById("logoWrap");
  botEnglish = document.getElementById("bot");
  botSpanish = document.getElementById("botSpanish");
  buttonsWrap = document.getElementById("buttonsWrap");
  logoWrap.style.display = "block";
  botEnglish.style.display = "none";
  botSpanish.style.display = "none";
  buttonsWrap.style.display = "none";
  function resetTimer() {
    //   console.log("reset: " + new Date().toLocaleString());
    if (t) {
      window.clearTimeout(t);
    }
    t = window.setTimeout(logout, timeout);
  }
  function logout() {
    //   console.log("logout : " + new Date().toLocaleString());
    if (firstTime == 0) {
      logoWrap.style.display = "block";
      buttonsWrap.style.display = "block";
      botEnglish.style.display = "none";
      botSpanish.style.display = "none";
      firstTime++;
     //  console.log(firstTime);
    }
  }
  resetTimer();
  //And bind the events to call `resetTimer()`           
  ["click", "mousemove", "keypress"].forEach(function (name) {
    //  console.log(name);
    document.getElementById("logoWrap").addEventListener(name, resetTimer);

  });

}

document.getElementById("logoWrap").addEventListener("click", openChatWindow);
document.getElementById("selectEnglish").addEventListener("click", englishLanguage);
document.getElementById("selectSpanish").addEventListener("click", spanishLanguage);


function openChatWindow() {
  firstTime = 1;
  logoWrap.style.display = "block";
  botEnglish.style.display = "none";
  botSpanish.style.display = "none";
  buttonsWrap.style.display = "block";
}
function turn_Off_On_Sound() {
 //  console.log('clciked mute');
  window.speechSynthesis.cancel();
  window.SpeechSynthesisUtterance.volume = 0
  //window.speechSynthesisInstance.pause()

}
function turn_On_Sound() {
 //  console.log('clciked un mute');
  window.SpeechSynthesisUtterance.volume = 10
}

function closeChatWindow() {
  // alert('xx')        
  logoWrap.style.display = "block";
  botEnglish.style.display = "none";
  botSpanish.style.display = "none";
  buttonsWrap.style.display = "none";

}

function englishLanguage() {
  function guid() {
    return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
      s4() + '-' + s4() + s4() + s4();
  }
  function s4() {
    return Math.floor((1 + Math.random()) * 0x10000)
      .toString(16)
      .substring(1);
  }
  var userId = guid().toUpperCase();
  var userName = 'User-' + Math.floor((1 + Math.random()) * 10000);
  var secret = 'rNUseb9nYfo.lliG5Vk0miXezJMH4SPwqUBlilz_jUQaJxB9AqKTRe0';
  var user = {
    id: userId,
    name: userName
  };
  var bot = {
    id: 'Demo-WebAppBot',
    name: ' Demo ChatBot'
  };
  var botConnection = new BotChat.DirectLine({
    secret: secret,
    webSocket: true
  });
 //  console.log(botConnection);
  //   const speechOptions = {
  //     speechRecognizer: new BotChat.Speech.BrowserSpeechRecognizer(),
  //     speechSynthesizer: new BotChat.Speech.BrowserSpeechSynthesizer()
  //   };
  const speechOptions = {
    speechRecognizer: new CognitiveServices.SpeechRecognizer({ subscriptionKey: 'd7e8194024f8476e8537cf8fa54f5d09' }),
    speechSynthesizer: new BotChat.Speech.BrowserSpeechSynthesizer()
  };
  BotChat.App({
    botConnection: botConnection,
    user: user,
    bot: bot,
    locale: "en-US",  
    resize: 'detect',
    speechOptions: speechOptions
  }, document.getElementById("bot"));
 //  console.log("hiii");
  document.querySelector("#bot .wc-header").innerHTML = '<span>Firstsource Help</span><span class="dot"></span><span id="" class="icon-close closeChatWindow"></span><span  class="myBtn chatIConNew icon-volume--down"></span><span  class="myBtn chatIConMute icon-volume--mute"></span> ';

  botConnection.postActivity({ type: "event", from: user, name: "firstMessage", value: "100281" }).subscribe(id => console.log(""));
  window.addEventListener('beforeunload', (event) => {
    //event.returnValue = `Are you sure you want to leave?`;
   botConnection.postActivity({ type: "endConversation", from: user, name: "endConversation", value: "endofconvo" }).subscribe(id => console.log("Conversation ended"));
   });
  

  $(".chatIConNew").click(function () {
    turn_Off_On_Sound();
  });
  $(".chatIConMute").click(function () {
    turn_On_Sound();
  });
  $(".closeChatWindow").click(function () {
    closeChatWindow();
  });
 
  // $(".wc-suggested-actions").bind('click', function () {
  //   alert('yes clicked');
  // });

  // $(".wc-suggested-actions").click(function () {
  //   alert('yes clicked one');
  // });
  logoWrap.style.display = "none";
  setTimeout(() => {
    botEnglish.style.display = "block";
  }, 1000);
  botSpanish.style.display = "none";
  buttonsWrap.style.display = "none";
}

function spanishLanguage(cubrefnum ="100281") {
  function guid() {
    return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
      s4() + '-' + s4() + s4() + s4();
  }
  function s4() {
    return Math.floor((1 + Math.random()) * 0x10000)
      .toString(16)
      .substring(1);
  }
  var userId = guid().toUpperCase();
  var userName = 'User-' + Math.floor((1 + Math.random()) * 10000);
  var secret = 'rNUseb9nYfo.lliG5Vk0miXezJMH4SPwqUBlilz_jUQaJxB9AqKTRe0';
  var user = {
    id: userId,
    name: userName
  };
  var bot = {
    id: 'Demo-WebAppBot',
    name: ' Demo ChatBot'
  };
  var botConnection = new BotChat.DirectLine({
    secret: secret,
    webSocket: true
  });
 //  console.log(botConnection);
  const speechOptions = {
    speechRecognizer: new CognitiveServices.SpeechRecognizer({ subscriptionKey: 'd7e8194024f8476e8537cf8fa54f5d09' }),
    speechSynthesizer: new BotChat.Speech.BrowserSpeechSynthesizer()
  };
  BotChat.App({
    botConnection: botConnection,
    user: user,
    bot: bot,
    resize: 'detect',
    locale: "es-ES",
    speechOptions: speechOptions
  }, document.getElementById("botSpanish"));
 //  console.log("hiii");
  document.querySelector("#botSpanish .wc-header").innerHTML = '<span>Firstsource Help</span><span class="dot"></span><span id="" class="icon-close closeChatWindow"></span><span  class="myBtn chatIConNew icon-volume--down"></span><span  class="myBtn chatIConMute icon-volume--mute"></span> ';

  botConnection.postActivity({ type: "event", from: user, name: "firstMessage", value: "100281" }).subscribe(id => console.log(""));
  window.addEventListener('beforeunload', (event) => {
    botConnection.postActivity({ type: "endConversation", from: user, name: "endConversation", value: "endofconvo" }).subscribe(id => console.log("Conversation ended"));
   });
  $(".chatIConNew").click(function () {
    turn_Off_On_Sound();
  });
  $(".chatIConMute").click(function () {
    turn_On_Sound();
  });
  $(".closeChatWindow").click(function () {
    closeChatWindow();
  });
  logoWrap.style.display = "none";
  botEnglish.style.display = "none";
  setTimeout(() => {
    botSpanish.style.display = "block";
  }, 1000);

  buttonsWrap.style.display = "none";
}



