//starting date:19th December 2018.
var ConnectTen = {};
var Utils = {};
Utils.Path = "";
var xDown = (yDown = null);
Utils.mobileDeviceFlag = false;
var questionId = 0, startTime = 0, scoredMarks = 0, totalMarks = 0, resId = 0, level = 1, label = '';
var ongoingTouches = [];

$(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("ConnectTen");
  setTimeout(function () {
    ConnectTen.init();
  }, 100);
});
//shows coverPage
ConnectTen.init = function () {
  document.title = ConnectTen.hardCodedData.gameTitle;
  $("#coverPage").show();
  $("#gamePage").hide();
  $("#levelPage").hide();
  $("#endPage").hide();
  $("#modalDiv").hide();
  $(".header")
    .not("#logo")
    .css("visibility", "hidden");
  document.body.style.backgroundImage =
    "url(" + Utils.Path + "images/skin/welcomebackground.png";
  ConnectTen.initializeVariables();
  ConnectTen.setAllImages();
  ConnectTen.createLevelDivs();
};

ConnectTen.showInstructions = function () {
  $("#instructionDiv").show();
}

ConnectTen.hideInstruction = function () {
  $("#instructionDiv").hide();
}

//initialization
ConnectTen.initializeVariables = function () {
  ConnectTen.clickCount = 0;
  ConnectTen.waitForNextClick;
  ConnectTen.clickedDivsArr = [];
  ConnectTen.score = 0;
  ConnectTen.minutes = 0;
  ConnectTen.secs = 0;
  ConnectTen.gridLength = 0;
  ConnectTen.gridAddition = 0;
  ConnectTen.numArr = [];
  ConnectTen.diffArr = [];
  ConnectTen.firstDivLineFlag = true;
  ConnectTen.width = 100;
  ConnectTen.zeroFlag = false;
};
//setting sources to all images
ConnectTen.setAllImages = function () {
  document.getElementById("homeImage").src = Utils.Path + "images/skin/home.png";
  document.getElementById("logoImage").src = Utils.Path + "images/skin/logo.png";
  document.getElementById("titleImg").src = Utils.Path + "images/skin/connect10_logo.png";
  document.getElementById("rotateImg").src = Utils.Path + "images/skin/reset1.png";
  document.getElementById("scoreImg").src = Utils.Path + "images/skin/score.png";
  document.getElementById("wrongImg").src = Utils.Path + "images/skin/wrong_move.png";
  document.getElementById("responseImg").src = Utils.Path + "images/skin/winEmoji.png";
  document.getElementById("gameOverImg").src = Utils.Path + "images/skin/game_over.png";
  document.getElementById("howToPlayImg").src = Utils.Path + "images/skin/help.png";
  document.getElementById("img1").src = Utils.Path + "images/skin/Connect101.png";
  document.getElementById("img2").src = Utils.Path + "images/skin/Connect102.png";
  document.getElementById("text1").innerHTML = ConnectTen.hardCodedData.instruction1;
  document.getElementById("text2").innerHTML = ConnectTen.hardCodedData.instruction2;
  document.getElementById("heading").innerHTML = ConnectTen.hardCodedData.howToPlay;
  document.getElementById("close").innerHTML = ConnectTen.hardCodedData.close;

  document.getElementById("grandTotal").style.backgroundImage = "url(" + Utils.Path + "images/skin/level_addition_sum_box.png";
  document.getElementById("score").style.backgroundImage = "url(" + Utils.Path + "images/skin/score_box.png";


  //document.getElementById("scoreText").innerHTML =
  ConnectTen.hardCodedData.score;
  //document.getElementById("timeText").innerHTML = ConnectTen.hardCodedData.time;
  document.getElementById("score").innerHTML = ConnectTen.score;
  document.getElementById("responseTextDiv").innerHTML = ConnectTen.hardCodedData.winMsg;

  //document.getElementById("time").innerHTML =
  //"0" + ConnectTen.minutes + " : " + "0" + ConnectTen.secs;
  //document.getElementById("textPara").innerHTML =
  //ConnectTen.hardCodedData.levelText;
  document.getElementById("hundred").innerHTML =
    ConnectTen.hardCodedData.hundred;
};
//divs for various levels of game.
ConnectTen.createLevelDivs = function () {
  var i = 0,
    div, img,
    parentDiv;
  parentDiv = document.getElementById("coverPage");

  for (var i = 1; i <= 3; i++) {
    div = document.createElement("DIV");
    div.setAttribute("id", "levelDiv" + i);
    if (i == 1)
      div.setAttribute("class", "nopadding col-xs-2 col-md-2 col-lg-2 col-sm-2 col-xs-offset-2 col-md-offset-2 col-lg-offset-2 col-sm-offset-2 ");
    else
      div.setAttribute("class", "nopadding col-xs-2 col-md-2 col-lg-2 col-sm-2 col-xs-offset-1 col-md-offset-1 col-lg-offset-1 col-sm-offset-1");
    // div.innerHTML = ConnectTen.levelName[i - 1];
    div.setAttribute("onclick", "ConnectTen.selectLevel(this.id);");
    div.setAttribute("level", i - 1);
    div.setAttribute("number", ConnectTen.levelArr[i - 1]);
    div.style.marginTop = "5vh";

    img = document.createElement("IMG");
    img.setAttribute("id", "levelImg" + i);
    img.setAttribute("class", "img-responsive nopadding col-xs-12 col-md-12 col-lg-12 col-sm-12");
    img.setAttribute("src", "images/skin/" + ConnectTen.levelImgArr[i - 1]);
    div.appendChild(img);
    parentDiv.appendChild(div);
  }
};

