var Makeaword = {};
var Utils = {};
Utils.path = '';
var questionId = 0, startTime = 0, scoredMarks = 0, totalMarks = 0, resId = 0, mobileDeviceFlag = false, level,label;
var scoreFlag = false;
Makeaword.globalVariables = function () {
	//Makeaword.selectRandomLetter=[];
	Makeaword.selectRandomWord = [];
	Makeaword.randomNumber;  //generate random number
	Makeaword.selectWord = 0; //word selected from the json
	Makeaword.countQue = 0;  //to count no of questions
	Makeaword.Attempt = 0;  //to check question is already attempt or not
	Makeaword.letter;    //used to store selected letter
	Makeaword.level;
	scoreHomeFlag =false;
	Makeaword.flag = true;  // is used to go back page 
	Makeaword.vowel = ["a", "e", "i", "o", "u"];
	document.getElementById("myBar").style.width = 100 + '%';
	var elem = document.getElementById("myBar");
	elem.style.background = "#4CAF50";
}
$(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 = "url(" + Utils.path + Makeaword.coverImage + ")";
	document.getElementById("playbtnid").src = Utils.path + "images/Play.png";
	document.getElementById("homeid").src = Utils.path + "images/Home.png";
	document.getElementById("logoid").src = Utils.path + "images/logo.png";
	document.getElementById("bnext").src = Utils.path + "images/Next.png";
	document.getElementById("soundbtn").src = Utils.path + "images/Sound.png";
	document.getElementById("ansdiv").src = Utils.path + "images/Snowman.png";
	document.getElementById("levelImg1").src = "images/Level_1.png";
	document.getElementById("levelImg2").src = "images/Level_2.png";
}
Makeaword.levelPage = function () {
	scoredMarks = 0;
	document.body.style.backgroundImage = Utils.path + "url('images/BG.png')";
	document.getElementById("levelPage").style.display = "block";
	document.getElementById("homeid").style.visibility = "visible";
	document.getElementById("coverpage").style.display = "none";
	document.getElementById("endpage").style.display = "none";
	document.getElementById("levelDiv1").setAttribute("setLevel", 1);
	document.getElementById("levelDiv2").setAttribute("setLevel", 2);
}
Makeaword.getLevelId = function (object) {
	Makeaword.level = parseInt(document.getElementById(object.id).getAttribute("setLevel"));
	level = Makeaword.level;
	Makeaword.gamePage();
}
Makeaword.gamePage = function () {
	document.getElementById("acknowledgementDiv").innerHTML = "";
	Makeaword.globalVariables();
	document.getElementById('alphabets').innerHTML = "";
	document.getElementById('word').innerHTML = "";
	clearInterval(Makeaword.id);
	document.getElementById("coverpage").style.display = "none";
	document.getElementById("levelPage").style.display = "none";
	document.getElementById("endpage").style.display = "none";
	document.getElementById("gamepage").style.display = "block";
	document.getElementById("homeid").style.visibility = "visible";
	document.getElementById("myProgress").style.visibility = "visible";
	Makeaword.populate();
	Makeaword.move();
}
//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.AcceptWord();
}
//create maxim span 
Makeaword.AcceptWord = function () {
	Makeaword.lettersArray = [];
	Makeaword.wordsarray = [];
	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++;
		}
	}
	console.log(Makeaword.lettersArray);
	Makeaword.countQue++;
	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;
	Makeaword.flag = false;
	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++; }
		}
	}
	document.getElementById('ansdiv').src = Utils.path + "images/Snowman.png";
	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";
}

