// Game Started 8/11/2018
var FindIt={};
var Utils={};
Utils.Path='';
var questionId=0,startTime=0,scoredMarks=0,totalMark=0,resId=0,level=1,attempt="",newQuestionLoad=true;
Utils.mobileDeviceFlag=false;
$(document).ready(function()
{
	$(".se-pre-con").fadeOut("slow");
	if(navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i))
		Utils.mobileDeviceFlag=true;
	setTimeout(function()
	{
		FindIt.setImages();
		FindIt.setEvents();
		if(FindIt.hardCodedData['languageFont']!=null)
			setFontFamilyForLang("fonts/"+FindIt.hardCodedData['languageFont']);		
	},200);
});

setFontFamilyForLang=function(fontFilePath)
{       
  var newStyle = "<style>"+
            "@font-face {  "+
              "font-family: 'CustomFontFamily';"  +
                "src: url('"+fontFilePath+"') format('truetype');"+
                "}  </style>";
    $("head").append(newStyle);
    $("body").css("font-family","CustomFontFamily");            
}

FindIt.setImages=function()
{
	document.body.style.backgroundImage = "url("+Utils.Path+"images/Find-Alphabets.png)";
	document.getElementById('home').src=Utils.Path+"images/home.png";
	document.getElementById('logo').src=Utils.Path+"images/logo.png";
	document.getElementById('playImage').src=Utils.Path+"images/next.png";
	document.getElementById('playSoundBtn').src=Utils.Path+"images/sound.png";
	document.getElementById('replayImg').src=Utils.Path+"images/replay.png";
	document.getElementById('nextQueBtn').src=Utils.Path+"images/nextGame.png";
	document.getElementById('creditImg').src=Utils.Path+"images/credits.png";
	//document.getElementById('grid').style.backgroundImage = "url("+Utils.Path+'images/billdingwall1.png)';
	document.getElementById('footer').style.backgroundImage = "url("+Utils.Path+'images/wall.png)';
	//document.getElementById('suffixDiv').style.backgroundImage = "url("+Utils.Path+'images/window.png)';
}                                                                               

FindIt.setEvents=function()
{
	$("#home").click(function(){
		FindIt.callHome();
	});
	$("#playImage").click(function(){
		FindIt.playgame();
	});
	$("#sound").click(function(){
		FindIt.playSound();
	});
	$("#nextBtn").click(function(){
		getData();
	});
	$("#replayImg").click(function(){
			$("#acknowledgementDiv").empty();
			$("#endPage").hide();
		location.reload();
	});
	$("#credits").click(function(){
		$("#acknowledgementDiv").css("visibility","visible");
	});
}

// set suffix and sound
FindIt.playgame=function()
{
	FindIt.correct=0;
	FindIt.wrong=0;
	FindIt.reset();
	document.body.style.backgroundImage = "url(images/back.png)";
	document.getElementById("home").style.visibility="visible";
	document.getElementById("coverPage").style.display="none";
	document.getElementById("gamePage").style.display="block";
	document.getElementById('endPage').style.display="none";
	FindIt.copyJson=JSON.parse(JSON.stringify(FindIt.wordsData));
	FindIt.createDiv("#Grid0",3,"wordDivs col-xs-12 col-sm-12 col-md-12 col-lg-12 noPadding hex");
	FindIt.createDiv("#Grid2",3,"wordDivs col-xs-12 col-sm-12 col-md-12 col-lg-12 noPadding hex");
	FindIt.createDiv("#Grid1",1,"col-xs-12 col-sm-12 col-md-12 col-lg-12 noPadding");
	document.getElementById('wordDiv6').style.backgroundImage ="url("+Utils.Path+'images/tv.jpg)';
	document.getElementById('Grid0').style.backgroundImage ="url("+Utils.Path+'images/back2.jpg)';
	document.getElementById('Grid2').style.backgroundImage ="url("+Utils.Path+'images/back2.jpg)';
	getData();
}	

FindIt.reset=function(){
	FindIt.isLetterSelected=false;
	FindIt.totalscore=0;
	FindIt.indexArray=[];
	FindIt.nextTimeout=0;
	FindIt.questionArr=[];
	FindIt.attemptCnt=0;
	FindIt.questionCnt=0;
	FindIt.gridCnt=0;
	FindIt.optionCnt=6;
	FindIt.questionArr=[];
}

function getData(){
	$('.wordSpan').text('');
	clearTimeout(FindIt.answerAudio);
	FindIt.answerAudio=null;
	FindIt.questionArr=[];
	getUniqueValArr(FindIt.copyJson,FindIt.questionArr,	FindIt.optionCnt);
	FindIt.question=Math.floor(Math.random()*FindIt.questionArr.length);
	FindIt.answer=FindIt.questionArr[FindIt.question].word;
	console.log(FindIt.questionArr);
	console.log(FindIt.answer);
	FindIt.setQuestionAndOption();
}