//runs @ onclick of level-number from levelPage.
ConnectTen.selectLevel = function (div) {
  var levelNo;
  document.body.style.backgroundImage = "url(" + Utils.Path + "images/skin/maingamebackground.png";
  ConnectTen.level = parseInt(document.getElementById(div).getAttribute("number"));
  levelNo = document.getElementById(div).getAttribute("level")
  document.getElementById("levelImg").src = Utils.Path + "images/skin/" + ConnectTen.levelImgArr[levelNo];
  ConnectTen.index = parseInt(document.getElementById(div).getAttribute("level"));
  ConnectTen.gridLength = ConnectTen.gridlengthArr[ConnectTen.index];
  selectedLevel=parseInt(levelNo)
  switch(selectedLevel){
    case 0:$("#grid").css("margin-top","5vh")
    break;
    case 1:$("#grid").css("margin-top","1vh")
    break;
    case 2:$("#grid").css("margin-top","1vh")
    break;

  }
  ConnectTen.playGame();
};

//displays levels.
ConnectTen.showLevel = function () {
  $("#coverPage").hide();
  $("#levelPage").show();
};
//display gamePage
ConnectTen.playGame = function () {
  d = new Date();
  startTime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
  $("#coverPage").hide();
  $("#levelPage").hide();
  $("#gamePage").show();
  $(".header").css("visibility", "visible");
  document.getElementById("grandTotal").innerHTML = ConnectTen.level;
  ConnectTen.startTimer();
  ConnectTen.createNumberDivs();
  var div = document.getElementById("grid");
  xDown = null;
  yDown = null;
};

//timer of the game.
// ConnectTen.startTimer=function()
// {
// 	var sound;
// 	if(ConnectTen.countdownTimer)
// 	{
// 		clearInterval(ConnectTen.countdownTimer);
// 		ConnectTen.countdownTimer=null;
// 	}
// 	ConnectTen.countdownTimer=setInterval(function()
// 	{
// 		if(ConnectTen.secs<10)
// 		document.getElementById("time").innerHTML="0"+ConnectTen.minutes+" : "+"0"+ConnectTen.secs;
// 		else
// 		document.getElementById("time").innerHTML="0"+ConnectTen.minutes+" : "+ConnectTen.secs;
// 		ConnectTen.secs++;
// 		if(ConnectTen.secs==59)
// 		{
// 			setTimeout(function()
// 			{
// 				clearInterval(ConnectTen.countdownTimer);
// 				ConnectTen.secs=0;
// 				ConnectTen.minutes++;
// 				if(ConnectTen.minutes==5)
// 				{
// 					clearInterval(ConnectTen.countdownTimer);
// 					ConnectTen.endGame();
// 				}
// 				else
// 					ConnectTen.startTimer();
// 			},1000);
// 		}
// 	},1000);
// }

ConnectTen.startTimer = function () {
  var sound;
  if (ConnectTen.countdownTimer) {
    clearInterval(ConnectTen.countdownTimer);
    ConnectTen.countdownTimer = null;
  }
  ConnectTen.countdownTimer = setInterval(function () {
    ConnectTen.width -= 0.033;
    document.getElementById("progressBar").style.width = ConnectTen.width + "%";
    if (ConnectTen.width <= 0) {
      clearInterval(ConnectTen.countdownTimer);
      ConnectTen.endGame();
    }
    if (ConnectTen.width <= 20)
      $("#progressBar").addClass("progress-bar-danger");
  }, 100);
};

