					//21th Oct 2018
ListenRead={};
var Utils={};
Utils.Path='';
Utils.mobileDeviceFlag=false;
ListenRead.recordClickFlag=false;
ListenRead.questionArray=[];					//current question array
ListenRead.ansString="";						//recorded answer
ListenRead.noOfTimeQuestionAnswered=0;
ListenRead.skipFlag=true;
ListenRead.soundFlag=true;
ListenRead.wrongAnsFlag=false;
ListenRead.homeClickFlag=false;
ListenRead.skipedQ=0;
ListenRead.youReadFlag = true;
var index = 0; 					// next level counter
//ListenRead.levelName="Learn";						
var type="sentence",totalLength=0;
var questionId=0,startTime=0,scoredMarks=0,totalMarks=10,resId=0,levelNumber=1,noOfAttempts=0;
$(document).ready(function()
{
	$(".se-pre-con").fadeOut("slow");
	if ((navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) && !(window.location.href.includes('http')))
		Utils.mobileDeviceFlag=true;
	/* if(Utils.mobileDeviceFlag)
		Utils.Path=Android.getMediaPath(""); */
	setTimeout(function()
	{
		document.body.style.backgroundImage="url("+Utils.Path+ListenRead.coverImage+")";
		$("#coverpage").show();
		$("#gamepage,#endpage,#widgetDiv,#homeImg").hide();
		ListenRead.setImages();
	},300);
});
ListenRead.setImages=function()
{
	$("#homeImg").prop('src',Utils.Path+"images/skin/Home.png");
	$("#logoImg").prop('src',Utils.Path+"images/skin/logo.png");
	$("#playImg").prop('src',Utils.Path+"images/skin/Play.png");
	$("#bordImg").prop('src',Utils.Path+"images/skin/bord.png");
	$("#credit").prop('src',Utils.Path+"images/skin/credits.png");
	$("#teacherImg").prop('src',Utils.Path+"images/skin/standing_teacher.png");
	$("#replayImage").prop('src',Utils.Path+"images/skin/Replay.png");
	$("#soundImg").prop('src',Utils.Path+"images/skin/Sound.png");
	$("#nextImg").prop('src',Utils.Path+"images/skin/Next.png");
	$("#recordImg").prop('src',Utils.Path+"images/skin/Record.png");
}
ListenRead.playGame=function()
{
	document.body.style.backgroundImage="url("+Utils.Path+"images/skin/BG.png)";
	document.getElementById("tableDiv").style.backgroundImage="url("+Utils.Path+"images/skin/table.png)";
	$("#coverpage").hide();
	$("#gamepage,#questionDiv,#widgetDiv,#homeImg,#characterDiv,#teacherImg,#tableDiv").show();
	//shuffleArray(ListenRead.Data);
	for (let i = 0; i < ListenRead.Data.length; i++) 
		totalLength+=ListenRead.Data[i].data.length;
	
	console.log(totalLength);
	if(Utils.mobileDeviceFlag)
		ListenRead.levelName=Android.isPractice();			//true - PRACTICE   false - LEARN
	ListenRead.showWordSentence();
}
ListenRead.showWordSentence=function()
{
	ListenRead.noOfTimeQuestionAnswered=0;
	document.getElementById("message").style.visibility="hidden";
	document.getElementById("acknowledgementDiv").innerHTML="";
	ListenRead.skipFlag=true;
	var d=new Date();
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	ListenRead.random=Math.floor(Math.random()*ListenRead.Data[index].data.length);
	while($.inArray(ListenRead.random,ListenRead.questionArray)!=-1)
	ListenRead.random=Math.floor(Math.random()*ListenRead.Data[index].data.length);
	ListenRead.questionArray.push(ListenRead.random);
	document.getElementById("question").innerHTML=ListenRead.Data[index].data[ListenRead.random].question;
	ListenRead.answer=ListenRead.Data[index].data[ListenRead.random].question;
	ListenRead.playSound();
	questionId=parseInt(ListenRead.Data[index].data[ListenRead.random].questionId);
}
ListenRead.playSound=function()
{
	ListenRead.soundFlag=true;
	$("#teacherImg").prop('src',Utils.Path+"images/skin/standing_teacher.png");
	if(ListenRead.levelName)
	{
		$("#teacherImg").prop('src',Utils.Path+"images/skin/teacher.gif");
		document.getElementById('playSound').pause();
		document.getElementById('playSound').src=Utils.Path+"sounds/instruction/"+ListenRead.pleaseRead;
		document.getElementById('playSound').play();
	}
	else
		getDuration(Utils.Path+"sounds/"+ListenRead.Data[index].data[ListenRead.random].sound);
}
function getDuration(src)
{
	var duration=0;
	if(ListenRead.soundFlag)
	{
		$('#playSound').on("loadedmetadata", function()
		{
			ListenRead.soundFlag=false;
			duration=$('#playSound').prop('duration');
			$('#playSound').unbind("loadedmetadata");
			ListenRead.playNextSound(duration);
		});
		$('#playSound').prop('src',src);
	}
}
ListenRead.playNextSound=function(duration)
{
	document.getElementById('playSound').play();
	if(ListenRead.youReadFlag)
	{
		ListenRead.youReadFlag=false;
		if(ListenRead.soundTimer)
		{
			clearTimeout(ListenRead.soundTimer);
			ListenRead.soundTimer=null;
		}
		ListenRead.soundTimer=setTimeout(function()
		{
			$("#teacherImg").prop('src',Utils.Path+"images/skin/teacher.gif");
			document.getElementById('playSound').pause();
			document.getElementById('playSound').src=Utils.Path+"sounds/instruction/"+ListenRead.youReadSound;
			document.getElementById('playSound').play();
		},duration*1200);
	}
}
ListenRead.record=function()
{
	ListenRead.stopSound();
	ListenRead.ansString="";
	ListenRead.homeClickFlag=true;
	ListenRead.skipFlag=false;
	ListenRead.wrongAnsFlag=true;
	clearTimeout(ListenRead.soundTimer);
	if(ListenRead.recordClickFlag==false)
	{
		ListenRead.recordClickFlag=true;
		if(Utils.mobileDeviceFlag)
			Android.startStt("eng",ListenRead.answer);
		$("#soundImg").css("pointer-events","none");
		$("#recordImg").prop('src',Utils.Path+"images/skin/Stop.png");
		$("#recordImg").addClass("blink_me");
		if(ListenRead.checkStatus)
		{
            clearInterval(ListenRead.checkStatus);
            ListenRead.checkStatus = null;
        }
		ListenRead.checkStatus = setInterval(function() 
		{
			if (Utils.mobileDeviceFlag)
				ListenRead.ansString = Android.getSttResult();
			console.log(ListenRead.ansString);
			if('$$$'!== ListenRead.ansString)
			{
				clearInterval(ListenRead.checkStatus);
				if(ListenRead.ansString !== "###" && ListenRead.ansString !== "")
					 ListenRead.checkAnswer();
				else
				{
					ListenRead.ansString="";
					console.log('Stops');
					$("#soundImg").css("pointer-events","auto");
				}
				ListenRead.recordClickFlag = false;
				$("#recordImg").prop('src',Utils.Path+"images/skin/Record.png");
				$('#recordImg').removeClass('blink_me');	
			}	
		},500);
	}
	else
	{
		ListenRead.recordClickFlag = false;
		if(Utils.mobileDeviceFlag)
			Android.stopStt();
		//ListenRead.ansString="";
		if(ListenRead.ansString !== "###" && ListenRead.ansString !== "")
					 ListenRead.checkAnswer();
		else
		{
			ListenRead.ansString="";
			console.log('Stops');
			$("#soundImg").css("pointer-events","auto");
		}
		clearInterval(ListenRead.checkStatus);
		$("#soundImg").css("pointer-events","auto");
		$("#recordImg").prop('src',Utils.Path+"images/skin/Record.png");
		$("#recordImg").removeClass("blink_me");
	}
}
ListenRead.checkAnswer=function()
{
	ListenRead.noOfTimeQuestionAnswered++;
	ListenRead.stopSound();
	$("#soundImg").css("pointer-events","none");
	noOfAttempts++;
	ListenRead.skipFlag=false;
	ListenRead.homeClickFlag=false;
	$("#characterDiv").show();
	if(ListenRead.ansString=="true")
	{
		scoredMarks=10;
		$("#question").css("color","rgb(17, 245, 26)");
		document.getElementById("playSound").src=Utils.Path+ListenRead.Sound[0];
		document.getElementById("playSound").play();
		document.getElementById("characterImg").src=Utils.Path+ListenRead.Image[0];
		ListenRead.calculateScore();
		console.log(ListenRead.Data[index].data[ListenRead.random].question,totalLength,type);
		if(Utils.mobileDeviceFlag)
			Android.addLearntData(ListenRead.Data[index].data[ListenRead.random].question,totalLength,type);
		if(ListenRead.nextQueTimer)
		{
			clearTimeout(ListenRead.nextQueTimer);
			ListenRead.nextQueTimer=null;
		}
		ListenRead.nextQueTimer=setTimeout(function(){
			ListenRead.nextQuestion();
		},1500);
	}
	else
	{
		scoredMarks=0;
		ListenRead.wrongAnsFlag=true;
		document.getElementById("playSound").src=Utils.Path+ListenRead.Sound[1];
		document.getElementById("playSound").play();
		document.getElementById("characterImg").src=Utils.Path+ListenRead.Image[1];
	}
	setTimeout(function(){$("#characterDiv").hide();},1500);
	$("#soundImg").css("pointer-events","auto");
}

