/** JavaScript Document
 *  @author Nityanand Ojha
 */

const grade = 3;

var fntSize = [40, 35, 30];
var rubricfntSize = fntSize[grade - 1] + "px";
var quesfntSize = fntSize[grade - 1] + "px";
var optionFntSize = fntSize[grade - 1] - 10 + "px";
var fontFace = "Rubric, sans-serif";
var fontFace1 = "Ans, sans-serif";

var rArr = [
  "Amazing",
  "Brilliant",
  "Excellent",
  "Good",
  "GoodJob",
  "Superb",
  "Welldone",
  "Wonderful",
];
const cArr = ["Oops"];
var wArr = [];
var nArr = [
  "ThinkAgain",
  "LetsTryAgain",
  "LetsGiveAnotherShot",
  "TryOnceAgain",
];

var colorObj = { initial: "#fff", select: "rgb(89, 231, 231)" }; //"#99ffff"};
var storageArr = [];
var _main;
var _rubricTxt;
var _quesTxt;
var srialNo;
var option;
var correctMC;
var audio;
var animDiv;
var submitBtn;
var answerBtn;
var isBlank = false;
var blankTxtField = null;
var checkSol;
var nextBtn;
var preBtn;
var nextpreNav;
var selMC;
var Qno = 0;
var _quesImg;
var correctanswer=0
var _mainDivQue;
var _checkBtnImg;

const introDelay = 1000;
var introVid;
//////// G for grammer, V for vocabulary..........

var sourceOGG = "";
var sourceMP3 = "";

//var introType = "";
var scorm = pipwerks.SCORM;
scorm.version = "1.2";

let scormInitialized = false;

// SCORM Initialization (on page load)
function initializeSCORM() {
  if (typeof pipwerks.SCORM !== 'undefined' && !scormInitialized) {
      pipwerks.SCORM.init();  // Initialize SCORM connection
      pipwerks.SCORM.set("cmi.core.score.raw", 0); // Initialize score
      pipwerks.SCORM.set("cmi.core.lesson_status", "incomplete"); // Set lesson status to incomplete
      scormInitialized = true;  // Mark SCORM as initialized
  }
}

try {
  initializeSCORM();
} catch (error) {
  console.log("initializeSCORM" ,error);
}



startIntro();
function startIntro() {
  try {
    if (SCORM.init()) {
        console.log("SCORM session initialized.");
    } else {
        console.warn("SCORM API not available. Running in local mode.");
    }
} catch (error) {
    console.warn("SCORM initialization failed. Running in local mode.", error);
}
  introVid = document.createElement("video");
  titleText = document.createElement("div");
  document.body.appendChild(introVid);

  var sourceWebm = document.createElement("source");
  var sourceMp4 = document.createElement("source");
  introVid.appendChild(sourceWebm);
  introVid.appendChild(sourceMp4);
  introVid.style.position = "absolute";
  introVid.style.top = "0px";

  introVid.addEventListener("ended", onEnd);

  //titleText.style.width = "1280px";
  titleText.style.position = "absolute";
  titleText.style.fontFamily = "Title";
  titleText.style.opacity = 0;
  titleText.style.textAlign = "center";
  titleText.style.fontSize = "50px";
  document.body.appendChild(titleText);

  if (data.intro.length == 0) introVid.style.visibility = "hidden";

  switch (data.intro.toLowerCase()) {
    case "g":
      titleText.innerHTML = data.title || data._title;
      titleText.style.width = "630px";
      titleText.style.top = "400px";
      titleText.style.left = "590px";
      titleText.style.color = "#000";
      sourceWebm.src = "../__intro/Title_Grammar.webm";
      sourceMp4.src = "../__intro/Title_Grammar.mp4";
      introVid.load();
      introVid.play();
      introVid.play();
      setTimeout(showTitle, 3000);
      break;
    case "v":
      titleText.innerHTML = data.title || data._title;
      titleText.style.width = "712px";
      titleText.style.top = "400px";
      titleText.style.left = "205px";
      titleText.style.color = "#000";
      sourceWebm.src = "../__intro/Title_Vocab.webm";
      sourceMp4.src = "../__intro/Title_Vocab.mp4";
      introVid.load();
      introVid.play();
      setTimeout(showTitle, 5000);
      break;
    default:
      feedbackVid.init();
      introVid.removeEventListener("ended", onEnd);
      $(titleText).animate({ opacity: 0 }, 1);
      $(introVid).animate({ opacity: 0 }, 1, odd_one_out);
      document.getElementById("main").style.backgroundImage =
        "url('Assets/images/bg.jpg')";
      break;
  }

  //submitBtn = document.getElementById("submit");
  //answerBtn = document.getElementById("answer");
  //submitBtn.style.visibility = "hidden";
  //answerBtn.style.visibility = "hidden";
}

