//september 2022
var VerbLed = {};
var Utils = {};
Utils.path = "";
var questionId = 0, startTime = 0, scoredMarks = 0, totalMarks = 9, resId = 0, label, scoreFlag = false;
var word="",totalNos=0,type="sentence";
var randNo;
var randLevel=0;
var textCnt=0;
 Utils.mobileDeviceFlag = false;
 var nextClickCnt=0;
 VerbLed.queArr=[];
 VerbLed.homeFlag=false;
 VerbLed.selectAnsCnt=0;
 VerbLed.dropCnt=0;
$(document).ready(function () {
	if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i))
		Utils.mobileDeviceFlag = true;
	/* if(Utils.mobileDeviceFlag)
	{
		Utils.path=Android.getMediaPath('makeRhymeWord');
	} */
	$(".se-pre-con").fadeOut("slow");
	setTimeout(function () {
		VerbLed.setImageSource();
		VerbLed.Init();
	}, 100);
});

VerbLed.setImageSource = function () {
	document.body.style.backgroundImage = Utils.path + "url('images/bg.png')";
	document.getElementById("home").src = Utils.path + "images/Home.png";
	document.getElementById("logo").src = Utils.path + "images/logo.png";
	document.getElementById("playbutton").src = Utils.path + "images/Play.png"; 
	document.getElementById("pharcesDiv").style.backgroundImage= Utils.path + "url('images/block.png')";
	document.getElementById("replatbtn").src = Utils.path + "images/replay.png";
	document.getElementById("creditbtn").src = Utils.path + "images/credits.png";
	document.getElementById("queChart").style.backgroundImage= Utils.path + "url('images/swimimgTank.png')";
	document.getElementById("next").src=Utils.path+"images/next.png"; 
	if(VerbLed.hardcodedData.languageFont!=null)
	setFontFamilyForLang(VerbLed.hardcodedData.languageFont);
}
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");
}

