WordSearch={};
var Utils={};
Utils.Path='';
Utils.mobileDeviceFlag=false;
var questionId=0,startTime=0,scoredMarks=0,totalMarks=10,resId=0,levelNo;
var correctCnt=0,skipCnt=0,countDown=60;
WordSearch.queArr=[];
WordSearch.idArr=[];
WordSearch.homeFlag="goback";
$(document).ready(function()
{
	$(".se-pre-con").fadeOut("slow");
	if(navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i))
		Utils.mobileDeviceFlag=true;
	/* if(Utils.mobileDeviceFlag)
		Utils.Path=Android.getMediaPath(""); */
	setTimeout(function()
	{
		document.getElementById("coverpage").style.display="block";
		document.getElementById("gamepage").style.display="none";
		document.getElementById("modalPage").style.display="none";
		document.getElementById("widgetDiv").style.display="none";
		document.getElementById("middlepage").style.display="none";
		document.getElementById("homeImg").style.display="none";
		//document.getElementById("characterDiv").style.visibility="hidden";
		WordSearch.setImages();
		WordSearch.createMiddlePageDivs();
	},300);
});
WordSearch.setImages=function()
{
	document.body.style.backgroundImage="url("+Utils.Path+"images/Cover.png)";
	document.getElementById("homeImg").src=Utils.Path+"images/Home.png";
	document.getElementById("logoImg").src=Utils.Path+"images/logo.png";
	document.getElementById("playImg").src=Utils.Path+"images/Play.png";
	document.getElementById("reloadImg").src=Utils.Path+"images/reload.png";
	document.getElementById("checkImg").src=Utils.Path+"images/Done.png";
	document.getElementById("characterImg").src=Utils.Path+"images/standingBoy.png";
}
WordSearch.playGame=function()
{
	WordSearch.homeFlag="goback";
	document.getElementById("playSound").pause();	
	$("#modalDiv").removeClass("animated zoomIn");
	clearInterval(WordSearch.Timer);
	WordSearch.queArr=[];
	countDown=60;
	$("#modalPage").hide();
	document.getElementById("coverpage").style.display="none";
	document.getElementById("middlepage").style.display="block";
	document.getElementById("gamepage").style.display="none";
	document.getElementById("questionSet").style.display="none";
	document.getElementById("widgetDiv").style.display="block";
	document.getElementById("homeImg").style.display="block";
	document.body.style.backgroundImage="url("+Utils.Path+"images/BG.png)";
}
WordSearch.createMiddlePageDivs=function()
{
	var levelDiv,levelImg;
	for(var i=0;i<3;i++)
	{
		levelDiv=document.createElement("div");
		levelDiv.setAttribute("id","levelDiv"+i);
		levelDiv.setAttribute("class","noPadding col-md-3 col-xs-3 col-lg-3 col-sm-3 col-sm-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-offset-1");
		document.getElementById("middlepage").append(levelDiv);

		levelImg=document.createElement("img");
		levelImg.setAttribute("id","levelImg"+i);
		levelImg.setAttribute("class","img-responsive noPadding col-lg-10 col-md-10 col-xs-10 col-sm-10");
		levelImg.setAttribute("src",Utils.Path+"images/"+WordSearch.levelImages.imgName+i+WordSearch.levelImages.imgType);
		levelImg.setAttribute("onclick","WordSearch.selectLevel(this)");
		levelDiv.appendChild(levelImg);
		$(levelImg).data("levelNum",i);
	}
}
WordSearch.selectLevel=function(element)
{
	clearTimeout(WordSearch.autoCheck);
	if($(element).data("levelNum")==0)
		levelNo=0;
	else if($(element).data("levelNum")==1)
		levelNo=1;
	else
		levelNo=2;
	document.getElementById("gamepage").style.display="block";
	document.getElementById("questionSet").style.display="block";
	document.getElementById("middlepage").style.display="none";
	WordSearch.createLetterDivs(WordSearch.levels[levelNo].rows,WordSearch.levels[levelNo].cols,WordSearch.levels[levelNo].className);
	document.getElementById("counterDiv").innerHTML=countDown;
	WordSearch.createQuestionSetDivs();
	WordSearch.showWordsInGrid();
}
WordSearch.createLetterDivs=function(rows,cols,className)
{
	var mainDiv,letterDiv,letterNum=1;
	document.getElementById("gridDiv").innerHTML="";
	for(var i=0;i<rows;i++)
	{
		mainDiv=document.createElement("div");
		mainDiv.setAttribute("id","mainDiv"+i);
		mainDiv.setAttribute("class","noPadding displayClass col-lg-12 col-md-12 col-xs-12 col-sm-12");
		document.getElementById("gridDiv").append(mainDiv);
		for(j=0;j<cols;j++)
		{
			letterDiv=document.createElement("div");
			letterDiv.setAttribute("id","letterDiv"+letterNum);
			letterDiv.setAttribute("class","noPadding borderClass "+className);
			var randomBalloon=Math.floor((Math.random()*(5-1))+1);
			letterDiv.style.backgroundImage="url("+Utils.Path+"images/"+WordSearch.balloonImage.imgName+randomBalloon+WordSearch.balloonImage.imgType+")";
			letterDiv.setAttribute("onclick","WordSearch.selectLetter(this)");
			mainDiv.append(letterDiv);
			letterNum++;
		}
	}
}
WordSearch.createQuestionSetDivs=function()
{
	var quesDiv,questions;
	document.getElementById("questionSet").innerHTML="";
	WordSearch.queArr=populate(WordSearch.Data,WordSearch.queArr,WordSearch.levels[levelNo].words);
	questions=document.createElement("div");
	questions.setAttribute("id","questions");
	questions.setAttribute("class","noPadding "+WordSearch.levels[levelNo].questionClass);
	document.getElementById("questionSet").appendChild(questions);
	for(var i=0;i<WordSearch.levels[levelNo].words;i++)
	{
		quesDiv=document.createElement("div");
		quesDiv.setAttribute("id","quesDiv"+i);
		quesDiv.setAttribute("class","noPadding col-xs-1 col-sm-1 col-md-1 col-lg-1 wordsClass");
		questions.appendChild(quesDiv);
	}
}
WordSearch.showWordsInGrid=function()
{
	var randomIndex=0,splitQueArr=[],indexArray=[],letterNum=1,randomLetter,wholeSplitArr=[];
	correctCnt=0;
	WordSearch.homeFlag="selectLevel";
	$(".wordsClass").css("text-decoration","none");
	$("#checkDiv").css("pointer-events","none");
	if(WordSearch.Timer)
	{
		clearInterval(WordSearch.Timer);
		WordSearch.Timer=null;
	}
	WordSearch.Timer=setInterval(function()
	{
		countDown-=1;
		document.getElementById("counterDiv").innerHTML=countDown;
		if(countDown==0)
		{
			clearInterval(WordSearch.Timer);
			WordSearch.showModal();
		}
	},1000);
	if(levelNo==0)
	{
		$(".borderClass").css("font-size","3.5vw");
		$(".borderClass").css("margin","1%");
		/* $(".borderClass").css("width","12vw"); */
	}
	else if(levelNo==1)
	{
		$(".borderClass").css("margin-left","2vw");
		$(".borderClass").css("margin-top","1vh");
		$(".borderClass").css("width","7vw");
	}
	else if(levelNo==2)
	{
		$(".borderClass").css("margin-left","2vw");
		$(".borderClass").css("margin-top","1vh");
		$(".borderClass").css("width","12vw");
	}
	var d=new Date();
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	//console.log(WordSearch.queArr);
	for(var i=0;i<WordSearch.queArr.length;i++)
	{
		splitQueArr=WordSearch.queArr[i].split("");
		for(j=0;j<splitQueArr.length;j++)									//gets random indices
		{
			randomIndex=Math.floor((Math.random()*(WordSearch.levels[levelNo].rows*WordSearch.levels[levelNo].cols)+1));
			while($.inArray(randomIndex,indexArray)!==-1)
				randomIndex=Math.floor((Math.random()*(WordSearch.levels[levelNo].rows*WordSearch.levels[levelNo].cols)+1));
			indexArray.push(randomIndex);
			wholeSplitArr.push(splitQueArr[j]);
		}
	}
	for(var i=0;i<WordSearch.levels[levelNo].words;i++)							//display question Set
		document.getElementById("quesDiv"+i).innerHTML=WordSearch.queArr[i];
	shuffleArray(wholeSplitArr);
	for(var i=0;i<wholeSplitArr.length;i++)									//display Letters in different positions
		document.getElementById("letterDiv"+indexArray[i]).innerHTML=wholeSplitArr[i];
	for(j=0;j<WordSearch.levels[levelNo].rows*WordSearch.levels[levelNo].cols;j++)//display remainining Letters			
	{
		if(document.getElementById("letterDiv"+letterNum).innerHTML=="")
		{
			randomLetter=Math.floor(Math.random()*WordSearch.letter.length);
			while($.inArray(WordSearch.letter[randomLetter],wholeSplitArr)!==-1)
				randomLetter=Math.floor(Math.random()*WordSearch.letter.length);
			document.getElementById("letterDiv"+letterNum).innerHTML = WordSearch.letter[randomLetter];
		}
		letterNum++;
	}
}
WordSearch.selectLetter=function(str)
{
	//$(str).css("pointer-events","none");
	WordSearch.idArr.push(str.id);
	$("#checkDiv").css("pointer-events","auto");
	document.getElementById("characterImg").src=Utils.Path+"images/boy.gif";
	document.getElementById("playSound").pause();	
	document.getElementById("playSound").src=Utils.Path+"sounds/ting.mp3";
	document.getElementById("playSound").play();
	$(str).css("background-color","yellow");
	if(WordSearch.autoCheck)
	{
		clearTimeout(WordSearch.autoCheck);
		WordSearch.autoCheck=null;
	}
	WordSearch.autoCheck=setTimeout(function()
	{
		WordSearch.checkAnswer();
	},3000);
}
WordSearch.checkAnswer=function()
{
	var answer="";
	$("#gridDiv").css("pointer-events","none");
	for(var i=0;i<WordSearch.idArr.length;i++)
		answer+=$("#"+WordSearch.idArr[i]).text();
	document.getElementById("playSound").pause();
	//document.getElementById("characterDiv").style.visibility="visible";
	$("#checkDiv").css("pointer-events","none");
	console.log(answer);
	if(WordSearch.queArr.includes(answer))
	{
		scoredMarks=10;
		for(var i=0;i<WordSearch.idArr.length;i++)
		{
			$("#"+WordSearch.idArr[i]).css("background-color","green");
			$("#"+WordSearch.idArr[i]).css("opacity","0.6");
			$("#"+WordSearch.idArr[i]).addClass("animated fadeOutUp");
			$("#"+WordSearch.idArr[i]).css("pointer-events","none");
		}
		document.getElementById("playSound").src=Utils.Path+WordSearch.Sound[0];
		document.getElementById("playSound").play();
		document.getElementById("characterImg").src=Utils.Path+WordSearch.Image[0];
		correctCnt++;
		for(var i=0;i<$("#questions").children().length;i++)
		{
			if($("#quesDiv"+i).text()==answer)
			{
				$("#quesDiv"+i).css("text-decoration","line-through");
				break;
			}
		}
		if(WordSearch.nextQueTimer)
		{
			clearTimeout(WordSearch.nextQueTimer);
			WordSearch.nextQueTimer=null;
		}
		if(correctCnt==WordSearch.levels[levelNo].words)
			WordSearch.showModal();
	}
	else
	{
		scoredMarks=0;
		for(var i=0;i<WordSearch.idArr.length;i++)
		{
			$("#"+WordSearch.idArr[i]).css("background-color","red");
			$("#"+WordSearch.idArr[i]).addClass("animated tada");
		}
		document.getElementById("playSound").src=Utils.Path+WordSearch.Sound[1];
		document.getElementById("playSound").play();
		document.getElementById("characterImg").src=Utils.Path+WordSearch.Image[1];
		setTimeout(function(){
			for(var i=0;i<WordSearch.idArr.length;i++)
			{
				$("#"+WordSearch.idArr[i]).css("background-color","transparent");
				$("#"+WordSearch.idArr[i]).removeClass("animated tada");
			}
		},1000);
	}
	setTimeout(function(){
		//document.getElementById("characterDiv").style.visibility="hidden";
			document.getElementById("characterImg").src=Utils.Path+"images/standingBoy.png";
		$("#gridDiv").css("pointer-events","auto");
		WordSearch.idArr=[];
		},1000);
	console.log(resId,questionId,scoredMarks,totalMarks,1,startTime);
	if(Utils.mobileDeviceFlag)
		Android.addScore(resId,questionId,scoredMarks,totalMarks,1,startTime);
}
WordSearch.nextQuestion=function()
{
	clearTimeout(WordSearch.nextQueTimer);
	skipCnt++;
	WordSearch.resetAll();
	$("#checkDiv").css("pointer-events","auto");
	$(".borderClass").css("pointer-events","auto");
	//document.getElementById("characterDiv").style.visibility="hidden";
	document.getElementById("playSound").pause();
	WordSearch.createLetterDivs(WordSearch.levels[levelNo].rows,WordSearch.levels[levelNo].cols,WordSearch.levels[levelNo].className);
	WordSearch.createQuestionSetDivs();
	WordSearch.showWordsInGrid();
}
WordSearch.playAgain=function()
{
	skipCnt=0;
	correctCnt=0;
	$("#modalDiv").removeClass("animated zoomIn");
	document.getElementById("modalPage").style.display="none";
	document.getElementById("gamepage").style.display="block";
	document.getElementById("widgetDiv").style.display="block";
	document.getElementById("questionSet").style.display="block";
	document.getElementById("message").style.visibility="hidden";
	WordSearch.createLetterDivs(WordSearch.levels[levelNo].rows,WordSearch.levels[levelNo].cols,WordSearch.levels[levelNo].className);
	WordSearch.resetAll();
	WordSearch.createQuestionSetDivs();
	WordSearch.showWordsInGrid();
}
WordSearch.resetAll=function()
{
	countDown=60;
	clearInterval(WordSearch.Timer);
	clearTimeout(WordSearch.autoCheck);
	WordSearch.queArr=[];
	WordSearch.idArr=[];
	document.getElementById("counterDiv").innerHTML=countDown;
	WordSearch.queArr=populate(WordSearch.Data,WordSearch.queArr,WordSearch.levels[levelNo].words);
}
WordSearch.callHome=function()
{
	if(WordSearch.homeFlag=="selectLevel")
		WordSearch.playGame();
	else
		location.reload(true);
}
WordSearch.showModal=function()
{
	clearInterval(WordSearch.Timer);
	document.getElementById("modalPage").style.display="block";
	$("#modalDiv").addClass("animated zoomIn");
	document.getElementById("playAgainButton").innerHTML=WordSearch.hardcoded.playAgainButton;
	document.getElementById("chooseLevelButton").innerHTML=WordSearch.hardcoded.chooseLevelButton;
	document.getElementById("gameOverDiv").innerHTML=WordSearch.hardcoded.gameOver;
	document.getElementById("timeDiv").innerHTML="Time Taken : "+(60-countDown)+" Sec";
	document.getElementById("totalWordsDiv").innerHTML="Total Words : "+WordSearch.levels[levelNo].words;
	document.getElementById("correctwordsDiv").innerHTML="Correct Words : "+correctCnt;
}