const scaleIt = (element) => {
  const checkheight = window.innerHeight;
  const checkwidth = window.innerWidth;
  const mydiv = document.querySelector("div#main");
  const mydivHeight = mydiv.clientHeight;
  const mydivWidth = mydiv.clientWidth;

  // Calculate the scaling factor based on height and width
  let calculatedScaleH = (checkheight / mydivHeight).toFixed(2) - 0.18;
  let calculatedScaleW = (checkwidth / mydivWidth).toFixed(2);

  // Get the minimum of both scale factors
  let calculatedScale = Math.min(calculatedScaleH, calculatedScaleW).toFixed(2);

  // Apply the scale if it's less than 1
  if (calculatedScale < 1) {
    element.style.transform = `scale(${calculatedScale})`;
  } else {
    // Remove scaling if it exceeds the 1x factor
    element.style.transform = "scale(1)";
  }
};

// Optionally, you can call the function on window resize to adjust scaling dynamically
window.addEventListener("resize", () => scaleIt(elementToScale));

function showTitle() {
  $(titleText).animate({ opacity: 1 }, 1000);
}

function onEnd() {
  document.getElementById("main").style.backgroundImage =
    "url('Assets/images/bg.jpg')";
  feedbackVid.init();
  introVid.removeEventListener("ended", onEnd);
  $(titleText).animate({ opacity: 0 }, introDelay);
  $(introVid).animate({ opacity: 0 }, introDelay, odd_one_out);
}

function odd_one_out() {
  document.body.removeChild(titleText);
  titleText = null;
  document.body.removeChild(introVid);
  introVid = null;

  for (var i = 0; i < data.questions.optionArr.length; i++) {
    storageArr.push({ attempt: 0, option: undefined });
  }

  _main = getElement(MAINTAG);

  // addingNextPre();
  checkSol = createTag("div");
  checkSol.setAttribute("id", "checkSolution");

  _main.appendChild(checkSol);

  answerBtn = createTag("div");
  answerBtn.setAttribute("id", "answer");
  answerBtn.innerHTML = "SOLUTION";
  // checkSol.appendChild(answerBtn);

  submitBtn = createTag("div");
  submitBtn.setAttribute("id", "submit");
  submitBtn.innerHTML = "CHECK";
  checkSol.appendChild(submitBtn);

  submitBtn.addEventListener("click", onSubmitClick);
  submitBtn.style.pointerEvents = "none";
  submitBtn.style.opacity = 0.5;
  answerBtn.addEventListener("click", onSolutionClick);

  srialNo = document.createElement("div");

  _main = document.getElementById("main");
  audio = document.createElement("audio");
  _main.appendChild(audio);
  sourceMP3 = document.createElement("source");
  audio.appendChild(sourceMP3);
  sourceOGG = document.createElement("source");
  audio.appendChild(sourceOGG);

  _mainDivQue = document.createElement("div");
  _mainDivQue.className = "maindiv-questions";

  _main.appendChild(_mainDivQue);

  _rubricTxt = document.createElement("div");
  _main.appendChild(_rubricTxt);

  _rubricTxt.setAttribute("id", "rubricTxt");
  _rubricTxt.style.maxWidth = "calc(100% - 60px)";
  rubricAud = document.createElement("span");
  _rubricTxt.append(rubricAud);
  rubricAud.setAttribute("id", "rubricAud");
  rubricAud.classList.add("customButton");
  rubricAud.style.backgroundImage = "url('../__images/audOFF.png')";
  rubricAud.addEventListener("click", onRubricAudClicked);

  rubText = document.createElement("span");
  _rubricTxt.append(rubText);
  // rubText.innerHTML = data._rubric || data.rubric;

  /*//_rubricTxt.innerHTML = data.rubric;
	_rubricTxt.style.background = "#fff";
	_rubricTxt.style.color = "#3d8eb9";
	_rubricTxt.style.padding = 10+"px";
	_rubricTxt.style.borderTopRightRadius = 10+"px";
	_rubricTxt.style.borderBottomRightRadius = 10+"px";
	_rubricTxt.style.marginTop = 15+"px";
	_rubricTxt.style.fontSize = rubricfntSize;
	_rubricTxt.style.fontFamily = fontFace;
	_rubricTxt.style.display = 'inline-block';
	_rubricTxt.style.width = Math.min(_rubricTxt.offsetWidth, 1150)+"px";
	
	*/

  // addQuestion(0);
  soundComplete();
  positionCheckBtn();

  // Example usage
  const elementToScale = document.querySelector("div#main");
  scaleIt(elementToScale);
}