//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;
	label=singleMarathiWord;
	document.getElementById("audio").src = Utils.path + 'sounds/' + Makeaword.selectRandomWord[Makeaword.selectWord].sound;
	document.getElementById("audio").play();
	Makeaword.displayLetter();
}
//progress bar
Makeaword.move = function () {
	var width = 100; //progres bar start with 100
	var elem = document.getElementById("myBar");
	document.getElementById("myBar").style.width = width + '%';
	Makeaword.id = setInterval(
		function frame() {
			if (width <= 0) {
				clearInterval(Makeaword.id);
				scoreFlag = true;
				Makeaword.nextQue();
			} else {
				if (width == 30) {
					elem.style.background = "red";
					width--;
					elem.style.width = width + '%';
				} else {
					width--;
					elem.style.width = width + '%';
				}
			}
		}, 600);
}
//to dislay letters
Makeaword.displayLetter = function () {
	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();
}
//get selected letter
Makeaword.getData = function (object) {
	Makeaword.letter = document.getElementById(object.id).getAttribute("data");
	scoreFlag = true;
	scoreHomeFlag =true;
	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) {
	document.getElementById("chkbtn").style.pointerEvents = "auto";
	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 () {
	scoreHomeFlag =false;
	var counter = 0; //is used for the scoremarks
	var count = 0;  //used to check the null innerHTML of specific letter
	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) {
		if (Makeaword.Attempt < 2) {
			document.getElementById('ansdiv').style.visibility = 'visible';
			document.getElementById("soundbtn").style.pointerEvents = "none";
			$("#alphabets").css('pointer-events', 'none');
			$("#word").css('pointer-events', 'none');
			if (str == str1) {
				//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();
				document.getElementById("chkbtn").style.pointerEvents = "none";
				clearInterval(Makeaword.id);
				setTimeout(function () {
					document.getElementById('ansdiv').src = Utils.path + "images/Snowman.png";
					document.getElementById("soundbtn").style.pointerEvents = "auto";
					document.getElementById("chkbtn").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();
				if (Makeaword.timeOut) {
				clearTimeout(Makeaword.timeOut);
				Makeaword.timeOut = null;
				}
				Makeaword.timeout = setTimeout(function () {
					Makeaword.nextQue()
				}, 2000);
			}
			else {
				document.getElementById("ansdiv").src = Utils.path + "images/Snowman-Sad.GIF";
				if (Makeaword.Attempt == 1) {
					document.getElementById("audio").src = Utils.path + 'sounds/instruction/Better_luck_next_time.mp3';
				} else {
					document.getElementById("audio").src = Utils.path + 'sounds/instruction/Try_again.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;
				if (Makeaword.Attempt == 1)
					Makeaword.scoreFunction();
			}
			Makeaword.Attempt++;
			if (Makeaword.Attempt == 2) {
				document.getElementById("bnext").style.pointerEvents = "none";
				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, level, startTime, label);
	if (Utils.mobileDeviceFlag) {
		Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
	}
}
//display next question
Makeaword.nextQue = function () {
	clearTimeout(Makeaword.timeout);
	if (scoreFlag == false) {
		scoredMarks = -1;
		Makeaword.scoreFunction();
	}
	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("bnext").style.pointerEvents = "auto";
	if (Makeaword.countQue == 5) {
		Makeaword.Gameover();
	}
	else {
		Makeaword.Attempt = 0;
		var elem = document.getElementById("myBar");
		elem.style.background = "#4CAF50";
		Makeaword.selectWord++;
		document.getElementById('alphabets').innerHTML = "";
		document.getElementById('word').innerHTML = "";
		clearInterval(Makeaword.id);
		clearTimeout(Makeaword.timeout);
		Makeaword.AcceptWord();
		Makeaword.displayLetter();
		Makeaword.move();
	}
}
//go return back to cover page
Makeaword.callHome = function () {
	document.getElementById("bnext").style.pointerEvents = "auto";
	Makeaword.letter = "";
	totalMarks = 0;
	scoredMarks = 0;
	clearTimeout(Makeaword.timeout);
	document.getElementById("audio").pause();
	if (Makeaword.flag == false) {
		Makeaword.flag = true;
		document.getElementById("levelPage").style.display = "block";
		document.getElementById("homeid").style.visibility = "visible";
		document.getElementById("coverpage").style.display = "none";
		document.getElementById("gamepage").style.display = "none";
		document.getElementById("endpage").style.display = "none";
		document.getElementById("myProgress").style.visibility = "hidden";
		if (scoreHomeFlag == true) {
			scoredMarks = 0;
			Makeaword.scoreFunction();
		}
	}
	else {
		location.reload();
	}
}
//credit 
Makeaword.showCredits = function () {
	document.getElementById("acknowledgementDiv").style.display = "block";
}
//game over
Makeaword.Gameover = function () {
	document.getElementById("acknowledgementDiv").innerHTML = "";
	document.getElementById("coverpage").style.display = "none";
	document.getElementById("gamepage").style.display = "none";
	document.getElementById("endpage").style.display = "block";
	document.getElementById("acknowledgementDiv").style.display = "none";
	document.getElementById("myProgress").style.visibility = "hidden";
	document.getElementById("resetbtnid").src = Utils.path + "images/Replay.png";
	document.getElementById("creditbtnid").src = Utils.path + "images/Credits.png";
	document.getElementById("gameover").innerHTML = "GAME OVER";
	for (i = 0; i < Object.keys(Makeaword.AcknowledgementPageData).length; i++) {
		div = document.createElement('div');
		div.className = "fontClass nopadding col-xs-12 col-sm-12 col-md-12 col-lg-12";
		div.innerHTML = Object.keys(Makeaword.AcknowledgementPageData)[i] + " : " + Object.values(Makeaword.AcknowledgementPageData)[i];
		acknowledgementDiv.appendChild(div);
	}
	div = document.createElement('div');
	div.className = "fontClass firstLetter col-xs-12 col-sm-12 col-md-12 col-lg-12";
	div.innerHTML = Makeaword.hardcodedData['createdOn'];
	acknowledgementDiv.appendChild(div);
	//document.getElementById("acknowledgementDiv").style.display="none";
}