					//21th Oct 2018
ListenRead={};
var Utils={};
Utils.Path='';
Utils.mobileDeviceFlag=false;
ListenRead.recordClickFlag=false;
ListenRead.soundRecorded=false;
ListenRead.questionArray=[];
ListenRead.ansString="";
ListenRead.noOfTimeQuestionAnswered=0;
ListenRead.counter=0;
ListenRead.skipFlag=true;
ListenRead.soundFlag=true;
ListenRead.attemptFlag=false;
ListenRead.skipedQ=0;
ListenRead.homeClickFlag=false;
ListenRead.queFlag=true;
//ListenRead.internetAvailableFlag=false;
var questionId=0,startTime=0,scoredMarks=0,totalMarks=10,resId=0,levelNumber=2;
var noOfAttempts=0;
$(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(""); */
	setTimeout(function()
	{
		document.body.style.backgroundImage="url("+Utils.Path+ListenRead.coverImage+")";
		document.getElementById("coverpage").style.display="block";
		document.getElementById("gamepage").style.display="none";
		document.getElementById("endpage").style.display="none";
		document.getElementById("widgetDiv1").style.display="none";
		document.getElementById("homeImg").style.display="none";
		document.getElementById("tableDiv2").style.display="none";
		document.getElementById("listenDiv2").style.visibility = "hidden";
		ListenRead.setImages();
	},300);
});
ListenRead.setImages=function()
{
	document.getElementById("homeImg").src=Utils.Path+"images/Home.png";
	document.getElementById("logoImg").src=Utils.Path+"images/logo.png";
	document.getElementById("playImg").src=Utils.Path+"images/Play.png";
	document.getElementById("listenImg2").src=Utils.Path+"images/Listen.png";
	document.getElementById("rightImg2").src=Utils.Path+"images/right.png";
	document.getElementById("wrongImg2").src=Utils.Path+"images/wrong.png";
	document.getElementById("bordImg").src=Utils.Path+"images/bord.png";
	document.getElementById("credit").src=Utils.Path+"images/credits.png";
	$(".teacherClass").prop('src',Utils.Path+"images/standing_teacher.png");
	document.getElementById("replayImage").src=Utils.Path+"images/Replay.png";
	$(".soundClass").prop('src',Utils.Path+"images/Sound.png");
	$(".nextClass").prop('src',Utils.Path+"images/Next.png");
	$(".recordClass").prop('src',Utils.Path+"images/Record.png");
}
ListenRead.playGame=function()
{
	//ListenRead.internetAvailableFlag=Math.floor(Math.random()*2);
	ListenRead.internetAvailableFlag=true;
	document.body.style.backgroundImage="url("+Utils.Path+"images/BG.png)";
	document.getElementById("tableDiv1").style.backgroundImage="url("+Utils.Path+"images/table.png)";
	document.getElementById("tableDiv2").style.backgroundImage="url("+Utils.Path+"images/table.png)";
	document.getElementById("coverpage").style.display="none";
	document.getElementById("gamepage").style.display="block";
	document.getElementById("questionDiv").style.display="block";
	document.getElementById("widgetDiv1").style.display="block";
	document.getElementById("homeImg").style.display="block";
	document.getElementById("characterDiv").style.display="block";
	$(".teacherClass").css('display','block');
	if(ListenRead.internetAvailableFlag)
	{
		document.getElementById("widgetDiv1").style.display="block";
		document.getElementById("tableDiv1").style.display="block";
	}
	else
	{
		document.getElementById("widgetDiv2").style.display="block";
		document.getElementById("tableDiv2").style.display="block";
	}
	ListenRead.showWordSentence();
}
ListenRead.showWordSentence=function()
{
	ListenRead.noOfTimeQuestionAnswered=0;
	ListenRead.recordingArray=[];
	document.getElementById("message").style.visibility="hidden";
	document.getElementById("acknowledgementDiv").innerHTML="";
	clearTimeout(ListenRead.nextQueTimer);
	ListenRead.skipFlag=true;
	document.getElementById("listenDiv2").style.visibility = "hidden";
	$(".widgetClass").css("pointer-events","none");
	$(".widgetClass").css("visibility","hidden");
	ListenRead.counter=0;
	shuffleArray(ListenRead.Data);
	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.length);
	while($.inArray(ListenRead.random,ListenRead.questionArray)!=-1)
		ListenRead.random=Math.floor(Math.random()*ListenRead.Data.length);
	ListenRead.questionArray.push(ListenRead.random);
	document.getElementById("question").innerHTML=ListenRead.Data[ListenRead.random].question;
	document.getElementById("answer").innerHTML=ListenRead.Data[ListenRead.random].answer;
	ListenRead.answer=ListenRead.Data[ListenRead.random].question+" "+ListenRead.Data[ListenRead.random].answer;
	ListenRead.playSound();
	questionId=parseInt(ListenRead.Data[ListenRead.random].questionId);
}
ListenRead.playSound=function()
{
	ListenRead.soundFlag=true;
	ListenRead.queFlag=true;
	$(".teacherClass").prop('src',Utils.Path+"images/standing_teacher.png");
	getDuration(Utils.Path+"sounds/"+ListenRead.Data[ListenRead.random].questionSound);
}
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.soundTimer)
	{
		clearTimeout(ListenRead.soundTimer);
		ListenRead.soundTimer=null;
	}
	ListenRead.soundTimer=setTimeout(function()
	{
		if(ListenRead.queFlag==true)
		{
			ListenRead.queFlag=false;
			ListenRead.soundFlag=true;
			getDuration(Utils.Path+"sounds/"+ListenRead.Data[ListenRead.random].answerSound);
		}
		else
		{
			ListenRead.queFlag=true;
			$(".teacherClass").prop('src',Utils.Path+"images/teacher.gif");
			document.getElementById('playSound').src=Utils.Path+"sounds/"+ListenRead.youReadSound;
			document.getElementById('playSound').play();
		}
	},duration*800);
}
ListenRead.record=function()
{
	ListenRead.stopSound();
	ListenRead.ansString="";
	ListenRead.homeClickFlag=true;
	clearTimeout(ListenRead.youReadTimer);
	clearTimeout(ListenRead.soundTimer);
	if(ListenRead.recordClickFlag==false)
	{
		ListenRead.recordClickFlag=true;
		if(Utils.mobileDeviceFlag)
			Android.startStt("eng",ListenRead.answer);
		$(".soundClass").css("pointer-events","none");
		document.getElementById("recordImg1").src=Utils.Path+"images/Stop.png";
		$(".recordClass").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);
				//ListenRead.recordClickFlag = false;
				if(ListenRead.ansString !== "###" && ListenRead.ansString !== "")
					 ListenRead.checkAnswer();
				else
				{
					ListenRead.ansString="";
					console.log('Stops');
					$(".soundClass").css("pointer-events","auto");
				}
				ListenRead.recordClickFlag = false;
				document.getElementById('recordImg1').src =Utils.Path+'images/Record.png';
				$('.recordClass').removeClass('blink_me');	
			}	
			
		},500);
	}
	else
	{
		ListenRead.recordClickFlag = false;
		if(Utils.mobileDeviceFlag)
			Android.stopStt();
		ListenRead.ansString="";
		clearInterval(ListenRead.checkStatus);
		$(".soundClass").css("pointer-events","auto");
		document.getElementById("recordImg1").src=Utils.Path+"images/Record.png";
		$(".recordClass").removeClass("blink_me");
		/* setTimeout(function(){
			if (Utils.mobileDeviceFlag)
				ListenRead.ansString = Android.getSttResult();
			if('$$$'!== ListenRead.ansString)
			{
			   if(ListenRead.ansString !== "###" && ListenRead.ansString !== "")
					ListenRead.checkAnswer(ListenRead.ansString);
			}
		},1500); */
	}
}
ListenRead.recordOffline=function()
{
	var recodingpiece=0,index=0;
	scoredMarks=10;
	ListenRead.homeClickFlag=true;
	clearTimeout(ListenRead.youReadTimer);
	clearTimeout(ListenRead.soundTimer);
	ListenRead.noOfTimeQuestionAnswered++;
	document.getElementById('playSound').pause();
	ListenRead.stopSound();
	clearTimeout(ListenRead.soundTimer);
	$(".widgetClass").css("pointer-events","none");
	if(ListenRead.recordClickFlag==false)
	{
		document.getElementById('playSound').pause();
		document.getElementById("listenDiv2").style.visibility = "hidden";
		$(".soundClass").css("pointer-events","none");
		$("#listenDiv2").css("pointer-events","none");
		$(".widgetClass").css("visibility","hidden");
		ListenRead.recordClickFlag=true;
		ListenRead.soundRecorded=true;
		document.getElementById("recordImg2").src=Utils.Path+"images/Stop.png";
		$("#recordImg2").addClass("blink_me");
		
		recodingpiece='myRecording_'+ListenRead.counter+'.mp3';
		if(Utils.mobileDeviceFlag)
			Android.startRecording(recodingpiece);
		if($.inArray(recodingpiece,ListenRead.recordingArray)>=0)
		{
			index=ListenRead.recordingArray.indexOf(recodingpiece);
			ListenRead.recordingArray.splice(index,1);
			ListenRead.recordingArray.push(recodingpiece);
		}
		else
			ListenRead.recordingArray.push(recodingpiece);
	}
	else 
	{
		if(Utils.mobileDeviceFlag)
			Android.stopRecording();
		document.getElementById('playSound').pause();
		ListenRead.recordClickFlag=false;
		$(".soundClass").css("pointer-events","auto");
		$("#listenDiv2").css("pointer-events","auto");
		document.getElementById("listenDiv2").style.visibility = "visible";
		document.getElementById("recordImg2").src=Utils.Path+"images/Record.png";
		$(".recordClass").removeClass("blink_me");
	}
}
ListenRead.listenSound=function()
{
	var duration;
	ListenRead.stopSound();
	if(ListenRead.soundRecorded==true)
	{
		ListenRead.soundRecorded=false;
		document.getElementById('listenImg2').src=Utils.Path+"images/Stop.png";
		$('#listenImg2').addClass("blink_me");
		$(".recordClass").css("pointer-events","none");
		$(".soundClass").css("pointer-events","none");
		$(".widgetClass").css("visibility","hidden");
		document.getElementById('playSound').pause();
		if(ListenRead.timer)
		{
			clearTimeout(ListenRead.timer);
			ListenRead.timer=null;
		}
		ListenRead.timer=setTimeout(function(){
			if(Utils.mobileDeviceFlag)
				Android.audioPlayerForStory('/'+'myRecording_'+ListenRead.counter+'.mp3',null);
			console.log('/'+'myRecording_'+ListenRead.counter+'.mp3');
		},duration*1000);	
	}
	else if(ListenRead.soundRecorded==false)
	{	
		ListenRead.soundRecorded=true;
		$(".widgetClass").css("pointer-events","auto");
		$(".widgetClass").css("visibility","visible");
		clearTimeout(ListenRead.timer);
		document.getElementById('playSound').pause();
		if(Utils.mobileDeviceFlag)
		{
			Android.stopAudioPlayer();
			Android.stopRecording();
		}
		document.getElementById('listenImg2').src=Utils.Path+"images/Listen.png";
		$('#listenImg2').removeClass("blink_me");
		$(".recordClass").css("pointer-events","auto");
		$(".soundClass").css("pointer-events","auto");
	}
}
ListenRead.checkAnswer=function(str)
{
	ListenRead.noOfTimeQuestionAnswered++;
	ListenRead.stopSound();
	$(".soundClass").css("pointer-events","none");
	noOfAttempts++;
	ListenRead.skipFlag=false;
	ListenRead.homeClickFlag=false;
	document.getElementById("characterDiv").style.display="block";
	if(ListenRead.ansString=="true")
	{
		scoredMarks=10;
		$(".textClass").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();
		ListenRead.callNextQ();
	}
	else
	{
		scoredMarks=0;
		ListenRead.attemptFlag=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(){
		document.getElementById("characterDiv").style.display="none";
	},1500);
	$(".soundClass").css("pointer-events","auto");
}
ListenRead.checkAnswerOffline=function(element)
{
	ListenRead.stopSound();
	$(".soundClass").css("pointer-events","none");
	noOfAttempts++;
	ListenRead.skipFlag=false;
	ListenRead.homeClickFlag=false;
	document.getElementById("characterDiv").style.display="block";
	if(element=="rightImg2")
	{
		scoredMarks=10;
		$(".textClass").css("color","rgb(17, 245, 26)");
		ListenRead.calculateScore();
		$(".recordClass").css("pointer-events","none");
		$("#listenDiv2").css("pointer-events","none");
		$("#wrongDiv2").css("pointer-events","none");
		document.getElementById("playSound").src=Utils.Path+ListenRead.Sound[0];
		document.getElementById("playSound").play();
		document.getElementById("characterImg").src=Utils.Path+ListenRead.Image[0];
		ListenRead.callNextQ();
	}
	else
	{
		scoredMarks=0;
		$(".widgetClass").css("visibility","hidden");
		$("#listenDiv2").css("visibility","hidden");
		ListenRead.attemptFlag=true;
		$("#rightDiv2").css("pointer-events","none");
		$(".recordClass").css("pointer-events","none");
		document.getElementById("playSound").src=Utils.Path+ListenRead.Sound[1];
		document.getElementById("playSound").play();
		document.getElementById("characterImg").src=Utils.Path+ListenRead.Image[1];
	}
	setTimeout(function(){
		document.getElementById("characterDiv").style.display="none";
		$(".recordClass").css("pointer-events","auto");
	},1500);
	$(".widgetClass").css("pointer-events","none");
}
ListenRead.callNextQ=function()
{
	if(ListenRead.nextQueTimer)
	{
		clearTimeout(ListenRead.nextQueTimer);
		ListenRead.nextQueTimer=null;
	}
	ListenRead.nextQueTimer=setTimeout(function(){
		ListenRead.nextQuestion();
	},1500);
}
ListenRead.nextQuestion=function()
{
	ListenRead.queFlag=true;
	clearTimeout(ListenRead.nextQueTimer);
	clearTimeout(ListenRead.youReadTimer);
	clearTimeout(ListenRead.soundTimer);
	ListenRead.homeClickFlag=false;
	ListenRead.stopSound();
	ListenRead.skipedQ++;
	$(".textClass").css("color","white");
	$("#wrongDiv2").css("pointer-events","auto");
	if(ListenRead.attemptFlag)
	{
		scoredMarks=0;
		ListenRead.attemptFlag=false;
		ListenRead.calculateScore();
	}
	if(ListenRead.skipFlag)
	{
		scoredMarks=-1;
		ListenRead.calculateScore();
	}
	document.getElementById("characterDiv").style.display="none";

	clearInterval(ListenRead.checkStatus);
	if(ListenRead.skipedQ==5)
	{
		clearTimeout(ListenRead.soundTimer);
		ListenRead.questionArray=[];
		document.getElementById("message").style.visibility="visible";
		document.getElementById("endpage").style.display="block";
		document.getElementById("gamepage").style.display="none";
		document.getElementById("widgetDiv1").style.display="none";
		document.getElementById("widgetDiv2").style.display="none";
		document.getElementById("acknowledgementDiv").style.display="none";
		document.getElementById('message').innerHTML=ListenRead.hardcoded['gameOver'];
		ListenRead.createHardcodeddivs();
	}
	else
	{
		ListenRead.resetAll();
		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(Utils.mobileDeviceFlag)
	{
		Android.stopAudioPlayer();
		Android.stopRecording();
	}
	if(ListenRead.attemptFlag)
	{
		scoredMarks=0;
		ListenRead.attemptFlag=false;
		ListenRead.calculateScore();
	}
	if(ListenRead.homeClickFlag)
	{
		scoredMarks=-1;
		ListenRead.calculateScore();
	}
	ListenRead.stopSound();
	clearTimeout(ListenRead.soundTimer);
	location.reload(true);
}
ListenRead.stopSound=function()
{
	clearTimeout(ListenRead.soundTimer);
	document.getElementById("playSound").pause();
}
ListenRead.playAgain=function()
{
	ListenRead.resetAll();
	document.getElementById("message").style.display="block";
	document.getElementById('endpage').style.display="none";
	document.getElementById("gamepage").style.display="block";
	document.getElementById("widgetDiv1").style.display="block";
	document.getElementById("widgetDiv2").style.display="block";
	ListenRead.skipedQ=0;
	ListenRead.questionArray=[];
	ListenRead.showWordSentence();
}
ListenRead.resetAll=function()
{
	ListenRead.recordClickFlag=false;
	ListenRead.soundRecorded=false;
	ListenRead.skipFlag=true;
	clearTimeout(ListenRead.soundTimer);
	clearTimeout(ListenRead.nextQueTimer);
	clearTimeout(ListenRead.youReadTimer);
	noOfAttempts=0;
	$(".recordClass").prop('src',Utils.Path+"images/Record.png");
	document.getElementById('listenImg2').src=Utils.Path+"images/Listen.png";
	$(".recordClass").removeClass("blink_me");
	$('#listenImg2').removeClass("blink_me");
	$(".soundClass").css("pointer-events","auto");
	$(".recordClass").css("pointer-events","auto");
}
ListenRead.calculateScore=function()
{
	ListenRead.attemptFlag=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()
{
	document.getElementById("acknowledgementDiv").style.display="block";
}