Whquestion2 = {}; //namespace
var Utils = {};
Utils.Path = " ";
Utils.mobileDeviceFlag = false;
//global variable 
var resId = 0, questionId = 0, startTime = 0, totalMarks = 10, level = 4, scoredMarks = 0, label = "null", scoreFlag = "false", wrongSoundDuration = 0,array = [0,1,2],clicked_ans =" ";

$(document).ready(function () {
    $(".se-pre-con").fadeOut("slow");
    if ((navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) && !(window.location.href.includes('http')))
        Utils.mobileDeviceFlag = true;
    /* if (Utils.mobileDeviceFlag)
        Utils.Path = Android.getMediaPath("Wh-Question2"); */
    setTimeout(function () {Whquestion2.Initilize(); }, 200);
});

Whquestion2.Initilize = function () {
   // $("#playGame").css("bottom","4vw");
    document.body.style.backgroundImage = "url('images/skin/cover.gif')";
    Whquestion2.setImage();
    document.title = Whquestion2.hardcodedData.title;
    $("#nextStep").addClass("pulse");
    $("#homeDiv").css('visibility', 'hidden');
    $("#widget").hide();
    $("#gamePage").hide();
    $("#endPage").hide();
    wrongBuzzerDuration();
   Whquestion2.variableInitilization();
    Whquestion2.createAcknowledgementDivs();
}
Whquestion2.setImage = function () { //set All images
    document.getElementById("homeImg").src = Utils.Path + "images/skin/home.png";
    document.getElementById("logoImg").src = Utils.Path + "images/skin/logo.png";
    document.getElementById("nextStep").src = Utils.Path + "images/skin/play.png";
    document.getElementById("soundImg").src = Utils.Path + "images/skin/sound.png";
    document.getElementById("nextImg").src = Utils.Path + "images/skin/next.png";
    document.getElementById("playAgain").src = Utils.Path + "images/skin/replay.png";
    document.getElementById("creditImg").src = Utils.Path + "images/skin/credits.png";
    document.getElementById("quesBox").style.backgroundImage = Utils.Path+"url('images/skin/grid box.png')"
    document.getElementById("gameOver").innerHTML = Whquestion2.hardcodedData.gameOver;
   // document.getElementById("instrText").innerHTML = Whquestion2.hardcodedData.instrText;
}
Whquestion2.variableInitilization = function(){
    Whquestion2.questionArray  = [] ; // 5 unique ques
    Whquestion2.cnt = 0 ; //current ques count
    Whquestion2.wrongTimer; // wrong ans timer
    Whquestion2.rightAnsTimer ; 
   // Whquestion2.nextQuesTimer ; //next ques timer
    Whquestion2.wrongAnsCnt = 0 ;
    Whquestion2.soundFlag = false;
    Whquestion2.hintTimer =0;
   
}
Whquestion2.startGame = function(){
    document.body.style.backgroundImage = "url('images/skin/bg.png')";
    $("#homeDiv").css('visibility', 'visible');
    $("#widget").show();
    $("#gamePage").show();
    $("#playGame").hide();
     Whquestion2.questionArray = populate(Whquestion2.data , Whquestion2.questionArray , 5);
     console.log(Whquestion2.questionArray);
    Whquestion2.createoptionDiv();
    Whquestion2.setData();
}
Whquestion2.playInstruction = function()
{
    document.getElementById("playSound").pause();
    document.getElementById("playSound").src = Utils.Path+"sound/instruction.mp3";
    document.getElementById("playSound").play();
}
Whquestion2.createoptionDiv = function(){
    var optionDivTag,spanDiv,hintDiv;
    Whquestion2.playInstruction();
    for(var i=0;i<3;i++){
        optionDivTag = document.createElement("div"); 

        optionDivTag.setAttribute("id","optionDivId"+i);
        optionDivTag.setAttribute("class","noPadding bgClass optionStyle optionClass col-sm-3 col-xs-3 col-md-3 col-lg-3 col-sm-offset-0 col-xs-offset-0 col-lg-offset-0 col-md-offset-0");
        optionDivTag.setAttribute("onclick","Whquestion2.checkAns(this.id)");
        optionDivTag.style.backgroundImage = Utils.Path+"url('images/skin/time box.png')";
        document.getElementById("optionDiv").appendChild(optionDivTag);
    }
    for(var i=0;i<3;i++){
        hintDiv = document.createElement("div");
        hintDiv.setAttribute("id","hintDiv"+i);
        hintDiv.setAttribute("class","noPadding bgClass hintStyle showHintClass col-sm-1 col-xs-1 col-md-1 col-lg-1 col-sm-offset-2 col-xs-offset-2 col-lg-offset-2 col-md-offset-2");
        $(hintDiv).data("data",i);
        hintDiv.setAttribute("onclick","Whquestion2.showHint(this.id)");
        hintDiv.style.backgroundImage = Utils.Path+"url('images/skin/help.png')";
        document.getElementById("hintDivId").appendChild(hintDiv);
    }
}
Whquestion2.setData = function(){
    d = new Date();  //date object
	startTime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();  //to get the current date
    
    var optionArr =[];
    //document.getElementById("playSound").pause();
         shuffle(array);
     //optionArr = shuffle(Whquestion2.data[Whquestion2.questionArray[Whquestion2.cnt]].optionArr);
    questionId = Whquestion2.data[Whquestion2.questionArray[Whquestion2.cnt]].questionId;
    $("#ques1").text(Whquestion2.data[Whquestion2.questionArray[Whquestion2.cnt]].ques);
    for(var i=0;i<3;i++){
        $("#optionDivId"+i).text(Whquestion2.data[Whquestion2.questionArray[Whquestion2.cnt]].optionArr[array[i]]);
        $("#optionDivId"+i).data("dataText",Whquestion2.data[Whquestion2.questionArray[Whquestion2.cnt]].optionArr[array[i]]);
        if(Whquestion2.data[Whquestion2.questionArray[Whquestion2.cnt]].optionArr[array[i]] == Whquestion2.data[Whquestion2.questionArray[Whquestion2.cnt]].correctAns)
               $("#optionDivId"+i).addClass("opacityClass");
    }
}
/* Whquestion2.playInstruction = function(){
    document.getElementById("playSound").pause();
    document.getElementById("playSound").src = Utils.Path+Whquestion2.data[Whquestion2.questionArray[Whquestion2.cnt]].sound;
    document.getElementById("playSound").play(); 
    
} */
Whquestion2.checkAns = function(clicked_option){
    clearTimeout(Whquestion2.wrongTimer);
    clearTimeout(Whquestion2.nextQuesTimer);
    clearTimeout( Whquestion2.hintTimer );
    document.getElementById("playSound").pause();
    scoreFlag = true;
    if(Whquestion2.wrongTimer){
        Whquestion2.wrongTimer = null ;
        clearTimeout(Whquestion2.wrongTimer);
    }
   
   if($("#"+clicked_option).data("dataText") == Whquestion2.data[Whquestion2.questionArray[Whquestion2.cnt]].correctAns){
        $("#"+clicked_option).addClass('checkAnsClass');
        $("#"+clicked_option).addClass('animated flash');
         scoredMarks = 10 ;
         $(".optionStyle  , #soundImg ,.hintStyle").css("pointer-events","none");
         Whquestion2.rightAns();
      
    }
    else{
        document.getElementById("playSound").src = Utils.Path+"sound/BuzzerWrong.mp3";
        document.getElementById("playSound").play();
        $("#"+clicked_option).addClass('checkwrongAnsClass');
        $("#"+clicked_option).addClass('animated shake');
        $("#"+clicked_option).css({'opacity':"0.5" , "pointer-events":"none"});
        scoredMarks = 0;
        Whquestion2.wrongAnsCnt++ ;
        Whquestion2.wrongTimer = setTimeout(function(){
            $(".optionStyle").removeClass('checkwrongAnsClass');
            $(".shake").removeClass('animated shake');
        },250);
        if(Whquestion2.wrongAnsCnt == 1){
            $(".optionStyle , .hintStyle").css("pointer-events","none");
            Whquestion2.rightAnsTimer = setTimeout(() => {
                $(".opacityClass").addClass("checkAnsClass");
                $(".optionStyle").css("opacity","0.5");
                $(".opacityClass").css("opacity","1");
                Whquestion2.rightAns();
            }, wrongSoundDuration*1000);
        }
    }
} 
Whquestion2.rightAns = function(){
    
     Whquestion2.soundFlag = true;
     src = Utils.Path +"sound/"+Whquestion2.data[Whquestion2.questionArray[Whquestion2.cnt]].sound;
    Whquestion2.getDuration(src);
}
Whquestion2.getDuration = function(src){
    var duration = 0;
    document.getElementById("playSound").pause();
    clearTimeout(Whquestion2.rightAnsTimer);
    clearTimeout(Whquestion2.wrongTimer);
    clearTimeout( Whquestion2.nextBtnTimer);
    if(Whquestion2.soundFlag){
        $('#playSound').on("loadedmetadata", function () {
            Whquestion2.soundFlag = false;
            duration = $('#playSound').prop('duration');
            $('#playSound').unbind("loadedmetadata");
            Whquestion2.nextBtnTimer = setTimeout(() => {
                $("#nextImg").addClass("bounce");
            }, duration*1000);;
        });
        document.getElementById("playSound").src = src;
        document.getElementById("playSound").play();
    }
}
Whquestion2.nextQues = function(){
    clearTimeout(Whquestion2.rightAnsTimer);
    clearTimeout(Whquestion2.wrongTimer);
    clearTimeout( Whquestion2.nextBtnTimer);
    $("#nextImg").removeClass('bounce');
    $(".optionStyle").css({"color":"white"});
    $(".opacityClass").removeClass("opacityClass");
    $(".hintStyle").addClass("showHintClass");
    document.getElementById("playSound").pause();
    if(!scoreFlag)
         scoredMarks = -1;
    label = Whquestion2.data[Whquestion2.questionArray[Whquestion2.cnt]].correctAns;
    Whquestion2.scoreFunction();
    scoreFlag = false;
    Whquestion2.wrongAnsCnt = 0;
     $(".flash").removeClass('animated flash');
    $(".optionStyle,.hintStyle").css({"pointer-events":"auto" , "opacity":"1"});
    $("#soundImg").css("pointer-events","auto");
    $(".optionStyle").text("");
    $(".quesStyle").text("");
    $(".checkAnsClass").removeClass("checkAnsClass");
    Whquestion2.cnt++;
    if(Whquestion2.cnt < 5 ){
          Whquestion2.setData();
    }
    else{
        Whquestion2.gameOver();
    }
}
Whquestion2.showHint = function(clicked_Div){
    document.getElementById("playSound").pause();
    if(Whquestion2.hintTimer)
    {
        Whquestion2.hintTimer = null;
        clearTimeout(Whquestion2.hintTimer);
    }
    var idNum = $("#"+clicked_Div).data("data");
    if( $("#"+clicked_Div).hasClass("showHintClass")){
       
          $("#optionDivId"+idNum).css({"transform":"rotateX(360deg)" , "color":"rgba(0,255,241,1)"}); 
          $("#"+clicked_Div).removeClass("showHintClass");
         // $("#optionDivId"+idNum).text("");
          $("#optionDivId"+idNum).text(Whquestion2.data[Whquestion2.questionArray[Whquestion2.cnt]].hintText[array[idNum]]);
         // $("#spanDivId"+idNum).show();
          // $(".optionStyle , .hintStyle").css({"pointer-events":"none"});
          Whquestion2.hintTimer = setTimeout(function(){
                 //$(".optionStyle , .hintStyle").css({"pointer-events":"auto"});
                $("#optionDivId"+idNum).css("transform",""); 
                 //$("#optionDivId"+idNum).text(Whquestion2.data[Whquestion2.questionArray[Whquestion2.cnt]].optionArr[array[idNum]]);
             },200);
    }
    else{
        $("#optionDivId"+idNum).css({"transform":"rotateX(360deg)" , "color":"white"}); 
        $("#"+clicked_Div).addClass("showHintClass");
        $("#optionDivId"+idNum).text(Whquestion2.data[Whquestion2.questionArray[Whquestion2.cnt]].optionArr[array[idNum]]);
        Whquestion2.hintTimer = setTimeout(function(){
            //$(".optionStyle , .hintStyle").css({"pointer-events":"auto"});
           $("#optionDivId"+idNum).css("transform",""); 
            //$("#optionDivId"+idNum).text(Whquestion2.data[Whquestion2.questionArray[Whquestion2.cnt]].optionArr[array[idNum]]);
        },200);
    }
    
  //  $(".opacityClass").addClass("checkAnsClass");
    //$(".optionStyle").css("opacity","0.5");
    //$(".opacityClass").css("opacity","1");
   // $("#nextImg").addClass('bounce');

}
Whquestion2.replayGame = function () { //playAgain
    document.getElementById("playSound").pause();
    clearTimeout(Whquestion2.nextQuestimer);
    clearTimeout(Whquestion2.wrongTimer);
    Whquestion2.resetVariable();
    Whquestion2.questionArray = populate(Whquestion2.data , Whquestion2.questionArray , 5);
    $(".optionStyle").css({"color":"white"});
    $(".optionStyle").text("");
    $(".quesStyle").text("");
    $("#endPage").hide();
    //$("#instrText").css("visibility","visible");
     $("#gamePage").show();
     Whquestion2.playInstruction();
     Whquestion2.setData();
}

