var FlipIt={};
var Utils = {};
Utils.Path='';
Utils.mobileDeviceFlag=false;
var questionId=0,startTime=0,scoredMarks=0,totalMarks=10,resId=0,level=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("FlipIt");  */
	setTimeout(function()
	{
		FlipIt.init();
	},100);
});

FlipIt.init=function()
{
	$("#coverPage").show();
	$("#levelPage").hide();
	$("#gamePage").hide();
	$("#lastPage").hide();
	$(".header").not("#logo").css("visibility","hidden");
	document.body.style.backgroundImage = "url("+Utils.Path+"images/bgCover.jpg";
	FlipIt.setAllImages();
	FlipIt.createOptionDivs();
}

FlipIt.initializeVariables=function()
{
	FlipIt.levelIndex=0;
	FlipIt.question;
	FlipIt.firstAttemptFlag=true;
	FlipIt.correctAnsFlag=false;
	FlipIt.continueCorrect=0;
	FlipIt.wordLevel=0;
	FlipIt.noOfQuestions=0;
}

FlipIt.setAllImages=function()
{
	document.getElementById("homeImage").src=Utils.Path+"images/home.png";
	document.getElementById("logoImage").src=Utils.Path+"images/logo.png";
	document.getElementById("play").src=Utils.Path+"images/Play.png";
	document.getElementById("next").src=Utils.Path+"images/Next.png";
	document.getElementById("sound").src=Utils.Path+"images/Sound.png";
	document.getElementById("levelImg1").src=Utils.Path+"images/Level_01.png";
	document.getElementById("levelImg2").src=Utils.Path+"images/Level_02.png";
	document.getElementById("lastPara").innerHTML=FlipIt.hardCodedData.gameOver;
	document.getElementById("message").innerHTML=FlipIt.hardCodedData.message;
}

FlipIt.createOptionDivs=function()
{
	var i=0,parentDiv,option,para;
	parentDiv=document.getElementById("optionDiv");
	for(i=1;i<=4;i++)
	{
		option=document.createElement("DIV");
		option.setAttribute("id","optionDiv"+i);
		if(i%2==1)
			option.setAttribute("class","optionDivs nopadding col-xs-5 col-md-5 col-lg-5 col-sm-5");
		else
			option.setAttribute("class","optionDivs nopadding col-xs-5 col-md-5 col-lg-5 col-sm-5 col-xs-offset-1 col-md-offset-1 col-lg-offset-1 col-sm-offset-1");
		option.setAttribute("onclick","FlipIt.checkAnswer(this.id);");
		option.style.backgroundImage="url("+Utils.Path+"images/bg.jpeg";
		parentDiv.appendChild(option);
	
		para=document.createElement("P");
		para.setAttribute("id","option"+i);
		para.setAttribute("class","options");
		option.appendChild(para);
		
	}
}

FlipIt.showLevel=function()
{
	$("#coverPage").hide();
	$("#gamePage").hide();
	$("#levelPage").show();
	$("#lastPage").hide();
	$(".header").not("#message").css("visibility","visible");
	document.body.style.backgroundImage = "url("+Utils.Path+"images/NG.jpg";
	document.getElementById("level1").setAttribute("level",1);
	document.getElementById("level2").setAttribute("level",2);
	FlipIt.initializeVariables();
}
FlipIt.selectLevel=function(levelId)
{
	document.body.style.backgroundImage = "url("+Utils.Path+"images/NG.jpg";
	level=parseInt(document.getElementById(levelId).getAttribute("level"));
	FlipIt.levelIndex=level-1;
	FlipIt.playGame();			
}


FlipIt.playGame=function()
{
	rNo=0;
	
	$("#levelPage").hide();
	$("#gamePage").show();
	$("#lastPage").hide();
	$(".header").css("visibility","visible");
	FlipIt.setQuestion();
}