function onRubricAudClicked(e) {
  rubricAud.style.backgroundImage = "url('../__images/audON.png')";

  sourceOGG.src = "Assets/rubric/rubric.ogg";
  sourceMP3.src = "Assets/rubric/rubric.mp3";
  audio.load();

  audio.play();
  audio.addEventListener("ended", rubComplete);
}

function rubComplete() {
  audio.removeEventListener("ended", rubComplete);
  rubricAud.style.backgroundImage = "url('../__images/audOFF.png')";
  // addQuestion(0);
}

function soundComplete() {
  //audio.removeEventListener("ended", soundComplete);
  addQuestion(0);
  // scaleIt(_main);
}

function addingNextPre() {
  nextpreNav = document.createElement("div");
  nextpreNav.className = "nextpreNav";
  nextpreNav.setAttribute("id", "nextpreNav");
  nextpreNav.style.top = 306 + "px";
  nextpreNav.style.left = 165 + "px";
  _main.appendChild(nextpreNav);

  nextBtn = document.createElement("div");
  nextBtn.style.visibility = "hidden";
  nextBtn.setAttribute("id", "nextBtn");

  preBtn = document.createElement("div");
  preBtn.style.visibility = "hidden";
  preBtn.setAttribute("id", "preBtn");
  // disableObj(preBtn);
  nextpreNav.appendChild(nextBtn);
  nextpreNav.appendChild(preBtn);

  nextpreNav.addEventListener("click", onNavigationClick);
}

function onNavigationClick(e) {
  if (e.target.id == "preBtn") {
    submitBtn.style.pointerEvents = "none";
    submitBtn.style.opacity = 0.5;
    removePrevious();
    addQuestion(--Qno);
    handlingDoneQues();

    if (!Qno) {
      disableObj(e.target);
      return;
    }
    // enableObj(getElement("nextBtn"));
  } else if (e.target.id == "nextBtn") {
    submitBtn.style.pointerEvents = "none";
    submitBtn.style.opacity = 0.5;
    Qno++;
    removePrevious();
    addQuestion(Qno);
    handlingDoneQues();

    if (Qno == data.questions.optionArr.length - 1) {
      console.log(Qno, " dddddddddddddddddddd ", data.questions.optionArr.length - 1);
      disableObj(e.target);
      return;
    }
    // enableObj(getElement("preBtn"));
  }
}

