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.beforeId="";
	scoreHomeFlag =false;
	Makeaword.idFlag=false;
	Makeaword.blankFiledId;
	Makeaword.showHint=false;
	Makeaword.correctSapnID;
	Makeaword.blankFieldCounter=0;
	Makeaword.correctAnswerCounter=0;
}
$(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("sound").src = Utils.path + "images/Sound.png";
	document.getElementById("wrongWordInstruction").style.backgroundImage = Utils.path + "url('images/title box.png')";
	document.getElementById("FillCorrectWord").style.backgroundImage = Utils.path + "url('images/Q box.png')";
}
Makeaword.gamePage = function () {
	document.body.style.backgroundImage = Utils.path + "url('images/mashal-animation-bg.gif')";
	document.getElementById("acknowledgementDiv").innerHTML = "";
	Makeaword.globalVariables();
	clearInterval(Makeaword.id);
	document.getElementById("coverpage").style.display = "none";
	document.getElementById("endpage").style.display = "none";
	document.getElementById("gamepage").style.display = "block";
	document.getElementById("bottomGrid").style.display = "block";
	document.getElementById("homeid").style.visibility = "visible";
	//document.getElementById("ShowCorrectWord").style.visibility = "hidden";
	Makeaword.populate();
Makeaword.playSound();
}
Makeaword.populate = function () {
	for (var j = 0; j < 1; 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]);
	}
	console.log(Makeaword.selectRandomWord);
	Makeaword.showQuestionOptions();
}
Makeaword.playSound=function()
{
	document.getElementById("audio").pause();
	document.getElementById("audio").src = Utils.path + 'Sounds/instruction/instruction2.mp3';
	document.getElementById("audio").play();					
}
Makeaword.showQuestionOptions=function()
{
	document.getElementById("wrongWordInstruction").innerHTML =Makeaword.Instructions[0];
	var a=Makeaword.selectRandomWord[Makeaword.countQue].correctSentence.split(" ");
		for(var i=0;i<a.length;i++)
		{
		var div = document.createElement("span");
		div.setAttribute("id", "div" + (i + 1));
		div.setAttribute("class", " nopadding words text-center");
		document.getElementById('FillCorrectWord').appendChild(div);
		if((Makeaword.selectRandomWord[Makeaword.countQue].options).includes(a[i])){
			div.setAttribute("class", "drop words dash");
			$(div).data("data",a[i]);
			div.innerHTML =a[i];
			Makeaword.blankFieldCounter++;
			$(div).css({'color':'red'});		
			}
		else{
		div.innerHTML =a[i];}}
		var options=Makeaword.selectRandomWord[Makeaword.countQue].options;
		for(var i=0;i<options.length;i++)
		{
		var div = document.createElement("div");
		div.setAttribute("id", "optiondiv" + (i + 1));
		div.setAttribute("class", " nopadding  bottomSentences words1 text-center col-sm-2 col-lg-2 col-md-2 col-xs-2");
		document.getElementById('ShowOptions').appendChild(div);
		
		var span = document.createElement("span");
		span.setAttribute("id", "span" + (i + 1));
		span.setAttribute("class", " nopadding drag  text-center");
		div.appendChild(span);
		
		if(i==0 || i==2 || i==4 ||i==6 ||i==8){
		div.style.backgroundImage = Utils.path + "url('images/option box.png')";}
		else{div.style.backgroundImage = Utils.path + "url('images/option box2.png')";}
		span.innerHTML =options[i];
		}
		$(".drag").draggable({
        revert: "invalid", //text is not drop anywhere
        helper: "clone", // create "copy" with original properties
		containment: 'html', 
		});
		$(".drop").droppable({
			drop: function(event, ui) {
				var src = ui.draggable;
				var srcdiv = src.parent();
				var destdiv = $(this);
				var d=src.children();
				var dest = destdiv.children();
				var a=$(ui.draggable).text();
				var b = $(this).data("data");
				a=a.replace(/\,/g,"");
				a=a.trim(" ");
				Makeaword.Attempt++;
				console.log(a,b);
					document.getElementById("audio").pause();
				//document.getElementById("ShowCorrectWord").innerHTML =Makeaword.selectRandomWord[Makeaword.countQue].correctSentence;
					if(a==b)
					{
						$(this).droppable({
					disabled: true
					});
					Makeaword.correctAnswerCounter++;
					$(destdiv).html(a);
					$(d).html(a);
					$(src).css({ "top": 0, "left": 0 });
					$(dest).css({ "top": 0, "left": 0 });
					document.getElementById("audio").src = Utils.path + 'Sounds/instruction/welldone.mp3';
					document.getElementById("audio").play();
					$(destdiv).css({'color':'green'});
					destdiv.innerHTML=b;
					//$(destdiv).css({"visibility":"visible"});
					$(destdiv).css("text-decoration", "none");
					}
					else{
						$(destdiv).html(a);
						$(d).html(a);
						$(d).addClass('ui-draggable ui-draggable-handle"');
						$(destdiv).css({'color':'red'});
						destdiv.innerHTML=b;
						//$(destdiv).css({'visibility':'visible'});
						$(destdiv).css("text-decoration", "line-through");
						document.getElementById("audio").src = Utils.path + 'Sounds/instruction/Try_again.mp3';
						document.getElementById("audio").play();
						ui.draggable.animate(ui.draggable.data("origPosition"),"slow");
					}
					if(	Makeaword.correctAnswerCounter== Makeaword.blankFieldCounter){
						$("#bnext").addClass("blink_me");
					document.getElementById("ShowOptions").style.pointerEvents="none";}
			}
		});
}
Makeaword.allowDrop = function(event) {
    event.preventDefault();
}

Makeaword.callHome=function()
{
	    location.reload();
}
Makeaword.nextQue=function()
{
		document.getElementById("audio").pause();
			Makeaword.countQue++;
	if(Makeaword.countQue<1)
	{
		var a=Makeaword.selectRandomWord[Makeaword.countQue].sentence.split(" ");
		document.getElementById("ShowOptions").innerHTML=" ";
		document.getElementById("FillCorrectWord").innerHTML=" ";
		document.getElementById("ShowOptions").style.pointerEvents="auto";
		$("#bnext").removeClass("blink_me");
		Makeaword.Attempt=0;
		Makeaword.showQuestionOptions();
	}
	else{
		$("#bnext").removeClass("blink_me");
		Makeaword.Gameover();
	}
}
//credit 
Makeaword.showCredits = function () {
	document.getElementById("acknowledgementDiv").style.display = "block";
}
Makeaword.replay=function()
{
	document.getElementById("ShowOptions").innerHTML=" ";
	document.getElementById("FillCorrectWord").innerHTML=" ";
	document.getElementById("ShowOptions").style.pointerEvents="auto";
	document.getElementById("bottomGrid").style.display = "block";
	Makeaword.gamePage();
}
//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("bottomGrid").style.display = "none";
	document.getElementById("resetbtnid").src = Utils.path + "images/Replay.png";
	document.getElementById("creditbtnid").src = Utils.path + "images/Credits.png";
	document.getElementById("acknowledgementDiv").style.backgroundImage = Utils.path + "url('images/title box.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['Created On'];
	acknowledgementDiv.appendChild(div);
	//document.getElementById("acknowledgementDiv").style.display="none";
}