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

ABCD.init=function()
{
	document.body.style.backgroundImage="url("+Utils.Path+"images/Bg.png)";
	$("#coverPage").show();
	$("#gamePage").hide();
	$(".header").not("#logo").css("visibility","hidden");
	ABCD.initializeVariables();
	ABCD.setImages();
	ABCD.createDivs();
	//ABCD.createABCDDivs();	

}
//Initialization of variables.
ABCD.initializeVariables=function()
{
	ABCD.alphabetArr=[];
	ABCD.questionCounter=0;
	ABCD.loadmetaDataFlag=false;
	ABCD.playWordSoundFlag=true;
	ABCD.lampFlag=false;
	ABCD.correctCounter=0;
	ABCD.attemptCounter=0;
	ABCD.score=0;
}
//Sets the images to all buttons and image tags.
ABCD.setImages=function()
{
	document.getElementById('play').src=Utils.Path+"images/playButton.png";
	document.getElementById('homeImage').src=Utils.Path+"images/Home.png";
	document.getElementById('logoImage').src=Utils.Path+"images/logo.png";
	document.getElementById("soundImg").src=Utils.Path+"images/Sound.png";
	document.getElementById('nextImg').src=Utils.Path+"images/Next.png";
	document.getElementById("score").innerHTML=ABCD.hardCodedData.score+ABCD.score;
}

ABCD.playGame=function()
{
	//document.body.style.backgroundColor="#387DED";
	$("#coverPage").hide();
	$("#gamePage").show();
	$(".header").css("visibility","visible");
	ABCD.setOptions();
	ABCD.setQuestion();
}
ABCD.createDivs=function()
{
	var i,div,img,parentDiv,para;
	parentDiv=document.getElementById("optionDiv");
	for(i=1;i<=9;i++)
	{
		div=document.createElement("DIV");
		div.setAttribute("id","optionDiv"+i);
		div.setAttribute("class","options nopadding col-xs-4 col-md-4 col-lg-4 col-sm-4");
		div.setAttribute("onclick","ABCD.checkAnswer(this.id);");
		
		img=document.createElement("IMG");
		img.setAttribute("id","image"+i);
		img.setAttribute("class","img-responsive nopadding col-xs-12 col-md-12 col-lg-12 col-sm-12 images");
		
		
		para=document.createElement("P");
		para.setAttribute("id","para"+i);
		para.setAttribute("class","letters");
		div.appendChild(img);
		div.appendChild(para);
		parentDiv.appendChild(div);
	}
}
ABCD.setOptions=function()
{
	var i,j,k,randomLetter,rNo,imgArr=[],randomArr=[],randomIndex,lampCounter=0;
	for(i=0;i<4;i++)
	{
		randomLetter=Math.floor(Math.random()*ABCD.alphabets.length);
		if(ABCD.alphabetArr.includes(randomLetter))
		{
			i--;
			continue;
		}
		else
		{
			ABCD.alphabetArr.push(randomLetter);
		}
	}
	console.log(ABCD.alphabetArr);
	for(j=0;j<ABCD.alphabetArr.length;j++)
	{
		imgArr=[];
		for(k=0;k<2;k++)
		{
			randomImg=Math.floor(Math.random()*3);
			rNo=Math.floor(Math.random()*9+1);
			if(imgArr.includes(randomImg)||randomArr.includes(rNo))
			{
				k--;
				continue;
			}
			else
			{
				imgArr.push(randomImg);
				randomArr.push(rNo);	
				console.log(randomArr);
				
				document.getElementById("image"+rNo).src=Utils.Path+"images/"+ABCD.alphabets[ABCD.alphabetArr[j]].image[randomImg];
				document.getElementById("optionDiv"+rNo).setAttribute("letter",ABCD.alphabets[ABCD.alphabetArr[j]].letter);
				document.getElementById("optionDiv"+rNo).setAttribute("sound",ABCD.alphabets[ABCD.alphabetArr[j]].sound);
				document.getElementById("optionDiv"+rNo).setAttribute("position","unclicked");
				document.getElementById("optionDiv"+rNo).setAttribute("flag",0);
				document.getElementById("para"+rNo).innerHTML=document.getElementById("optionDiv"+rNo).getAttribute("letter");
				if(k==1)
				{
					lampCounter++;
					if(lampCounter<3)
					{
						document.getElementById("optionDiv"+rNo).setAttribute("flag",1);
						//img=document.createElement("IMG");
						//img.setAttribute("id","lamp"+lampCounter);
						//img.setAttribute("class","img-responsive col-xs-12 col-md-12 col-lg-12 col-sm-12");
						//img.setAttribute("src",Utils.Path+"images/lamp.gif");
						//img.style.display="none";
						//document.getElementById("optionDiv"+rNo).appendChild(img);
					}
				}
			}
		}	
			
	}
	while(1)
	{
		rNo=Math.floor(Math.random()*9+1);
		if(!(randomArr.includes(rNo)))
		break;
	}
	console.log(rNo);
	
	while(1)
	{
		randomIndex=Math.floor(Math.random()*ABCD.alphabets.length);
		if(!(ABCD.alphabetArr.includes(randomIndex)))
		break;
	}
	console.log(randomIndex);
	
	document.getElementById("image"+rNo).src=Utils.Path+"images/"+ABCD.alphabets[randomIndex].image[0];
	document.getElementById("optionDiv"+rNo).setAttribute("letter",ABCD.alphabets[randomIndex].letter);
	document.getElementById("optionDiv"+rNo).setAttribute("sound",ABCD.alphabets[randomIndex].sound);
	document.getElementById("optionDiv"+rNo).setAttribute("position","unclicked");
	document.getElementById("para"+rNo).innerHTML=document.getElementById("optionDiv"+rNo).getAttribute("letter");
	
}

