//16th may 2019
var Muhanware = {};
var Utils = {};
Utils.Path = '';
var questionId=0,startTime=0,scoredMarks=0,totalMarks=0,resId=0,label="",scoreFlag=false;
Utils.mobileDeviceFlag=false;
var word="",totalNos=0,type="sentence";
$(document).ready(function () {
	$(".se-pre-con").fadeOut("slow");
	if ((navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) && !(window.location.href.includes('http')))
        Utils.mobileDeviceFlag = true;
	setTimeout(function () {
		Muhanware.setImages();
	
		if (Muhanware.hardCodedData['languageFont'] != null)
			setFontFamilyForLang("fonts/" + Muhanware.hardCodedData['languageFont']);
	}, 200);
});

setFontFamilyForLang = function (fontFilePath) {
	var newStyle = "<style>" +
		"@font-face {  " +
		"font-family: 'CustomFontFamily';" +
		"src: url('" + fontFilePath + "') format('truetype');" +
		"}  </style>";
	$("head").append(newStyle);
	$("body").css("font-family", "CustomFontFamily");
}
//set all images
Muhanware.setImages = function () {
	document.getElementById('home').src = Utils.Path + "images/home.png";
	document.getElementById('logo').src = Utils.Path + "images/logo.png";
	document.getElementById('playImage').src = Utils.Path + "images/Play.png";
	document.getElementById('next').src = Utils.Path + "images/Next.png";
	document.body.style.backgroundImage = "url(" + Utils.Path + "images/coverPage.png" + ")";
	document.getElementById('help').src = Utils.Path + "images/showHelp.png";
	document.getElementById("replatbtn").src = Utils.Path + "images/playAgain.png";
	document.getElementById("creditbtn").src = Utils.Path + "images/credits.png";
	document.getElementById('questionBoard').style.backgroundImage = "url(" + Utils.Path + "images/name_plate1.png"+")";
	document.getElementById('acknowledgementDiv').style.backgroundImage = "url(" + Utils.Path + "images/op.png"+")";
	document.getElementById('gamePage').style.display = 'none';
	$("#queDiv").css({"visibility":"hidden"});
}
Muhanware.initializeVariables = function () {
	Muhanware.optionArr = [];			//Array for four options 
	Muhanware.queArr = [];				//array for 5 questions
	Muhanware.wrongAnsDiv;
	Muhanware.queCnt = 0;
	Muhanware.nextQueTimer = 0;
	Muhanware.ansClicked=false;	
	Muhanware.practice=false;	
	Muhanware.helpFlag=false;
}
//onclick play button to start game or display que page
Muhanware.playGame = function () {
	Muhanware.initializeVariables();
	if(Utils.mobileDeviceFlag)
	Muhanware.practice=Android.isPractice();
	if(Muhanware.practice)
		$("#help").hide();
	else
	{
		document.getElementById("help").setAttribute("helpData","off");
		$("#help").show();
	}
	
	document.body.style.backgroundImage = "url(" + Utils.Path + "images/bg.png"+")";
	document.getElementById('coverPage').style.display = 'none';
	document.getElementById('gamePage').style.display = 'block';
	$("#queDiv").css({"visibility":"visible"});
	document.getElementById('homeDiv').style.visibility = 'visible';
	document.getElementById("finalPage").style.display = "none";
	getUniqueValArr(Muhanware.data, Muhanware.queArr, 5);
	Muhanware.showQuestion();
	Muhanware.changeOptions();
}
//to display que and four options
Muhanware.showQuestion = function () {
	Muhanware.createOptions();
	document.getElementById('help').src = Utils.Path + "images/showHelp.png";
	document.getElementById("help").setAttribute("helpData","off");
	$(".options").css({"pointer-events":"auto"});
	//$(".options").removeClass("rotateOption");
	//$(".spanElements").addClass("animated flipInX");
	$("#queText").addClass("animated fadeInDown");
	
	if (Muhanware.nextRotation) {
		clearTimeout(Muhanware.nextRotation);
		Muhanware.nextRotation = null;
	}
	Muhanware.nextRotation = setTimeout(function () {
		$(".options").removeClass("rotateOption");
		$("#queText").removeClass("animated fadeInDown");
		$("#next").css({"pointer-events":"auto"});
	},900);
	Muhanware.ansClicked=false;
	Muhanware.helpFlag=false;
	Muhanware.attemptCnt=0;				//how many times attempt the que.
	totalMarks=10;
	questionId=parseInt(Muhanware.queArr[Muhanware.queCnt].questionId);
	word=Muhanware.queArr[Muhanware.queCnt].options;
	totalNos=Muhanware.data.length;
	d = new Date();  //date object
	startTime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();  //to get the current date
	//document.getElementById('frogImg').src = Utils.Path + "images/Frog_Correct-ANS_01.png";
	document.getElementById('queText').innerHTML = Muhanware.queArr[Muhanware.queCnt].phrase;
	Muhanware.optionArr.push(Muhanware.queArr[Muhanware.queCnt]);
	getUniqueValArr(Muhanware.data, Muhanware.optionArr, 3);
	shuffle(Muhanware.optionArr);
	Muhanware.changeOptions();
	console.log(Muhanware.optionArr);
}
//create dynamic div for create options
Muhanware.createOptions = function () {
var div, span;
var col=4;
document.getElementById("optionsDiv").innerHTML="";

	for (var i = 0; i < col; i++) {
		div = document.createElement('div');
		div.setAttribute('class', 'nopadding text-center options col-xs-12 col-sm-12 col-md-12 col-lg-12');
		div.setAttribute('id', 'option'+ (i));
		optionsDiv.appendChild(div);
		document.getElementById('option'+ (i)).style.backgroundImage = "url(" + Utils.Path + "images/arrow_1.png" + ")";
		span = document.createElement('div');
		span.setAttribute('id', 'span'+ (i));
		span.setAttribute('class', 'spanElements helpClass removeHelp parentClass col-xs-10 col-sm-10 col-md-10 col-lg-10 col-xs-offset-1 col-sm-offset-1 col-lg-offset-1 col-md-offset-1');
		div.setAttribute('onclick', 'Muhanware.checkAnswer(this.id)');
		div.appendChild(span);
	}
}
//function for change the options when next que will display
Muhanware.changeOptions = function () {

	$(".options").addClass("rotateOption");
	$('.spanElements').css({ "visibility": "visible" });
	$('.options').css({ "visibility": "visible" });
	
		for (var i = 0; i < Muhanware.optionArr.length; i++) {
			$("#option"+i).prop('value', Muhanware.optionArr[i].options);
			$("#span"+i).text(Muhanware.optionArr[i].options);
			$("#span"+ i).addClass('helpClass');
		}
	}
