var PattaPalat={};
var Utils={};
Utils.Path='';
Utils.mobileDeviceFlag=false;
var questionId=0,startTime=0,scoredMarks=0,totalMark=1,resId=0;
PattaPalat.todaysData=[];

$(document).ready(function()
{
	/*if(navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i))
		Utils.mobileDeviceFlag=true;
	
	if(Utils.mobileDeviceFlag)
	{	
		Utils.Path=Android.getMediaPath("Patta_palat"); 
   		PattaPalat.todaysData=(Android.getTodaysLetters()).split(',');
	}
	else*/
		//PattaPalat.todaysData=["क","त"];
	
	setTimeout(function(){
		PattaPalat.init();
	});
},300);

//function to set images
	PattaPalat.setImages=function()
{
	$('#logo').attr("src",Utils.Path+"images/logo.png");
	$('#modalImg').attr("src",Utils.Path+"images/open.png");
	$('#soundImg').attr("src",Utils.Path+"images/Sound.png");
	$('#playImg').attr("src",Utils.Path+"images/Play.png");
	$('#playImg').attr("onclick","PattaPalat.playGame()");
	$('#soundImg').attr("onclick","PattaPalat.playInstruction()");
	$('#soundImg').css('pointer-events','none');
	$("#modalMsg").text(PattaPalat.hardCodedData.correctMsg);
};

PattaPalat.resetData=function()
{
	//PattaPalat.copyJson={}
	PattaPalat.shuffledArray=[];
	PattaPalat.picturesObjects=[];
	PattaPalat.correctCnt=0;
	PattaPalat.currentCard1=-1;
	PattaPalat.totalCharacters=6;
	PattaPalat.playedLetterCnt=0;
	PattaPalat.intervl;
	PattaPalat.totalPlayCnt=0;
	PattaPalat.starCnt=1;
	//PattaPalat.totalCorrectCnt=0;
	PattaPalat.letterSound;
};

function shuffleArray(array)
{
	var i,j,temp;
	for (i = array.length - 1; i > 0; i--) 
	{
	    j = Math.floor(Math.random() * (i + 1));
	    temp = array[i];
	    array[i] = array[j];
	    array[j] = temp;
	}
	return array;
}

PattaPalat.randomGenerator=function(array){
		return (Math.floor(Math.random() * array.length+0));
};

PattaPalat.init=function(){
	$('body').css('backgroundImage','url('+Utils.Path+'images/BG2.jpeg)');
	$('#coverPage').show();
	$('#gamePage,#lastPage,#soundImg').hide();
	PattaPalat.setImages();
};
//this function will execute on click of play button
PattaPalat.playGame=function()
{	
	PattaPalat.resetData();
	$('body').css('backgroundImage','url('+Utils.Path+'images/BG1.jpeg)');
	$("#coverPage").hide();
	$("#gamePage,#soundImg").show();
	//PattaPalat.letters=PattaPalat.todaysData.slice(0);
	//PattaPalat.copyJson=JSON.parse(JSON.stringify(PattaPalat.Data));
//	PattaPalat.createStars();
	PattaPalat.generateCardData();
	PattaPalat.totalCorrectCnt=0;
	PattaPalat.createCards("mainDiv",PattaPalat.totalCharacters,"cardDiv");
};

PattaPalat.createStars=function(){
	var temp,windowImg,windowDiv,i,numberOfStars=2;
	for(i=1;i<=numberOfStars;i++)
	{
		windowDiv= document.createElement("div");
		windowDiv.setAttribute("id","starDiv"+i); 
		windowDiv.setAttribute("class","noPadding col-xs-5 col-md-5 col-lg-5 col-sm-5"); 

		windowImg= document.createElement("img");
		windowImg.setAttribute("id","starImg"+i); 
		windowImg.setAttribute("class","img-responsive"); 
		windowImg.setAttribute("src",Utils.Path+"images/star5.png");
		$(windowDiv).append(windowImg);
		$("#starDiv").append(windowDiv);
	}
};

PattaPalat.giveStars=function(id){
	$("#starImg"+id).prop("src",Utils.Path+"images/star4.png");
};
PattaPalat.generateCardData=function()
{
	var d;
	d=new Date();
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	console.log(startTime);
	//letterIndex=PattaPalat.randomGenerator(PattaPalat.todaysData);
	
	//firstLetter=PattaPalat.todaysData[letterIndex];
	//PattaPalat.letterSound=($.inArray(firstLetter,PattaPalat.Consonants))
	//questionId=PattaPalat.Data[PattaPalat.letterSound].questionId;
	//console.log(questionId);
	$('.windowImg').css('pointer-events','none');
	
	PattaPalat.showPictures();
	//oldLetter=firstLetter;
	PattaPalat.picturesObjects=PattaPalat.fillCharacterArray(PattaPalat.wordsData);
	PattaPalat.shuffledArray=shuffleArray(PattaPalat.picturesObjects);
	PattaPalat.randomIndex = Math.floor(Math.random()*PattaPalat.shuffledArray.length);

	PattaPalat.answer=PattaPalat.shuffledArray[PattaPalat.randomIndex].word;
	//PattaPalat.currentAudio=PattaPalat.shuffledArray[PattaPalat.randomIndex].sound;
	PattaPalat.playInstruction();
	console.log(PattaPalat.shuffledArray,PattaPalat.answer,	PattaPalat.randomIndex );
};

