					//22th Oct 2018
var ShowMe={};
var Utils={};
Utils.Path='';
Utils.mobileDeviceFlag=false;
var questionId=0,startTime=0,scoredMarks=0,resId=0,totalMarks=10,noOfAttemptedCnt=0,correctCnt=0;
ShowMe.skipedQ=0;
ShowMe.homeFlag=null;
ShowMe.alreadyShownObject=[];
ShowMe.tempArr=[];
ShowMe.totalAttmptCnt=0;
ShowMe.counterFlag=false;
ShowMe.soundFlag=true;
ShowMe.seconds=0;
ShowMe.levelNumber=0;
//function for loader
$(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.getElementById('coverpage').style.display="block";
		document.getElementById('gamepage').style.display="none";
		document.getElementById('endpage').style.display="none";
		//document.getElementById("homeImg").style.visibility = "hidden";
		ShowMe.setImages();
		ShowMe.init();
	},300);
});
ShowMe.setImages=function()
{
	document.body.style.backgroundImage="url("+Utils.Path+ShowMe.coverImage+")";
	document.getElementById("showMePageDiv").style.backgroundImage="url("+Utils.Path+"images/board.png)";
	//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("nextImage").src=Utils.Path+"images/Next.png";
	document.getElementById("sound").src=Utils.Path+"images/Sound.png";
}
ShowMe.init=function()
{
	ShowMe.randomLevel=0;
	ShowMe.tempArr=JSON.parse(JSON.stringify(ShowMe.data[0].list[ShowMe.levelNumber].data));
}
ShowMe.playGame=function()
{
	document.body.style.backgroundImage="url("+Utils.Path+"images/bg.png)";
	document.getElementById('coverpage').style.display="none";
	document.getElementById('gamepage').style.display="none";
	//document.getElementById("homeImg").style.display = "block";
	ShowMe.createDivs(showMePageDiv,"picture","pictureClass pointerClass  noPadding col-md-3 col-xs-3 col-lg-3 col-sm-3",3);
	$(".borderClass").css("backgroundImage","url(images/blue_pach.png)");
	ShowMe.createStars();
	ShowMe.showObjects();
}
ShowMe.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/star1.png");
		$(newDiv).append(newImage);
		$("#starDiv").append(newDiv);
	}
	$("#starDiv").css("visibility","hidden");
};
//creates No. of Divs
ShowMe.createDivs=function(parentDiv,id,className,no)
{
	var ShowMeDiv,pictureDiv,img;
	for(var j=0;j<no;j++)
	{
		pictureDiv= document.createElement("div");
		pictureDiv.setAttribute("id",id+j); 
		pictureDiv.setAttribute("class",className); 
		parentDiv.appendChild(pictureDiv);

		img=document.createElement('img');
		img.setAttribute("id",id+"img"+j); 
		img.setAttribute("class"," borderClass img-responsive col-lg-12 col-md-12 col-xs-12 col-sm-12");
		pictureDiv.appendChild(img);
		img.setAttribute("onclick","ShowMe.checkAnswer(this)"); 
	}
}
//Show Objects on the screen
ShowMe.showObjects=function()
{
	var d,imgCnt=0;
	d=new Date();
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	document.getElementById('gamepage').style.display="block";
	ShowMe.uniqueImgArr=[];
	ShowMe.uniqueImgArr=populate(ShowMe.tempArr,ShowMe.uniqueImgArr,ShowMe.hardcorded.number);
	for(var j=0;j<3;j++)
	{
		$("#pictureimg"+j).prop("src",Utils.Path+"images/"+ShowMe.tempArr[ShowMe.uniqueImgArr[imgCnt]].imgName);
		$("#pictureimg"+j).data("ans",ShowMe.tempArr[ShowMe.uniqueImgArr[imgCnt]].name);
		imgCnt++;
	}
	ShowMe.uniqueQue= Math.floor(Math.random()*(ShowMe.uniqueImgArr.length));
	while($.inArray(ShowMe.uniqueImgArr[ShowMe.uniqueQue].name,ShowMe.alreadyShownObject)!==-1)
		ShowMe.uniqueQue= Math.floor(Math.random()*(ShowMe.uniqueImgArr.length));
	ShowMe.alreadyShownObject.push(ShowMe.tempArr[ShowMe.uniqueImgArr[ShowMe.uniqueQue]].name);
	questionId=parseInt(ShowMe.tempArr[ShowMe.uniqueImgArr[ShowMe.uniqueQue]].questionId);
	ShowMe.playInstruction();	
}
ShowMe.playInstruction=function()
{
	var source;
	ShowMe.soundFlag=true;
	clearTimeout(ShowMe.timeOut);
	clearInterval(ShowMe.noResponseTimer);
	ShowMe.noResponseTimer=null;
	ShowMe.seconds=0;
	$(".pointerClass").css("pointer-events","none");
	document.getElementById("playSound").src=Utils.Path+'sounds/'+ShowMe.hardcorded.instruction
	document.getElementById("playSound").play();
	setTimeout(function()
	{
		clearTimeout(ShowMe.timeOut);
		clearInterval(ShowMe.noResponseTimer);
		$(".pointerClass").css("pointer-events","auto");
		source=Utils.Path+'sounds/'+ShowMe.tempArr[ShowMe.uniqueImgArr[ShowMe.uniqueQue]].sound;
		getDuration(source);
	},1500);
	
}
function getDuration(src)
{
	var duration=0;
	if(ShowMe.soundFlag)
	{
		$('#playSound').on("loadedmetadata", function()
		{
			ShowMe.soundFlag=false;
			duration=$('#playSound').prop('duration');
			$('#playSound').unbind("loadedmetadata");
			ShowMe.playNextSound(duration);
		});
		$('#playSound').prop('src',src);
	}
}
ShowMe.playNextSound=function(duration)
{
	document.getElementById('playSound').play();
	if(ShowMe.soundTimer)
	{
		clearTimeout(ShowMe.soundTimer);
		ShowMe.soundTimer=null;
	}
	ShowMe.soundTimer=setTimeout(function()
	{
		document.getElementById('playSound').pause();
		clearTimeout(ShowMe.soundTimer);
		ShowMe.startTimer();
	},duration*1000);
}
ShowMe.startTimer=function()
{
	ShowMe.seconds=0;
	if(ShowMe.noResponseTimer)
	{
		clearInterval(ShowMe.noResponseTimer);
		ShowMe.noResponseTimer=null;
	}	
	ShowMe.noResponseTimer=setInterval(function() 
	{
		ShowMe.seconds++;
		console.log(ShowMe.seconds);
		if(ShowMe.seconds>=7)
		{
			clearInterval(ShowMe.noResponseTimer);
			ShowMe.noResponseTimer=null;
			document.getElementById("playSound").src=Utils.Path+"sounds/"+ShowMe.hardcorded.noResponce;
			document.getElementById("playSound").play();
			$(".pointerClass").css("pointer-events","auto");
			if(ShowMe.timeOut)
			{
				clearTimeout(ShowMe.timeOut);
				ShowMe.timeOut=null;
			}
			ShowMe.timeOut=setTimeout(function()
			{
				clearTimeout(ShowMe.timeOut);
				ShowMe.startTimer();
				return 0;
			},2000);
		}
	},1000);
} 
//Check Answer
ShowMe.checkAnswer=function(element)
{	
	var randomSound;
	clearInterval(ShowMe.noResponseTimer);
	clearTimeout(ShowMe.timeOut);
	noOfAttemptedCnt++;
	ShowMe.totalAttmptCnt++;
	if(($(element).data('ans')==ShowMe.tempArr[ShowMe.uniqueImgArr[ShowMe.uniqueQue]].name))
	{
		scoredMarks=10;
		$(element).addClass("animated zoomIn");
		correctCnt++;
		randomSound=Math.floor(Math.random()*(ShowMe.correctAns.length));
		document.getElementById('playSound').src=Utils.Path+'sounds/'+ShowMe.correctAns[randomSound];
		document.getElementById('playSound').play();
	}
	else
	{
		scoredMarks=0;
		$(element).addClass("animated shake");
		$(element).parent().css("pointer-events","none");
		document.getElementById('playSound').src=Utils.Path+'sounds/'+ShowMe.wrongAns;
		document.getElementById('playSound').play();
	}
	$(".pointerClass").css("pointer-events","none");
	if(ShowMe.nextQ){
		clearTimeout(ShowMe.nextQ);
		ShowMe.nextQ=null;
	}
	ShowMe.nextQ=setTimeout(function(){
		$(element).removeClass("animated zoomIn");
		$(element).removeClass("animated shake");
			ShowMe.nextQuestion();
	},2000);
	if(noOfAttemptedCnt==1)
	{
		console.log(resId,questionId,scoredMarks,totalMarks,1,startTime,ShowMe.tempArr[ShowMe.uniqueImgArr[ShowMe.uniqueQue]].name);
		if(Utils.mobileDeviceFlag)
			Android.addScore(resId,questionId,scoredMarks,totalMarks,1,startTime,ShowMe.tempArr[ShowMe.uniqueImgArr[ShowMe.uniqueQue]].name);
	}
}
//Load Next Question Of The Same Story
ShowMe.nextQuestion=function()
{
	clearTimeout(ShowMe.instruction);
	clearTimeout(ShowMe.myTimer);
	clearTimeout(ShowMe.nextQ);
	ShowMe.skipedQ++;
	$(".borderClass").removeClass("animated zoomIn");
	$(".borderClass").removeClass("animated shake");
	noOfAttemptedCnt=0;
	document.getElementById('playSound').pause();
	ShowMe.tempArr.splice(ShowMe.uniqueImgArr[ShowMe.uniqueQue],1);	
	if(ShowMe.skipedQ==5)
	{
		ShowMe.homeFlag=null;
		clearTimeout(ShowMe.soundTimer);
		clearInterval(ShowMe.noResponseTimer);
		document.getElementById('playSound').pause();
		ShowMe.giveStars();
		document.getElementById("starDiv").style.display="block";
		document.getElementById("gamepage").style.display="none";
		$('#endpage').show();
		document.getElementById('message').innerHTML=ShowMe.hardcorded.gameOver;
		console.log(correctCnt,ShowMe.totalAttmptCnt);
		ShowMe.totalAttmptCnt=0;
		if(Utils.mobileDeviceFlag)
			Android.StudentCertification(correctCnt,ShowMe.skipedQ,"1");
	}
	else
		ShowMe.showObjects(ShowMe.levelNumber);
}
ShowMe.giveStars=function()
{
	var i,numberOfStars=correctCnt;
	console.log(numberOfStars);
	for(i=1;i<=numberOfStars;i++)
		document.getElementById("starImg"+i).setAttribute("src",Utils.Path+"images/star2.png");
	if(numberOfStars!=0)
	{
		document.getElementById('playSound').src=Utils.Path+'sounds/Star'+numberOfStars+'.mp3';
		document.getElementById('playSound').play();
	}
	$("#starDiv").css("visibility","visible");
};
ShowMe.callHome=function()
{
	document.getElementById('playSound').pause();
	clearTimeout(ShowMe.instruction);
	clearTimeout(ShowMe.myTimer);
	clearTimeout(ShowMe.nextQ);
	clearTimeout(ShowMe.soundTimer);
	location.reload(true);
}
ShowMe.nextGame=function()
{
	if(Utils.mobileDeviceFlag)
		Android.GotoNextGame();
}