var ShabdKhel={};
var Utils={};
Utils.Path='';
Utils.mobileDeviceFlag=false;
var questionId=0,startTime=0,scoredMarks=0,totalMarks=10,resId=0;
ShabdKhel.shabd='';
ShabdKhel.attempt=0;
ShabdKhel.correctFlag=0;
ShabdKhel.score=0;
ShabdKhel.Boardscore=0;
ShabdKhel.questionCount=0;
ShabdKhel.questionArray=[];
ShabdKhel.wrongRightArray=['vehichleDiv1','vehichleDiv2','vehichleDiv3'];
$(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("Shabdkhel");  

	setTimeout(function()
	{
		if(ShabdKhel.hardcodedData['languageFont']!=null)
			setFontFamilyForLang("fonts/"+ShabdKhel.hardcodedData['languageFont']);
		document.body.style.backgroundImage="url("+Utils.Path+"images/"+ShabdKhel.hardcodedData.bgImage+")";
		document.getElementById('coverpage').style.display="block";
		document.getElementById('gamepage').style.display="none";
		document.getElementById('nextDiv').style.display="none";
		document.getElementById('endpage').style.display="none";
		document.getElementById('home').style.visibility="hidden";
		document.getElementById('soundid').style.display="none";
		ShabdKhel.setImage();
	},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");            
}
ShabdKhel.showInstruction=function()
{
	document.getElementById('headerId').innerHTML=ShabdKhel.hardcodedData.header;
	document.getElementById('instructionDiv').style.display="block";
	document.getElementById('instructionPara').innerHTML=ShabdKhel.hardcodedData.instruction;
	document.getElementById('soundid').style.display="none";
	document.getElementById('scoreDiv').style.visibility="hidden";
	document.getElementById('nextDiv').style.display="none";
	document.getElementById('gamepage').style.display="none";
	document.getElementById('playGame').style.display="none";
	document.getElementById('skip').src=Utils.Path+"images/Play.png";
	document.body.style.backgroundImage="url("+Utils.Path+"images/BG.png)";
}
ShabdKhel.playGame=function()
{
	document.getElementById('scoreDiv').style.visibility="visible";
	document.getElementById('instructionDiv').style.display="none";
	document.getElementById('gamepage').style.display="block";
	document.getElementById('scoreDiv').innerHTML=ShabdKhel.hardcodedData.scoreMessage + " : "+ 0;
	document.body.style.backgroundImage="url("+Utils.Path+"images/BG.png)";
	document.getElementById('scoreDiv').style.backgroundImage="url("+Utils.Path+"images/Score-box.png)";
	document.getElementById('gamepage').style.display="block";
	document.getElementById('coverpage').style.display="none";
	document.getElementById('home').style.visibility="visible";
	document.getElementById('soundid').style.display="block";
	document.getElementById('nextDiv').style.display="block";
	ShabdKhel.createDiv();
}
// set Images//
ShabdKhel.setImage=function()
{
	document.getElementById('logo').src=Utils.Path+"images/logo.png";
	document.getElementById('play').src=Utils.Path+"images/Play.png";
	document.getElementById('home').src=Utils.Path+"images/Home.png";
	document.getElementById('next').src=Utils.Path+"images/Next.png";
	document.getElementById('sound').src=Utils.Path+"images/Sound.png";
	document.getElementById('playAgain').src=Utils.Path+"images/replay.png";
	document.getElementById('credit').src=Utils.Path+"images/credits.png";
}
//create div//
 ShabdKhel.createDiv=function()
 {
	ShabdKhel.checkedArray=['vehichleDiv1','vehichleDiv2','vehichleDiv3'];
	var divElement='',paraElement='',parentDiv;
	for(var i=1;i<=3;i++)
	{
		parentDiv= document.createElement('div');
		parentDiv.setAttribute('id','vehichle'+i);
		parentDiv.setAttribute('class','noPaddingcls col-sm-12 col-lg-12 col-md-12 col-xs-12');
		divElement= document.createElement('div');
		divElement.setAttribute('id','vehichleDiv'+i);
		divElement.setAttribute('class','backgroundProp noPaddingcls col-sm-3 col-lg-3 col-md-3 col-xs-3');
		divElement.style.backgroundImage="url("+Utils.Path+ShabdKhel.imageArray[i-1]+".png)";
		divElement.setAttribute('data',ShabdKhel.imageArray[i-1]);
		divElement.setAttribute('onclick','ShabdKhel.checkWord(this)');
		paraElement=document.createElement('para');
		paraElement.setAttribute('id','para'+i);
		paraElement.setAttribute('class','paraCls noPaddingcls col-sm-3 col-lg-3 col-md-3 col-xs-3');
		parentDiv.append(divElement);
		divElement.append(paraElement);
		vehichleDiv.append(parentDiv);
	}
	ShabdKhel.getWord();
 }
// set word and play sound//
ShabdKhel.getWord=function()
{
	ShabdKhel.carClickedFlag=false;
	ShabdKhel.shabd=Math.floor(Math.random() * (ShabdKhel.wordsData.length));
	ShabdKhel.questionArray.push(ShabdKhel.wordsData[ShabdKhel.shabd].word);
	questionId=parseInt(ShabdKhel.wordsData[ShabdKhel.shabd].questionId);
	ShabdKhel.splitletters();
	ShabdKhel.setWordsToBoxes();
}
ShabdKhel.playSound=function()
{
	document.getElementById('playsound').src=Utils.Path+"sounds/"+ShabdKhel.wordsData[ShabdKhel.shabd].sound;
	document.getElementById('playsound').play();
}
//split word into array//
ShabdKhel.splitletters=function()
{
	var shabd='',splitWord='';
	shabd=ShabdKhel.wordsData[ShabdKhel.shabd].word;
	shabd=shabd.trim();
	splitWord=shabd.split('');
	for(var i=0;i<splitWord.length;i++)
	{
		if(ShabdKhel.hindiKanhaMatraArr.includes(splitWord[i]))
		{
			splitWord[i-1]=(splitWord[i-1].concat(splitWord[i]));
			splitWord.splice(i,1);
			i--;
		}
	}
	randomLetter=Math.floor(Math.random() * splitWord.length);
	ShabdKhel.makeRandomWords(splitWord,randomLetter);
}
//make words//
ShabdKhel.makeRandomWords=function(splitWord,randomLetter)
{
	var word='';
	for(var i=0;ShabdKhel.questionArray.length<3;i++)
	{
		randomVariable=Math.floor(Math.random() * (ShabdKhel.mulAkshare.length));
		randomMulakshare=Math.floor(Math.random() * (ShabdKhel.mulAkshare[randomVariable].list.length));
		while(randomMulakshare == ShabdKhel.mulAkshare[randomVariable].list.length-1)
		{
			randomMulakshare=Math.floor(Math.random() * (ShabdKhel.mulAkshare[randomVariable].list.length));
		} 
		if(randomLetter==0)
			word=splitWord[randomLetter].concat(ShabdKhel.mulAkshare[randomVariable].list[randomMulakshare].word);
		else
			word=ShabdKhel.mulAkshare[randomVariable].list[randomMulakshare].word.concat(splitWord[randomLetter]);
		flag=ShabdKhel.checkData(word,ShabdKhel.questionArray);
		if(!(flag))
			ShabdKhel.questionArray.push(word);
	}
	//shuffle words
	for(var i=0;i<ShabdKhel.questionArray.length;i++)
	{
		var randomVar=Math.floor(Math.random() * (ShabdKhel.questionArray.length));
		var temp = ShabdKhel.questionArray[randomVar];
		ShabdKhel.questionArray[randomVar] = ShabdKhel.questionArray[i];
		ShabdKhel.questionArray[i] = temp;
	}
}

ShabdKhel.checkData=function(data,tempArray)
{
	var flag=0;
	for(var j=0;j<tempArray.length;j++)
	{
		if(tempArray[j]==data)
		{
			flag=1;
			break;
		}
	}
	return flag;
} 

// set words to boxes//
ShabdKhel.setWordsToBoxes=function()
{	
	var d=new Date();
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	$('.setnext').css('pointer-events','auto');
	for(var i=0;i<ShabdKhel.questionArray.length;i++)
	{
		document.getElementById('para'+(i+1)).innerHTML=ShabdKhel.questionArray[i];
	}
	document.getElementById('playsound').src=Utils.Path+"sounds/"+ShabdKhel.wordsData[ShabdKhel.shabd].sound;
	document.getElementById('playsound').play();
	ShabdKhel.moveCar();
}
ShabdKhel.moveCar=function()
{
	ShabdKhel.vehichlepos=0;
	ShabdKhel.moveCarInterval=setInterval(function()
	{
		ShabdKhel.vehichlepos=ShabdKhel.vehichlepos+0.2;
		for(var i=0;i<ShabdKhel.wrongRightArray.length;i++)
		{
			document.getElementById(ShabdKhel.wrongRightArray[i]).style.left=ShabdKhel.vehichlepos +'%';
		}
		if(ShabdKhel.vehichlepos > 70)
		{
			clearInterval(ShabdKhel.moveCarInterval);
			ShabdKhel.nextTimeout=setTimeout(function()
			{
				clearInterval(ShabdKhel.moveCarInterval);
				clearTimeout(ShabdKhel.nextTimeout);
				ShabdKhel.nextTimeout=null;
				ShabdKhel.nextWord();
			},800);
		}
	},100);
	
}
//click on word and show joker//
ShabdKhel.checkWord=function(obj)
{
	var vehichlepos=0,objId,divElement='',parentId,dashvehiclepos=-45,divData='',j=0;
	ShabdKhel.carClickedFlag=true;
	sib=$(obj).children().eq(0).prop('id');
	objId=$(obj).prop('id');
	parentId=$(obj).parent().prop('id');
	divData=document.getElementById(objId).getAttribute('data');
	divData=divData+'_hit';
	shabd=ShabdKhel.wordsData[ShabdKhel.shabd].word;
	$('.backgroundProp').css('pointer-events','none');
	
	ShabdKhel.nextTimeout=null;
	if(document.getElementById(sib).innerHTML==shabd)
	{
		ShabdKhel.rightAns(objId);
	}
	else
	{
		ShabdKhel.wrongAns(objId,sib,parentId,divData);
	}
	document.getElementById('scoreDiv').innerHTML=ShabdKhel.hardcodedData.scoreMessage + " : "+ShabdKhel.Boardscore;
}	
ShabdKhel.rightAns=function(objId)
{
	clearInterval(ShabdKhel.moveCarInterval);
	clearTimeout(ShabdKhel.nextTimeout);
	vehichlepos=0;
	ShabdKhel.correctFlag=1;
	checkedArray=['vehichleDiv1','vehichleDiv2','vehichleDiv3'];
	ShabdKhel.wrongRightArray.splice(ShabdKhel.wrongRightArray.indexOf(objId),1);
	console.log(ShabdKhel.wrongRightArray);
	rightVehichlePos=ShabdKhel.vehichlepos;
	ShabdKhel.questionCount++;
	if(ShabdKhel.attempt==1)
	{
		ShabdKhel.score=ShabdKhel.score+5;
		ShabdKhel.Boardscore=ShabdKhel.Boardscore+5;	
	}
	else
	{
		ShabdKhel.score=ShabdKhel.score+10;
		ShabdKhel.Boardscore=ShabdKhel.Boardscore+10;
	}
	ShabdKhel.carClickedFlag=false;
	console.log(resId,questionId,ShabdKhel.score,totalMarks,1,startTime);
	if(Utils.mobileDeviceFlag)
		Android.addScore(resId,questionId,ShabdKhel.score,totalMarks,1,startTime);
	document.getElementById('playsound').src=Utils.Path+"sounds/Carpassinghorn.mp3"
	document.getElementById('playsound').play();
	if(ShabdKhel.rightInterval)
	{
		clearInterval(ShabdKhel.rightInterval);
		ShabdKhel.rightInterval=null;
	}
	ShabdKhel.rightInterval=setInterval(function()
	{
		ShabdKhel.vehichlepos++;
		rightVehichlePos=rightVehichlePos+2;
		for(var i=0;i<ShabdKhel.wrongRightArray.length;i++)
		{
			document.getElementById(ShabdKhel.wrongRightArray[i]).style.left=ShabdKhel.vehichlepos +'%';
		}
		document.getElementById(objId).style.left=rightVehichlePos + '%';
		if(rightVehichlePos > 73)
		{
			clearInterval(ShabdKhel.rightInterval);
			document.getElementById(objId).style.visibility="hidden";
			if(ShabdKhel.nextTime)
			{
				clearTimeout(ShabdKhel.nextTime);
				ShabdKhel.nextTime=null;
			}
			ShabdKhel.nextTime=setTimeout(function()
			{
				ShabdKhel.nextWord();
			},800);
		}
	},120);
}
ShabdKhel.wrongAns=function(objId,sib,parentId,divData)
{
	wrongCheckedArray=['vehichleDiv1','vehichleDiv2','vehichleDiv3'];
	ShabdKhel.wrongRightArray.splice(ShabdKhel.wrongRightArray.indexOf(objId),1);
	console.log(ShabdKhel.wrongRightArray);
	vehichlepos=0,dashvehiclepos=-45,j=0,divElement='';
	ShabdKhel.attempt++;
	divElement=document.createElement('div');
	divElement.setAttribute('id','dashingVehicle'+ShabdKhel.attempt);
	divElement.setAttribute('class','backgroundProp noPaddingcls col-sm-2 col-lg-2 col-md-2 col-xs-2');
	divElement.style.backgroundImage="url("+Utils.Path+"images/Red-car.png)";
	$(divElement).insertBefore( $('#'+objId));
	divElement.style.position='relative';
	divElement.style.left='-41%';
	dashvehiclepos=ShabdKhel.vehichlepos;
	document.getElementById(parentId).append(divElement);
	 if(ShabdKhel.attempt==2)
		ShabdKhel.questionCount++;
		ShabdKhel.vehichlepos++;
		dashvehiclepos=dashvehiclepos-30;
		for(var i=0;i<ShabdKhel.wrongRightArray.length;i++)
		{
			document.getElementById(ShabdKhel.wrongRightArray[i]).style.left=ShabdKhel.vehichlepos +'%';
		}
		document.getElementById('dashingVehicle'+ShabdKhel.attempt).style.left=dashvehiclepos + '%';
		clearInterval(ShabdKhel.wrongInterval);
		document.getElementById('playsound').src=Utils.Path+"sounds/carrash.mp3"
		document.getElementById('playsound').play();
		dashvehiclepos=dashvehiclepos+8;
		document.getElementById('dashingVehicle'+ShabdKhel.attempt).style.backgroundImage="url("+Utils.Path+"images/Red-car_hit.png)";
		document.getElementById(objId).style.backgroundImage="url("+Utils.Path+""+divData+".png)";
		j=ShabdKhel.checkedArray.indexOf(objId);
		ShabdKhel.checkedArray.splice(j,1);
		for(var i=0;i<ShabdKhel.checkedArray.length;i++)
		{
			document.getElementById(ShabdKhel.checkedArray[i]).style.pointerEvents="auto";
		} 
		if(ShabdKhel.attempt==2)
		{	
			$('.backgroundProp').css('pointer-events','none');
			if(ShabdKhel.nextTime)
			{
				clearTimeout(ShabdKhel.nextTime);
				ShabdKhel.nextTime=null;
			}
			ShabdKhel.nextTime=setTimeout(function()
			{
				ShabdKhel.nextWord();
			},800);
		}
}
// next word//
ShabdKhel.nextWord=function()
{
	ShabdKhel.wrongRightArray=['vehichleDiv1','vehichleDiv2','vehichleDiv3'];
	if(ShabdKhel.attempt==1 && ShabdKhel.correctFlag==0)
		ShabdKhel.questionCount++;
	if(ShabdKhel.carClickedFlag)
	{
		console.log(resId,questionId,ShabdKhel.score,totalMarks,1,startTime);
		if(Utils.mobileDeviceFlag)
			Android.addScore(resId,questionId,ShabdKhel.score,totalMarks,1,startTime);
	}
	else if(ShabdKhel.carClickedFlag == false && ShabdKhel.correctFlag==0)
	{
		console.log(resId,questionId,-2,totalMarks,1,startTime);
		if(Utils.mobileDeviceFlag)
			Android.addScore(resId,questionId,-2,totalMarks,1,startTime);
	}
	clearInterval(ShabdKhel.rightInterval);
	clearInterval(ShabdKhel.wrongInterval);
	clearInterval(ShabdKhel.moveCarInterval);
	clearTimeout(ShabdKhel.nextTime);
	clearTimeout(ShabdKhel.nextTimeout);
	ShabdKhel.nextTimeout=null;
	document.getElementById('playsound').pause();
	if(ShabdKhel.questionCount<5)
	{
		ShabdKhel.attempt=0;
		ShabdKhel.questionArray=[];
		vehichleDiv.innerHTML='';
		ShabdKhel.createDiv();
	}
	else
		ShabdKhel.showLastPage();
	ShabdKhel.correctFlag=0;
	ShabdKhel.score=0;
	ShabdKhel.carClickedFlag=false;
}

ShabdKhel.HomePage=function()
{
	if(ShabdKhel.carClickedFlag)
	{
		console.log(resId,questionId,ShabdKhel.score,totalMarks,1,startTime);
		if(Utils.mobileDeviceFlag)
			Android.addScore(resId,questionId,ShabdKhel.score,totalMarks,1,startTime);
	}
	location.reload(true);
}

ShabdKhel.showLastPage=function()
{
	document.getElementById('soundid').style.display="none";
	document.getElementById('nextDiv').style.display="none";
	document.getElementById('scoreDiv').style.visibility="hidden";
	document.getElementById("endpage").style.display="block";
	document.getElementById("gamepage").style.display="none";
	document.getElementById('gameOver').innerHTML=ShabdKhel.hardcodedData.message2;
	for(i=0;i<Object.keys(ShabdKhel.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(ShabdKhel.AcknowledgementPageData)[i]+" : "+Object.values(ShabdKhel.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.innerHTML=ShabdKhel.hardcodedData['createdOn'];
	acknowledgementDiv.appendChild(div);
	document.getElementById("acknowledgementDiv").style.display="none";
} 
ShabdKhel.showCredits=function()
{
	document.getElementById("acknowledgementDiv").style.display="block";
}
ShabdKhel.playAgain=function()
{
	ShabdKhel.shabd='';
	ShabdKhel.attempt=0;
	ShabdKhel.correctFlag=0;
	ShabdKhel.score=0;
	ShabdKhel.Boardscore=0;
	ShabdKhel.questionCount=0;
	ShabdKhel.carClickedFlag=false;
	ShabdKhel.questionArray=[];
	ShabdKhel.wrongRightArray=['vehichleDiv1','vehichleDiv2','vehichleDiv3'];
	clearInterval(ShabdKhel.rightInterval);
	clearInterval(ShabdKhel.wrongInterval);
	clearInterval(ShabdKhel.moveCarInterval);
	clearTimeout(ShabdKhel.nextTime);
	clearTimeout(ShabdKhel.nextTimeout);
	ShabdKhel.attempt=0;
	ShabdKhel.questionArray=[];
	vehichleDiv.innerHTML='';
	document.getElementById('soundid').style.display="block";
	document.getElementById('nextDiv').style.display="block";
	document.getElementById('scoreDiv').style.visibility="visible";
	document.getElementById("endpage").style.display="none";
	document.getElementById("gamepage").style.display="block";
	acknowledgementDiv.innerHTML='';
	document.getElementById('scoreDiv').innerHTML=ShabdKhel.hardcodedData.scoreMessage + " : "+ 0;
	ShabdKhel.createDiv();
}