					//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.skipedQ=0;
ListenRead.seconds=0;							// 7 second timer
var questionId=0,startTime=0,scoredMarks=0,totalMarks=10,resId=0,correctCnt=0,levelNumber=1,noOfAttempts=0,queCnt=0;
var type="sentence",totalLength;
$(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").hide();
		ListenRead.setImages();
	},300);
});
ListenRead.setImages=function()
{
	$("#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");
	$("#teacherImg").prop('src',Utils.Path+"images/skin/standing_teacher.png");
	$("#recordImg").prop('src',Utils.Path+"images/skin/Record.png");
	$("#soundImg").prop('src',Utils.Path+"images/skin/Sound.png");
	$("#nextImg").prop('src',Utils.Path+"images/skin/Skip.png");
	$("#nextGame").prop('src',Utils.Path+"images/skin/Next.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,#characterDiv,#teacherImg,#tableDiv").show();
	shuffleArray(ListenRead.Data);
	totalLength=ListenRead.Data.length;
	ListenRead.questionArray=populate(ListenRead.Data,ListenRead.questionArray,5);
	ListenRead.createStars();
	ListenRead.showWordSentence();
}
ListenRead.createStars=function()
{
	var newImage,newDiv,i,numberOfStars=5;
	for(i=1;i<=numberOfStars;i++)
	{
		newDiv= document.createElement("div");
		newDiv.setAttribute("id","starDiv"+i); 
		newDiv.setAttribute("class","noPadding stars col-xs-2 col-md-2 col-lg-2 col-sm-2"); 

		newImage= document.createElement("img");
		newImage.setAttribute("id","starImg"+i); 
		newImage.setAttribute("class","img-responsive noPadding col-sm-12 col-lg-12 col-md-12 col-xs-12"); 
		newImage.setAttribute("src",Utils.Path+"images/skin/star1.png");
		$(newDiv).append(newImage);
		$("#starDiv").append(newDiv);
	}
	$("#starDiv").css("visibility","hidden");
}
ListenRead.showWordSentence=function()
{
	ListenRead.noOfTimeQuestionAnswered=0;
	ListenRead.skipFlag=true;
	$("#responseBoxDiv").hide();
	$("#widgetDiv").show();
	var d=new Date();
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	$("#recordImg").prop('src',Utils.Path+"images/skin/Record.png");
	$("#recordImg").removeClass("blink_me");
	document.getElementById("question").innerHTML=ListenRead.questionArray[queCnt].question;
	ListenRead.answer=ListenRead.questionArray[queCnt].question;
	ListenRead.playNextSound();
	questionId=parseInt(ListenRead.questionArray[queCnt].questionId);
}
ListenRead.playNextSound=function()
{
	var randomSound;
	ListenRead.seconds=0;
	if(ListenRead.soundTimer)
	{
		clearTimeout(ListenRead.soundTimer);
		ListenRead.soundTimer=null;
	}
	ListenRead.soundTimer=setTimeout(function()
	{
		$("#teacherImg").prop('src',Utils.Path+"images/skin/teacher.gif");
		randomSound=Math.floor(Math.random()*ListenRead.youReadSound.length);
		document.getElementById('playSound').src=Utils.Path+"sounds/instruction/"+ListenRead.youReadSound[randomSound];
		document.getElementById('playSound').play();
		ListenRead.startTimer();
	},300);
}
ListenRead.startTimer=function()
{
	if(ListenRead.noResponseTimer)
	{
		clearInterval(ListenRead.noResponseTimer);
		ListenRead.noResponseTimer=null;
		ListenRead.seconds=0;
	}	
	ListenRead.noResponseTimer=setInterval(function() 
	{
		ListenRead.seconds++;
		if(ListenRead.seconds>=7)
		{
			clearTimeout(ListenRead.soundTimer);
			ListenRead.soundTimer=null;
			document.getElementById("playSound").src=Utils.Path+"sounds/instruction/"+ListenRead.noResponce;
			document.getElementById("playSound").play();
			clearInterval(ListenRead.noResponseTimer);
			if(ListenRead.timeOut)
			{
				clearTimeout(ListenRead.timeOut);
				ListenRead.timeOut=null;
			}	
			ListenRead.timeOut=setTimeout(function()
			{
				ListenRead.seconds=0;
				ListenRead.startTimer();
			},2000);
		}
	},1000);
}
ListenRead.record=function()
{
	ListenRead.stopSound();
	clearInterval(ListenRead.noResponseTimer);
	ListenRead.ansString="";
	ListenRead.homeClickFlag=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
	{
		$("#soundImg").css("pointer-events","auto");
		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");
			ListenRead.seconds=0;
			ListenRead.startTimer();
		}
		clearInterval(ListenRead.checkStatus);
		$("#soundImg").css("pointer-events","auto");
		$("#recordImg").prop('src',Utils.Path+"images/skin/Record.png");
		$("#recordImg").removeClass("blink_me");
		ListenRead.seconds=0;
		ListenRead.startTimer();
	}
}
ListenRead.checkAnswer=function(str)
{
	ListenRead.noOfTimeQuestionAnswered++;
	ListenRead.stopSound();
	clearInterval(ListenRead.noResponseTimer);
	noOfAttempts++;
	ListenRead.skipFlag=false;
	$("#characterDiv").show();
	if(ListenRead.ansString=="true")
	{
		scoredMarks=10;
		$("#question").css("color","rgb(17, 245, 26)");
		correctCnt++;
		document.getElementById("playSound").src=Utils.Path+ListenRead.Sound[0];
		document.getElementById("playSound").play();
		document.getElementById("characterImg").src=Utils.Path+ListenRead.Image[0];
		console.log(ListenRead.answer,totalLength,type);
		if(Utils.mobileDeviceFlag)
			Android.addLearntData(ListenRead.answer,totalLength,type);
		
	}
	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];
	}
	ListenRead.calculateScore();
	ListenRead.questionArray.splice(queCnt,1);
	if(ListenRead.nextQueTimer)
	{
		clearTimeout(ListenRead.nextQueTimer);
		ListenRead.nextQueTimer=null;
	}
	ListenRead.nextQueTimer=setTimeout(function(){
		ListenRead.nextQuestion();
	},1500);
	setTimeout(function(){$("#characterDiv").hide();},1500);
}
ListenRead.nextQuestion=function()
{
	//ListenRead.calculateScore();
	clearTimeout(ListenRead.nextQueTimer);
	clearInterval(ListenRead.checkStatus);
	clearTimeout(ListenRead.timeOut);
	clearInterval(ListenRead.noResponseTimer);
	ListenRead.stopSound();
	if(ListenRead.skipFlag)
		queCnt++;
	ListenRead.skipedQ++;
	$("#question").css("color","white");
	$("#characterDiv").hide();
	if(ListenRead.questionArray.length==0)
		ListenRead.lastPage();
	else if(queCnt==ListenRead.questionArray.length)
	{
		$('#responseBoxDiv').show();
		//$('#gamepage,#widgetDiv').hide();
		//$("#soundDiv").css("visibility", "hidden");
		$('#questionMsg').text(ListenRead.modalText);
	}
	else
	{
		ListenRead.resetAll();
		ListenRead.showWordSentence();
	}
}
ListenRead.showNext=function(element)
{
	if($(element).text()=="No")
		ListenRead.lastPage();
	else
		ListenRead.showRemaining();
}
ListenRead.lastPage=function()
{
	clearTimeout(ListenRead.soundTimer);
	clearInterval(ListenRead.noResponseTimer);
	document.getElementById('playSound').pause();
	ListenRead.giveStars();
	ListenRead.questionArray=[];
	$("#endpage").show();
	$("#gamepage,#widgetDiv,#responseBoxDiv").hide();
	document.getElementById('message').innerHTML=ListenRead.hardcorded;
	console.log(correctCnt,ListenRead.skipedQ);
	if(Utils.mobileDeviceFlag)
		Android.StudentCertification(correctCnt,ListenRead.skipedQ,"8");
}
ListenRead.showRemaining=function()
{
	queCnt=0;
	$('#responseBoxDiv').hide();
	$('#gamepage').show();
	ListenRead.resetAll();
	ListenRead.showWordSentence();
}
ListenRead.giveStars=function()
{
	var i,numberOfStars=correctCnt;
	for(i=1;i<=numberOfStars;i++)
		document.getElementById("starImg"+i).setAttribute("src",Utils.Path+"images/skin/star2.png");
	if(numberOfStars!=0)
	{
		document.getElementById('playSound').src=Utils.Path+'sounds/instruction/Star'+numberOfStars+'.mp3';
		document.getElementById('playSound').play();
	}
	$("#starDiv").css("visibility","visible");
}
ListenRead.stopSound=function()
{
	clearTimeout(ListenRead.soundTimer);
	document.getElementById("playSound").pause();
}
ListenRead.resetAll=function()
{
	ListenRead.recordClickFlag=false;
	ListenRead.skipFlag=true;
	noOfAttempts=0;
	clearTimeout(ListenRead.soundTimer);
	clearTimeout(ListenRead.nextQueTimer);
	$("#recordImg").prop('src',Utils.Path+"images/skin/Record.png");
	$("#recordImg").removeClass("blink_me");
	$("#recordImg").css("pointer-events","auto");
	$("#soundImg").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.nextGame=function()
{
	if(Utils.mobileDeviceFlag)
		Android.GotoNextGame();
}