function disableObj(obj) {
  obj.style.pointerEvents = "none";
  obj.style.opacity = 0.5;
}

function enableObj(obj) {
  obj.style.pointerEvents = "auto";
  obj.style.opacity = 1;
}

function getElement(str) {
  return document.getElementById(str);
}

function remove_quesTxt() {
  _mainDivQue.removeChild(_quesTxt);
  _mainDivQue.removeChild(_quesImg);
	_quesTxt = null;
	_quesImg = null;
}


const flatArr =  data.questions.optionArr.flat();

const hasPng = flatArr.some(file => typeof file === 'string' && file.endsWith('.png'));
const hasJpg = flatArr.some(file => typeof file === 'string' && file.endsWith('.jpg'));



function addQuestion(indx) {
  //feedbackVid.doReset();
  submitBtn.style.visibility = "visible";
  answerBtn.style.visibility = "visible";

  // nextBtn.style.visibility = "visible";
  // preBtn.style.visibility = "visible";

  _quesTxt && remove_quesTxt();

  _quesTxt = document.createElement("div");
  _mainDivQue.appendChild(_quesTxt);
  // _quesTxt.style.position = "absolute";
  _quesTxt.innerHTML = data.questions.quesArr[indx].ques;

  _quesImg = document.createElement('img');
  if (data.questions.imgArr && data.questions.imgArr[indx]) {
    _quesImg.src = "Assets/img/"+data.questions.imgArr[indx];
    _quesImg.style.display = "block"; // Show the image
    //  _quesImg.style.width = "300px";
    //  _quesImg.style.height = "auto";
  } else {
    _quesImg.style.display = "none"; // Hide the image if not available
  }

  _quesImg.classList.add("quesImg");
  _mainDivQue.appendChild(_quesImg);

  if (data.questions.quesArr[indx].ques.indexOf("<input") > -1) {
    isBlank = true;
    blankTxtField = document.getElementById("txtF");
    blankTxtField.style.fontSize = quesfntSize;
    blankTxtField.style.fontFamily = fontFace1;
    blankTxtField.style.textAlign = "center";
    blankTxtField.style.width = blankSize[indx] + "px";
  } else {
    isBlank = false;
  }
  console.log(isBlank, " isBlank___");
  _quesTxt.style.top = _rubricTxt.offsetHeight + 70 + "px";
  _quesTxt.style.left = 80 + "px";
  _quesTxt.style.marginBottom = 20 + "px";
  // _quesTxt.style.backgroundImage = "url(Assets/images/question-bg.jpeg)";
  _quesTxt.style.padding = "20px 10px 10px";
  _quesTxt.style.border = "0px solid #F29B34";
  _quesTxt.style.fontSize = quesfntSize;
  _quesTxt.style.color = "#000";
  // _quesTxt.style.textShadow = "1px 2px 10px #000000e6";
  _quesTxt.style.fontFamily = fontFace1;

  var cWidth = Math.min(_quesTxt.offsetWidth, 1000);
  _quesTxt.style.width = cWidth + "px";



if(hasPng || hasJpg == true)
{


  for (var i = 0; i < data.questions.optionArr[indx].length; i++) {
    option = document.createElement("img");
    _mainDivQue.appendChild(option);
    // option.style.position = "absolute";
    	option.src = "Assets/img/"+data.questions.optionArr[indx][i];
    option.className = "optionDesign";
    option.style.textAlign = "center";
    option.style.fontFamily = fontFace1;
    option.style.fontSize = optionFntSize;
    // option.style.background = "transparent";
    option.id = "op" + i;
    option.style.cursor = "pointer";

    // var optionW = Math.max(option.offsetWidth, 150);
  	// option.style.width = "240px";
		option.style.height = "180px";
    option.style.display = "inline-block";
    option.style.margin = "5px";

    option.addEventListener("click", onOPClick);
  }

}
else{
    for (var i = 0; i < data.questions.optionArr[indx].length; i++) {
    option = document.createElement("div");
    _mainDivQue.appendChild(option);
    // option.style.position = "absolute";
    option.innerHTML = data.questions.optionArr[indx][i];
    option.className = "optionDesign";
    option.style.textAlign = "center";
    option.style.fontFamily = fontFace1;
    option.style.fontSize = optionFntSize;
    // option.style.background = "transparent";
    option.id = "op" + i;
    option.style.cursor = "pointer";

    // var optionW = Math.max(option.offsetWidth, 150);
    option.style.minWidth = "110px";
    option.style.display = "inline-block";
    option.style.margin = "5px";

    option.addEventListener("click", onOPClick);
  }
}




  correctMC = document.getElementById("op" + data.questions.Aarr[Qno]);
  console.log(correctMC.innerHTML, " cccccc");

  if (optionHorizontalArrange) {
    hArrangingOptions(indx);
  } else {
    vArrangingOptions(indx);
  }

  addNumbering(indx);
}

