MixedFraction = {};
var Utils ={};
Utils.Path = " ";
//global variable 
var resId = 0, questionId = 0, startTime = 0, scoredMarks = 0, totalMarks = 10, label = "null", level = 3, scoreFlag = false,max = 10 , min = 4,gameAttemptFlag = false,setDataFlag = false,finalAnsDiv=0,finalAnsFlag = false;
$(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("Fraction Addition"); */
    setTimeout(function () { MixedFraction.Initilize(); }, 200);
});

MixedFraction.Initilize = function(){
    document.body.style.backgroundImage = "url('images/Cover.png')";
    MixedFraction.setImage();
	document.title = MixedFraction.hardcoded.title;
    $("#homeDiv").css('visibility','hidden');
    $("#gamePage").hide();
    $("#endPage").hide();
    $("#nextStep").addClass("pulse");
    $("#soundImg").hide();
    MixedFraction.variableInitilization();
    MixedFraction.createAcknowledgementDivs();
}

MixedFraction.setImage = function(){ //set all images
   // document.body.style.backgroundImage = "url('images/bg.png')";
    document.getElementById("homeImg").src = Utils.Path+"images/Home.png";
    document.getElementById("logoImg").src = Utils.Path+"images/logo.png";
    document.getElementById("nextStep").src = Utils.Path+"images/Play.png";
     document.getElementById("soundImg").src = Utils.Path+"images/Sound.png";
    document.getElementById("nextImg").src = Utils.Path+"images/Go.png";
    document.getElementById("innerQuesDivId").innerHTML = MixedFraction.hardcoded.questionText;
    document.getElementById("playAgain").src=Utils.Path+"images/replay.png";
    document.getElementById("creditImg").src=Utils.Path+"images/Credits.png";
    document.getElementById("textId").innerHTML = MixedFraction.hardcoded.ansText;
    document.getElementById("innerQuesDivId").style.backgroundImage = Utils.Path+"url('images/title bar.png')";
    document.getElementById("givenFindData").style.backgroundImage = Utils.Path+"url('images/grid box.png')";
    document.getElementById("digitalQuesDiv").style.backgroundImage = Utils.Path+"url('images/rule box.png')";
 /*    document.getElementById("numberDiv").style.backgroundImage = Utils.Path+"url('images/grid box.png')"; */
}
MixedFraction.startGame = function(){
    document.body.style.backgroundImage = "url('images/bg.png')";
    $("#homeDiv").css('visibility','visible');
    $("#gamePage").show();
    $("#playGame").hide();
    $("#digitalProblemQues").hide();
    numberKey();
    MixedFraction.calculation();
    MixedFraction.createDiv();
}
MixedFraction.variableInitilization = function(){
      MixedFraction.blinkId = 0; //blinked id number
      MixedFraction.currentQues = 0; // question number
      MixedFraction.DivId; //blinked id
      MixedFraction.questionArray=[];
      MixedFraction.Ans;
      MixedFraction.reminder;
      MixedFraction.ansStep;
      MixedFraction.correctAns;
      MixedFraction.wrongAns;
      MixedFraction.instructionFlag = false;
      MixedFraction.rightAnsFlag = false;
}
MixedFraction.calculation = function(){
    clearTimeout(MixedFraction.correctAns);
    clearTimeout(MixedFraction.wrongAns);
    document.getElementById("playSound").pause(); 
 var upperVal , lowerVal ,randomArry=[],minVal = 3,upvalArr=[],i=0;
 while(upvalArr.length < 5){
    var randVal = Math.floor(Math.random()*(max-min)+min);
    if(upvalArr.indexOf(randVal) == -1){
        upvalArr.push(randVal);
    }
 }
 while(MixedFraction.questionArray.length < 5){
  //  upperVal = Math.floor(Math.random()*(max-min)+min);
  upperVal = upvalArr[i]
    if(upperVal%2 == 0){
        if(upperVal == 6)
          lowerVal = MixedFraction.evenNumber(upperVal , minVal);
        else
          lowerVal = MixedFraction.oddNumber(upperVal ,minVal);
    }
    else{
          lowerVal = MixedFraction.evenNumber(upperVal , minVal);
    }
  if((randomArry.indexOf(upperVal) == -1) ||(randomArry.indexOf(upperVal) == -1)){
        randomArry.push(upperVal);
        randomArry.push(lowerVal);
    }
    MixedFraction.questionArray.push(randomArry);
    randomArry=[];
    i++;
 }
  console.log(MixedFraction.questionArray);
  MixedFraction.calculateAns();
}
MixedFraction.oddNumber = function(num1,num2){
    for(var i=num2 ; i < num1 ;i++ ){
        lowerVal = Math.floor(Math.random()*(num1 - num2)+num2);
        if(lowerVal % 2 ==0)
          continue;
        else
          break;
    }
    return lowerVal;
}
MixedFraction.evenNumber = function(num1,num2){
    for(var i=num2 ; i < num1 ;i++ ){
    lowerVal = Math.floor(Math.random()*(num1 - num2)+num2);
    if(lowerVal % 2 !=0)
      continue;
    else
      break;
    }
    return lowerVal;
}

