var Makeaword={};
var Utils={};
Utils.path='';
var questionId=0,startTime=0,scoredMarks=0,totalMarks=0,resId=0,mobileDeviceFlag=false;
var scoreFlag=false;
Makeaword.globalVariables=function()
{
Makeaword.selectRandomLetter=[];
Makeaword.selectRandomWord=[];
Makeaword.randomNumber;  //generate random number
Makeaword.selectWord=0; //word selected from the json
Makeaword.letter;    //used to store selected letter
Makeaword.level;
Makeaword.seconds=0;
Makeaword.correctCounter=0;
Makeaword.AnswerTheQue;
Makeaword.vowel=["a","e","i","o","u"];
}
$(document).ready(function()
{
	if(navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i) && !(window.location.href.includes('http')))
		Utils.mobileDeviceFlag=true;
	$(".se-pre-con").fadeOut("slow");
	setTimeout(function()
	{
		Makeaword.setimage();
	},100);
},200);
//set the all images
Makeaword.setimage=function()
{
	document.body.style.backgroundImage=Utils.path+"url('images/Make-A-Word_Cover.png')";
	document.getElementById("playbtnid").src=Utils.path+"images/Play.png";
	document.getElementById("logoid").src=Utils.path+"images/logo.png";
	document.getElementById("skip").src=Utils.path+"images/Skip.png";
	document.getElementById("soundbtn").src=Utils.path+"images/Sound.png"; 
	document.getElementById("ansdiv").src=Utils.path+"images/Snowman.png";
}
Makeaword.getLevelId=function()
{
	document.body.style.backgroundImage=Utils.path+"url('images/BG.png')";
	document.getElementById("coverpage").style.display="none";
	Makeaword.level=Math.floor(Math.random()*2)+1;
	Makeaword.gamePage();
}
Makeaword.gamePage=function()
{
	Makeaword.globalVariables();
	document.getElementById('alphabets').innerHTML="";
	document.getElementById('word').innerHTML="";
	clearInterval(Makeaword.id);
	document.getElementById("coverpage").style.display="none";
	document.getElementById("endpage").style.display="none";
	document.getElementById("gamepage").style.display="block";
	Makeaword.populate();
}
//select seven random words from json
Makeaword.populate=function()
{
	for(var j=0;j<16;j++)
	{	
		Makeaword.randomNumber=Math.floor(Math.random()*Makeaword.alphabets.length);
		Makeaword.selectRandomLetter.push(Makeaword.alphabets[Makeaword.randomNumber]);
	}
	for(var j=0;j<5;j++)
	{	
		var randomNumber=Math.floor(Math.random()*Makeaword.data.length);
		while(Makeaword.selectRandomWord.includes(Makeaword.data[randomNumber]))
			{
			 randomNumber=Math.floor(Math.random()*Makeaword.data.length);
			}
		Makeaword.selectRandomWord.push(Makeaword.data[randomNumber]);
	}
	Makeaword.queCnt= Makeaword.selectRandomWord.length;
	Makeaword.AcceptWord();	
}
//create maxim span 
Makeaword.AcceptWord=function()
{	
	Makeaword.lettersArray=[];
	Makeaword.wordsarray=[];
	//console.log(Makeaword.selectWord);
	var singleWord=Makeaword.selectRandomWord[Makeaword.selectWord].word; //select word from the json
    var alphabet=singleWord.split("");  //split word and store alphabet 
		for(var j=0;j<alphabet.length;j++)
		{
			Makeaword.wordsarray.push(alphabet[j]);
			for(var i=0;i<Makeaword.alphabets.length;i++)
			{
				var el=Makeaword.alphabets[i].word;
				if(alphabet[j]==el)
				{
					if(!Makeaword.vowel.includes(alphabet[j]))
					{
					Makeaword.lettersArray.push(Makeaword.alphabets[i]);
					}
				}
			}
		}
		Makeaword.displayWord();
		var alphabet;
		for(var j=Makeaword.lettersArray.length;j<12;)
		{
			alphabet =Math.floor(Math.random()*Makeaword.alphabets.length);
			while(Makeaword.lettersArray.includes(Makeaword.alphabets[alphabet]))
				{
				 alphabet=Math.floor(Math.random()*Makeaword.alphabets.length);
				}
				var letter=Makeaword.alphabets[alphabet].word;
				if(!Makeaword.vowel.includes(letter))
				{
					Makeaword.lettersArray.push(Makeaword.alphabets[alphabet]);
					j++;
				}
		}
		Makeaword.shuffle(Makeaword.lettersArray);	
		Makeaword.dynamicLetterDiv(); 
}
//create word div
Makeaword.displayWord=function()
{
	scoreFlag=false;
	d=new Date();  //date object
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();  //to get the current date
	Makeaword.vowelCount=0;
	for(var j=0;j<Makeaword.wordsarray.length;j++)
	{
		wordsdivision = document.createElement("div");
		wordsdivision.setAttribute("id","wordsdiv"+(j+1));
		wordsdivision.setAttribute("class"," nopadding words text-center col-sm-1 col-lg-1 col-md-1 col-xs-1 ");
		document.getElementById('word').appendChild(wordsdivision);
		wordsdivision.style.backgroundColor = "white";
		var word=Makeaword.wordsarray[j];
		wordsdivision.setAttribute("onclick","Makeaword.SetData(this)");
		wordsdivision.setAttribute("worddata",word);
		$("#wordsdiv"+(j+1)).addClass('animated bounceIn');
		if(Makeaword.level==1)
		{
			if(Makeaword.vowel.includes(Makeaword.wordsarray[j]))
			{
				wordsdivision.innerHTML=Makeaword.wordsarray[j];
				wordsdivision.style.pointerEvents="none";				
			}else{totalMarks++;}
		}
		else
		{
			if(!Makeaword.vowel.includes(Makeaword.wordsarray[j]))
			{
				wordsdivision.innerHTML=Makeaword.wordsarray[j];
				wordsdivision.style.pointerEvents="none";				
			}else{totalMarks++;}
		}
	}
	var buttonDiv= document.createElement("div");
	buttonDiv.setAttribute("id","checkid");
	buttonDiv.setAttribute("class"," nopadding");
	document.getElementById('word').appendChild(buttonDiv);
		
	var img=document.createElement("img");
	img.setAttribute("id","chkbtn");
	img.setAttribute("class","nopadding img-responsive col-lg-12 col-md-12 col-sm-12 col-xs-12");
	img.setAttribute("onclick","Makeaword.checkAnswer()");
	document.getElementById('checkid').appendChild(img);
	document.getElementById("chkbtn").src=Utils.path+"images/Check.png";	
}
Makeaword.startTimer=function()
{
	clearTimeout(Makeaword.soundTimer);
	if(Makeaword.noResponseTimer)
	{
		clearInterval(Makeaword.noResponseTimer);
		Makeaword.noResponseTimer=null;
		Makeaword.seconds=0;
	}	
	Makeaword.noResponseTimer=setInterval(function() 
	{
		Makeaword.seconds++;
		if(Makeaword.seconds>=7)
		{
		clearTimeout(Makeaword.soundTimer);
		Makeaword.soundTimer=null;
		document.getElementById("audio").src=Utils.path+'sounds/instruction/please_ans_the_ques.mp3';
		document.getElementById("audio").play();  
		clearInterval(Makeaword.noResponseTimer);
		if(Makeaword.timeOut)
		{
		clearTimeout(Makeaword.timeOut);
		Makeaword.timeOut=null;
		}
		Makeaword.timeOut=setTimeout(function()
		{
		Makeaword.seconds=0;
		Makeaword.startTimer();
		},2000);
		}
	},1000);
} 