function hArrangingOptions(indx) {
  for (var i = 0; i < data.questions.optionArr[indx].length; i++) {
    var cc = document.getElementById("op" + i);

    if (i == 0) {
      cc.style.left = 120 + "px";
    } else {
      var prevOp = document.getElementById("op" + (i - 1));
      cc.style.left =
        parseInt(prevOp.style.left, 10) +
        (prevOp.offsetWidth + spacingBetweenOption) +
        "px";
    }

    cc.style.top =
      parseInt(_quesTxt.style.top, 10) +
      (_quesTxt.offsetHeight + optionBelow) +
      "px";
  }
}

function vArrangingOptions(indx) {
  for (var i = 0; i < data.questions.optionArr[indx].length; i++) {
    var cc = document.getElementById("op" + i);

    if (i == 0) {
      cc.style.top =
        parseInt(_quesTxt.style.top, 10) +
        (_quesTxt.offsetHeight + optionBelow) +
        "px";
    } else {
      var prevOp = document.getElementById("op" + (i - 1));
      cc.style.top =
        parseInt(prevOp.style.top, 10) +
        (prevOp.offsetHeight + spacingBetweenOption) +
        "px";
    }

    cc.style.left = 100 + "px";
    //cc.style.top = parseInt(_quesTxt.style.top,10)+(_quesTxt.offsetHeight+60)+"px";
  }
}

function addNumbering(indx) {
  srialNo.style.position = "absolute";
  srialNo.innerHTML = indx + 1;
  srialNo.style.width = 35 + "px";
  srialNo.style.textAlign = "center";
  srialNo.style.fontSize = quesfntSize;
  srialNo.style.fontFamily = fontFace1;
  srialNo.style.border = "8px solid rgb(173 83 10)";
  srialNo.style.borderRadius = "50%";
  _mainDivQue.appendChild(srialNo);
  srialNo.style.top = "-42px";
  srialNo.style.left = 0 + "px";
  srialNo.style.right = 0 + "px";
  srialNo.style.margin = "0 auto";
  srialNo.style.backgroundColor = "#fff";
  srialNo.style.padding = "8px";
}

function onOPClick(e) {
  selMC = e.target;

  for (var i = 0; i < data.questions.optionArr[Qno].length; i++) {
    var getChild = document.getElementById("op" + i);
    setColor(getChild, colorObj.initial);
  }
  setColor(selMC, colorObj.select);

  selMC.ans = selMC.id.replace("op", "");
  isBlank && (blankTxtField.value = selMC.innerHTML);
  enableBTN();
}