FlipIt.setQuestion=function()
{
	var d,i=0,rNo=0,question,option,flag=0,optionArr=[],randOption=0;
	d=new Date();
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	FlipIt.firstAttemptFlag=true;
	FlipIt.noOfQuestions++;
	if(FlipIt.wordLevel>2)
		$(".options").css("font-size","3.5vw");
	if(FlipIt.wordLevel>4)
		$(".options").css("font-size","3vw");
	if(FlipIt.noOfQuestions>14)
		FlipIt.endGame();
	else
	{	
		for(i=0;i<4;i++)
		{
			rNo=Math.floor(Math.random()*FlipIt.data[FlipIt.wordLevel].sentence.length);
			
			while(optionArr.includes(rNo))
				rNo=Math.floor(Math.random()*FlipIt.data[FlipIt.wordLevel].sentence.length);
			optionArr.push(rNo)
			if(FlipIt.levelIndex==0)
				option=FlipIt.data[FlipIt.wordLevel].sentence[rNo].english;
			else
				option=FlipIt.data[FlipIt.wordLevel].sentence[rNo].hindi;
			document.getElementById("option"+(i+1)).innerHTML=option;
		}
		randOption=Math.floor(Math.random()*optionArr.length);
		FlipIt.question=FlipIt.data[FlipIt.wordLevel].sentence[optionArr[randOption]];
		questionId=FlipIt.data[FlipIt.wordLevel].sentence[optionArr[randOption]].questionId;
		FlipIt.playSound();
	}
}

FlipIt.endGame=function()
{
	$("#gamePage").hide();
	$("#lastPage").show();
}

FlipIt.checkAnswer=function(divId)
{
	document.getElementById("playSound").pause();
	if(FlipIt.levelIndex==0)
		if(document.getElementById(divId).children[0].innerHTML==FlipIt.question.english)
			FlipIt.correctAnsFlag=true;
		else
			FlipIt.correctAnsFlag=false;
	else
		if(document.getElementById(divId).children[0].innerHTML==FlipIt.question.hindi)
			FlipIt.correctAnsFlag=true;
		else
			FlipIt.correctAnsFlag=false;
	if(FlipIt.correctAnsFlag==true)
	{
		scoredMarks=10;
		document.getElementById("playSound").src=Utils.Path+"sounds/CorrectAksharkhadi.mp3";
		$(".optionDivs").css("pointer-events","none");
		$("#soundDiv").css("pointer-events","none");
		FlipIt.nextQuestionTimeout=setTimeout(function()
		{
			FlipIt.showNextQuestion();
		},2000);
	}
	else
	{
		scoredMarks=0;
		FlipIt.firstAttemptFlag=false;
		document.getElementById("playSound").src=Utils.Path+"sounds/wrongSoundAksharkhadi.mp3";
	}
	document.getElementById("playSound").play();
	console.log(resId,questionId,scoredMarks,totalMarks,level,startTime);
	if(Utils.mobileDeviceFlag)
		Android.addScore(resId,questionId,scoredMarks,totalMarks,level,startTime);	
	if(FlipIt.firstAttemptFlag==true)
	{
		FlipIt.continueCorrect++;
		if(FlipIt.continueCorrect==2)
		{
			FlipIt.continueCorrect=0;
			FlipIt.wordLevel++;
			
		}
	}
}

FlipIt.playSound=function()
{
	if(Utils.mobileDeviceFlag)
	{
		if(FlipIt.levelIndex==0)
			Android.playTts(FlipIt.question.hindi,"hin");
		else
			Android.playTts(FlipIt.question.english,"eng");
	}
	else
	{
		if(FlipIt.levelIndex==0)
			saySomething(FlipIt.question.hindi);
		else
			saySomething(FlipIt.question.english);
	}
	
//ListenRead.stopSound=function()
//{
//if(Utils.mobileDeviceFlag)
//Android.stopTts();
//else
//stopSomething();
//}		
}
function saySomething(whatToSay)
{
	var synth,speech;
	synth = window.speechSynthesis;
	if('speechSynthesis' in window)
	{
		speech = new SpeechSynthesisUtterance(whatToSay);
		speech.lang = 'hi-IN'; //en-US
		window.speechSynthesis.speak(speech);
	}
} 
FlipIt.showNextQuestion=function()
{
	clearTimeout(FlipIt.nextQuestionTimeout);
	$(".optionDivs").css("pointer-events","auto");
	$("#soundDiv").css("pointer-events","auto");
	document.getElementById("playSound").pause();
	FlipIt.setQuestion();
}

FlipIt.callhome=function()
{
	
		
	if(document.getElementById("levelPage").style.display=="block")
		location.reload();
	else
	{
		document.getElementById("playSound").pause();
		clearTimeout(FlipIt.nextQuestionTimeout);
		FlipIt.initializeVariables();
		FlipIt.showLevel();
	}	
	
}

function saySomething(whatToSay)
{
if('speechSynthesis' in window)
{
speech = new SpeechSynthesisUtterance(whatToSay);
speech.lang = 'hi-IN'; //en-US
//speech.rate=0.7;
window.speechSynthesis.speak(speech);
}
//speech.onend = function() { $("#soundDiv").css("pointer-events","auto"); };
}


