var AksharKhadi={};
var Utils={};
Utils.Path='';
Utils.mobileDeviceFlag=false;
var questionId=0,startTime=0,scoredMarks=0,totalMarks=50,resId;
AksharKhadi.queCnt=0;
AksharKhadi.mainArray=[];
AksharKhadi.mainQueArray=[];
AksharKhadi.nextGrid=0;
AksharKhadi.nextSound=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("Aksharkhadi"); 
	setTimeout(function(){
		AksharKhadi.setAllImages();
		AksharKhadi.init();
	},200);
});

AksharKhadi.setAllImages=function()
{
	$('#playGame').prop('src',Utils.Path+'Images/Aksharkhadi_Play.png');
	$('#home').prop('src',Utils.Path+'Images/Aksharkhadi_Home.png');
	$('#speaker').prop('src',Utils.Path+'Images/Aksharkhadi_Sound.png');
	$('#next').prop('src',Utils.Path+'Images/Aksharkhadi_Next.png');
	$('#logo').prop('src',Utils.Path+'Images/logo.png');
	$('#wrongimg1').prop('src',Utils.Path+'Images/Wrong_02.png');
	$('#wrongimg2').prop('src',Utils.Path+'Images/Wrong_01.png');
	$('#wrongImg').prop('src',Utils.Path+'Images/joker.png');
	$('#playAgain').prop('src',Utils.Path+'Images/Aksharkhadi_Play-Again.png');
};

AksharKhadi.init=function(){
	
	$('#playGame').text(AksharKhadi.hardCodeddata.play);
	$('#Flip,#title').hide();
	$('#coverPage').show();
	$('#background').css({'backgroundImage':'url('+Utils.Path+'Images/'+AksharKhadi.hardCodeddata['coverPage']+')'});
};

AksharKhadi.getGameDataAfterTimeOut=function()
{
	document.getElementById('playSound').src=Utils.Path+'sounds/ting.mp3';
	document.getElementById('playSound').play();
	AksharKhadi.getGameData();
	setTimeout(function(){
		AksharKhadi.showQuestions();
	},1000);
}

AksharKhadi.getGameData=function()
{
	AksharKhadi.gameOpened=false;
	scoredMarks=0;
	$('#background').css({'backgroundImage':'url('+Utils.Path+'Images/'+AksharKhadi.hardCodeddata['BG']+')'});
	$('#Flip').show();
	$('#coverPage').hide();
	$('#next,#homeDiv,#home,#speaker').show();
	$('#wrongimg1,#wrongimg2').hide();
	var randomIndex=0,tempArray=[];
	AksharKhadi.counter=0,AksharKhadi.correctAnswerCount=0;

	$('.slide').addClass('clickable');
	$('.optionForFlip').css({'display':'block'});
	$('.clickable').css({'pointer-events': 'none'});
	$('.defaultImage').removeClass('flipIt');
	$('#lastGamePage,#divContainingFullImage').hide();
	$('#soundDiv,#homeDiv,#divContainingSplitImages').fadeIn('slow');
for(var i=0;i<5;i++)
{
	AksharKhadi.arrOfSelectedWords=[];
	AksharKhadi.fiveQueArray=[];
	AksharKhadi.arrOfSelectedWords=populate(AksharKhadi.wordsData,AksharKhadi.arrOfSelectedWords, 9);
	AksharKhadi.fiveQueArray=populate(AksharKhadi.arrOfSelectedWords,AksharKhadi.fiveQueArray, 5);
	AksharKhadi.arrOfSelectedWords=shuffleArray(AksharKhadi.arrOfSelectedWords);
	AksharKhadi.tempArray=AksharKhadi.arrOfSelectedWords.slice();
	AksharKhadi.mainArray[i]=AksharKhadi.arrOfSelectedWords;
	AksharKhadi.mainQueArray[i]=AksharKhadi.fiveQueArray;
}	
console.log(AksharKhadi.mainArray);
console.log(AksharKhadi.mainQueArray);
	AksharKhadi.fillGrid();
}

function populate(array1, array2, length)
{
	var randomIndex;
	while (array2.length !== length) 
	{
		randomIndex = Math.floor(Math.random()*array1.length);
		if($.inArray(array1[randomIndex],array2) === -1)
		array2.push(array1[randomIndex]);
	}
	return array2;
}

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;
}

AksharKhadi.playSoundAgain=function()
{
	document.getElementById('playSound').play();
};

AksharKhadi.showQuestions=function(tempArray)
{
	
	//AksharKhadi.randomQuestion=Math.floor(Math.random()*AksharKhadi.tempArray.length);
	document.getElementById('playSound').src=Utils.Path+'sounds/'+AksharKhadi.mainQueArray[AksharKhadi.nextGrid][AksharKhadi.nextSound]['sound'];
	$('#title').data('question',AksharKhadi.mainQueArray[AksharKhadi.nextGrid][AksharKhadi.nextSound]['word'].trim());
	questionId=AksharKhadi.mainQueArray[AksharKhadi.nextGrid][AksharKhadi.nextSound]['questionId'];
	document.getElementById('playSound').play();
	$('.clickable').css({'pointer-events': 'auto'});
}