//suffle the array
Makeaword.shuffle=function(lettersArray)
{
	for( var i=0;i<Makeaword.lettersArray.length;i++)
	{
		var j=Math.floor(Math.random() * (lettersArray.length-1));
		var temp=Makeaword.lettersArray[i];
		Makeaword.lettersArray[i]=Makeaword.lettersArray[j];
		Makeaword.lettersArray[j]=temp;
	}
	return lettersArray;
}
//create a letter div
Makeaword.dynamicLetterDiv=function() 
{
	if(Makeaword.level==1)
	{
		for(var i=0;i<Makeaword.lettersArray.length;i++)
		{
			var division = document.createElement("div");
			division.setAttribute("id","div"+(i+1));
			division.setAttribute("class"," nopadding letters flag text-center col-sm-2 col-lg-2 col-md-2 col-xs-2 col-sm-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-offset-1 ");
			document.getElementById('alphabets').appendChild(division);
			division.style.backgroundColor = "white";
			$("#div"+(i+1)).addClass('animated bounceIn');
		}
	}
	else
	{
		for(var i=0;i<Makeaword.vowel.length;i++)
		{
			var division = document.createElement("div");
			division.setAttribute("id","div"+(i+1));
			division.setAttribute("class"," nopadding letters flag text-center col-sm-5 col-lg-5 col-md-5 col-xs-5 col-sm-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-offset-1 ");
			document.getElementById('alphabets').appendChild(division);
			division.style.backgroundColor = "white";
			$("#div"+(i+1)).addClass('animated bounceIn');
		}
	}
	var singleMarathiWord=Makeaword.selectRandomWord[Makeaword.selectWord].langWord;
	questionId=Makeaword.selectRandomWord[Makeaword.selectWord].questionId;
	document.getElementById("marathiWord").innerHTML=singleMarathiWord;
	document.getElementById("audio").src=Utils.path+'sounds/'+Makeaword.selectRandomWord[Makeaword.selectWord].sound;
	document.getElementById("audio").play();
	Makeaword.displayLetter();
}
//to dislay letters
Makeaword.displayLetter=function()
{
	Makeaword.startTimer();
	if(Makeaword.level==1)
	{
		for(var i=0;i<Makeaword.lettersArray.length;i++)
		{
			var letter=Makeaword.lettersArray[i].word;
			var letterSound=Makeaword.lettersArray[i].sound;
			var subLetter=Makeaword.lettersArray[i].langWord;
			subLetter=subLetter.sub();
			document.getElementById('div'+(i+1)).innerHTML=letter+" "+subLetter;
			document.getElementById('div'+(i+1)).setAttribute("data",letter);
			document.getElementById('div'+(i+1)).setAttribute("soundData",letterSound);
			document.getElementById('div'+(i+1)).setAttribute("onclick","Makeaword.getData(this)");
		}
	}
	else
	{
		var k=1;
		for(var i=0;i<Makeaword.alphabets.length;i++)
		{
			var letter=Makeaword.alphabets[i].word;
			var subLetter=Makeaword.alphabets[i].langWord;
			var letterSound=Makeaword.alphabets[i].sound;
			//subLetter=subLetter.sub();
			if(Makeaword.vowel.includes(letter))
			{
			document.getElementById('div'+(k)).innerHTML=letter;
			document.getElementById('div'+(k)).setAttribute("data",letter);
			document.getElementById('div'+(k)).setAttribute("soundData",letterSound);
			document.getElementById('div'+(k)).setAttribute("onclick","Makeaword.getData(this)");
			k++;
			}
		
		}
	}
}
//play sound
Makeaword.playSound=function(a)
{
	document.getElementById("audio").src=Utils.path+'sounds/'+Makeaword.selectRandomWord[Makeaword.selectWord].sound;
	document.getElementById("audio").play();
	Makeaword.startTimer();
} 
//get selected letter
Makeaword.getData=function(object)
{
	clearInterval(Makeaword.noResponseTimer);
	Makeaword.startTimer();
	Makeaword.letter=document.getElementById(object.id).getAttribute("data");
	if(Makeaword.level==1)
	{
		for(var i=0;i<Makeaword.lettersArray.length;i++)
		{
			document.getElementById('div'+(i+1)).style.backgroundColor="white";
		}
		for(var j=0;j<Makeaword.wordsarray.length;j++)
		{
			document.getElementById('wordsdiv'+(j+1)).style.backgroundColor="white";
		}
	}else{
		for(var j=0;j<Makeaword.wordsarray.length;j++)
		{
			document.getElementById('wordsdiv'+(j+1)).style.backgroundColor="white";
		}
		for(var i=0;i<Makeaword.vowel.length;i++)
		{
		document.getElementById('div'+(i+1)).style.backgroundColor="white";
		}
	}  
	var number=document.getElementById(object.id).getAttribute("soundData");
	document.getElementById("chkbtn").style.pointerEvents = "auto";
	document.getElementById("audio").pause();
    document.getElementById("audio").src=Utils.path+'sounds/'+number;
	document.getElementById("audio").play();
	document.getElementById(object.id).style.backgroundColor="ffff66";
}
//set selected letter
Makeaword.SetData=function(object)
{
	clearInterval(Makeaword.noResponseTimer);
	Makeaword.startTimer();
	if(Makeaword.letter)
	{
	document.getElementById(object.id).style.setProperty("color","");
	document.getElementById(object.id).innerHTML=Makeaword.letter;
	document.getElementById(object.id).style.backgroundColor="ffff66";
	}
}
//check answer 
 Makeaword.checkAnswer=function()
 {
	var counter=0; 
	var count=0;  //used to check the null innerHTML of specific letter
	clearTimeout(Makeaword.noResponseTimer);
	var str=$("#word").children().text();
	var str1=Makeaword.selectRandomWord[Makeaword.selectWord].word; 
	word=Makeaword.selectRandomWord[Makeaword.selectWord].word;
	type="word";
	totalNos=Makeaword.data.length;
	for(var i=0;i<Makeaword.wordsarray.length;i++)
	{
		if(document.getElementById('wordsdiv'+(i+1)).innerHTML=="")
		{
			count++;
		}
	}
	if(count==0)
	{
			Makeaword.selectRandomWord.splice(Makeaword.selectWord,1);
			document.getElementById('ansdiv').style.visibility='visible';
			document.getElementById("chkbtn").style.pointerEvents = "none";
			document.getElementById("soundbtn").style.pointerEvents = "none";
			$("#alphabets").css('pointer-events', 'none');
			$("#word").css('pointer-events', 'none');
			if(str==str1)
			{
				scoreFlag=true;
				console.log(word,totalNos,type);
				if(Utils.mobileDeviceFlag)
				{
				Android.addLearntData(word,totalNos,type); 
				} 
				document.getElementById("ansdiv").src=Utils.path+"images/Snowman-Happy.GIF";
				document.getElementById("audio").src=Utils.path+'sounds/instruction/Brilent.mp3';
				document.getElementById("audio").play();
				clearInterval(Makeaword.id);
				setTimeout(function(){
						document.getElementById("ansdiv").src=Utils.path+"images/Snowman.png";
						document.getElementById("chkbtn").style.pointerEvents = "auto";
						document.getElementById("soundbtn").style.pointerEvents = "auto";
						$("#alphabets").css('pointer-events', 'auto');
						$("#word").css('pointer-events', 'auto');
					},2000);
					if(Makeaword.level==1)
				{
					for(var i=0;i<Makeaword.lettersArray.length;i++)
					{
						document.getElementById('div'+(i+1)).style.backgroundColor="white";
					}
					for(var j=0;j<Makeaword.wordsarray.length;j++)
					{
						document.getElementById('wordsdiv'+(j+1)).style.backgroundColor="white";
					}
				}else{
					for(var j=0;j<Makeaword.wordsarray.length;j++)
					{
						document.getElementById('wordsdiv'+(j+1)).style.backgroundColor="white";
					}
					for(var i=0;i<Makeaword.vowel.length;i++)
					{
					document.getElementById('div'+(i+1)).style.backgroundColor="white";
					}
				} 
				Makeaword.correctCounter++;
				for(var i=0;i<Makeaword.wordsarray.length;i++)
				{
					var letter=document.getElementById('wordsdiv'+(i+1)).getAttribute("worddata");
					var letter1=document.getElementById('wordsdiv'+(i+1)).innerHTML;
					if(letter==letter1)
					{
					counter++;	
					}
				}
				var score=Makeaword.wordsarray.length-totalMarks;
				scoredMarks=counter-score;
				Makeaword.scoreFunction();
				Makeaword.timeout=setTimeout(function(){
					Makeaword.nextQue()},2000);
			}
			else 
			{
				scoreFlag=true;
				document.getElementById("ansdiv").src=Utils.path+"images/Snowman-Sad.GIF";
				document.getElementById("audio").src=Utils.path+'sounds/instruction/Better_luck_next_time.mp3';
				document.getElementById("audio").play();
				setTimeout(function(){
						document.getElementById("ansdiv").src=Utils.path+"images/Snowman.png";
						document.getElementById("chkbtn").style.pointerEvents = "auto";
						document.getElementById("soundbtn").style.pointerEvents = "auto";
						$("#alphabets").css('pointer-events', 'auto');
						$("#word").css('pointer-events', 'auto');
				},2000);
				if(Makeaword.level==1)
				{
					for(var i=0;i<Makeaword.lettersArray.length;i++)
					{
						document.getElementById('div'+(i+1)).style.backgroundColor="white";
					}
					for(var j=0;j<Makeaword.wordsarray.length;j++)
					{
						document.getElementById('wordsdiv'+(j+1)).style.backgroundColor="white";
					}
				}else
				{
					for(var j=0;j<Makeaword.wordsarray.length;j++)
					{
						document.getElementById('wordsdiv'+(j+1)).style.backgroundColor="white";
					}
					for(var i=0;i<Makeaword.vowel.length;i++)
					{
					document.getElementById('div'+(i+1)).style.backgroundColor="white";
					} 
				} 
				for(var i=0;i<Makeaword.wordsarray.length;i++)
				{
					var letter=document.getElementById('wordsdiv'+(i+1)).getAttribute("worddata");
					var letter1=document.getElementById('wordsdiv'+(i+1)).innerHTML;
					if(letter==letter1)
					{
					counter++;	
					}
				}
				var score=Makeaword.wordsarray.length-totalMarks;
				scoredMarks=counter-score;
				Makeaword.scoreFunction();
				Makeaword.timeout=setTimeout(function(){
				Makeaword.nextQue()},2000);
			}
	}
	else
	{
	document.getElementById("chkbtn").style.pointerEvents = "none";
	}
}
 //score Function