ListenRead.nextQuestion=function()
{
	clearInterval(ListenRead.checkStatus);
	clearTimeout(ListenRead.nextQueTimer);
	clearTimeout(ListenRead.soundTimer);
	ListenRead.homeClickFlag=false;
	ListenRead.stopSound();
	ListenRead.skipedQ++;
	$("#question").css("color","white");
	if(ListenRead.wrongAnsFlag)
	{
		scoredMarks=0;
		ListenRead.wrongAnsFlag=false;
		ListenRead.calculateScore();
	}
	if(ListenRead.skipFlag)
	{
		scoredMarks=-1;
		ListenRead.calculateScore();
	}
	$("#characterDiv").hide();
	
	if(ListenRead.skipedQ==15)
	{
		clearTimeout(ListenRead.soundTimer);
		ListenRead.questionArray=[];
		document.getElementById("message").style.visibility="visible";
		$("#endpage").show();
		$("#gamepage,#widgetDiv,#acknowledgementDiv").hide();
		document.getElementById('message').innerHTML=ListenRead.hardcoded['gameOver'];
		ListenRead.createHardcodeddivs();
	}
	else
	{
		ListenRead.resetAll();
		if (ListenRead.skipedQ == 5 || ListenRead.skipedQ == 10 || ListenRead.skipedQ == 15)
			index++;
		ListenRead.showWordSentence();
	}
}
ListenRead.createHardcodeddivs=function()
{
	var ackDiv;
	for(var i=0;i<Object.keys(ListenRead.acknowledgementData).length;i++)
	{
		ackDiv= document.createElement("div");
		ackDiv.setAttribute("id","ack"+i); 
		ackDiv.setAttribute("class","noPadding firstLetter text-center fontClass col-sm-12 col-lg-12 col-md-12 col-xs-12"); 
		ackDiv.innerHTML=Object.keys(ListenRead.acknowledgementData)[i]+" : "+Object.values(ListenRead.acknowledgementData)[i];
		document.getElementById("acknowledgementDiv").appendChild(ackDiv);
	}
	div=document.createElement('div');
	div.className="noPadding fontClass firstLetter text-center col-xs-12 col-sm-12 col-md-12 col-lg-12";
	div.innerHTML=ListenRead.hardcoded['createdOn'];
	document.getElementById("acknowledgementDiv").appendChild(div);
}