AksharKhadi.fillGrid=function()
{
	var i=1,imagePath;
	AksharKhadi.randomQuestion=0;
	//AksharKhadi.randomImg=Math.floor(Math.random()*AksharKhadi.imageSet.length);
	var d=new Date();
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	for(var j=1;j<4;j++)
	{
		for(var k=0;k<3;k++)
		{
			$('#imgDiv'+(i-1)).data('answer',AksharKhadi.mainArray[AksharKhadi.nextGrid][AksharKhadi.randomQuestion]['word'].trim());
			//imagePath=Utils.Path+''+AksharKhadi.imageSet[AksharKhadi.randomImg]['img'];
			/* $('#imgDiv'+(i-1)).data('backImg',imagePath+''+i+''+AksharKhadi.imageSet[AksharKhadi.randomImg]['type']); */
			$('#imgDiv'+(i-1)).data('frontImg',Utils.Path+''+AksharKhadi.frontimageSet['img']+''+i+''+AksharKhadi.frontimageSet['type']);
			
				$('#option'+(i-1)).text(AksharKhadi.mainArray[AksharKhadi.nextGrid][AksharKhadi.randomQuestion]['word'].trim());
				$('#img'+j+'_'+k).hide();

			i++;
			AksharKhadi.randomQuestion++;
		//	AksharKhadi.mainArray[i].splice(AksharKhadi.randomQuestion,1);
		}
	}	
}

AksharKhadi.checkAns=function(evt)
{
	var source,frontImg;
	AksharKhadi.gameOpened=true;
	AksharKhadi.counter++;
	AksharKhadi.nextSound++;
	$('.clickable').css({'pointer-events': 'none'});
	wrongImgArray=['backWrong1.png','backWrong2.png'];
	if($('#'+evt).data('answer')==$('#title').data('question'))
	{
		AksharKhadi.correctAnswerCount++;
		$('#'+evt).find('img').show();
		$('#'+evt).find('p').hide();
		$('#'+evt).removeClass('clickable');
		source=$('#'+evt).data('backImg');
		frontImg=$('#'+evt).data('frontImg');
		$('#'+evt).find('img').prop('src',frontImg);
		document.getElementById('playSound').src=Utils.Path+'sounds/CorrectAksharkhadi.mp3';
		document.getElementById('playSound').play();
		scoredMarks+=10;
	}	
	else
	{
		scoredMarks+=0;
		$('#'+evt).addClass("shake");
	//	randomImg=Math.floor(Math.random()*wrongImgArray.length);
		$('#wrongimg1,#wrongimg2').show();
		document.getElementById('playSound').src=Utils.Path+'sounds/wrongSoundAksharkhadi.mp3';
		document.getElementById('playSound').play();
		for(var i=0;i<9;i++)
		{
			if($('#title').data('question')==$('#imgDiv'+i).data('answer'))
			{
				var offsetOfImage=$('#imgDiv'+i).offset();
				break;
			}
		}
	//	$('#imgDiv'+i).find('img').prop('src',Utils.Path+'Images/'+wrongImgArray[randomImg]);
		$('#imgDiv'+i).find('img').show();
		$('#imgDiv'+i).find('p').hide();
	}
	AksharKhadi.tempArray.splice(AksharKhadi.randomQuestion,1);
	if(AksharKhadi.nextQuestionTimer)
	{
				clearTimeout(AksharKhadi.nextQuestionTimer);
				AksharKhadi.nextQuestionTimer = null;
	}
	AksharKhadi.nextQuestionTimer=setTimeout(function()
	{
		if(AksharKhadi.counter<5)
		{	
			AksharKhadi.showQuestions();
			$('.clickable').css({'pointer-events': 'auto'});
		}
		else
		{
			//$("#next").addClass("blink_me");
			$('.clickable').css({'pointer-events': 'none'});
			console.log(resId,questionId,scoredMarks,totalMarks,1,startTime);
			if(Utils.mobileDeviceFlag)
				Android.addScore(resId,questionId,scoredMarks,totalMarks,1,startTime);
			AksharKhadi.gameOpened=false;
		}
	},2000);	
}

AksharKhadi.showNextQuestion=function()
{
	clearTimeout(AksharKhadi.nextQuestionTimer);
	
	AksharKhadi.nextSound=0;
	AksharKhadi.queCnt++;
	AksharKhadi.nextGrid++;
	if(AksharKhadi.queCnt==5)
	{
		alert("game over");
	}
	else
	{
		if(AksharKhadi.gameOpened==true)
		{
			console.log(resId,questionId,scoredMarks,totalMarks,1,startTime);
			if(Utils.mobileDeviceFlag)
				Android.addScore(resId,questionId,scoredMarks,totalMarks,1,startTime);
		}
		AksharKhadi.fillGrid();
			AksharKhadi.showQuestions();
	}
};

AksharKhadi.callHome=function()
{
	clearTimeout(AksharKhadi.nextQuestionTimer);
	if(AksharKhadi.gameOpened==true)
	{
		console.log(resId,questionId,scoredMarks,totalMarks,1,startTime);
		if(Utils.mobileDeviceFlag)
			Android.addScore(resId,questionId,scoredMarks,totalMarks,1,startTime);
	}
	setTimeout(function(){
		location.reload(true);
	},200);
}