function onSubmitClick(e) {
  if (selMC.ans == data.questions.Aarr[Qno]) {
    setColor(selMC, "#009900");
    isBlank && (blankTxtField.style.color = "#009900");
    setTimeout(drawNextQuestion, 3000);
    // playFeedback("right");
    correctanswer+=1
    // scorm.set("cmi.core.lesson_status", "incomplete");
    var status=scorm.get("cmi.core.lesson_status");
    if (status === "incomplete"){
      scorm.set("cmi.core.score.raw", correctanswer);
    }
    feedbackVid.playCorrectFeedback(postSubmit);
    startStoring();
  } else {
    storageArr[Qno].attempt++;
    setColor(selMC, "#ff0000");
    isBlank && (blankTxtField.style.color = "#ff0000");
    setTimeout(drawIncorrectQuestion, 4000);
    // playFeedback("wrong");
    feedbackVid.playWrongFeedback(postSubmit);
  }
  disableOptions();
  disableBTN();
}

function postSubmit() {}

function drawNextQuestion() {
  // enableObj(getElement("preBtn"));

  if (Qno < data.questions.optionArr.length - 1) {
    Qno++;
    removePrevious();
    addQuestion(Qno);
  } else {
    disableOptions();
    disableBTN();
    // disableObj(nextBtn);
    // disableObj(preBtn);
    answerBtn.style.pointerEvents = "none";
    answerBtn.style.opacity = 0.5;
  }

  makeAnimationSpace();
  selMC = null;
  disableBTN();
  answerBtn.style.pointerEvents = "auto";
  answerBtn.style.opacity = 1;
  handlingDoneQues();
  positionCheckBtn();

  if (Qno < data.questions.optionArr.length - 1) {
    // enableObj(nextBtn);
  }

  if (Qno) {
    // enableObj(preBtn);
  }
}

function drawIncorrectQuestion() {
  enableOptions();
  answerBtn.style.pointerEvents = "auto";
  answerBtn.style.opacity = 1;

  if (Qno < data.questions.optionArr.length - 1) {
    // enableObj(nextBtn);
  }

  if (Qno) {
    // enableObj(preBtn);
  }

  for (var i = 0; i < data.questions.optionArr[Qno].length; i++) {
    setColor(selMC, colorObj.initial);
  }
  isBlank && (blankTxtField.style.color = "#000000");
  isBlank && (blankTxtField.value = "");

  if (storageArr[Qno].attempt >= attempt) {
    selMC = null;
    // playFeedback("correct");
    setColor(correctMC, "#009900");

    isBlank && (blankTxtField.value = correctMC.innerHTML);
    isBlank && (blankTxtField.style.color = "#009900");
    disableOptions();
    startStoring();

    if (Qno < data.questions.optionArr.length - 1) {
      setTimeout(drawNextQuestion, 3000);
            feedbackVid.playCorrectFeedback(postSubmit);
    } else {
      console.log("activity finished 2222222222222");
      // disableObj(nextBtn);
      // disableObj(preBtn);
      answerBtn.style.pointerEvents = "none";
      answerBtn.style.opacity = 0.5;
        feedbackVid.playCorrectFeedback(postSubmit);
      setTimeout(makeAnimationSpace, 3000);
    }

    // disableObj(nextBtn);
    // disableObj(preBtn);
    answerBtn.style.pointerEvents = "none";
    answerBtn.style.opacity = 0.5;
  }
}

function onSolutionClick() {
  for (var i = 0; i < data.questions.optionArr[Qno].length; i++) {
    setColor(document.getElementById("op" + i), colorObj.initial);
  }
  setColor(correctMC, "#009900");
  isBlank && (blankTxtField.value = correctMC.innerHTML);
  isBlank && (blankTxtField.style.color = "#009900");
  disableOptions();
  disableBTN();

  setTimeout(drawNextQuestion, 3000);

  // playFeedback("correct");
  startStoring();
}

function startStoring() {
  storageArr[Qno].option = correctMC;
}

function handlingDoneQues() {
  console.log(" 2222222222222222222");
  answerBtn.style.pointerEvents = "auto";
  answerBtn.style.opacity = 1;

  if (storageArr[Qno].option != undefined) {
    disableOptions();
    setColor(correctMC, "#009900");
    isBlank && (blankTxtField.value = correctMC.innerHTML);
    isBlank && (blankTxtField.style.color = "#009900");

    answerBtn.style.pointerEvents = "none";
    answerBtn.style.opacity = 0.5;
  }
}