VerbLed.Init = function () {
	document.getElementById("home").style.display = "none";
	document.getElementById("gamePage").style.display = "none";
	document.getElementById("finalPage").style.display="none";
	document.getElementById("levelPage").style.display="none";
}
//Initialize variables  
VerbLed.initVariables = function ()
{
	VerbLed.rno=0;
	VerbLed.growingCnt=0;
	VerbLed.sampleArray=[];
	VerbLed.sampleQueArray=[];
}
VerbLed.playGame = function () {
	document.body.style.backgroundImage = Utils.path + "url('images/bg.png')";
	$("#coverPage").hide();
	VerbLed.createLevels();
	document.getElementById("coverPage").style.display="none";
	document.getElementById("levelPage").style.display="block";
	document.getElementById("home").style.display = "block";	//
	VerbLed.initVariables();
}
VerbLed.createLevels = function () {
	for(var i=0;i<2;i++)
	{
		var levelDiv = document.createElement("div");
		levelDiv.setAttribute("id","levelDiv"+i);
		levelDiv.setAttribute("class","levels col-sm-5 col-lg-5 col-md-5 col-xs-5 col-sm-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-offset-1");
		document.getElementById('levelCategory').appendChild(levelDiv);
		document.getElementById("levelDiv"+i).style.backgroundImage= Utils.path + "url('images/block.png')";
		document.getElementById("levelDiv"+i).style.backgroundSize="100% 100%"; 
		$(levelDiv).data("level",i);
		$(levelDiv).text(VerbLed.typeArray[i]);
		levelDiv.setAttribute("onclick","VerbLed.startGame(this.id)");
	}
}
VerbLed.startGame=function(Object)
{
	$("#gamePage").show();
	$("#levelPage").hide();
	VerbLed.type=$("#"+Object).data("level");
	shuffleArray(VerbLed.data);
	VerbLed.play();
}
VerbLed.play = function (){
	VerbLed.sampleQueArray=VerbLed.data[nextClickCnt].list.slice();
	VerbLed.sampleArray=VerbLed.data[nextClickCnt].list.slice();
	if(VerbLed.type==0)
	{
		$("#growingPage,#queDiv").show();
		$("#vanishingPage").hide();
		$("#phrases").text(VerbLed.gameData.phrases);
		document.getElementById("pharcesDiv").innerHTML="";//
		document.getElementById("queDiv").innerHTML="";
		$("#finalAnsDiv").hide();
		shuffleArray(VerbLed.sampleArray);
		VerbLed.createPhrasesDiv();
	}
	else{
		$("#growingPage").hide();
		$("#vanishingPage").show();
		document.getElementById("outerSentenceDiv").innerHTML="";
		VerbLed.createSpan();
	}
	VerbLed.showQue();
}
VerbLed.createPhrasesDiv = function ()
{
	for(var i=0;i<VerbLed.data[nextClickCnt].list.length;i++)
	{
		var optionDiv = document.createElement("Div");
		optionDiv.setAttribute("id","optionDiv"+i);
		optionDiv.setAttribute("class","nopadding text col-xs-10 col-sm-10 col-lg-10 col-md-10 col-sm-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-offset-1");
		document.getElementById('pharcesDiv').appendChild(optionDiv);

		var optionSpan = document.createElement("Span");
		optionSpan.setAttribute("id","optionSpan"+i);
		optionSpan.setAttribute("class","nopadding text drag");
		document.getElementById("optionDiv"+i).appendChild(optionSpan);
	}
	if(VerbLed.type==0)
	{
		for(var i=0;i<VerbLed.sampleArray.length;i++)
			$("#optionSpan"+i).text(" "+VerbLed.sampleArray[i].ans);
	}
}
VerbLed.createSpan = function ()//vanishing
{
	var colorArr=["orchid","burlywood","darkgoldenrod","darkorange","darksalmon"];
	for(var i=0;i<VerbLed.data[nextClickCnt].list.length;i++)
	{
		var spans = document.createElement("Span");
		spans.setAttribute("id","span"+i);
		spans.setAttribute("class","nopadding spanElement text ");
		document.getElementById('outerSentenceDiv').appendChild(spans);
		spans.setAttribute("onclick","VerbLed.selectSpan(this.id)");
		
		if(i==0)
		{
			$(spans).text(VerbLed.data[nextClickCnt].list[1].ans+" ");
			$(spans).data("queData",VerbLed.data[nextClickCnt].list[1].que +" ?");
		}
		else if(i==1)
		{
			$(spans).text(VerbLed.data[nextClickCnt].list[0].ans+" ");
			$(spans).data("queData",VerbLed.data[nextClickCnt].list[0].que +" ?");
		}
		else
		{
			$(spans).text(VerbLed.data[nextClickCnt].list[i].ans+" ");
			$(spans).data("queData",VerbLed.data[nextClickCnt].list[i].que +" ?");
		}
		$(spans).css('background-color',colorArr[i] );

	}
}
VerbLed.selectSpan=function(object)
{
	$(".spanElement").css({"color":"black","pointerEvents":"none"});
	$("#outerAnsDiv").text("");
	document.getElementById("playSound").src=Utils.path+'sounds/Instructions/ting.mp3';
	document.getElementById("playSound").play();
	$(".spanElement").removeClass("blink_me");
	if($("#outerQueDiv").text()==$("#"+object).data("queData"))
	{
		VerbLed.selectAnsCnt++;
		$("#"+object).css({"color":"blue"});
		$("#outerAnsDiv").text($("#"+object).text());
		VerbLed.sampleArray.splice(VerbLed.rno,1);
		if (VerbLed.nextQueTimer) {
			clearTimeout(VerbLed.nextQueTimer);
			VerbLed.nextQueTimer = null;
		}
		VerbLed.nextQueTimer = setTimeout(function () {
			
			setTimeout(function () {
				//$("#outerAnsDiv").removeClass("animated flash");
				$("#"+object).addClass("animated flash");
				setTimeout(function () {
					if(VerbLed.sampleArray.length==0)
					{
						$("#next").addClass("blink_me");
					}
					else
					{
					$("#outerAnsDiv").text("");
					$("#"+object).text("");
					shuffleArray(VerbLed.sampleArray);
					VerbLed.showQue();
					$(".spanElement").css({"color":"black"});
					}
				},1000);
			},2000);
			var rSound=Math.floor(Math.random()*VerbLed.rightSounds.length);
			document.getElementById("playSound").src=Utils.path+'sounds/Instructions/'+VerbLed.rightSounds[rSound];
				document.getElementById("playSound").play();	
		}, 1000);	
	}
	else
	{
		$(".spanElement").css({"pointerEvents":"auto"});
		$("#"+object).css({"color":"red"});
		$("#outerAnsDiv").text($("#"+object).text());
		if(VerbLed.selectAnsCnt==1)
			document.getElementById("playSound").src=Utils.path+'sounds/Instructions/One_more_try_please__Aditi.mp3';
		else
			document.getElementById("playSound").src=Utils.path+'sounds/Instructions/wrong_answer!_Aditi.mp3';
		document.getElementById("playSound").play();
		/* if(VerbLed.selectAnsCnt==2)
		{ */
			/* setTimeout(function () {
			$("#span"+VerbLed.rno).css({"color":"blue"});
		$("#outerAnsDiv").text($("#span"+VerbLed.rno).text());
		VerbLed.data[nextClickCnt].list.splice(VerbLed.rno,1);
			},1000);
			setTimeout(function () {
				$("#span"+VerbLed.rno).text("");
				$("#outerAnsDiv").text("");
				VerbLed.showQue();
			},4000); */
		}
	//}
}