//pushes random numbers according to level in the grid.
ConnectTen.createNumberDivs = function () {
  var i = 0,
    j = 0,
    counter = 0,
    container, img,
    parentDiv,
    numberDiv,
    rno = 0,
    innerDiv,
    dummyDiv,
    maxNumCount = 0;
  parentDiv = document.getElementById("grid");

  dummyDiv = document.createElement("Div");
  dummyDiv.setAttribute("id", "dummyDiv");
  dummyDiv.setAttribute(
    "class",
    ConnectTen.classArr[ConnectTen.index].dummyDivClass
  );
  parentDiv.appendChild(dummyDiv);

  innerDiv = document.createElement("Div");
  innerDiv.setAttribute("id", "innerDiv");
  innerDiv.setAttribute("class", "col-xs-12 col-md-12 col-lg-12");

  for (j = 1; j <= ConnectTen.gridLength; j++) {
    container = document.createElement("DIV");
    container.setAttribute(
      "class",
      "nopadding container col-xs-12 col-md-12 col-lg-12 col-sm-12 "
    );
    for (i = 1; i <= ConnectTen.gridLength; i++) {
      counter++;
      numberDiv = document.createElement("DIV");
      numberDiv.setAttribute("id", "numDiv" + counter);
      numberDiv.setAttribute(
        "class", ConnectTen.classArr[ConnectTen.index].numberClass
      );
      //numberDiv.addEventListener('touchstart', handleStart, false);
      //numberDiv.addEventListener('touchend', handleTouchEnd, false);
      //numberDiv.addEventListener('touchmove', handleMove, false);
      numberDiv.setAttribute("onclick", "ConnectTen.countClick(this.id);");
      numberDiv.setAttribute("divNo", counter);
      //numberDiv.style.backgroundImage="url("+Utils.Path+"images/brick.png";
      rno = Math.floor(Math.random() * ConnectTen.level);
      if (rno == ConnectTen.level - 1) {
        maxNumCount++;
      }
      while (rno == ConnectTen.level - 1 && maxNumCount > 5) {
        rno = Math.floor(Math.random() * ConnectTen.level);
      }
      // numberDiv.setAttribute("number", rno);
      numberDiv.innerHTML = rno;
      numberDiv.style.backgroundColor = ConnectTen.colorArr[rno];

      // img = document.createElement("IMG")
      // img.setAttribute("id", "numImg" + counter);
      // img.setAttribute("class", "images img-responsive nopadding col-xs-12 col-md-12 col-lg-12 col-sm-12");
      // img.src = Utils.Path + "images/skin/" + ConnectTen.numImgArr[rno];
      // numberDiv.appendChild(img);
      container.appendChild(numberDiv);
    }
    innerDiv.appendChild(container);
  }
  dummyDiv.appendChild(innerDiv);
  // document.getElementById("innerDiv").style.backgroundImage = "url(" + Utils.Path + "images/skin/neon_box_for_main_grid.png";
  ConnectTen.setLottery();
  //document.getElementById("innerDiv").style.backgroundImage = "url("+Utils.Path+"images/brick.png";
  //ConnectTen.possibleChain()
};
//shows endPage when there is no combination of addition or time is up
ConnectTen.endGame = function () {
  clearTimeout(ConnectTen.waitForNextClick);
  document.getElementById("playSound").pause();
  $("#gamePage").hide();
  $("#endPage").show();
  $("#progressBarDiv").css("visibility", "hidden");
  $("#rotate").css("visibility", "hidden");
  document.body.style.backgroundImage =
    "url(" + Utils.Path + "images/skin/gameoverbackground.png";
  scoredMarks = ConnectTen.score;
  document.getElementById("scoredMarks").innerHTML = ConnectTen.hardCodedData.scoredMarks + " " + ConnectTen.score;
  console.log(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
  if (Utils.mobileDeviceFlag)
    Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
  // document.getElementById("gameOverText").innerHTML =ConnectTen.hardCodedData.gameOver;
  // document.getElementById("scoreMarks").innerHTML =ConnectTen.hardCodedData.scoredMarks + " " + ConnectTen.score;
  // if (ConnectTen.secs < 10)
  //   document.getElementById("timeTaken").innerHTML =
  //     ConnectTen.hardCodedData.timeTaken +
  //     "  " +
  //     ConnectTen.minutes +
  //     " : " +
  //     "0" +
  //     ConnectTen.secs;
  // else
  //   document.getElementById("timeTaken").innerHTML =
  //     ConnectTen.hardCodedData.timeTaken +
  //     "  " +
  //     ConnectTen.minutes +
  //     " : " +
  //     ConnectTen.secs;
};

//counts the clicks clicked by user.
ConnectTen.countClick = function (divId) {
  var divNo = 0;
  clearTimeout(ConnectTen.waitForNextClick);
  $(".numbers").css("pointer-events", "none");
  document.getElementById(divId).style.backgroundColor = "rgba(255,255,255,1)";
  divNo = parseInt(document.getElementById(divId).getAttribute("divNo"));
  if (divNo % ConnectTen.gridLength != 0) {
    if (!ConnectTen.numArr.includes(divNo + 1))
      document.getElementById("numDiv" + (divNo + 1)).style.pointerEvents =
        "auto";
  }
  if ((divNo - 1) % ConnectTen.gridLength != 0) {
    if (!ConnectTen.numArr.includes(divNo - 1))
      document.getElementById("numDiv" + (divNo - 1)).style.pointerEvents =
        "auto";
  }
  if (divNo / ConnectTen.gridLength <= ConnectTen.gridLength - 1) {
    if (!ConnectTen.numArr.includes(divNo + ConnectTen.gridLength))
      document.getElementById(
        "numDiv" + (divNo + ConnectTen.gridLength)
      ).style.pointerEvents = "auto";
  }
  if (divNo / ConnectTen.gridLength > 1) {
    if (!ConnectTen.numArr.includes(divNo - ConnectTen.gridLength))
      document.getElementById(
        "numDiv" + (divNo - ConnectTen.gridLength)
      ).style.pointerEvents = "auto";
  }

  ConnectTen.clickCount++;
  ConnectTen.clickedDivsArr.push(divId);
  //console.log(ConnectTen.clickedDivsArr);
  var num = parseInt(document.getElementById(divId).getAttribute("divNo"));
  ConnectTen.numArr.push(num);
  if (ConnectTen.clickCount > 1) {
    if (
      Math.abs(
        ConnectTen.numArr[ConnectTen.clickCount - 1] -
        ConnectTen.numArr[ConnectTen.clickCount - 2]
      ) == 1
    )
      document.getElementById(
        ConnectTen.clickedDivsArr[ConnectTen.clickCount - 1]
      ).style.backgroundImage =
        "url(" + Utils.Path + "images/horizontal.png" + ")";
    else
      document.getElementById(
        ConnectTen.clickedDivsArr[ConnectTen.clickCount - 1]
      ).style.backgroundImage =
        "url(" + Utils.Path + "images/vertical.png" + ")";
    ConnectTen.diffArr.push(
      ConnectTen.numArr[ConnectTen.clickCount - 1] -
      ConnectTen.numArr[ConnectTen.clickCount - 2]
    );
    if (
      Math.abs(
        ConnectTen.numArr[ConnectTen.clickCount - 2] -
        ConnectTen.numArr[ConnectTen.clickCount - 1]
      ) == 1
    ) {
      if (ConnectTen.clickCount > 2) {
        if (
          ConnectTen.diffArr[ConnectTen.clickCount - 2] !=
          ConnectTen.diffArr[ConnectTen.clickCount - 3]
        ) {
          ConnectTen.drawLines();
        } else
          document.getElementById(
            ConnectTen.clickedDivsArr[ConnectTen.clickCount - 2]
          ).style.backgroundImage =
            "url(" + Utils.Path + "images/horizontal.png" + ")";
      }
      if (ConnectTen.firstDivLineFlag) {
        ConnectTen.firstDivLineFlag = false;
        document.getElementById(
          ConnectTen.clickedDivsArr[0]
        ).style.backgroundImage =
          "url(" + Utils.Path + "images/horizontal.png" + ")";
      }
    } else {
      if (
        Math.abs(
          ConnectTen.numArr[ConnectTen.clickCount - 2] -
          ConnectTen.numArr[ConnectTen.clickCount - 1]
        ) == ConnectTen.gridLength
      ) {
        if (ConnectTen.clickCount > 2) {
          if (
            ConnectTen.diffArr[ConnectTen.clickCount - 2] !=
            ConnectTen.diffArr[ConnectTen.clickCount - 3]
          ) {
            ConnectTen.drawLines();
          } else
            document.getElementById(
              ConnectTen.clickedDivsArr[ConnectTen.clickCount - 2]
            ).style.backgroundImage =
              "url(" + Utils.Path + "images/vertical.png" + ")";
        }
        if (ConnectTen.firstDivLineFlag) {
          ConnectTen.firstDivLineFlag = false;
          document.getElementById(
            ConnectTen.clickedDivsArr[0]
          ).style.backgroundImage =
            "url(" + Utils.Path + "images/vertical.png" + ")";
        }
      }
    }
    // ConnectTen.waitForNextClick=setTimeout(function()
    // {
    // 	ConnectTen.checkAddition(divId);
    // },1500);
  }
  if (ConnectTen.waitForNextClick) {
    clearTimeout(ConnectTen.waitForNextClick);
    ConnectTen.waitForNextClick = null;
  }
  ConnectTen.waitForNextClick = setTimeout(function () {
    ConnectTen.checkAddition(divId);
  }, 1500);
};
//draws the lines showing the Path by which the user has cliked the numbers.
ConnectTen.drawLines = function () {
  if (
    ConnectTen.diffArr[ConnectTen.clickCount - 2] -
    ConnectTen.diffArr[ConnectTen.clickCount - 3] ==
    ConnectTen.gridLength - 1
  )
    document.getElementById(
      ConnectTen.clickedDivsArr[ConnectTen.clickCount - 2]
    ).style.backgroundImage = "url(" + Utils.Path + "images/corner1.png" + ")";

  if (
    ConnectTen.diffArr[ConnectTen.clickCount - 2] -
    ConnectTen.diffArr[ConnectTen.clickCount - 3] ==
    ConnectTen.gridLength + 1
  )
    document.getElementById(
      ConnectTen.clickedDivsArr[ConnectTen.clickCount - 2]
    ).style.backgroundImage = "url(" + Utils.Path + "images/corner2.png" + ")";

  if (
    ConnectTen.diffArr[ConnectTen.clickCount - 2] -
    ConnectTen.diffArr[ConnectTen.clickCount - 3] ==
    -(ConnectTen.gridLength + 1)
  )
    document.getElementById(
      ConnectTen.clickedDivsArr[ConnectTen.clickCount - 2]
    ).style.backgroundImage = "url(" + Utils.Path + "images/corner3.png" + ")";

  if (
    ConnectTen.diffArr[ConnectTen.clickCount - 2] -
    ConnectTen.diffArr[ConnectTen.clickCount - 3] ==
    -(ConnectTen.gridLength - 1)
  )
    document.getElementById(
      ConnectTen.clickedDivsArr[ConnectTen.clickCount - 2]
    ).style.backgroundImage = "url(" + Utils.Path + "images/corner4.png" + ")";
};

//checks addition according to the level is correct or not.
ConnectTen.checkAddition = function (divId) {
  // if(Math.abs(ConnectTen.numArr[ConnectTen.clickCount-2]-ConnectTen.numArr[ConnectTen.clickCount-1])==ConnectTen.gridLength)
  // 	document.getElementById(divId).style.backgroundImage = "url("+Utils.Path+'images/vertical.png'+")";
  // else
  // 	document.getElementById(divId).style.backgroundImage = "url("+Utils.Path+'images/horizontal.png'+")";
  ConnectTen.firstDivLineFlag = true;
  ConnectTen.numArr = [];
  ConnectTen.diffArr = [];
  var i = 0,
    addition = 0,
    num = 0,
    rno,
    minus = -1,
    ctr;
  for (i = 0; i < ConnectTen.clickedDivsArr.length; i++) {
    num = parseInt(
      document.getElementById(ConnectTen.clickedDivsArr[i]).innerHTML);
    addition = addition + num;
  }
  console.log(addition);
  if (addition == ConnectTen.level) {
    document.getElementById("playSound").src = Utils.Path + "sounds/CorrectAksharkhadi.mp3";
    document.getElementById("playSound").play();
    ConnectTen.score = ConnectTen.score + ConnectTen.clickCount * 10;
    document.getElementById("score").innerHTML = ConnectTen.score;
    if (
      ConnectTen.zeroFlag &&
      ConnectTen.clickedDivsArr.includes(ConnectTen.lotteryNum)
    ) {
      ctr = parseInt(
        document.getElementById(ConnectTen.lotteryNum).getAttribute("lottery")
      );
      document.getElementById(ConnectTen.lotteryNum).removeAttribute("lottery");
      if (ctr) {
        minus = -2;
        document.getElementById("hundred").innerHTML = "--";
        document.getElementById("playSound").src =
          Utils.Path + "sounds/Cheering.mp3";
      } else {
        minus = 1;
        document.getElementById("hundred").innerHTML = "++";
        document.getElementById("playSound").src =
          Utils.Path + "sounds/Anahhh.mp3";
      }
      document.getElementById("playSound").play();
      ConnectTen.zeroFlag = false;
      $("#" + ConnectTen.lotteryNum).removeClass("zoomIn");
      //$("#bonusModal").show();
      setTimeout(function () {
        //ConnectTen.score=ConnectTen.score+100;
        //document.getElementById("score").innerHTML=ConnectTen.score;
        //$("#bonusModal").hide();
      }, 1500);
    }

    for (i = 0; i < ConnectTen.clickedDivsArr.length; i++) {
      num = parseInt(
        document.getElementById(ConnectTen.clickedDivsArr[i]).innerHTML);

      if (minus == 1) {
        document.getElementById(ConnectTen.clickedDivsArr[i]).innerHTML = num + minus;
        // document.getElementById(ConnectTen.clickedDivsArr[i]).setAttribute("number", num - 1);
        document.getElementById(ConnectTen.clickedDivsArr[i]).style.backgroundColor = ConnectTen.colorArr[num + minus];
        //ConnectTen.colorArr[num + minus];
      } else {
        if (num > 0) {
          if (minus == -2 && num == 1) {
            document.getElementById(ConnectTen.clickedDivsArr[i]).innerHTML = "";
            //document.getElementById(ConnectTen.clickedDivsArr[i]).setAttribute("number", num - 1);
            document.getElementById(ConnectTen.clickedDivsArr[i]).style.backgroundColor = "";
          } else {
            document.getElementById(ConnectTen.clickedDivsArr[i]).innerHTML = num + minus;
            //document.getElementById(ConnectTen.clickedDivsArr[i]).setAttribute("number", num - 1);
            document.getElementById(ConnectTen.clickedDivsArr[i]).style.backgroundColor =
              ConnectTen.colorArr[num + minus];
          }
        } else {
          document
            .getElementById(ConnectTen.clickedDivsArr[i])
            .setAttribute("flag", 0);
        }
        if (
          document
            .getElementById(ConnectTen.clickedDivsArr[i])
            .getAttribute("flag") == 0
        ) {
          document
            .getElementById(ConnectTen.clickedDivsArr[i])
            .removeAttribute("flag");
          divNo = parseInt(
            document
              .getElementById(ConnectTen.clickedDivsArr[i])
              .getAttribute("divNo")
          );
          document.getElementById("numDiv" + divNo).innerHTML = "";
          document.getElementById("numDiv" + divNo).style.backgroundColor = "";
        }
      }
      $("#" + ConnectTen.clickedDivsArr[i]).addClass("animated rotateIn");
    }

    // for(i=0;i<ConnectTen.clickedDivsArr.length;i++)
    // {
    // 	num=parseInt(document.getElementById(ConnectTen.clickedDivsArr[i]).innerHTML);
    // 	if(document.getElementById(ConnectTen.clickedDivsArr[i]).getAttribute("flag")==0)
    // 	{
    // 		document.getElementById(ConnectTen.clickedDivsArr[i]).removeAttribute("flag");
    // 		divNo=parseInt(document.getElementById(ConnectTen.clickedDivsArr[i]).getAttribute("divNo"));
    // 		document.getElementById("numDiv"+divNo).innerHTML="";
    // 	}
    // }
  } else {
    $("#modalDiv").show();
    document.getElementById("playSound").src =
      Utils.Path + "sounds/Try_again.mp3";
    document.getElementById("playSound").play();
    setTimeout(function () {
      $("#modalDiv").hide();
      for (var i = 1; i <= ConnectTen.gridLength * ConnectTen.gridLength; i++) {
        num = document.getElementById("numDiv" + i).innerHTML;
        document.getElementById("numDiv" + i).style.backgroundColor = ConnectTen.colorArr[num];
      }
    }, 1000);
  }
  ConnectTen.clickCount = 0;
  $(".numbers").css("background-image", "");
  // $(".numbers").css("background-color", "");
  $(".numbers").css("pointer-events", "auto");
  ConnectTen.clickedDivsArr = [];
  setTimeout(function () {
    ConnectTen.checkEmptyPlaces();
    // document.getElementById(ConnectTen.lotteryNum).removeAttribute("lottery");
    // $("#" + ConnectTen.lotteryNum).removeClass("zoomIn");
    ConnectTen.lotteryNum = "";
    if (ConnectTen.zeroFlag == false) {
      ConnectTen.rno = Math.floor(Math.random() * 2);
      if (ConnectTen.rno == 1) ConnectTen.setLottery();
    }
  }, 1000);
};

//checks whether there are empty places in between the grid and fills them if any.
ConnectTen.checkEmptyPlaces = function () {
  var i = 0,
    num,
    div,
    iterationFlag = false;
  for (
    i = ConnectTen.gridLength + 1;
    i <= ConnectTen.gridLength * ConnectTen.gridLength;
    i++
  ) {
    $(".numbers").removeClass("animated rotateIn");
    if (
      document.getElementById("numDiv" + i).innerHTML == "" &&
      document.getElementById("numDiv" + (i - ConnectTen.gridLength))
        .innerHTML != ""
    ) {
      iterationFlag = true;
      num = document.getElementById("numDiv" + (i - ConnectTen.gridLength)).innerHTML;
      document.getElementById("numDiv" + i).style.backgroundColor = ConnectTen.colorArr[num];
      document.getElementById("numDiv" + i).innerHTML = num;
      document.getElementById("numDiv" + (i - ConnectTen.gridLength)).innerHTML = "";
      document.getElementById("numDiv" + (i - ConnectTen.gridLength)).style.backgroundColor = "";

      document.getElementById("numDiv" + i).style.visibility = "hidden";
      div = "numDiv" + i;
      setTimeout(function () {
        document.getElementById(div).style.visibility = "visible";
        $("#" + div).addClass("animated fadeInDown");
      }, 0);
      if (iterationFlag == true) ConnectTen.checkEmptyPlaces();
    }
    $(".numbers").removeClass("animated fadeInDown");
  }
  ConnectTen.checkGridAddition();
  return 0;
};

//rotates the grid clockwise in 90 degree
ConnectTen.rotateGrid = function () {
  var temp = ConnectTen.gridLength * (ConnectTen.gridLength - 1) + 1,
    tempArr = [];
  for (i = temp; ; i -= ConnectTen.gridLength) {
    if (i > 0) {
      num = document.getElementById("numDiv" + i).innerHTML;
      tempArr.push(num);
    } else {
      temp++;
      i = temp + ConnectTen.gridLength;
      if (temp > ConnectTen.gridLength * ConnectTen.gridLength) break;
      else continue;
    }
  }
  console.log(tempArr);
  $(".numbers").removeClass("animated fadeInDown");
  $("#innerDiv").addClass("rotate");
  $(".numbers").addClass("rotateLeft");
  $("#" + ConnectTen.lotteryNum).removeClass("zoomIn");

  setTimeout(function () {
    $(".numbers").css("background-color", "transparent");
    for (i = 1; i <= ConnectTen.gridLength * ConnectTen.gridLength; i++) {
      document.getElementById("numDiv" + i).innerHTML = tempArr[i - 1];
      document.getElementById("numDiv" + i).style.backgroundColor = ConnectTen.colorArr[tempArr[i - 1]];
      // document.getElementById("numDiv" + i).style.color =
      // ConnectTen.colorArr[tempArr[i - 1]];
    }
    $("#innerDiv").removeClass("rotate");
    $(".numbers").removeClass("rotateLeft");
    ConnectTen.checkEmptyPlaces();
    if (ConnectTen.rno == 1)
      document.getElementById(ConnectTen.lotteryNum).removeAttribute("lottery");
    $("#" + ConnectTen.lotteryNum).removeClass("zoomIn");
    ConnectTen.lotteryNum = "";
    ConnectTen.setLottery();
  }, 1000);
  //ConnectTen.showModal();
};

//finds the addition of all numbers in the grid(exit criteria).
ConnectTen.checkGridAddition = function () {
  var i = 0,
    num;
  ConnectTen.gridAddition = 0;
  for (i = 1; i <= ConnectTen.gridLength * ConnectTen.gridLength; i++) {
    num = document.getElementById("numDiv" + i);
    if (document.getElementById("numDiv" + i).innerHTML != "") {
      ConnectTen.gridAddition =
        ConnectTen.gridAddition +
        parseInt(document.getElementById("numDiv" + i).innerHTML);
    }
  }
  if (ConnectTen.gridAddition < ConnectTen.level) {
    clearInterval(ConnectTen.countdownTimer);
    setTimeout(function () {
      document.getElementById("scoreMsg").innerHTML = ConnectTen.hardCodedData.scoredMarks + " " + ConnectTen.score;

      ConnectTen.showModal();
    }, 1000);
  }
  //console.log(ConnectTen.gridAddition);
};

ConnectTen.showModal = function () {
  clearTimeout(ConnectTen.waitForNextClick);
  document.getElementById("playSound").pause();
  $("#correctWrongModal").show();
  document.getElementById("playSound").src = Utils.Path + "sounds/coorrect_beat.mp3";
  document.getElementById("playSound").play();
  scoredMarks = ConnectTen.score;
  console.log(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
  if (Utils.mobileDeviceFlag)
    Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
}

ConnectTen.callHome = function () {
  location.reload();
};

//checks for more combinations of addition.
// ConnectTen.checkMorePossibilities=function()
// {
// 	var i=0,upDown=ConnectTen.gridLength,leftRight=1,currDiv,leftNumbersArr=[];

// 	for(i=1;i<=(ConnectTen.gridLength*ConnectTen.gridLength);i++)
// 	{
// 		if(document.getElementById("divNo"+i).innerHTML!="")
// 		{
// 			leftNumbersArr.push(i);
// 		}
// 	}
// 	for(i=0;i<leftNumbersArr.length;i++)
// 			currDiv=leftNumbersArr[i];
// 			ConnectTen.checkPattern(currDiv);
// 			sum=parseInt(document.getElementById("divNo"+currDiv).innerHTML);
// 			if(currDiv/ConnectTen.gridLength!=0)
// 			sum=sum+(parseInt(document.getElementById("divNo"+(leftNumbersArr[i]+leftRight).innerHTML)))
// 			if(sum<10)
// 			{

// 			}
// }

ConnectTen.possibleChain = function () {
  ConnectTen.chainArr = [];
  var i,
    randPlace,
    nextPlace,
    addition = 0,
    traverseArr = [1, -1, ConnectTen.gridLength, -ConnectTen.gridLength];
  randPlace = Math.floor(Math.random() * Math.pow(ConnectTen.gridLength, 2));
  while (document.getElementById("numDiv" + randPlace).innerHTML == "")
    randPlace = Math.floor(Math.random() * Math.pow(ConnectTen.gridLength, 2));
  addition = parseInt(document.getElementById("numDiv" + randPlace).innerHTML);
  ConnectTen.chainArr.push(randPlace);
  while (addition < 10) {
    randNum = Math.floor(Math.random() * traverseArr.length);
    while (ConnectTen.chainArr.includes(randNum)) {
      randNum = Math.floor(Math.random() * traverseArr.length);
    }
    nextPlace = randPlace + traverseArr[randNum];
    randPlace = nextPlace;
    addition =
      addition +
      parseInt(document.getElementById("numDiv" + randPlace).innerHTML);
    ConnectTen.chainArr.push(randPlace);
  }
  if (addition > 10) {
    ConnectTen.possibleChain();
    return 0;
  } else console.log(ConnectTen.chainArr);
};

ConnectTen.setLottery = function () {
  ConnectTen.zeroFlag = false;
  ConnectTen.zeroArr = [];
  var i, number, randZero, rno;
  for (i = 1; i <= Math.pow(ConnectTen.gridLength, 2); i++) {
    number = parseInt(document.getElementById("numDiv" + i).innerHTML);
    if (number == 0) {
      ConnectTen.zeroFlag = true;
      ConnectTen.zeroArr.push(i);
    }
  }
  if (ConnectTen.zeroFlag) {
    randZero = Math.floor(Math.random() * ConnectTen.zeroArr.length);
    ConnectTen.lotteryNum = "numDiv" + ConnectTen.zeroArr[randZero];
    rno = Math.floor(Math.random() * 2);
    document.getElementById(ConnectTen.lotteryNum).setAttribute("lottery", rno);
    document.getElementById(ConnectTen.lotteryNum).style.backgroundColor = "#140743";
  }
};