function removePrevious() {
  for (var i = 0; i < data.questions.optionArr[Qno].length; i++) {
    var getPrevOption = document.getElementById("op" + i);
    _mainDivQue.removeChild(getPrevOption);
  }
}

function playFeedback(val) {
  var ranNum;
  switch (val) {
    case "right":
      ranNum = parseInt(Math.random() * rArr.length);
      console.log(ranNum);
      sourceOGG.src = "../__audio/" + rArr[ranNum] + ".ogg";
      sourceMP3.src = "../__audio/" + rArr[ranNum] + ".mp3";
      break;
    case "wrong":
      wArr = cArr;
      if (storageArr[Qno].attempt == 1) {
        wArr = wArr.concat(nArr);
      }
      if (storageArr[Qno].attempt == attempt) {
        for (var ii = 0; ii < nArr.length; ii++) {
          if (wArr.indexOf(nArr[ii]) > -1) {
            wArr.splice(wArr.indexOf(nArr[ii]), 1);
          }
        }
      }

      ranNum = parseInt(Math.random() * wArr.length);
      sourceOGG.src = "../__audio/" + wArr[ranNum] + ".ogg";
      sourceMP3.src = "../__audio/" + wArr[ranNum] + ".mp3";
      break;
    case "correct":
      sourceOGG.src = "../__audio/ThisIsTheCorrectAns.ogg";
      sourceMP3.src = "../__audio/ThisIsTheCorrectAns.mp3";
      break;
    case "completed":
      sourceOGG.src = "../__audio/completed.ogg";
      sourceMP3.src = "../__audio/completed.mp3";
      break;
  }

  audio.load();
  audio.play();
}
window.addEventListener("unload", function () {
  // Terminate SCORM session on exit
  try {
    SCORM.save();
    SCORM.quit();
} catch (error) {
    console.warn("SCORM session quit failed. Running in local mode.", error);
}
});

function makeAnimationSpace() {
  if (Qno) {
    // enableObj(preBtn);
  }
  for (var i = 0; i < data.questions.optionArr.length; i++) {
    if (storageArr[i].option == undefined) {
      return;
    }
  }
  
   feedbackVid.playCorrectFeedback(postSubmit);
  setTimeout(ani, 2000);
  // var actComDiv = document.createElement("div");
  // document.body.appendChild(actComDiv);
  // actComDiv.style.position = "absolute";
  // actComDiv.style.left = 0 + "px";
  // actComDiv.style.top = 0 + "px";
  // actComDiv.style.width = 0 + "vw";
  // actComDiv.style.height = 100 + "vh";
  // actComDiv.style.zIndex = "200";
  // actComDiv.style.backgroundColor = "#fff";

  // animDiv = document.createElement("div");
  // actComDiv.appendChild(animDiv);
  // animDiv.style.position = "absolute";
  // animDiv.style.width = 100 + "vw";
  // animDiv.style.height = 243 + "px";
  // animDiv.style.top = 243 + "px";
  // animDiv.style.overflow = "hidden";
  // animDiv.style.backgroundImage = "url(Assets/images/activityComplete.png)";
  // animDiv.style.backgroundSize = "100% 100%";
  // animDiv.style.backgroundRepeat = "no-repeat";
  // $(actComDiv).animate({ width: 10000 }, 500);
  // setTimeout(completedDelay, 500);

  // var actComDiv = document.createElement("div");
  // document.body.appendChild(actComDiv);
  // actComDiv.style.position = "fixed";
  // actComDiv.classList.add("actCompleted");
  // actComDiv.style.left = 0 + "px";
  // actComDiv.style.top = 0 + "px";
  // actComDiv.style.width = "100%";
  // actComDiv.style.height = "100vh";
  // actComDiv.style.zIndex = "200";
  // actComDiv.style.backgroundImage = "url(./Assets/images/act.png)";
  // actComDiv.style.backgroundSize = "cover";
  // actComDiv.style.backgroundRepeat = "no-repeat";

  // actComDiv.style.backgroundAttachment = "fixed";
  // actComDiv.style.backgroundPosition = "bottom";

  // $(actComDiv).animate(500);

  try {
    scorm.set("cmi.core.lesson_status", "completed");
    console.log("Lesson marked as completed in SCORM.");
  
    scorm.save();
  } catch (error) {
    console.warn("SCORM completion failed. Running in local mode.", error);
  }

}
function ani()
{
  var actComDiv = document.createElement("div");
  document.body.appendChild(actComDiv);
  actComDiv.style.position = "fixed";
  actComDiv.classList.add("actCompleted");
  actComDiv.style.left = 0 + "px";
  actComDiv.style.top = 0 + "px";
  actComDiv.style.width = "100%";
  actComDiv.style.height = "100vh";
  actComDiv.style.zIndex = "200";
  actComDiv.style.backgroundImage = "url(./Assets/images/act.png)";
  actComDiv.style.backgroundSize = "cover";
  actComDiv.style.backgroundRepeat = "no-repeat";

  actComDiv.style.backgroundAttachment = "fixed";
  actComDiv.style.backgroundPosition = "bottom";

  $(actComDiv).animate(500);


}