ListenRead.homeClick=function()
{
	if(ListenRead.wrongAnsFlag)
	{
		scoredMarks=0;
		ListenRead.wrongAnsFlag=false;
		ListenRead.calculateScore();
	}
	else if(ListenRead.homeClickFlag)
	{
		scoredMarks=0;
		ListenRead.calculateScore();
	}
	ListenRead.stopSound();
	location.reload(true);
}
ListenRead.stopSound=function()
{
	clearTimeout(ListenRead.soundTimer);
	document.getElementById("playSound").pause();
}
ListenRead.playAgain=function()
{
	ListenRead.resetAll();
	$("#endpage").hide();
	$("#gamepage,#widgetDiv,#message").show();
	ListenRead.skipedQ = 0;
	index=0
	ListenRead.questionArray=[];
	ListenRead.showWordSentence();
}
ListenRead.resetAll=function()
{
	ListenRead.recordClickFlag=false;
	ListenRead.skipFlag=true;
	ListenRead.youReadFlag=true;
	clearTimeout(ListenRead.soundTimer);
	clearTimeout(ListenRead.nextQueTimer);
	noOfAttempts=0;
	$("#recordImg").prop('src',Utils.Path+"images/skin/Record.png");
	$("#recordImg").removeClass("blink_me");
	$("#soundImg").css("pointer-events","auto");
	$("#recordImg").css("pointer-events","auto");
}
ListenRead.calculateScore=function()
{
	ListenRead.wrongAnsFlag=false;
	console.log(resId,questionId,scoredMarks,noOfAttempts,levelNumber,startTime,ListenRead.answer);
		if(Utils.mobileDeviceFlag)
			Android.addScore(resId,questionId,scoredMarks,noOfAttempts,levelNumber,startTime,ListenRead.answer);
}
ListenRead.showCredit=function()
{
	$("#acknowledgementDiv").show();
}