function getUniqueValArr(inputArr,outputArr,length)
{
	for(var i=0;i<length;i++)
	{
		var randNum=Math.floor(Math.random()*inputArr.length);
		if(outputArr.includes(inputArr[randNum]) || randNum==11)
		{
			i--;
			continue;
		}
		else
		{
			outputArr.push(inputArr[randNum]);	
		}
	}
}

//create 16 div
FindIt.createDiv=function(parentDiv,gridCnt,cls)			
{
	var wordDiv,spanDiv;
	for(var j=0;j<gridCnt;j++)
	{
		wordDiv=document.createElement('div');
		wordDiv.setAttribute("id","wordDiv"+FindIt.gridCnt);
		wordDiv.setAttribute("class",cls);
		spanDiv=document.createElement('span');
		spanDiv.setAttribute("id","wordSpan"+FindIt.gridCnt);
		if(gridCnt==1)
			spanDiv.setAttribute("class","wordSpan question center");
		else{
			wordDiv.setAttribute("onclick","FindIt.checkAnswer(this.id)");
			spanDiv.setAttribute("class","wordSpan center");
		}
		$(parentDiv).append(wordDiv);
		$(wordDiv).append(spanDiv);
		FindIt.gridCnt++;
	}	
}

FindIt.setQuestionAndOption=function(){
	for(var i=0;i<FindIt.optionCnt;i++)
	{
				$("#wordSpan"+i).text(FindIt.questionArr[i].word);
				$("#wordDiv"+i).data("setWord",FindIt.questionArr[i].word);
	}
	if(i==6){
		$("#wordSpan"+6).text(FindIt.questionArr[FindIt.question].langWord);
		$("#wordDiv"+6).data("setWord",FindIt.questionArr[FindIt.question].word);
	}	
	FindIt.playSound();
}


/* //Chng (Callback)
function getDuration(src,callback)
{
	var duration=0;
	if(getDurationFlag)
	{
		$('#playSound').on("loadedmetadata", function(){
			getDurationFlag=false;
			duration=$('#playSound').prop('duration');
			$('#playSound').unbind('loadedmetadata');
			callback(duration);
	    });
	} 
    $('#playSound').prop('src',src);
}

FindIt.playSound=function()
{
	clearTimeout(FindIt.answerAudio);
	getDurationFlag=true;
	callback=FindIt.playInstruction;
	getDuration(Utils.Path+"sounds/where_is.mp3",callback);
	
}; 
FindIt.playLevelAudio=function(duration){
	document.getElementById("playSound").play();
	FindIt.answerAudio=setTimeout(function(){
	FindIt.playSound();
	},duration*1000);
};

FindIt.playInstruction=function(duration){
	if(newQuestionLoad==true)
		$('.spanclass1').css('pointer-events','none');
	
	document.getElementById("playSound").play();
	FindIt.answerAudio=setTimeout(function(){
		document.getElementById("playSound").src=Utils.Path+'sounds/'+FindIt.questionArr[FindIt.questionCnt].soundURL;
		document.getElementById("playSound").play();
		FindIt.answerAudio=setTimeout(function(){
			if(newQuestionLoad==true){
				$('.spanclass1').css('pointer-events','auto');
				newQuestionLoad=false;
			}
		},duration*800);
	},duration*1000);
}
*/
FindIt.playSound=function()
{
	document.getElementById("playSound").pause();
	FindIt.answerAudio=setTimeout(function(){
		document.getElementById("playSound").src=Utils.Path+'sounds/'+FindIt.questionArr[FindIt.question].sound;
		document.getElementById("playSound").play();
	},500);
}

FindIt.checkAnswer=function(id)
{
//	alert(id);
	var getClickedOption;
	getClickedOption=$("#"+id).children()[0].innerHTML;
	if(getClickedOption==FindIt.answer)
	{
		//alert('Correct');
		document.getElementById("playSound").src=Utils.Path+'sounds/CorrectSound.mp3';
		document.getElementById("playSound").play();
		FindIt.attemptCnt++;
		//$("#wordSpan"+6).addClass("animated zoomIn");
		setTimeout(function(){
			if(FindIt.attemptCnt<5){
		//	$("#wordSpan"+6).removeClass("animated zoomIn");
				getData();
			}
			else
				FindIt.showEndPage();
		},2000);
	}
	else
	{
		/* $("#wordSpan"+6).addClass("animated shake");
		setTimeout(function(){
			$("#wordSpan"+6).removeClass("animated shake");
		},2500); */
		//alert('Wrong');
		document.getElementById("playSound").src=Utils.Path+'sounds/wrong.mp3';
		document.getElementById("playSound").play();
	}
/* 	$('#'+myID).css('pointer-events','none');
	document.getElementById("playSound").src=Utils.Path+'sounds/Hollowwooddoor.mp3';
	document.getElementById("playSound").play(); */
}