Whquestion2.resetVariable = function () { //reset all variable
    $(".flash").removeClass('animated flash');
    $(".optionStyle").css("pointer-events","auto");
    $(".checkAnsClass").removeClass("checkAnsClass");
     scoreFlag = false;
     $(".opacityClass").removeClass("opacityClass");
     $(".hintStyle").addClass("showHintClass");
     Whquestion2.wrongAnsCnt = 0;
     Whquestion2.cnt = 0;
     Whquestion2.soundFlag = false;
     Whquestion2.questionArray = [];
}
Whquestion2.goHome = function () {
    clearTimeout(Whquestion2.nextQuestimer);
    clearTimeout(Whquestion2.wrongTimer);
  document.getElementById("playSound").pause();
    if(scoreFlag)
      Whquestion2.scoreFunction();

    location.reload();
}

Whquestion2.gameOver = function () { // game over
    document.getElementById("playSound").pause();
    clearTimeout(Whquestion2.nextQuestimer);
    $("#gamePage").hide();
    $("#endPage").show();
   // $("#instrText").css("visibility","hidden");
    $("#acknowledgementDiv").hide();
}

Whquestion2.createAcknowledgementDivs = function () { //create Aknowlegment div
    var i = 0, div;
    for (i = 0; i < Object.keys(Whquestion2.AcknowledgementPageData).length; i++) {
        div = document.createElement('div');
        div.className = "fontClass firstLetter col-xs-12 col-sm-12 col-md-12 col-lg-12";
        div.innerHTML = Object.keys(Whquestion2.AcknowledgementPageData)[i] + " : " + Object.values(Whquestion2.AcknowledgementPageData)[i];
        acknowledgementDiv.appendChild(div);
    }
    div = document.createElement('div');
    div.className = "fontClass firstLetter col-xs-12 col-sm-12 col-md-12 col-lg-12";
    div.style.marginBottom = "2vh";
    acknowledgementDiv.appendChild(div);
}