VerbLed.populateQue = function ()
{
	VerbLed.rno=VerbLed.data[nextClickCnt].list.length;
	/* if(VerbLed.type==1)
	{
		for(var i=1;i<VerbLed.data[nextClickCnt].list.length;i++)
		{
			VerbLed.queArr.push(VerbLed.data[nextClickCnt].list[i]);
		}
	} */
}

VerbLed.showQue=function()
{
	/* var queArraylist=["Action","Who","What","Where","When"];//what optional
	if(VerbLed.data[nextClickCnt].list.length==4)
		queArraylist.splice(2,1);
	console.log(queArraylist); */
	if(VerbLed.type==0)
	{
	//	shuffleArray(VerbLed.data[nextClickCnt].list);
		//VerbLed.dragdrop();
		
		$("#queDiv,#ansDiv").show();
		VerbLed.dropCnt=0;
		var span = document.createElement("Div");
		span.setAttribute("id","Qspan"+ VerbLed.growingCnt);
		span.setAttribute("class","nopadding spanElement text col-sm-2 col-lg-2 col-md-2 col-xs-2");
		/* if(VerbLed.growingCnt==1)
			document.getElementById('queDiv').prepend(span);
		else */
			document.getElementById('queDiv').appendChild(span);
		$("#Qspan"+VerbLed.growingCnt).text(" "+VerbLed.sampleQueArray[VerbLed.growingCnt].que);
		VerbLed.dragdrop();
	}
	else
	{
		$(".spanElement").css({"pointerEvents":"auto"});
		if(VerbLed.sampleQueArray.length==VerbLed.sampleArray.length)
		{
			$("#outerQueDiv").text(VerbLed.sampleQueArray[VerbLed.rno].que +" ?");
		}
		else
		{
			VerbLed.rno=Math.floor(Math.random()*VerbLed.sampleArray.length);
			$("#outerQueDiv").text(VerbLed.sampleArray[VerbLed.rno].que +" ?");
		}
	}
}
VerbLed.nextQue=function() 	//next button		
{
	document.getElementById("finalAnsDiv").innerHTML="";
	document.getElementById("ansDiv").innerHTML="";
	VerbLed.initVariables();
	VerbLed.selectAnsCnt=0;
	$("#next").removeClass("blink_me");
	if(!VerbLed.ansClicked)
		scoredMarks=-1;
	VerbLed.scoreFunction();
	nextClickCnt++;
	if(VerbLed.type==0)
	{
		VerbLed.dropCnt=0;
	}
	else
	{
		$("#outerAnsDiv").text("");
		$(".sentenceDiv").css({'pointer-events':'auto'});
	}
	if(nextClickCnt==5)
	{
		document.getElementById("finalPage").style.display = "block";
		document.getElementById("gamePage").style.display = "none";
		VerbLed.gameOver();
	}
	else
	{
		VerbLed.play();
	}

}

