//7th May 2019
var Preposition={};
var Utils = {};
Utils.Path='';
Utils.mobileDeviceFlag=false;
Preposition.loadmetaDataFlag=false;
Preposition.recordClickFlag=false;
Preposition.answer="";
var questionId=0,startTime=0,scoredMarks=0,resId=0,totalMarks=10;
$(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("Preposition");   */
	setTimeout(function()
	{
		Preposition.init();
		Preposition.setImages();
		Preposition.harcodedData();
	},100);
},200);
Preposition.init=function()
{
	$('#gamePage,#endPage').hide();
};
Preposition.harcodedData=function()
{
	$("#gameOverText").text(Preposition.hardcodedData.gameover);
	$('#conceptBy').text(Preposition.hardcodedData.concept_by);
	$('#artWork').text(Preposition.hardcodedData.art_work);
	$('#audioEditing').text(Preposition.hardcodedData.audio_editing);
	$('#music').text(Preposition.hardcodedData.music);
	$('#developerName').text(Preposition.hardcodedData.developed_by);
	$('#date').text(Preposition.hardcodedData.date);
}
// set Images//
Preposition.setImages=function()
{
	document.getElementById('play').src=Utils.Path+"images/Play.png";
	document.getElementById('homeImage').src=Utils.Path+"images/Home.png";
	document.getElementById('logoImage').src=Utils.Path+"images/logo.png";
	document.getElementById('soundImg').src=Utils.Path+"images/Sound.png";
	document.getElementById('recordImg').src=Utils.Path+"images/Record.png";
	document.getElementById('nextImg').src=Utils.Path+"images/Next.png";
	document.body.style.backgroundImage="url("+Utils.Path+"images/Bg.png)";
};
//function which executes on play button click
Preposition.playGame=function()
{
	d=new Date();				
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	$('#coverPage,#animalsCard,#Card').hide();
	$('#homeImage,#imageDiv,#questionText,#answerText').css('visibility','visible');
	$('#logoImage,#gamePage').show();
	Preposition.tempArr=JSON.parse(JSON.stringify(Preposition.data));
	console.log(Preposition.tempArr);
	Preposition.showQuestion();
};
//function which executes on play button click
Preposition.showQuestion=function()
{
	var d=new Date();
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	randomQuestion=Math.floor(Math.random()*Preposition.tempArr.length);
	console.log(randomQuestion);
	$("#questionText").text(Preposition.tempArr[randomQuestion].Question);
	$("#answerText").text(Preposition.tempArr[randomQuestion].Answer);
	$("#imageDiv").css('backgroundImage','url('+Utils.Path+"images/"+Preposition.tempArr[randomQuestion].Image+')');
	//Preposition.answer=Preposition.tempArr[randomQuestion].Answer;
	Preposition.getAnswer(Preposition.tempArr[randomQuestion].Answer);
	Preposition.tempArr.splice(randomQuestion,1);
};
Preposition.getAnswer=function(str)
{
	var index=0;
	str=str.trim();
	arr=str.split(' ');
	for(var i=0;i<Preposition.prepositionArray.length;i++)
	{
		index=arr.indexOf(Preposition.prepositionArray[i]);
		if(index>=0)	
			break;
	}
	for(var i=index;i<arr.length;i++)
		Preposition.answer=Preposition.answer+" "+arr[i];
	Preposition.answer=Preposition.answer.trim();
	console.log(Preposition.answer);
};

Preposition.record=function()
{
	Preposition.ansString="";
	if(Preposition.recordClickFlag==false)
	{
		Preposition.recordClickFlag=true;
		if(Utils.mobileDeviceFlag)
		Android.startStt("eng",Preposition.answer);
		$("#recordImg").prop('src',Utils.Path+"images/Stop.png");
		$("#recordImg").addClass("blink_me");
		if(Preposition.checkStatus)
		{
		   clearInterval(Preposition.checkStatus);
		   Preposition.checkStatus = null;
		}
		Preposition.checkStatus = setInterval(function() 
		{
			if(Utils.mobileDeviceFlag)
				Preposition.ansString = Android.getSttResult();
			console.log(Preposition.ansString);
			if('$$$'!== Preposition.ansString)
			{
				clearInterval(Preposition.checkStatus);
				if(Preposition.ansString !== "###" && Preposition.ansString !== "")
					Preposition.checkAnswer();
				else
				{
					console.log('Stops');
				}
				Preposition.recordClickFlag = false;
				$("#recordImg").prop('src',Utils.Path+"images/Record.png");
				$('#recordImg').removeClass('blink_me');
			}
		},500);
	}	
	else
	{
		Preposition.recordClickFlag = false;
		if(Utils.mobileDeviceFlag)
			Android.stopStt();
		Preposition.ansString="";
		clearInterval(Preposition.checkStatus);
		$("#recordImg").prop('src',Utils.Path+"images/Record.png");
		$("#recordImg").removeClass("blink_me");
	}
};
//function which checks answer
Preposition.checkAnswer=function()
{
	var arr=[];
	$("#recordImg").prop('src',Utils.Path+"images/Record.png");
	arr=Preposition.sttString.split(' ');
	alert(arr);
	  if(Preposition.ansString=="true")
	{
		scoredMarks=10;
		alert(Preposition.ansString+"Correct");
		/*  document.getElementById("playSound").src=Utils.Path+Preposition.Sound[0];
		document.getElementById("playSound").play();   */
		if(Utils.mobileDeviceFlag)
			Android.addScore(resId,questionId,scoredMarks,totalMarks,1,startTime);
	}
	else
	{
		scoredMarks=0;
		alert(Preposition.ansString+"Wrong");
		 /* document.getElementById("playSound").src=Utils.Path+Preposition.Sound[1];
		document.getElementById("playSound").play();  */
		if(Utils.mobileDeviceFlag)
			Android.addScore(resId,questionId,scoredMarks,totalMarks,1,startTime);
	}
	if(Preposition.nextQueTimer)
	{
		clearTimeout(Preposition.nextQueTimer);
		Preposition.nextQueTimer=null;
	}
	Preposition.nextQueTimer=setTimeout(function(){
		Preposition.nextQuestion();
	},1500); 
	//setTimeout(function(){$("#characterDiv").hide();},1500);
};
//function which executes on next button click
Preposition.nextQuestion=function()
{
	 if(Preposition.nextQueTimer)
	{
		clearTimeout(Preposition.nextQueTimer);
		Preposition.nextQueTimer=null;
	} 
	if(Preposition.tempArr.length!=0)
	{
		Preposition.recordClickFlag = false;
		if(Utils.mobileDeviceFlag)
			Android.stopStt();
		Preposition.ansString="";
		Preposition.answer="";
		clearInterval(Preposition.checkStatus);
		Preposition.showQuestion();	
	}
	else
	{
		$("#gamePage").hide();
		$("#endPage").show();	
	}	
};
//function which executes on home button click
Preposition.goToHome=function()
{
	location.reload(true);	
};	
