var Preposition_2 = {};
var Utils = {};
Utils.path = '';
var src,srcdiv,destdiv,dest,wrongAnswer;
Preposition_2.globalVariables = function ()
{
	Preposition_2.selectRandomWord = [];
	Preposition_2.randomNumber;  //generate random number
	Preposition_2.countQue = 0;  //to count no of questions
	Preposition_2.Attempt = 0;  //to check question is already attempt or not
	Preposition_2.blankFiledId;
	Preposition_2.correctSapnID;
	Preposition_2.correctWord;
	Preposition_2.rightAnswerTimer;
	Preposition_2.rightAnswerShowTimer;
	Preposition_2.clickedOptionData;
}
$(document).ready(function () {
	if(navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i) && !(window.location.href.includes('http')))
		Utils.mobileDeviceFlag = true;
	$(".se-pre-con").fadeOut("slow");
	setTimeout(function () {
		Preposition_2.setimage();
	}, 100);
}, 200);
//set the all images
Preposition_2.setimage = function () {
	document.body.style.backgroundImage = "url(" + Utils.path + Preposition_2.coverImage + ")";
	document.getElementById("playbtnid").src = Utils.path + "images/Play.png";
	document.getElementById("homeid").src = Utils.path + "images/Home.png";
	document.getElementById("logoid").src = Utils.path + "images/logo.png";
	document.getElementById("bnext").src = Utils.path + "images/Next.png";
	document.getElementById("AllOptions").style.backgroundImage  = Utils.path + "url('images/option bar.png')";
	document.getElementById("sound").src = Utils.path + "images/Sound.png";
	//document.getElementById("HoleQuestion").style.backgroundImage = Utils.path + "url('images/Qbox.png')";
	document.getElementById("acknowledgementDiv").style.backgroundImage = Utils.path + "url('images/option box.png')";
	document.getElementById("gameover").style.backgroundImage = Utils.path + "url('images/option box.png')";
	document.getElementById("resetbtnid").src = Utils.path + "images/Replay.png";
	document.getElementById("creditbtnid").src = Utils.path + "images/Credits.png";
	document.getElementById("showgif").src = Utils.path + "images/Qbox.png";
}
Preposition_2.gamePage = function () {
	Preposition_2.globalVariables();
	document.body.style.backgroundImage = Utils.path + "url('images/BG.png')";
	document.getElementById("acknowledgementDiv").innerHTML = "";
	$("#coverpage,#endpage").css( {"display":"none"});
	$("#gamepage").css( {"display":"block"});
	$("#homeid").css( {"visibility":"visible"});
	$("#ShowCorrectWord").css( {"visibility":"hidden"});
	Preposition_2.populate();
}