ABCD.setQuestion=function()
{
	ABCD.questionCounter++;
	if(ABCD.questionCounter>4)
	{
		ABCD.questionCounter=0;
		
	}
	console.log(ABCD.alphabetArr);
	document.getElementById("questionDiv").innerHTML=ABCD.alphabets[ABCD.alphabetArr[ABCD.questionCounter-1]].letter;
	document.getElementById("playSound").src=Utils.Path+"sounds/"+ABCD.alphabets[ABCD.alphabetArr[ABCD.questionCounter-1]].sound;
	document.getElementById("playSound").play();
	
	
}

ABCD.showNextQuestion=function()
{
	document.getElementById("playSound").pause();
	ABCD.initializeVariables();
	ABCD.setOptions();
	ABCD.setQuestion();
}
ABCD.playSound=function()
{
	document.getElementById("playSound").src=Utils.Path+"sounds/"+ABCD.alphabets[ABCD.alphabetArr[ABCD.questionCounter-1]].sound;
	document.getElementById("playSound").play();
}

ABCD.checkAnswer=function(div)
{
	
	var letter,sound,random,parentDiv;
	random=Math.floor(Math.random()*8)+1;
	parentDiv=document.getElementById(div);
	letter=parentDiv.getAttribute("letter");
	if(letter==questionDiv.innerHTML)
	{
		ABCD.attemptCounter++;
		document.getElementById("playSound").src=Utils.Path+"sounds/welldone.mp3";
		if(parentDiv.getAttribute("position")=="unclicked")
		{
			if(parentDiv.getAttribute("flag")==1)
			{
				ABCD.lampFlag=true;
				ABCD.imgSrc=parentDiv.children[0].src;
				parentDiv.children[0].src=Utils.Path+"images/lamp.gif";
				//parentDiv.children[0].style.display="block"; 
				ABCD.score+=50;
			}	
			else
				ABCD.score+=10;
			ABCD.correctCounter+=1;
			document.getElementById("score").innerHTML=ABCD.hardCodedData.score+ABCD.score;
			parentDiv.setAttribute("position","clicked");	
			
		}
	}
	else
		document.getElementById("playSound").src=Utils.Path+"sounds/wrong.mp3";
	sound=document.getElementById("playSound").src;
	ABCD.loadmetaDataFlag=true;
	ABCD.getDuration(sound,div);	
	
}

ABCD.playAnimation=function(div)
{
	var source,arr=[];
	$("#"+div).removeClass('animated fadeIn');
	$("#"+div).addClass('animated flipInY');
	$('#'+div).children('img').css("display","none");
	$("#"+div).children('p').css("display","block");
	setTimeout(function()
	{
		$("#"+div).removeClass('animated flipInY');
		$("#"+div).addClass('animated fadeIn');
		$('#'+div).children('img').css("display","block");
		$("#"+div).children('p').css("display","none");
		if(ABCD.lampFlag)
		{
			document.getElementById(div).children[0].src=ABCD.imgSrc;
			ABCD.lampFlag=false;
		}
	},1000);	
	console.log(ABCD.correctCounter);
	if(ABCD.attemptCounter==8)
		setTimeout(function()
		{
			ABCD.showNextQuestion();
		},3000);
}


ABCD.getDuration=function(sound,div)
{
	var duration=0;
	$('#playSound').prop('src',sound);
	if(ABCD.loadmetaDataFlag)
	{
		$('#playSound').on("loadedmetadata", function(){
			ABCD.loadmetaDataFlag=false;
			$('#playSound').unbind("loadedmetadata");
			duration=$('#playSound').prop('duration');
			ABCD.playWordSound(duration,div);
			}	
		);
    }
    else
		$('#playSound').unbind("loadedmetadata");
}

ABCD.playWordSound=function(duration,div)
{
	var sound;
	document.getElementById("playSound").play();
	if(ABCD.playWordTime)
	{
		clearTimeout(ABCD.playWordTime);
		ABCD.playWordTime=null;
	}
	ABCD.playWordTime=setTimeout(function()
	{	
		if(ABCD.playWordSoundFlag)
		{
			ABCD.playWordSoundFlag=false;
			document.getElementById("playSound").src=Utils.Path+"sounds/It_is.mp3";
			sound=document.getElementById("playSound").src;
			console.log(sound);
			ABCD.loadmetaDataFlag=true;
			ABCD.getDuration(sound,div);
		}
		else
		{
			document.getElementById("playSound").src=Utils.Path+"sounds/"+document.getElementById(div).getAttribute("sound");
			document.getElementById("playSound").play(); 
			ABCD.playWordSoundFlag=true;
			ABCD.playAnimation(div);
			if(ABCD.correctCounter==2)
			{
				setTimeout(function()
				{
					ABCD.correctCounter=0;
					ABCD.setQuestion();
				},2000);
			}
		}
		
	},duration*1050);
}
ABCD.callhome=function()
{
	location.reload(true);
}
