//20July 2018
var Memorygame={};
var Utils = {};
Utils.Path='';
Utils.mobileDeviceFlag=false;
var questionId=0,startTime=0,scoredMarks=0,totalMarks=10,resId=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("Memorygame");  
	setTimeout(function()
	{
		Memorygame.init();
	},100);
},200);
Memorygame.init=function()
{
	Memorygame.clickCnt=0;
	Memorygame.correctCnt=0;
	Memorygame.level=0;
	Memorygame.score=0;
	Memorygame.consecutive=0;
	Memorygame.firstid="";
	Memorygame.moves=0;
	Memorygame.playFlag=false;
	Memorygame.levelCompleteFlag=false;
	$('#clockDiv').css('visibility','hidden');
	$('#gamePage,#endPage').hide();
	Memorygame.setImages();
	Memorygame.setHardcodedData();
};
//set harcoded data
Memorygame.setHardcodedData=function()
{
	$("score").text("0");
	$("#Yes").text(Memorygame.hardCodeddata.yes);
	$("#No").text(Memorygame.hardCodeddata.no);
	$("#message").text(Memorygame.hardCodeddata.message);
	$("#header").text(Memorygame.hardCodeddata.heading);
	$('#questionMsg').text(Memorygame.hardCodeddata.instruction);
	$('#start').text(Memorygame.hardCodeddata.playButtonText);
	$('#scoreText').text(Memorygame.hardCodeddata.scoreText);
	$('#conceptBy').text(Memorygame.hardCodeddata.concept_by);
	$('#artWork').text(Memorygame.hardCodeddata.art_work);
	$('#audioEditing').text(Memorygame.hardCodeddata.audio_editing);
	$('#music').text(Memorygame.hardCodeddata.music);
	$('#developerName').text(Memorygame.hardCodeddata.developed_by);
};
// set Images//
Memorygame.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('clockImage').src=Utils.Path+"images/stopwatch.png";
	document.getElementById('start').src=Utils.Path+"images/Play.png";
	document.getElementById('replayImg').src=Utils.Path+"images/replay.png";
	document.body.style.backgroundImage="url("+Utils.Path+"images/cover.jpg)";
};
//function which executes on play button click
Memorygame.playGame=function()
{
	$('#coverPage').hide();
	$('#homeImage').css('visibility','visible');
	$('#logoImage,#gamePage').show();
	document.body.style.backgroundImage="url("+Utils.Path+"images/BG.jpg)";
	document.getElementById('backgroundMusic').src=Utils.Path+"sounds/backgroundMusic.mp3";
	document.getElementById('backgroundMusic').loop=true;
	document.getElementById('backgroundMusic').play();
	Memorygame.endGameFlag=false;	
	$("#responseBoxDiv").show();
}
Memorygame.startGame=function()
{
	$('#scoreDiv').css('visibility','visible');
	$("#responseBoxDiv").hide();
	Memorygame.createGrid();
}
//function which starts the timer
Memorygame.setTimer=function()
{
	var time=40;
	$("#timerSpan").text(time);
	Memorygame.timer=setInterval(function() 
	{
		if(time==1)	
		{
			clearInterval(Memorygame.timer);
			$('#gamePage').hide();
			Memorygame.endGameFlag=true;
			$('#endPage').show();
			$('#clockDiv').css('visibility','hidden'); 
		}
		time--;
		$("#timerSpan").text(time);
	},1000);
}
//function which creates Div Grid
Memorygame.createGrid=function()
{
	var gridDiv,Div,img;
	var d=new Date();
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	
	gridDiv=document.createElement('div');
	gridDiv.setAttribute('id',"gridDiv");
	gridDiv.setAttribute('class',"nopadding col-xs-"+Memorygame.layout[Memorygame.level].gridDivClass+" "+"col-xs-"+Memorygame.layout[Memorygame.level].gridDivClass+" "+"col-xs-"+Memorygame.layout[Memorygame.level].gridDivClass+" "+"col-xs-"+Memorygame.layout[Memorygame.level].gridDivClass+" "+"col-xs-offset-"+Memorygame.layout[Memorygame.level].gridDivOffset+" "+"col-xs-offset-"+Memorygame.layout[Memorygame.level].gridDivOffset+" "+"col-xs-offset-"+Memorygame.layout[Memorygame.level].gridDivOffset+" "+"col-xs-offset-"+Memorygame.layout[Memorygame.level].gridDivOffset);
	document.getElementById('OuterDiv').appendChild(gridDiv);
	for(var i=0;i<Memorygame.layout[Memorygame.level].noOfImages;i++)
	{	
		Div=document.createElement('div');
		Div.setAttribute('id',"Div"+i);
		if(Memorygame.level==2 && (i==0 || i==5 || i==10 || i==15 || i==20))
			Div.setAttribute('class',"nopadding cell"+" "+"col-xs-"+Memorygame.layout[Memorygame.level].innerDivClass+" "+"col-xs-"+Memorygame.layout[Memorygame.level].innerDivClass+" "+"col-xs-"+Memorygame.layout[Memorygame.level].innerDivClass+" "+"col-xs-"+Memorygame.layout[Memorygame.level].innerDivClass+" "+"col-xs-offset-"+Memorygame.layout[Memorygame.level].innerDivOffset+" "+"col-xs-offset-"+Memorygame.layout[Memorygame.level].innerDivOffset+" "+"col-xs-offset-"+Memorygame.layout[Memorygame.level].innerDivOffset+" "+"col-xs-offset-"+Memorygame.layout[Memorygame.level].innerDivOffset);
		else
			Div.setAttribute('class',"nopadding cell"+" "+"col-xs-"+Memorygame.layout[Memorygame.level].innerDivClass+" "+"col-xs-"+Memorygame.layout[Memorygame.level].innerDivClass+" "+"col-xs-"+Memorygame.layout[Memorygame.level].innerDivClass+" "+"col-xs-"+Memorygame.layout[Memorygame.level].innerDivClass);
		
		/* if(Memorygame.level==1)
			$(Div).css('height','21.5vh');	 */
		
		img=document.createElement('img');
		img.setAttribute('id',"img"+i);
		img.setAttribute('class',"nopadding img-responsive imgcls col-xs-12 col-md-12 col-lg-12 col-sm-12");
		Div.appendChild(img);
		document.getElementById('gridDiv').appendChild(Div);
	}
	$('.cell').css('pointer-events','none');
	$('.cell').on('click',function(){
		Memorygame.checkAnswer(this.id);
	});
	$('.imgcls').addClass('animated flip');
	Memorygame.fillImageArray();
	setTimeout(function(){
		$('.imgcls').removeClass('animated flip');
		$('.cell').css('pointer-events','auto');
		$('.imgcls').css('visibility','hidden');
		//$('#clockDiv').css('visibility','visible');
		clearInterval(Memorygame.timer);
		//Memorygame.setTimer();
	},Memorygame.layout[Memorygame.level].duration);
}
//function which fills images into array to be shown on grid
Memorygame.fillImageArray=function()
{
	var pairs,randomIndex=0,remaining=0,imgArr=[];
	pairs=Math.floor(Memorygame.layout[Memorygame.level].noOfImages/2);// no of images in pairs
	remaining=Memorygame.layout[Memorygame.level].noOfImages-pairs*2;// no of random images rather than pairs
	console.log(remaining);
	//filling pairs
	for(var i=0;i<pairs;i++)
	{
		randomIndex=Math.floor(Math.random()*Memorygame.data.length);
		while($.inArray(randomIndex,imgArr)!=-1)
			randomIndex=Math.floor(Math.random()*Memorygame.data.length);
		imgArr.push(randomIndex);
		imgArr.push(randomIndex);
	}	
	//filling images rather than pairs
	for(var i=0;i<remaining;i++)
	{
		randomIndex=Math.floor(Math.random()*Memorygame.data.length);
		while($.inArray(randomIndex,imgArr)!=-1)
			randomIndex=Math.floor(Math.random()*Memorygame.data.length);
		imgArr.push(randomIndex);
	}	
	shuffleArray(imgArr);
	console.log(imgArr);
	Memorygame.setImagesToGrid(imgArr);
}
Memorygame.checkAnswer=function(obj)
{
	if(!Memorygame.levelCompleteFlag)
		Memorygame.playFlag=true;
	Memorygame.clickCnt++;
	$("#"+obj).children('img').css('visibility','visible');
	$("#"+obj).css('pointer-events','none');
	if(Memorygame.clickCnt==1)
	{	
		Memorygame.answer=$("#"+obj).data('answer');
		Memorygame.firstid=""+obj;
	}
	if(Memorygame.clickCnt==2)
	{
		Memorygame.moves++;
		if(Memorygame.answer==$("#"+obj).data('answer'))
		{	
			/* console.log(resId,questionId,10,totalMarks,Memorygame.level+1,startTime);
			 if(Utils.mobileDeviceFlag)
				Android.addScore(resId,questionId,10,totalMarks,Memorygame.level+1,startTime); */  
			$("#"+obj).children('img').addClass('animated bounceIn');
			$("#smileeDiv").css('display','block');
			document.getElementById('smileeImg').src=Utils.Path+"images/correct.png";
			document.getElementById('playSound').src=Utils.Path+"sounds/Hureyyy.mp3";
			document.getElementById('playSound').play();
			setTimeout(function(){$("#smileeDiv").css('display','none');},1000);
			Memorygame.correctCnt++;
			Memorygame.score+=10;
			Memorygame.consecutive++;
			$("#score").text(Memorygame.score);
			$("#"+obj).css('pointer-events','none');
			Memorygame.consecutive==3
			if(Memorygame.correctCnt==Memorygame.layout[Memorygame.level].levelchange)
			{
				Memorygame.playFlag=false;
				Memorygame.levelCompleteFlag=true;
				console.log(resId,questionId,Memorygame.moves,Memorygame.correctCnt,Memorygame.level+1,startTime);
				 if(Utils.mobileDeviceFlag)
					Android.addScore(resId,questionId,Memorygame.moves,Memorygame.correctCnt,Memorygame.level+1,startTime);  
				Memorygame.consecutive=0;
				Memorygame.correctCnt=0;
				Memorygame.level++;
				console.log(Memorygame.moves);
				Memorygame.moves=0;
				if(Memorygame.level==2)
				{
					//document.body.style.backgroundImage="url("+Utils.Path+"Images/Lastpage.jpg)";
					Memorygame.endGameFlag=true;
					setTimeout(function()
					{
						Memorygame.showEndPage();
					},2000); 
					$('#clockDiv').css('visibility','hidden');
				}
				else
				{	
					setTimeout(function(){
						$("#dialogBoxDiv").show();
					},1000);
				}
			}	
		}
		else
		{
			//Memorygame.score-=1;
			$("#score").text(Memorygame.score);
			Memorygame.consecutive=0;
			$("#"+obj).children('img').addClass('animated wobble');
			setTimeout(function(){$("#"+obj).children('img').css('visibility','hidden');},300);
			$("#"+Memorygame.firstid).children('img').css('visibility','hidden');
			$("#"+obj).css('pointer-events','auto');
			$("#"+Memorygame.firstid).css('pointer-events','auto');
			/* if(Utils.mobileDeviceFlag)
				Android.addScore(resId,questionId,0,totalMarks,Memorygame.level+1,startTime);
			console.log(resId,questionId,0,totalMarks,Memorygame.level+1,startTime); */
			document.getElementById('playSound').src=Utils.Path+"sounds/oops.mp3";
			document.getElementById('playSound').play();
			$("#smileeDiv").css('display','block');
			document.getElementById('smileeImg').src=Utils.Path+"images/wrong.png";
			setTimeout(function(){$("#smileeDiv").css('display','none');},300);
		}		
		Memorygame.clickCnt=0;
	}
}
//function which sets images to grid
Memorygame.setImagesToGrid=function(imgArr)
{
	for(var i=0;i<Memorygame.layout[Memorygame.level].noOfImages;i++)
	{	
		document.getElementById("img"+i).src=Utils.Path+"images/"+Memorygame.data[imgArr[i]].img;
		$("#Div"+i).data('answer',Memorygame.data[imgArr[i]].name);
	}	
};
Memorygame.nextLevel=function()
{
	Memorygame.levelCompleteFlag=false;
	Memorygame.clickCnt=0;
	$("#dialogBoxDiv").hide();
	$('#OuterDiv').empty();
	$('#clockDiv').css('visibility','hidden');
	Memorygame.createGrid();
};
Memorygame.showEndPage=function()
{
	Memorygame.playFlag=false;
	$("#dialogBoxDiv").hide();
	document.getElementById('playSound').pause();
	$('#gamePage').hide();
	$('#endPage').show();
};
//function which executes on home button click
Memorygame.goToHome=function()
{
	if(Memorygame.playFlag==true)
	{
		Memorygame.moves=0;
		console.log(resId,questionId,Memorygame.moves,Memorygame.layout[Memorygame.level].levelchange,Memorygame.level+1,startTime);
		if(Utils.mobileDeviceFlag)
			Android.addScore(resId,questionId,Memorygame.moves,Memorygame.layout[Memorygame.level].levelchange,Memorygame.level+1,startTime); 
	}	
		location.reload();
}	
Memorygame.playAgain=function()
{
	//if(Memorygame.endGameFlag)
		Memorygame.replay();	
};
//function which executes on replay button click
Memorygame.replay=function()
{
	$("#endPage").hide();
	$("#gamePage").show();
	Memorygame.endGameFlag=false;
	Memorygame.levelCompleteFlag=false;
	Memorygame.level=0;
	Memorygame.clickCnt=0;
	Memorygame.correctCnt=0;
	Memorygame.level=0;
	Memorygame.score=0;
	Memorygame.consecutive=0;
	Memorygame.firstid="";
	$("#score").text("0");
	$("#OuterDiv").empty();
	Memorygame.startGame();
}