Whquestion2.showAcknowledgement = function () { //show Aknowlegment
    $("#acknowledgementDiv").show();
}

Whquestion2.scoreFunction = function () { //score function
    console.log(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
    if (Utils.mobileDeviceFlag) {
        Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime);
    }
}
//get wrong buzzer duration
wrongBuzzerDuration = function(){
    document.getElementById("playSound").pause();
  
   $('#playSound').on("loadedmetadata", function () {
   // Whquestion2.soundFlag = false;
   wrongSoundDuration = $('#playSound').prop('duration');
    $('#playSound').unbind("loadedmetadata");
    
}); 
 document.getElementById("playSound").src = Utils.Path+"sound/BuzzerWrong.mp3"; 
 
}
//function for unique Question
function populate(array1, array2, length) {

    while (array2.length < length) {
        var randomImageData = Math.floor(Math.random() * array1.length);
        if (array2.indexOf(randomImageData) == -1) {
            array2.push(randomImageData);
        }
    }
    return array2;
}
//shuffle the options
function shuffle(sarray) {

    var arrayLength = sarray.length;
    var temp, random_index;
    while (arrayLength > 0) {
        random_index = Math.floor(Math.random() * arrayLength);
        arrayLength--;
        temp = sarray[arrayLength];
        sarray[arrayLength] = sarray[random_index];
        sarray[random_index] = temp;
    }

    return (sarray);
}