/* FindIt.loadNextQue=function(){
	if(FindIt.nextTimeout)
	{
		clearTimeout(FindIt.nextTimeout);
		FindIt.nextTimeout=null;
	}

	FindIt.nextTimeout=setTimeout(function()
	{
		FindIt.next();
	},2000);
}
FindIt.answerwiseMessage=function(imageName,soundName)
{
	$(".spanclass1").css({pointerEvents:'none'});
	document.getElementById('ansImgDiv').style.visibility="visible";
	document.getElementById('ansImg').src=Utils.Path+"images/"+imageName;
	document.getElementById('playSound').src=Utils.Path+"sounds/"+soundName;
	document.getElementById('playSound').play();
};*/
/* FindIt.next=function()
{
	var attemptedLetter=attempt.split(",").splice(0,attempt.split(",").length-1);
	attempt=attempt.slice(0,attempt.length-1);
	console.log(attemptedLetter.length+"**"+attempt);
	if(FindIt.isLetterSelected){
		scoredMarks=FindIt.correct;
		if(Utils.mobileDeviceFlag)
			Android.addScore(resId,questionId,scoredMarks,totalMark,level,startTime,attempt);
			//Android.addScore(resId,questionId,scoredMarks,totalMark,level,startTime);
			console.log(questionId,scoredMarks,totalMark,level,startTime,attempt);
	}
	

	if(FindIt.isLetterSelected==false){
		scoredMarks=-1;
		if(Utils.mobileDeviceFlag)
			Android.addScore(resId,questionId,scoredMarks,totalMark,level,startTime,attempt);
			//Android.addScore(resId,questionId,scoredMarks,totalMark,level,startTime);
		console.log(questionId,scoredMarks,totalMark,level,startTime,attempt);
	}
/* 
	FindIt.isLetterSelected=false;
	scoredMarks=0;
	attempt="";
	FindIt.questionCnt++;
	FindIt.attemptCnt++;
	newQuestionLoad=true;
	FindIt.correct=0;
	FindIt.wrong=0;
	FindIt.indexArray=[],FindIt.suffixCount=0;
	clearTimeout(FindIt.nextTimeout);
	document.getElementById("playSound").pause();
	document.getElementById('ansImgDiv').style.visibility="hidden";
	$(".spanclass2").css({color:'#ffffff'});
	$('.spanclass1').css('pointer-events','auto');
	FindIt.setQuestionAndOption(); 
}
 */
FindIt.showEndPage=function(){
	document.getElementById("gamePage").style.display="none";
	document.getElementById("endPage").style.display="block";
	$("#home,#acknowledgementDiv").css("visibility","hidden");
	FindIt.AcknowledgementPage();
	$("#gameOver").text(FindIt.hardCodedData.gameOver);
}

FindIt.AcknowledgementPage=function()
{
var i=0,div;
gameOver.innerHTML=FindIt.hardCodedData.gameOver;
console.log(Object.keys(FindIt.AcknowledgementPageData).length);
for(i=0;i<Object.keys(FindIt.AcknowledgementPageData).length;i++)
{
	div=document.createElement('div');
	div.className="fontClass firstLetter text-center col-xs-12 col-sm-12 col-md-12 col-lg-12";
	div.innerHTML=Object.keys(FindIt.AcknowledgementPageData)[i]+" : "+Object.values(FindIt.AcknowledgementPageData)[i];
	acknowledgementDiv.appendChild(div);
}
div=document.createElement('div');
div.className="fontClass firstLetter text-center col-xs-12 col-sm-12 col-md-12 col-lg-12";
div.innerHTML=FindIt.hardCodedData['createdOn'];
acknowledgementDiv.appendChild(div);

}


FindIt.callHome=function()
{
	var attemptedLetter=attempt.split(",").splice(0,attempt.split(",").length-1);
	attempt=attempt.slice(0,attempt.length-1); 
	console.log(attemptedLetter.length+"**"+attempt);

	if(FindIt.isLetterSelected)
	{		scoredMarks=FindIt.correct;
		if(Utils.mobileDeviceFlag)
			Android.addScore(resId,questionId,scoredMarks,totalMark,level,startTime,attempt);	
		//Android.addScore(resId,questionId,scoredMarks,totalMark,level,startTime);	
		console.log(questionId,scoredMarks,totalMark,level,startTime,attempt);
	}	
		
FindIt.isLetterSelected=false;
	document.getElementById("playSound").pause();
	location.reload();
}

