var Recognize={};
var Utils = {};
Utils.mobileDeviceFlag = false;
Recognize.checkStatus = 0;
Utils.Path = '';
Recognize.randomQuestion=0;
Recognize.questionCnt=0;
Recognize.correctCnt=0;
Recognize.attemptCnt=0;
Recognize.recordFlag=false;
Recognize.ansString="";
$(document).ready(function()
{
	$(".se-pre-con").fadeOut("slow");
	 if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i))
        Utils.mobileDeviceFlag = true;
	setTimeout(function()
	{
		Recognize.init();
	},300);
});
Recognize.loadTimer=function()
{
	var time = 60; /* how long the timer runs for */
	var initialOffset = '440';
	var i = 0;	
	Recognize.interval = setInterval(function()
	{
		$('.circle_animation').css('stroke-dashoffset', initialOffset-(i*(initialOffset/time)));
		$('h2').text(i);
		if(i == time)
		{	
			clearInterval(Recognize.interval);
			clearInterval(Recognize.ques);
			setTimeout(function(){Recognize.showlastPage();},1500);
		}	
		i++;  
	},1000);
	/* Recognize.ques= setInterval(function()
	{
			if(Recognize.questionCnt >= 1)
			{
				if(Utils.mobileDeviceFlag)
				{
					clearInterval(Recognize.checkStatus);
					document.getElementById('recordImg').src ='images/Record.png';
					$('#recordImg').removeClass('blink_me');
					Recognize.recordFlag = false;
					Android.stopStt();
				}
				Recognize.getData();
			}
	},5500); */
}
Recognize.showQuestion=function()
{
	Recognize.ques= setInterval(function()
	{
			if(Recognize.questionCnt >= 1)
			{
				if(Utils.mobileDeviceFlag)
				{
					clearInterval(Recognize.checkStatus);
					document.getElementById('recordImg').src ='images/Record.png';
					$('#recordImg').removeClass('blink_me');
					Recognize.recordFlag = false;
					Android.stopStt();
				}
				Recognize.getData();
			}
	},5500);
}
Recognize.init=function()
{
	$(".item").html(""+Recognize.hardCodeddata.imgcode);
	$('#gamePage,#levelPage,#lastPage,#homeImage,#bodyDiv,#soundImg').hide();
	$('#homeAndLogoDiv,#coverPage').show();
	$('body').css('backgroundImage','url(images/gamepage.jpg)');
	//Recognize.loadQuestion();
	Recognize.setImages();
}
Recognize.playGame=function()
{	
	$('#homeAndLogoDiv,#homeImage,#gamePage,#homeButton,#soundImg').show();	
	$('#coverPage,#lastPage').hide();
	Recognize.getData();
	Recognize.loadTimer();
	Recognize.showQuestion();
}
Recognize.setImages=function()
{
	$('#homeButton').attr('src','images/Home.png');
	$('#logoImage').attr('src','images/logo.png');
	$('#play').attr('src','images/Play.png');
	$('#soundImg').attr('src','images/Sound.png');
	$('#tryAgainImg').attr('src','images/Next.png');
	$('#recordImg').attr('src','images/Record.png');
	$('#nextImg').attr('src','images/Next.png');
}
Recognize.loadQuestion=function()
{
	Recognize.arrofQuestionsToShow=populate(Recognize.wordsData,Recognize.arrofQuestionsToShow,10);
	Recognize.arrofQuestionsToShow=shuffleArray(Recognize.arrofQuestionsToShow);
	console.log(Recognize.arrofQuestionsToShow);
}
Recognize.getData=function()
{
	Recognize.questionCnt++;
	Recognize.attemptCnt=0;
	Recognize.randomQuestion=Math.floor(Math.random()*Recognize.wordsData.length);
	$("#questionDiv").text(Recognize.wordsData[Recognize.randomQuestion]['question']);
	$("#image").attr("src",'Images/'+Recognize.wordsData[Recognize.randomQuestion]['img']);
	Recognize.answer=Recognize.wordsData[Recognize.randomQuestion]['ans'];	
	console.log(Recognize.answer);
	Recognize.wordsData.splice(Recognize.randomQuestion,1);
}
Recognize.startRecording=function()
{
	if(Recognize.recordFlag == false)
	{
        Recognize.recordFlag = true;
        document.getElementById('recordImg').src ='images/Stopthh.png';
		$('#recordImg').addClass('blink_me');
		if(Utils.mobileDeviceFlag)
            Android.startStt("eng");
        if(Recognize.checkStatus)
		{
            clearInterval(Recognize.checkStatus);
            Recognize.checkStatus = null;
        }
		 Recognize.checkStatus = setInterval(function() 
		 {
			 if(Utils.mobileDeviceFlag)
                Recognize.ansString = Android.getSttResult();
				 //alert(Recognize.ansString);
				 console.log('before not $ ' + Recognize.ansString);
			 if('$$$'!== Recognize.ansString)
			 {
				   //alert(Recognize.ansString);
				  clearInterval(Recognize.checkStatus);
				   if(Recognize.ansString !== "###" && Recognize.ansString !== "")
				   {
						//alert(Recognize.ansString);
				   } 
				   else
				   {
						console.log('Aapoaap Stop');
						clearInterval(Recognize.checkStatus);
						Recognize.recordFlag = false;
						document.getElementById('recordImg').src ='images/Record.png';
						$('#recordImg').removeClass('blink_me');
				   }
				    Recognize.checkAnswer(Recognize.ansString);
					clearInterval(Recognize.checkStatus);
					Recognize.recordFlag = false;
					document.getElementById('recordImg').src ='images/Record.png';
					$('#recordImg').removeClass('blink_me');	
			 }	
		}, 300);
	}	// first if of func	
	else
	{
		  if(Utils.mobileDeviceFlag)
		  {
            clearInterval(Recognize.checkStatus);
            document.getElementById('recordImg').src ='images/Record.png';
            $('#recordImg').removeClass('blink_me');
            Recognize.recordFlag = false;
            Android.stopStt();
		  }	
	}	
}
 Recognize.checkAnswer=function(str)
{
	alert(str);
	str=str.toLowerCase();
	if(str==Recognize.answer)
	{	
		document.getElementById("playSound").src="sounds/Correct.mp3";
		document.getElementById("playSound").play();
		Recognize.correctCnt++;	
		console.log(Recognize.correctCnt);
		//alert("correct"+Recognize.correctCnt);
	}
	else
	{
		//alert("wrong");
		document.getElementById("playSound").src="sounds/Wrong.mp3";
		document.getElementById("playSound").play();
	}	
	//alert("check"+str+Recognize.correctCnt);	
}
Recognize.nextQuestion=function()
{
	clearInterval(Recognize.ques);
	Recognize.showQuestion();
	if(Utils.mobileDeviceFlag)
	{
		clearInterval(Recognize.checkStatus);
		document.getElementById('recordImg').src ='images/Record.png';
		$('#recordImg').removeClass('blink_me');
		Recognize.recordFlag = false;
		Android.stopStt();
	}
	Recognize.getData();
}
Recognize.showlastPage=function()
{
	$('#lastPage').show();
	$('#gamePage,#soundImg').hide();	
	$('#lastPageText').text(Recognize.hardCodeddata.GameFinished);
	$('#CorrectDiv').text(Recognize.hardCodeddata.CorrectText+""+Recognize.correctCnt);
	$('#TotalDiv').text(Recognize.hardCodeddata.TotalText+""+Recognize.questionCnt);
}