Makeaword.scoreFunction=function()
{
   console.log(resId,questionId,scoredMarks,totalMarks,Makeaword.level,startTime);
   if(Utils.mobileDeviceFlag)
   {
		Android.addScore(resId,questionId,scoredMarks,totalMarks,Makeaword.level,startTime); 
   }   
}
//display next question
Makeaword.nextQue=function()
{
	clearTimeout(Makeaword.timeout);
	clearInterval(Makeaword.noResponseTimer);
		if(scoreFlag==false)
		{
			Makeaword.selectWord++;
		}
	totalMarks=0;
	Makeaword.letter="";
	scoredMarks=0;
	document.getElementById("audio").pause();
	document.getElementById("ansdiv").src=Utils.path+"images/Snowman.png";
	document.getElementById("chkbtn").style.pointerEvents = "auto";
	document.getElementById("skip").style.pointerEvents = "auto";
		if(Makeaword.selectRandomWord.length==0)
		{
			Makeaword.Gameover();
		}
		else if( Makeaword.selectWord==Makeaword.selectRandomWord.length)
		{
			clearInterval(Makeaword.noResponseTimer);
			document.getElementById("responseBoxDiv").style.display="block";
			document.getElementById("questionMsg").innerHTML="Do you want to answer the skipped questions ?"
			document.getElementById("Yes").innerHTML="Yes";
			document.getElementById("No").innerHTML="No";
		}
		else 
		{
			console.log(Makeaword.selectRandomWord);
			document.getElementById('alphabets').innerHTML="";
			document.getElementById('word').innerHTML="";
			clearTimeout(Makeaword.timeout);
			Makeaword.AcceptWord();	
		}
}
//display skip questions
Makeaword.showSkippedQuestions=function()
{
console.log(Makeaword.selectRandomWord);
document.getElementById("responseBoxDiv").style.display="none";
document.getElementById('alphabets').innerHTML="";
document.getElementById('word').innerHTML="";
clearInterval(Makeaword.id);
clearTimeout(Makeaword.timeout);
Makeaword.selectWord=0;
Makeaword.letter="";
Makeaword.AcceptWord();	
}