function completedDelay() {
  var activityText = document.createElement("img");
  animDiv.appendChild(activityText);
  activityText.src = "Assets/images/activity.png";
  activityText.setAttribute("id", "activityText");
  var completedText = document.createElement("img");
  animDiv.appendChild(completedText);
  completedText.src = "Assets/images/completed.png";
  completedText.setAttribute("id", "completedText");
  // playFeedback("completed");
}

function disableBTN() {
  // disableObj(nextBtn);
  // disableObj(preBtn);

  submitBtn.style.pointerEvents = "none";
  submitBtn.style.opacity = 0.5;
  answerBtn.style.pointerEvents = "none";
  answerBtn.style.opacity = 0.5;
}

function enableBTN() {
  submitBtn.style.pointerEvents = "auto";
  submitBtn.style.opacity = 1;
  answerBtn.style.pointerEvents = "auto";
  answerBtn.style.opacity = 1;

  if (Qno < data.questions.optionArr.length - 1) {
    // enableObj(nextBtn);
  }

  if (Qno) {
    // enableObj(preBtn);
  }
}

function disableOptions() {
  for (var k = 0; k < data.questions.optionArr[Qno].length; k++) {
    var getOp = document.getElementById("op" + k);
    getOp.style.pointerEvents = "none";
  }
}

function enableOptions() {
  for (var k = 0; k < data.questions.optionArr[Qno].length; k++) {
    var getOp = document.getElementById("op" + k);
    getOp.style.pointerEvents = "auto";
  }
}

function setColor(mc, co) {
  mc.style.backgroundColor = co;
}

function getElement(str) {
  return document.getElementById(str);
}

function createTag(str) {
  return document.createElement(str);
}

function positionCheckBtn() {
  var checkBtn = document.getElementById("checkSolution");
  setTimeout(
    (checkBtn.style.top =
      _mainDivQue.offsetTop + _mainDivQue.offsetHeight + 60 + "px"),
    2000
  );
}

/**orientation msg */

function checkOrientation() {
  var message = document.getElementById("rotateMessage");
  if (window.innerHeight > window.innerWidth) {
    message.style.display = "flex";
  } else {
    message.style.display = "none";
  }
}

window.addEventListener("resize", checkOrientation);
window.addEventListener("orientationchange", checkOrientation);

checkOrientation();

/***popup js */
document.getElementById("startButton").addEventListener("click", function () {
  document.getElementById("popup").style.display = "none";
});

document.getElementById("instruction").innerHTML = data.instruction;