Preposition_2.populate = function () {
	for (var j = 0; j < 5; j++) {
		var randomNumber = Math.floor(Math.random() * Preposition_2.data.length);
		while (Preposition_2.selectRandomWord.includes(Preposition_2.data[randomNumber])) {
			randomNumber = Math.floor(Math.random() * Preposition_2.data.length);
		}
		Preposition_2.selectRandomWord.push(Preposition_2.data[randomNumber]);
	}
	Preposition_2.showQuestionOptions();
	Preposition_2.playSound();
}
Preposition_2.playSound=function()
{
	document.getElementById("audio").pause();
	document.getElementById("audio").src = Utils.path + 'Sounds/instruction/instruction1.mp3';
	document.getElementById("audio").play();
}
Preposition_2.showQuestionOptions=function()
{
	document.getElementById("bnext").style.visibility = "hidden";
	document.getElementById("wrongWordInstruction").innerHTML =Preposition_2.selectRandomWord[Preposition_2.countQue].HindiSentence;
	var sentenceWordsInArray=Preposition_2.selectRandomWord[Preposition_2.countQue].sentence.split(" ");
	var wrongWord=Preposition_2.selectRandomWord[Preposition_2.countQue].wrongoption;
	Preposition_2.correctWord=Preposition_2.selectRandomWord[Preposition_2.countQue].correctAnswer;
	for(var i=0;i<sentenceWordsInArray.length;i++)
	{
		var div = document.createElement("span");
		div.setAttribute("id", "span" + (i + 1));
		div.setAttribute("class", " nopadding words text-center");
		document.getElementById('FillCorrectWord').appendChild(div);
		var word=sentenceWordsInArray[i].split("." || "?");
		if(word[0]==wrongWord)
		{
			div.setAttribute("class", " words dash");
			div.setAttribute("data", Preposition_2.correctWord);
			Preposition_2.blankFiledId="span" + (i + 1);
			div.innerHTML =wrongWord;
			div.style.color="red";
		}
		else
		{
		div.innerHTML =sentenceWordsInArray[i];
		}
	}
	var options=Preposition_2.selectRandomWord[Preposition_2.countQue].optionArr;
	for(var i=0;i<options.length;i++)
	{
		var div = document.createElement("div");
		div.setAttribute("id", "optiondiv" + (i + 1));
		div.setAttribute("class", " nopadding  bottomSentences words1 text-center col-sm-3 col-lg-3 col-md-3 col-xs-3");
		document.getElementById('ShowOptions').appendChild(div);
		var span = document.createElement("span");
		span.setAttribute("id", "optionSpan" + (i + 1));
		span.setAttribute("class", " nopadding   text-center");
		div.appendChild(span);
		div.style.backgroundImage = Utils.path + "url('images/option box.png')";
		span.setAttribute("onclick","Preposition_2.dragDropFunction(this)");
		span.innerHTML =options[i];
		if(options[i]==Preposition_2.correctWord)
			Preposition_2.correctSapnID="optiondiv" + (i + 1);
	}
}
Preposition_2.dragDropFunction=function(obj)
{
	Preposition_2.clickedOptionData="";
	Preposition_2.clickedOptionData=document.getElementById(obj.id).innerHTML;
	console.log(Preposition_2.clickedOptionData);
	Preposition_2.checkAnswer();
}
Preposition_2.checkAnswer=function()
{		
		Preposition_2.Attempt++;
		document.getElementById("audio").pause();
		document.getElementById("ShowCorrectWord").innerHTML =Preposition_2.selectRandomWord[Preposition_2.countQue].correctSentence;
		document.getElementById("bnext").style.visibility = "visible";
		$("#ShowCorrectWord").removeClass("animated fadeInDown");
		document.getElementById(Preposition_2.blankFiledId).innerHTML=Preposition_2.clickedOptionData;
		if(Preposition_2.clickedOptionData==Preposition_2.correctWord)
		{
			//document.getElementById(Preposition_2.blankFiledId).innerHTML="";
			$(destdiv).append(src);
			//$("#AllOptions").css( {"marginTop":"9vh"});
			$("#parentwrongWordInstruction").css( {"marginTop":"-34vh"});
			$("#FilltheBlank").css( {"marginTop":"-28vh"});
			//$("#showgif").css( {"height":"52vh"});
			document.getElementById(Preposition_2.correctSapnID).innerHTML=Preposition_2.correctWord;
			document.getElementById("showgif").src = Utils.path + "images/Q-box-open-1.gif";
			document.getElementById("audio").src = Utils.path + 'Sounds/instruction/welldone.mp3';
			document.getElementById("audio").play();
			document.getElementById("ShowOptions").style.pointerEvents="none";
			document.getElementById(Preposition_2.blankFiledId).style.color="green";
			clearTimeout(Preposition_2.rightAnswerTimer);
			Preposition_2.rightAnswerTimer=setTimeout(function(){
			document.getElementById("ShowCorrectWord").style.visibility = "visible";
			$("#ShowCorrectWord").addClass("animated fadeInDown");
			$("#bnext").addClass("blink_me");},2000);
			$("#"+Preposition_2.blankFiledId).css("text-decoration", "none");
		}
		else
		{
			if(Preposition_2.Attempt<2){
				//document.getElementById(Preposition_2.blankFiledId).innerHTML="";
				//$(destdiv).append(src);
				$("#optiondiv1").addClass("animated FlipInX");
				$("#optiondiv2").addClass("animated FlipInX");
				$("#optiondiv3").addClass("animated FlipInX");
				$("#optiondiv4").addClass("animated FlipInX");
				$("#"+Preposition_2.blankFiledId).css("text-decoration", "line-through");
				document.getElementById("ShowCorrectWord").style.visibility = "hidden";
				document.getElementById("audio").src = Utils.path + 'Sounds/instruction/Buzzer wrong 2.mp3';
				document.getElementById("audio").play();
				//src.animate(src.data("origPosition"),"slow");
				clearTimeout(wrongAnswer);
				wrongAnswer=setTimeout(function(){$("#optiondiv1").removeClass("animated FlipInX");
				$("#optiondiv2").removeClass("animated FlipInX");
				$("#optiondiv3").removeClass("animated FlipInX");
				$("#optiondiv4").removeClass("animated FlipInX");
				$("#"+Preposition_2.blankFiledId).css("text-decoration", "underline dotted black");
				document.getElementById("bnext").style.visibility = "visible";
				},1000);
			}
			else
			{
				$("#optiondiv1").addClass("animated FlipInX");
				$("#optiondiv2").addClass("animated FlipInX");
				$("#optiondiv3").addClass("animated FlipInX");
				$("#optiondiv4").addClass("animated FlipInX");
				document.getElementById("ShowOptions").style.pointerEvents="none";
				//document.getElementById(Preposition_2.blankFiledId).innerHTML="";
				$(destdiv).append(src);
				document.getElementById("audio").src = Utils.path + 'Sounds/instruction/Buzzer wrong 2.mp3';
				document.getElementById("audio").play();
				$("#"+Preposition_2.blankFiledId).css("text-decoration", "line-through");
				clearTimeout(Preposition_2.rightAnswerShowTimer);
				Preposition_2.rightAnswerShowTimer=setTimeout(function(){
					var a=document.getElementById(Preposition_2.blankFiledId).getAttribute("data");
					document.getElementById("ShowOptions").style.pointerEvents="none";
					document.getElementById(Preposition_2.blankFiledId).style.color="green";
					//$("#AllOptions").css( {"marginTop":"9vh"});
					$("#parentwrongWordInstruction").css( {"marginTop":"-35vh"});
					$("#FilltheBlank").css( {"marginTop":"-29vh"});
					//$("#showgif").css({"height":"52vh"});
					document.getElementById(Preposition_2.blankFiledId).innerHTML=a;
					document.getElementById("showgif").src = Utils.path + "images/Q-box-open-1.gif";
					Preposition_2.rightAnswerTimer=setTimeout(function(){
					document.getElementById("ShowCorrectWord").style.visibility = "visible";
					document.getElementById("bnext").style.visibility = "visible";
					$("#ShowCorrectWord").addClass("animated fadeInDown");
					$("#bnext").addClass("blink_me");},2000);
					$("#"+Preposition_2.blankFiledId).css("text-decoration", "none");
				},2400);
			}
		}
}
Preposition_2.callHome=function()
{
	location.reload();
}
Preposition_2.nextQue=function()
{
	clearTimeout(Preposition_2.rightAnswerShowTimer);
	clearTimeout(Preposition_2.rightAnswerTimer);
	document.getElementById("audio").pause();
	//$("#showgif").css( {"height":"42vh"});
	//$("#AllOptions").css( {"marginTop":"19vh"});
	$("#parentwrongWordInstruction").css( {"marginTop":"-25vh"});
	$("#FilltheBlank").css( {"marginTop":"-17vh"});
	$("#bnext").removeClass("blink_me");
	document.getElementById("ShowOptions").innerHTML=" ";
	document.getElementById("FillCorrectWord").innerHTML=" ";
	document.getElementById("ShowCorrectWord").innerHTML=" ";
	document.getElementById("showgif").src=' ';
	$("#ShowCorrectWord").css( {"visibility":"hidden"});
	document.getElementById("ShowOptions").style.pointerEvents="auto";
	document.getElementById("showgif").src = Utils.path + "images/Qbox.png";
	if(Preposition_2.countQue<4)
	{
		//var a=Preposition_2.selectRandomWord[Preposition_2.countQue].sentence.split(" ");
		Preposition_2.countQue++;
		Preposition_2.Attempt=0;
		Preposition_2.showQuestionOptions();
	}
	else{
		Preposition_2.Gameover();
	}
}
Preposition_2.Gameover = function () {
	document.getElementById("acknowledgementDiv").innerHTML = "";
	$("#coverpage,#gamepage,#acknowledgementDiv").css( {"display":"none"});
	document.getElementById("endpage").style.display = "block";
	document.getElementById("gameover").innerHTML = "GAME OVER";
	for (i = 0; i < Object.keys(Preposition_2.AcknowledgementPageData).length; i++) {
		div = document.createElement('div');
		div.className = " nopadding col-xs-12 col-sm-12 col-md-12 col-lg-12";
		div.innerHTML = Object.keys(Preposition_2.AcknowledgementPageData)[i] + " : " + Object.values(Preposition_2.AcknowledgementPageData)[i];
		acknowledgementDiv.appendChild(div);
	}
	div = document.createElement('div');
	div.className = " firstLetter col-xs-12 col-sm-12 col-md-12 col-lg-12";
	div.innerHTML = Preposition_2.hardcodedData['Created On'];
	acknowledgementDiv.appendChild(div);
}
Preposition_2.replay=function()
{
	Preposition_2.gamePage();
}
Preposition_2.showCredits = function () {
	$("#acknowledgementDiv").css( {"display":"block"});
}