//game over
Makeaword.Gameover=function()
{
	document.getElementById("responseBoxDiv").style.display="none";
	document.getElementById("coverpage").style.display="none";
    document.getElementById("gamepage").style.display="none";
	document.getElementById("endpage").style.display="block";
	document.getElementById("bnext").src=Utils.path+"images/Next.png";
	document.getElementById("gameover").innerHTML="GAME OVER";
		Makeaword.createStars();
		Makeaword.giveStars();
}
//function which creates star divs
Makeaword.createStars=function()
{
	var newImage,newDiv,i,numberOfStars=5;
	for(i=1;i<=5;i++)
	{
		newDiv= document.createElement("div");
		newDiv.setAttribute("id","starDiv"+i); 
		newDiv.setAttribute("class","nopadding  col-xs-2 col-md-2 col-lg-2 col-sm-2"); 
		$("#starsDiv").append(newDiv);
		
		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);
	}
}
 Makeaword.giveStars=function()
{
 var fractionVar,i,numberOfStars=0,temp;
Makeaword.total_No_of_Questions=5; 
ans=Makeaword.total_No_of_Questions/5;
temp=Makeaword.correctCounter/ans;
numberOfStars=Math.floor(temp);
temp=Math.round(temp);
console.log(numberOfStars);
console.log(temp); 
for(i=1;i<=Math.floor(numberOfStars);i++)
	document.getElementById("starImg"+i).setAttribute("src",Utils.path+"images/star2.png");
	if(numberOfStars!=0)
	{
	document.getElementById("audio").src=Utils.path+"Sounds/instruction/"+"star"+numberOfStars+".mp3";
	document.getElementById("audio").play();
	}
	console.log(Makeaword.correctCounter,Makeaword.queCnt);
	if(Utils.mobileDeviceFlag)
		Android.StudentCertification(Makeaword.correctCounter,Makeaword.queCnt,"8"); 
}
Makeaword.goNext=function()
{
if(Utils.mobileDeviceFlag)
	Android.GotoNextGame();
}
 