VerbLed.returnback=function()
{
	location.reload();
}
function shuffleArray(array)
{
	var i,j,temp;
	for (i = array.length - 1; i > 0; i--) 
	{
	    j = Math.floor(Math.random() * (i + 1));
	    temp = array[i];
	    array[i] = array[j];
	    array[j] = temp;
	}
	return array;
}

VerbLed.scoreFunction=function()		//score Function
{
	console.log(resId,questionId,scoredMarks,totalMarks,randLevel+1,startTime,label);
	if(Utils.mobileDeviceFlag)
	{
		Android.addScore(resId,questionId,scoredMarks,totalMarks,randLevel+1,startTime,label);
	}
}
VerbLed.gameOver = function () {
	document.getElementById("playSound").pause(); 
	clearInterval(VerbLed.noResponseTimer);
	document.getElementById("gamePage").style.display = "none";
	document.getElementById("finalPage").style.display="block";
	document.getElementById("responseBoxDiv").style.display="none";
	document.getElementById("acknowledgementDiv").innerHTML = "";
	document.getElementById('gameOver').innerHTML=VerbLed.hardcodedData.message2;
	document.getElementById("playSound").pause(); 
	for(i=0;i<Object.keys(VerbLed.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(VerbLed.AcknowledgementPageData)[i]+" : "+Object.values(VerbLed.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=VerbLed.hardcodedData['createdOn'];
	acknowledgementDiv.appendChild(div);
	document.getElementById("acknowledgementDiv").style.display="none";
}
VerbLed.showCredits=function()			//show credits on click credit button
{
	document.getElementById("acknowledgementDiv").style.display="block";
}
/* VerbLed.playAgain=function()
{
	VerbLed.play();
	$("#finalPage").hide();
	//$("#gamePage").show();
	document.getElementById("gamePage").style.display="block";
} */
function getDuration(src) {
	var duration = 0;
	//if (ShowMe.soundFlag) {
	$('#audio').on("loadedmetadata", function () {
		duration = $('#audio').prop('duration');
		$('#audio').unbind("loadedmetadata");
		VerbLed.playNextSound(duration);
	});
	$('#audio').prop('src', src);
}
VerbLed.playNextSound = function (duration)				//function for instruction
{
	document.getElementById('playSound').play();
	if (VerbLed.instructionTimer) {
		clearTimeout(VerbLed.instructionTimer);
		VerbLed.instructionTimer = null;
	}
	VerbLed.instructionTimer = setTimeout(function () {
	}, duration * 1000);
}
VerbLed.playInstruction = function () {						//play instruction
	if(VerbLed.flag)
		document.getElementById('playSound').src=Utils.path+'sounds/Instructions/'+ VerbLed.hardcodedData.instruction;
	else
		document.getElementById('playSound').src=Utils.path+'sounds/Instructions/'+ VerbLed.hardcodedData.instruction2;
	document.getElementById('playSound').play();
}

VerbLed.playAgain=function()		//credits
{
	VerbLed.initVariables();
	VerbLed.play(); 
	nextClickCnt=0;
	document.getElementById("finalPage").style.display="none";
	document.getElementById("gamePage").style.display="block";
}

/****************************************************************************************
 * **************************************************************************************
*/
VerbLed.dragdrop = function() {
    $(".drag").draggable({
		cursor: 'move',
		revert: 'invalid',
		helper: 'clone',
        create: function(event, ui) {},
      //  axis: "x",
        containment: "#growingPage",
        drag: function(event, ui) {
			
        }
    });
    $(".drop").droppable({
        drop: function(event, ui) {
			
			var id=$(this).attr('id');
			var text=$(this).data("ansData")
			VerbLed.dropCnt++;
			$(this).append(ui.draggable.clone());
			document.getElementById('playSound').pause();
			if(" "+VerbLed.sampleQueArray[VerbLed.growingCnt].ans==$(ui.draggable).text())
			{
				$(ui.draggable).draggable("option", "revert", false);
				var rSound=Math.floor(Math.random()*VerbLed.rightSounds.length);
				document.getElementById("playSound").src=Utils.path+'sounds/Instructions/'+VerbLed.rightSounds[rSound];
				document.getElementById("playSound").play();
				//$(this).droppable("disable");
				VerbLed.growingCnt++;
				if(VerbLed.growingCnt<VerbLed.data[nextClickCnt].list.length)
					VerbLed.showQue();
				else
				{
					VerbLed.growingCnt=0;
					setTimeout(function () {
						$("#queDiv,#ansDiv").hide();
						$("#finalAnsDiv").show();
						$("#finalAnsDiv").text(VerbLed.data[nextClickCnt].completeSentence);
						$("#next").addClass("blink_me");
					},2000);
				}
			}
			else
			{
				document.getElementById('playSound').src = Utils.path + 'sounds/Instructions/Try_Again!_Aditi.MP3';
				document.getElementById('playSound').play();
				setTimeout(function () {
					var deleteElm=$("#ansDiv").children().last().prop('id');
					var elem=document.getElementById(deleteElm);
					elem.parentElement.removeChild(elem);
					var elem=document.getElementById(deleteElm);
					elem.parentElement.removeChild(elem);
					document.getElementById("pharcesDiv").innerHTML="";
					VerbLed.createPhrasesDiv();
					VerbLed.dragdrop();
				},1500);
				$("#pharcesDiv").css("pointerEvents","none")
				setTimeout(function () {
					$("#pharcesDiv").css("pointerEvents","auto");
					var span = document.createElement("Span");
				span.setAttribute("id","ansSpan"+ VerbLed.growingCnt);
				span.setAttribute("class","nopadding ansSpan");
				document.getElementById('answerDiv').appendChild(span);
				$("#ansSpan"+ VerbLed.growingCnt).text(" "+VerbLed.data[nextClickCnt].list[VerbLed.growingCnt].ans);
				VerbLed.growingCnt++;
				if(VerbLed.growingCnt<VerbLed.data[nextClickCnt].list.length)
					VerbLed.showQue();
				},1000);
			}
			/* if(VerbLed.dropCnt==2)
			{
				$("#pharcesDiv").css("pointerEvents","none")
				setTimeout(function () {
					$("#pharcesDiv").css("pointerEvents","auto");
					var span = document.createElement("Span");
				span.setAttribute("id","ansSpan"+ VerbLed.growingCnt);
				span.setAttribute("class","nopadding ansSpan");
				document.getElementById('answerDiv').appendChild(span);
				$("#ansSpan"+ VerbLed.growingCnt).text(" "+VerbLed.data[nextClickCnt].list[VerbLed.growingCnt].ans);
				//	$("#"+id).droppable("disable");
				VerbLed.growingCnt++;
				if(VerbLed.growingCnt<VerbLed.data[nextClickCnt].list.length)
					VerbLed.showQue();
				},1000);
			} */
			textCnt=0;
			for(var i=0;i<VerbLed.data[nextClickCnt].list.length;i++)
			{
				if($("#ansDiv"+i).text()!="")
					textCnt++;
			}
			if(textCnt==5)
			{
				setTimeout(function () {
					$("#finalAnsDiv").show();
					$("#queDiv").hide();
					$("#finalAnsDiv").text(VerbLed.data[nextClickCnt].completeSentence);
					$("#next").addClass("blink_me");
				},3000);
			}
        }
    });
}