//on click option div check ans is right or wrong
Muhanware.checkAnswer = function (eleId) {
	if(Muhanware.helpFlag)
		Muhanware.attemptCnt=0;
	Muhanware.attemptCnt++;
	Muhanware.ansClicked=true;	
	scoreFlag=true;
	
	$(".options").css({"pointer-events":"none"}); //other div's pointer event none
	$("#help").css({"pointer-events":"none"});   //help buttons's pointer event none
	$("#next").css({"pointer-events":"none"});   //next buttons's pointer event none
	$('.spanElements').css({ "visibility": "visible" });
	
	$("#frogDiv").css({ "z-index": "10" });

		//check ans
		$('#frogDiv').css({ "visibility": "hidden" });
			var divId = $("#" + eleId).prop("id");
			$('#' + divId).prepend(Muhanware.wrongAnsDiv);
			
		if ($('#' + eleId).prop('value') == Muhanware.queArr[Muhanware.queCnt].options) {
			Muhanware.moveFrog(true);		//if ans is right
			$('#' + eleId).children().css({ "color": "rgb(242, 245, 68)" });
		}
		else {
			Muhanware.moveFrog(false);		//if ans is wrong
			$('#' + eleId).children().css({ "color": "red" });
		}
		document.getElementById("audio").play();
}
//after checking ans right or wrong change frog position and image and sound
Muhanware.moveFrog=function(actionFlag)
{
	if(actionFlag)
	{
		document.getElementById("audio").src = Utils.Path + 'sounds/Instructions/Right_answer__Aditi.mp3';
		document.getElementById("audio").play();
		$(".options").css({"pointer-events":"none"}); //other div's pointer event none
		$("#help").css({"pointer-events":"none"});
		$("#next").css({"pointer-events":"auto"});
		if(!Muhanware.helpFlag)
		{
			if(Muhanware.attemptCnt==1)
				scoredMarks = 10;
			else
				scoredMarks=5;
			Muhanware.scoreFunction ();
		}
		if(Muhanware.helpFlag==true)
		{
			Muhanware.ansClicked=true;	
		}
		else
			Muhanware.ansClicked=false;	
		$("#next").addClass("blink_me");
		console.log(word,totalNos,type);
		if(Utils.mobileDeviceFlag)
		{
			Android.addLearntData(word,totalNos,type);
		}
	}
	else
	{
		document.getElementById("audio").src = Utils.Path + 'sounds/Instructions/wrong_answer!_Aditi.mp3';
		document.getElementById("audio").play();
		if(Muhanware.attemptCnt==2)
		{
		scoredMarks=0;
		scoreFlag=true; 
		$("#help").css({"pointer-events":"auto"});
		$(".options").css({"pointer-events":"none"}); //other div's pointer event none
		}
		if (Muhanware.nextQureturnBackFrogeTimer) {
			clearTimeout(Muhanware.returnBackFrog);
			Muhanware.returnBackFrog = null;
		}
		setTimeout(function () {
			$(".options").css({"pointer-events":"auto"});
			$(".spanElements").css({"color":"white"});
			$("#help").css({"pointer-events":"auto"});
			$("#next").css({"pointer-events":"auto"});
		},1300);
		if(Muhanware.attemptCnt==2)
		{
			if (Muhanware.nextQueTimer) {
				clearTimeout(Muhanware.nextQueTimer);
				Muhanware.nextQueTimer = null;
			}
			Muhanware.nextQueTimer = setTimeout(function () {
				$("#next").addClass("blink_me");
				$(".options").css({"pointer-events":"none"}); //other div's pointer event none
				//Muhanware.nextQue();
			}, 1700);
		}
	}
}
//to display next que
Muhanware.nextQue = function () {
	clearTimeout(Muhanware.nextQueTimer);
	clearTimeout(Muhanware.nextRotation);
	$("#next").css({"pointer-events":"none"});
	$("#help").css({"pointer-events":"auto"});
	$(".spanElements").css({"color":"white"});
	$("#next").removeClass("blink_me");
	document.getElementById('audio').pause();
	$(".parentClass").addClass('helpClass');
	$(".helpClass").removeClass("animated zoomOut");
	if(Muhanware.ansClicked && scoreFlag==true)
	{
		scoredMarks=0;
		Muhanware.scoreFunction ();
	}
		if(Muhanware.attemptCnt==0 && Muhanware.ansClicked==false)
		{
			scoredMarks=-1;
			Muhanware.scoreFunction ();
		} 
	if (Muhanware.queCnt == 4)
		Muhanware.gameOver();
	else {
		Muhanware.queCnt++;
		Muhanware.optionArr = [];
		Muhanware.showQuestion();
	}
}
//get help or to known right ans to click on help button
Muhanware.helpFun = function () {
	$("#next").addClass("blink_me");
	$(".helpClass").removeClass("animated zoomOut");
	$(".helpClass").removeClass("animated zoomIn");
	Muhanware.helpFlag=true;
	clearTimeout(Muhanware.returnBackFrog);
	$(".options").css({"pointer-events":"none"}); //other div's pointer event none
	Muhanware.ansClicked=true;
	scoreFlag=true;
	var helpdata=document.getElementById("help").getAttribute("helpData");
	if(helpdata=="off")
	{
		document.getElementById("audio").src = Utils.Path + 'sounds/Instructions/ting.mp3';
		document.getElementById("audio").play();
		document.getElementById('help').src = Utils.Path + "images/hideHelp.png";
		$(".options").css({"pointer-events":"none"}); //other div's pointer event none 
		var currectAns = Muhanware.queArr[Muhanware.queCnt].options;
		document.getElementById("help").setAttribute("helpData","on");
		for (var i = 0; i < Muhanware.optionArr.length; i++)
		{
			var ans = document.getElementById("span"+ i).innerHTML;
			if (currectAns == ans) {
				$("#span"+ i).removeClass('helpClass');
				$("#span"+ i).removeClass('removeHelp');
				$(".helpClass").addClass("animated zoomOut");
			}
		}
	}
	else
	{
		document.getElementById('help').src = Utils.Path + "images/showHelp.png";
		$(".options").css({"pointer-events":"auto"}); //other div's pointer event none
		document.getElementById("help").setAttribute("helpData","off");
		$(".removeHelp").addClass("animated zoomIn");
	}
}
//display game over msg after solve all questions finalPage
Muhanware.gameOver = function () {
	document.getElementById("acknowledgementDiv").innerHTML = "";
	document.getElementById("gamePage").style.display = "none";
	$("#queDiv").css({"visibility":"hidden"});
	document.getElementById("finalPage").style.display = "block";
	document.getElementById('gameOver').innerHTML = Muhanware.hardCodedData.gameOver;

	for (var i = 0; i < Object.keys(Muhanware.AcknowledgementPageData).length; i++) {
		div = document.createElement('div');
		div.className = "fontClass firstLetter col-xs-12 col-sm-12 col-md-12 col-lg-12";
		div.innerHTML = Object.keys(Muhanware.AcknowledgementPageData)[i] + " : " + Object.values(Muhanware.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 = Muhanware.hardCodedData['createdOn'];
	acknowledgementDiv.appendChild(div);
	document.getElementById("acknowledgementDiv").style.display = "none";
}
//show credits on click credit button
Muhanware.showCredits = function ()			
{
	document.getElementById("acknowledgementDiv").style.display = "block";
}
//go to home or previous page
Muhanware.backToHome = function () 
{
	if(Muhanware.ansClicked)
	{
		scoredMarks = 0;
		Muhanware.ansClicked = false;
		Muhanware.scoreFunction();
	}
	location.reload(true);		//true-Reloads the current page from the server,false-from cache
	
}
//create dynamic img tag for frog img 
Muhanware.imgTag = function () {

	$(".options").css({"pointer-events":"auto"});	//pointer event auto to check again ans
	//$("#help").css({"pointer-events":"auto"}); 
	$("#next").css({"pointer-events":"auto"}); 
	imgDiv = document.createElement('img');
	imgDiv.setAttribute('class', 'img-responsive nopadding col-sm-10 col-lg-10 col-md-10 col-xs-10 col-sm-offset-0 col-lg-offset-0 col-md-offset-0 col-xs-offset-0');
	imgDiv.setAttribute('id', 'frogImg');
	frogDiv.appendChild(imgDiv);
	document.getElementById('frogImg').src = Utils.Path + "images/Frog_Correct-ANS_01.png";
	$('#frogDiv').css({ "visibility": "visible" });
	$('#frogImg').css({ 'top': '', 'left': '' });
}

Muhanware.scoreFunction=function()				//score Function
{
	console.log(resId,questionId,scoredMarks,totalMarks,1,startTime);
	if(Utils.mobileDeviceFlag)
	{
		Android.addScore(resId,questionId,scoredMarks,totalMarks,1,startTime);
	}
}