PattaPalat.playInstruction=function()
{
		document.getElementById("playSound").pause();
		PattaPalat.playAudio(PattaPalat.shuffledArray[PattaPalat.randomIndex].sound);
		console.log(PattaPalat.shuffledArray[PattaPalat.randomIndex].sound);
}

// Show Letters After each Question Load
PattaPalat.showPictures=function(){
	$('.windowImg').css('pointer-events','none');
	setTimeout(function(){
		$('.windowImg').attr("src",Utils.Path+"images/open.png");
		$('.pictures').show();	
		setTimeout(function(){
			$('.windowImg').attr("src",Utils.Path+"images/close.png");
			$('.pictures').hide();
			$('.windowImg').css('pointer-events','auto');
			$('#soundImg').css('pointer-events','auto');
		},2500);
	},1000);
};

//Fill characters in array
PattaPalat.fillCharacterArray=function(picturesArray)
{
	var temp=[],randomIndex,length=6;
	while(temp.length !== length) 
	{
		randomIndex = Math.floor(Math.random()*picturesArray.length);
		if(($.inArray(picturesArray[randomIndex],temp) === -1))
			temp.push(picturesArray[randomIndex]);
	}
	return temp;
};

//to create no of cards div 
PattaPalat.createCards=function(parentDiv,numberOfDivs,idName)
{
	var windowDiv,windowImg,picture,i;
	for(i=0;i<numberOfDivs;i++)
	{
		windowDiv= document.createElement("div");
		windowDiv.setAttribute("id",""+idName+''+i); 
		windowDiv.setAttribute("class","noPadding cards col-xs-3 col-md-3 col-lg-3 col-sm-3 col-xs-offset-1 col-sm-offset-1 col-md-offset-1 col-lg-offset-1"); 
		$(windowDiv).data("cardClick",0);
		
		windowImg= document.createElement("img");
		windowImg.setAttribute("id","cardImg"+i); 
		windowImg.setAttribute("class","img-responsive windowImg noPadding col-lg-12 col-md-12 col-xs-12 col-sm-12"); 
		windowImg.setAttribute("onclick","PattaPalat.cardClick(this)");
		windowImg.setAttribute("src", Utils.Path+"images/close.png");
		$(windowImg).data("correctAns",PattaPalat.shuffledArray[i].word);

		picture= document.createElement("img");
		picture.setAttribute("id","pictureImg"+i);
		picture.setAttribute("class","pictures img-responsive noPadding col-lg-5 col-md-5 col-xs-5 col-sm-5");
		$(picture).prop("src",Utils.Path+"images/"+PattaPalat.shuffledArray[i].img);
		
		$(windowImg).show();
		$(picture).hide();	
		$(windowDiv).append(windowImg);
		$(windowDiv).append(picture);
		$("#"+parentDiv).append(windowDiv);
	}
};

PattaPalat.cardClick=function(obj)
{
	var id,card1,card2;
	id=$(obj).attr("id");
	id=parseInt(id.substring(7));
	if(PattaPalat.currentCard1<0)
	{	
		card1=obj;
		PattaPalat.currentCard1=id;
		document.getElementById("playSound").pause();
		document.getElementById('playSound').src=Utils.Path+"sounds/"+PattaPalat.hardCodedData.openWindow;
		document.getElementById("playSound").play();	
		$("#cardImg"+PattaPalat.currentCard1).css('pointer-events','none');
	}
/*	if(id!=PattaPalat.currentCard1 && PattaPalat.currentCard1!=-1)
	{
		card2=obj;
		PattaPalat.currentCard2=id;
		//PattaPalat.playAudio(PattaPalat.hardCodedData.openWindow);
		document.getElementById("playSound").pause();
		document.getElementById('playSound').src=Utils.Path+"sounds/"+PattaPalat.hardCodedData.openWindow;
		document.getElementById("playSound").play();	
		$("#cardImg"+PattaPalat.currentCard2).css('pointer-events','none');
	}*/
	$("#cardImg"+id).addClass("animated flipInY");
	$("#cardImg"+id).attr("src",Utils.Path+"images/open.png");
	
	setTimeout(function()
	{
		$("#cardImg"+id).removeClass("animated flipInY");
		$("#pictureImg"+id).show();
	},200);

	if(PattaPalat.currentCard1>=0)
	{
		$(".windowImg").css('pointer-events','none');
		$("#soundImg").css('pointer-events','none');
		setTimeout(function(){
			PattaPalat.validate(PattaPalat.currentCard1);
			//PattaPalat.shuffledArray.splice(PattaPalat.randomIndex,1);
			setTimeout(function(){
				PattaPalat.generateCardData();
			},1300);
		},2000);
	}
};