MixedFraction.calculateAns = 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

    clearTimeout(MixedFraction.correctAns);
    clearTimeout(MixedFraction.wrongAns);
    document.getElementById("playSound").pause();
    scoreFlag = false;
    $("#soundImg").hide();
    MixedFraction.Ans = Math.floor(MixedFraction.questionArray[MixedFraction.currentQues][0] / MixedFraction.questionArray[MixedFraction.currentQues][1]) ;
   
    MixedFraction.reminder = MixedFraction.questionArray[MixedFraction.currentQues][0] - (MixedFraction.questionArray[MixedFraction.currentQues][1] * MixedFraction.Ans);

    MixedFraction.ansStep = MixedFraction.questionArray[MixedFraction.currentQues][1] * MixedFraction.Ans ; 
}
MixedFraction.setData = function(){
  $("#divident").data("data",MixedFraction.questionArray[MixedFraction.currentQues][0]);
  $("#divisor").data("data",MixedFraction.questionArray[MixedFraction.currentQues][1]);
  $("#divisionAns").data("data",MixedFraction.Ans);
  $("#multiplication1").data("data",MixedFraction.ansStep);
  $("#reminder1").data("data",MixedFraction.reminder) ;
  document.getElementById("minusSign").innerHTML = MixedFraction.hardcoded.sign;
  if(!setDataFlag){
    $("#nextImg").addClass("bounce");
    for(var i=0;i<2;i++){
        document.getElementById("innerQuesDiv"+i).innerHTML = MixedFraction.questionArray[MixedFraction.currentQues][i];
        $("#innerQuesDiv"+i).css('color','black');
    }
  }
  else{
    for(var i=0;i<2;i++){
        document.getElementById("innerdigitQuesDiv"+i).innerHTML = MixedFraction.questionArray[MixedFraction.currentQues][i];
    }
    $("#innerdigitAnsDiv0").data("data",MixedFraction.reminder);
    $("#innerdigitAnsDiv1").data("data",MixedFraction.questionArray[MixedFraction.currentQues][1]);
    $("#innerId").data("data", MixedFraction.Ans);
  }

}
MixedFraction.playInstruction = function(){ //play Instruction
    document.getElementById("playSound").src = "sounds/instructions/selectNoInstruction.mp3";
    document.getElementById("playSound").play();
}
//first Screen
MixedFraction.createDiv = function(){
   var quesDiv,quesMarkDiv,innerQues,equalSignDiv ;
    quesDiv = document.createElement("Div");
    quesDiv.setAttribute("id","quesDiv");
    quesDiv.setAttribute("Class","noPadding col-sm-2 col-xs-2 col-lg-2 col-md-2 col-sm-offset-4 col-xs-offset-4 col-lg-offset-4 col-md-offset-4");
    document.getElementById("givenFindData").appendChild(quesDiv);
    for(var i=0;i<2;i++){
        innerQues = document.createElement("Div");
        innerQues.setAttribute("id","innerQuesDiv"+i);
        innerQues.setAttribute("Class","noPadding innerQuesClass editField col-sm-10 col-xs-10 col-lg-10 col-md-10 col-sm-offset-1 col-xs-offset-1 col-lg-offset-1");
       // innerQues.innerHTML = MixedFraction.questionArray[MixedFraction.currentQues][i];
        quesDiv.appendChild(innerQues);
    }
      $("#innerQuesDiv1").css("border-top","3px solid black");
      //equalSign
   equalSignDiv = document.createElement("div");
   equalSignDiv.setAttribute("id","equalSignId");
   equalSignDiv.setAttribute("class","noPadding col-sm-1 col-xs-1 col-lg-1 col-md-1");
   document.getElementById("givenFindData").appendChild(equalSignDiv);
   equalSignDiv.innerHTML = MixedFraction.hardcoded.equalSign;

    //question mark sign
    quesMarkDiv = document.createElement("div");
    quesMarkDiv.setAttribute("id","quesMarkId");
    quesMarkDiv.setAttribute("class","noPadding col-sm-1 col-xs-1 col-lg-1 col-md-1");
    document.getElementById("givenFindData").appendChild(quesMarkDiv);
    quesMarkDiv.innerHTML = MixedFraction.hardcoded.questionMark;

    MixedFraction.setData();
}
//second screen
MixedFraction.showSecondScreen = function(){
    setDataFlag = true;
    document.getElementById("nextImg").src = Utils.Path+"images/Next.png";
    $("#soundImg").show();
   $("#wordProblemQues").hide();
    $("#innerQuesDivId").hide();
    if(!MixedFraction.instructionFlag)
       MixedFraction.playInstruction();
     MixedFraction.instructionFlag = true;
     $("#digitalProblemQues").show();
     $("#"+MixedFraction.blinkDivArray[MixedFraction.blinkId]).addClass("blink_me");
     var outerDiv = document.createElement("div");
     outerDiv.setAttribute("id","outerDiv");
     outerDiv.setAttribute("class","noPadding col-sm-12 col-xs-12 col-md-12 col-lg-12");
     document.getElementById("secondHalf").appendChild(outerDiv);
     for(var i=0;i<2;i++){
       var innerQues = document.createElement("Div");
        innerQues.setAttribute("id","innerdigitQuesDiv"+i);
        innerQues.setAttribute("Class","noPadding innerQuesClass editField col-sm-12 col-xs-12 col-lg-12 col-md-12");
            document.getElementById("question").appendChild(innerQues);
    }
    $("#innerdigitQuesDiv1").css("border-top","1px solid white");
      for(var i=0;i<2;i++){
        var innerQues = document.createElement("Div");
         innerQues.setAttribute("id","innerdigitAnsDiv"+i);
         innerQues.setAttribute("Class","noPadding innerQuesClass styleBorder innerQuesClass editField col-sm-10 col-xs-10 col-lg-10 col-md-10 col-sm-offset-1 col-xs-offset-1 col-lg-offset-1");
         if(i==0){
            innerQues.setAttribute("onclick","MixedFraction.giveDirectAns(this.id)");
            document.getElementById("outerDiv").appendChild(innerQues);
            $("#outerDiv").css("border-bottom","2px solid white");
            $("#outerDiv").css("height","14vh");
         }
         else
            document.getElementById("secondHalf").appendChild(innerQues);
         
    }
       $("#innerdigitAnsDiv1").css("top","2vh");
       document.getElementById("innerId").setAttribute("onclick","MixedFraction.giveDirectAns(this.id)");

       MixedFraction.setData();
}
MixedFraction.giveDirectAns = function(clicked_div){
    finalAnsFlag = true;
    $("#"+MixedFraction.blinkDivArray[MixedFraction.blinkId]).removeClass("blink_me");
    $("#"+MixedFraction.finalAnsBlinkDivArr[finalAnsDiv]).addClass("blink_me");
}
//function to display number into answer Div from numberPad
function displayNumber(elementInformation) {
    scoreFlag = true;
    clearTimeout(MixedFraction.correctAns);
    clearTimeout(MixedFraction.wrongAns);
    document.getElementById("playSound").pause();
    document.getElementById(elementInformation.id).style.color="blue";
    setTimeout(function()
    { 
         document.getElementById(elementInformation.id).style.color ='white';  
    }, 250);
      MixedFraction.DivId = $('.blink_me').attr("id");
	
    clicked_id = elementInformation.id;  //id of the clicked button of the numberPad
    if (MixedFraction.colorTimerId) {
        clearTimeout(MixedFraction.colorTimerId);
        MixedFraction.colorTimerId = null;
    }
    text = $("#" + clicked_id).text();
    $("#"+MixedFraction.DivId).append(text);
    if($("#"+MixedFraction.DivId).text() == $("#"+MixedFraction.DivId).data("data"))
    {
       $(".buttonClass").css("pointer-events","none");
        if(MixedFraction.correctAns){
            clearTimeout(MixedFraction.correctAns);
            MixedFraction.correctAns = null;
        }
        $("#"+MixedFraction.DivId).css("color","white");
        MixedFraction.correctAns = setTimeout(function(){
            $("#"+MixedFraction.DivId).css("color","white");
            $("#"+MixedFraction.DivId).removeClass("blink_me");
            if(finalAnsFlag){
                finalAnsDiv++;
                $("#"+MixedFraction.finalAnsBlinkDivArr[finalAnsDiv]).addClass("blink_me");
            }
             else{
                MixedFraction.blinkId++;
                 if(MixedFraction.blinkId == 3)
                 $("#ansStep1").css("visibility","visible");
                $("#"+MixedFraction.blinkDivArray[MixedFraction.blinkId]).addClass("blink_me");
            }
            $(".buttonClass").css("pointer-events","auto");
        },300);

        if((MixedFraction.blinkId == MixedFraction.blinkDivArray.length - 1) || (finalAnsDiv == MixedFraction.finalAnsBlinkDivArr.length-1) ){
            MixedFraction.rightAnsFlag = true;
            $(".buttonClass").css("pointer-events","none");
            $("#soundImg").css("pointer-events","none");
            document.getElementById("playSound").src = Utils.Path + 'sounds/instructions/Cheering.mp3';
            document.getElementById("playSound").play();
           MixedFraction.nextQuestimer = setTimeout(function(){
            MixedFraction.nextQues();  
        },2300);
      }
    
    }
    else{
        $(".buttonClass").css("pointer-events","none");
        if(MixedFraction.wrongAns){
            clearTimeout(MixedFraction.wrongAns);
            MixedFraction.wrongAns = null;
        }
        $("#"+MixedFraction.DivId).css("color","red");
        MixedFraction.wrongAns = setTimeout(function(){
            $("#"+MixedFraction.DivId).css("color","white");
            $("#"+MixedFraction.DivId).text("");
            $(".buttonClass").css("pointer-events","auto");
        },300);
    }
}
MixedFraction.nextQues = function(){
    document.getElementById("playSound").pause();
    $("#soundImg").css("pointer-events","auto");
    clearTimeout(MixedFraction.nextQuestimer); 
    clearTimeout(MixedFraction.correctAns);
    clearTimeout(MixedFraction.wrongAns);
    $(".buttonClass").css("pointer-events","auto");
   if(!gameAttemptFlag){
        $(".bounce").removeClass("bounce");
        gameAttemptFlag = true;
        finalAnsFlag = false;
        MixedFraction.showSecondScreen();
    }
     else  {
        document.getElementById("nextImg").src = Utils.Path+"images/Go.png";
         if(!scoreFlag)
           scoredMarks = -1
         else
           {
               if(MixedFraction.rightAnsFlag)
                  scoredMarks = 10;
               else
                  scoredMarks = 0;
           }
           MixedFraction.scoreFunction();
        if(MixedFraction.currentQues<4){
        $("#ansStep1").css("visibility","hidden");
         setDataFlag = false;
         gameAttemptFlag = false;
         finalAnsFlag = false;
         MixedFraction.rightAnsFlag = false;
         finalAnsDiv = 0;
         MixedFraction.blinkId = 0;
        $("#wordProblemQues").show();
        $("#innerQuesDivId").show();
        $("#digitalProblemQues").hide();
        gameAttemptFlag = false;
        MixedFraction.currentQues++;
        for(var i=0;i<2;i++){
            document.getElementById("innerQuesDiv"+i).innerHTML = " "
        }
        $("#divident").text("");
        $("#divisor").text("");
       $("#divisionAns").text("");
       $("#multiplication1").text("");
      $("#reminder1").text("");
      document.getElementById("question").innerHTML = "";
      document.getElementById("innerId").innerHTML = "";
      document.getElementById("secondHalf").innerHTML = "";
        MixedFraction.calculateAns();
            MixedFraction.setData();
     }
     else{
        document.getElementById("playSound").pause();
        MixedFraction.gameOver();
     }
    }
    
}
MixedFraction.gameOver = function () {
    clearTimeout(MixedFraction.nextQuestimer); 
    clearTimeout(MixedFraction.correctAns);
    clearTimeout(MixedFraction.wrongAns);
    document.getElementById("playSound").pause();
    document.getElementById("gamePage").style.display = "none";
    document.getElementById("endPage").style.display = "block";
    document.getElementById("gameOver").innerHTML = MixedFraction.hardcoded.gameOver;
 }
 MixedFraction.replayGame = function(){
    clearTimeout(MixedFraction.nextQuestimer); 
    clearTimeout(MixedFraction.correctAns);
    clearTimeout(MixedFraction.wrongAns);
    document.getElementById("playSound").pause();
    document.getElementById("gamePage").style.display = "block";
    document.getElementById("endPage").style.display = "none";
    document.getElementById("gameOver").innerHTML ="";
    MixedFraction.resetVal();
    for(var i=0;i<2;i++){
        document.getElementById("innerQuesDiv"+i).innerHTML = " "
    }
    $("#divident").text("");
    $("#divisor").text("");
   $("#divisionAns").text("");
   $("#multiplication1").text("");
  $("#reminder1").text("");
  document.getElementById("question").innerHTML = "";
  document.getElementById("innerId").innerHTML = "";
  document.getElementById("secondHalf").innerHTML = "";
    $("#digitalProblemQues").hide();
    $("#wordProblemQues").show();
    $("#innerQuesDivId").show();
    MixedFraction.calculation();
    MixedFraction.setData();

  }
 MixedFraction.resetVal = function(){
     MixedFraction.questionArray=[];
     MixedFraction.currentQues = 0;
     MixedFraction.blinkId = 0;
     MixedFraction.instructionFlag = false;
     MixedFraction.rightAnsFlag = false;
     setDataFlag = false;
     gameAttemptFlag  = false;
     finalAnsDiv =0;
 }
MixedFraction.goHome = function(){
    clearTimeout(MixedFraction.nextQuestimer); 
    clearTimeout(MixedFraction.correctAns);
    clearTimeout(MixedFraction.wrongAns);
    document.getElementById("playSound").pause();
    if(MixedFraction.rightAnsFlag)
    scoredMarks = 10;
  else
    scoredMarks = 0;
     MixedFraction.scoreFunction();
    location.reload();
}
MixedFraction.createAcknowledgementDivs = function () {
    var i = 0, div;
    for (i = 0; i < Object.keys(MixedFraction.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(MixedFraction.AcknowledgementPageData)[i] + " : " + Object.values(MixedFraction.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);
}
MixedFraction.showAcknowledgement = function () {
    $("#acknowledgementDiv").show();
}
MixedFraction.scoreFunction = function () {
    console.log(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
    if (Utils.mobileDeviceFlag) {
        Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime);
    }
}