PattaPalat.validate=function(card1){
	clickedCard1Data=$("#cardImg"+card1).data("correctAns");
	console.log(clickedCard1Data,PattaPalat.answer);
	//clickedCard2Data=$("#letterSpan"+card2).text();
	if(clickedCard1Data===PattaPalat.answer)
	{
		scoredMarks=1;
		PattaPalat.totalPlayCnt++;
		PattaPalat.totalCorrectCnt++;
		if(Utils.mobileDeviceFlag)
			Android.addScore(resId,questionId,scoredMarks,totalMark,1,startTime);
		console.log(questionId,scoredMarks,totalMark,startTime);
		
		PattaPalat.answerAudio=setTimeout(function(){
			PattaPalat.playAudio(PattaPalat.hardCodedData.correctAudio);
			$("#soundImg").css('pointer-events','auto');
			$("#responseBoxDiv").show();
		},300);
		setTimeout(function()
		{
			//PattaPalat.playInstruction();
			PattaPalat.afterBothCardShowHideCards();
			PattaPalat.correctCnt++;
			$("#responseBoxDiv").hide();
			PattaPalat.setDataAfterCorrect();
		},1800);	
	}
	else
	{
		scoredMarks=0;PattaPalat.totalPlayCnt++;
		PattaPalat.correctCnt=0;
		if(Utils.mobileDeviceFlag)
			Android.addScore(resId,questionId,scoredMarks,totalMark,1,startTime);
		console.log(questionId,scoredMarks,totalMark,startTime);
		
		PattaPalat.answerAudio=PattaPalat.playAudio(PattaPalat.hardCodedData.wrongAudio);
		
		setTimeout(function(){
			PattaPalat.afterBothCardShowHideCards();
			PattaPalat.setDataAfterCorrect();
		},2000);
	}

};
PattaPalat.playAudio=function(audio){
	PattaPalat.answerAudio=setTimeout(function(){
			document.getElementById('playSound').src=Utils.Path+"sounds/"+audio;
			document.getElementById("playSound").play();	
	},250);
};
PattaPalat.afterBothCardShowHideCards=function(){
	$("#pictureImg"+PattaPalat.currentCard1).hide();
	$("#cardImg"+PattaPalat.currentCard1).attr("src",Utils.Path+"images/close.png");
	/*$("#pictureImg"+PattaPalat.currentCard2).hide();
	$("#cardImg"+PattaPalat.currentCard2).attr("src",Utils.Path+"images/close.png");*/
	$(".windowImg").css( 'pointer-events', 'auto');
	PattaPalat.currentCard1=-1;
}

PattaPalat.setDataAfterCorrect=function()
{
	var length;
	if(PattaPalat.correctCnt==3||PattaPalat.totalPlayCnt==9)	
	{	
		PattaPalat.playedLetterCnt++;
		if(PattaPalat.correctCnt==3)
		{	//PattaPalat.giveStars(PattaPalat.starCnt);
			PattaPalat.starCnt++;
		}
		if(Utils.mobileDeviceFlag)	
			Android.StudentLevelDetection(PattaPalat.totalCorrectCnt,PattaPalat.totalPlayCnt);
			
		console.log("Student Detection ("+PattaPalat.totalCorrectCnt+" "+PattaPalat.totalPlayCnt+")");
		PattaPalat.correctCnt=0;
		PattaPalat.totalPlayCnt=0;
		
		if(PattaPalat.playedLetterCnt==2)	
		{
			$("#lastPage").hide();
			$("#gamePage").hide();
			$("#soundImg").hide();	
			document.getElementById('playSound').src='';
			setTimeout(function(){
				$("#responseBoxDiv").hide();
			},300);
			$('#lastPage').show();
			$("#lastMsg").text(PattaPalat.hardCodedData.endMsg);
		}
		
	}
	PattaPalat.showPictures();
	if(PattaPalat.playedLetterCnt<=2)	
	{
		//PattaPalat.shuffledArray=shuffleArray(PattaPalat.picturesObjects);
		length=PattaPalat.shuffledArray.length;
		for(var i=0;i<length;i++)	
		{
			$("#pictureImg"+i).prop("src",Utils.Path+"images/"+PattaPalat.shuffledArray[i].img);
			$("#cardImg"+i).data("correctAns",PattaPalat.shuffledArray[i].word);
			
		}
	}		
};