//startingDate:6-07-2020
var BirdInfo = {};
var Utils = {};
Utils.Path = '';
Utils.mobileDeviceFlag = false;
var questionId = 0, startBirdInfo = 0, scoredMarks = 0, totalMarks = 10, resId = 0, level = 1, label = '';
var birdNo;
BirdInfo.istrFlag = true;
$(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;
	/* if(Utils.mobileDeviceFlag) 
		Utils.Path=Android.getMediaPath("BirdInfo");  */
	setTimeout(function () {
		BirdInfo.init();
	}, 100);
});

BirdInfo.init = function () {
	document.title = Object.values(BirdInfo.AcknowledgementPageData)[0];
	$("#coverPage").show();
	$("#gamePage").hide();
	$("#endPage").hide();
	$(".header").not("#logo").css("visibility", "hidden");
	//document.body.style.backgroundImage = "url(" + Utils.Path + "images/Bg.png";
	document.body.style.backgroundImage = "url(" + Utils.Path + "images/skin/" + BirdInfo.hardCodedData.cover_page;
	BirdInfo.initializeVariables();
	BirdInfo.setAllImages();
	BirdInfo.createAcknowledgementDivs();
	$("#birdName").addClass("animated pulse");
	$("#birdImgDiv").addClass("bounce");
}

BirdInfo.initializeVariables = function () {
	BirdInfo.correctCtr = 0;
	BirdInfo.ansArr = [];
	BirdInfo.corrArr = [];
	BirdInfo.wrongArr = [];
	BirdInfo.primeNumArr = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 71, 73, 79, 83, 89, 97];
	$('#optionDiv').empty();
}
BirdInfo.setAllImages = function () {
	document.getElementById("homeImage").src = Utils.Path + "images/skin/Home.png";
	document.getElementById("logoImage").src = Utils.Path + "images/skin/logo.png";
	document.getElementById("playImg").src = Utils.Path + "images/skin/Play.png";
	document.getElementById("goImg").src = Utils.Path + "images/skin/Play.png";
	//document.getElementById("next").src = Utils.Path + "images/skin/Next.png";
	document.getElementById("creditImg").src = Utils.Path + "images/skin/credits.png";
	document.getElementById("replayImg").src = Utils.Path + "images/skin/replay.png";
	document.getElementById("gameOver").innerHTML = BirdInfo.hardCodedData.gameOver;
	document.getElementById("soundImg").src = Utils.Path + "images/skin/Sound.png";
	document.getElementById("HindiName").style.backgroundImage = "url(" + Utils.Path + "images/skin/hindi_title.png";
	document.getElementById("EnglishName").style.backgroundImage = "url(" + Utils.Path + "images/skin/english_title.png";
	document.getElementById("info").style.backgroundImage = "url(" + Utils.Path + "images/skin/info_box.png";
	document.getElementById("birdImgDiv").style.backgroundImage = "url(" + Utils.Path + "images/skin/image_box.png";
	document.getElementById("birdList").style.backgroundImage = "url(" + Utils.Path + "images/skin/scroll_box.png";
}
BirdInfo.showInstr = function () {
	$("#coverPage").hide();
	$("#instrPage").show();
	document.body.style.backgroundImage = "url(" + Utils.Path + "images/skin/bg.png";
	document.getElementById("instruction").innerHTML = BirdInfo.hardCodedData.instruction;
	document.getElementById("playSound").src = Utils.Path + "sounds/instructions/info.mp3";
	document.getElementById("playSound").play();
}
BirdInfo.playGame = function () {
	document.body.style.backgroundImage = "url(" + Utils.Path + "images/skin/bg.png";
	document.getElementById("playSound").pause();
	$("#coverPage").hide();
	$("#instrPage").hide();
	$("#gamePage").show();
	$(".header").css("visibility", "visible");
	//BirdInfo.setQuestion();
	BirdInfo.cteateCatagoryDivs();
	$(".catagories").addClass("blinker");
	$("#birdList").css("visibility", "hidden");

	//BirdInfo.generateNumbers();

}

BirdInfo.cteateCatagoryDivs = function () {
	var i;
	parentdiv = document.getElementById("catagory");
	for (i = 0; i < BirdInfo.questions.length; i++) {
		birdDiv = document.createElement("DIV");
		birdDiv.setAttribute("id", "catagory" + i);
		birdDiv.setAttribute("num", i);
		birdDiv.setAttribute("type", i);
		birdDiv.setAttribute("class", "catagories col-xs-11 col-md-11 col-lg-11 col-sm-11 col-xs-offset-1 col-md-offset-1 col-lg-offset-1 col-sm-offset-1");
		birdDiv.setAttribute("onclick", "BirdInfo.createBirdDivs(this.id);");
		birdDiv.style.backgroundImage = "url(" + Utils.Path + "images/skin/english_title.png";
		birdDiv.innerHTML = BirdInfo.questions[i].category;
		//birdDiv.innerHTML = BirdInfo.catagoryArr[i];
		parentdiv.appendChild(birdDiv);
	}
}

BirdInfo.createBirdDivs = function (ele) {
	var i, ctr = 0;
	document.getElementById("playSound").pause();
	if (BirdInfo.istrFlag) {
		document.getElementById("playSound").src = Utils.Path + "sounds/instructions/instruction.mp3";
		document.getElementById("playSound").play();
		BirdInfo.istrFlag = false;
	}
	$("#birdList").empty();
	$("#soundImg").css("visibility", "hidden");
	$(".catagories").css("color", "white");
	$("#" + ele).css("color", "rgb(33 235 33)");
	//document.getElementById(ele).style.color = ""
	parentdiv = document.getElementById("birdList");
	num = parseInt(document.getElementById(ele).getAttribute("type"));
	for (i = 0; i < BirdInfo.questions[num].data.length; i++) {
		ctr++;
		birdDiv = document.createElement("DIV");
		birdDiv.setAttribute("id", "birdDiv" + i);
		birdDiv.setAttribute("num", i);
		birdDiv.setAttribute("catagory", num);
		birdDiv.setAttribute("class", " birdDivs nopadding");
		birdDiv.setAttribute("onclick", "BirdInfo.showDetails(this.id);");
		parentdiv.appendChild(birdDiv);

		birdImg = document.createElement("IMG");
		birdImg.setAttribute("id", "birdImg" + i);
		birdImg.setAttribute("class", "img-responsive birdImages nopadding col-xs-12 col-md-12 col-lg-12 col-sm-12");
		birdImg.setAttribute("src", Utils.Path + "images/" + BirdInfo.questions[num].data[i].Image);
		birdDiv.appendChild(birdImg);
	}
	$(".loader").show();
	$('.loader').css('display', 'flex');
	var imgs = document.images, len = imgs.length, counter = 0;
	[].forEach.call(imgs, function (img) {
		if (img.complete)
			incrementCounter();
		else
			img.addEventListener('load', incrementCounter, false);
	});
	function incrementCounter() {
		counter++;
		if (counter === len)
			$(".loader").hide();
	}
	$(".catagories").removeClass("blinker");
	$("#birdList").css("visibility", "visible");
	$("#birdList").addClass("blinker");
	$("#birdName").css("visibility", "hidden");
	$("#birdImgDiv").css("visibility", "hidden");
	$("#info").css("visibility", "hidden");
	$("#soundImg").css("pointer-events", "none");
	//document.getElementById("playSound").pause();
	clearTimeout(BirdInfo.playsoundTimer);
}

BirdInfo.showDetails = function (ele) {
	BirdInfo.playsoundFlag = true;
	$("#birdName").css("visibility", "visible");
	$("#birdImgDiv").css("visibility", "visible");
	$("#info").css("visibility", "visible");
	$("#soundImg").css("visibility", "visible");
	$("#soundImg").css("pointer-events", "auto");
	$("#birdList").removeClass("blinker");

	document.getElementById("playSound").pause();
	birdNo = parseInt(document.getElementById(ele).getAttribute("num"));
	catNo = parseInt(document.getElementById(ele).getAttribute("catagory"));
	document.getElementById("HindiName").innerHTML = BirdInfo.questions[catNo].data[birdNo].Bird_name_hindi;
	document.getElementById("spelling").innerHTML = BirdInfo.questions[catNo].data[birdNo].Bird_name;
	document.getElementById("Devnagari").innerHTML = BirdInfo.questions[catNo].data[birdNo].Bird_name_marathi;
	//console.log(BirdInfo.questions[birdNo].description);
	document.getElementById("nativeSpan1").innerHTML = Object.keys(BirdInfo.questions[catNo].data[birdNo].Description)[0] + " : ";
	document.getElementById("nativeSpan2").innerHTML = Object.values(BirdInfo.questions[catNo].data[birdNo].Description)[0];
	document.getElementById("foodSpan1").innerHTML = Object.keys(BirdInfo.questions[catNo].data[birdNo].Description)[1] + " : ";
	document.getElementById("foodSpan2").innerHTML = Object.values(BirdInfo.questions[catNo].data[birdNo].Description)[1];
	document.getElementById("birdImg").src = Utils.Path + ("images/" + BirdInfo.questions[catNo].data[birdNo].Image);
	$(".loader").show();
	$('.loader').css('display', 'flex');
	var imgs = document.images, len = imgs.length, counter = 0;
	[].forEach.call(imgs, function (img) {
		if (img.complete)
			incrementCounter();
		else
			img.addEventListener('load', incrementCounter, false);
	});
	function incrementCounter() {
		counter++;
		if (counter === len)
			$(".loader").hide();
		document.getElementById("playSound").src = Utils.Path + ("sounds/" + BirdInfo.questions[catNo].data[birdNo].birdNameAudio);
		document.getElementById("playSound").play();
		BirdInfo.loadmetaDataFlag = true;
		sound = Utils.Path + ("sounds/" + BirdInfo.questions[catNo].data[birdNo].birdNameAudio);
		BirdInfo.getDuration(sound);
	}

}


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

			BirdInfo.playInstruction(duration);
		}
		);
	}
	else
		$('#playSound').unbind("loadedmetadata");
}

BirdInfo.playInstruction = function (duration) {
	var word, source;
	document.getElementById("playSound").play();
	if (BirdInfo.playsoundTimer) {
		clearTimeout(BirdInfo.playsoundTimer);
		BirdInfo.playsoundTimer = null;
	}
	BirdInfo.playsoundTimer = setTimeout(function () {
		if (BirdInfo.playsoundFlag) {
			BirdInfo.playsoundFlag = false;
			source = Utils.Path + "sounds/instructions/soundof.mp3";
			BirdInfo.loadmetaDataFlag = true;
			BirdInfo.getDuration(source);
		}
		else {
			BirdInfo.playBirdVoice();
		}
	}, 1000 * duration + 800);
}

BirdInfo.playBirdVoice = function () {
	clearTimeout(BirdInfo.playsoundTimer);
	document.getElementById("playSound").src = Utils.Path + ("sounds/" + BirdInfo.questions[catNo].data[birdNo].birdVoiceAudio);
	document.getElementById("playSound").play();
}

/*************** */
BirdInfo.onNextClick = function () {
	if (!(BirdInfo.attemptFlag))
		scoredMarks = -1;
	BirdInfo.nextQuestion();
}

BirdInfo.nextQuestion = function () {
	document.getElementById("playSound").pause();
	clearTimeout(BirdInfo.nextQuestionBirdInfor);
	$(".options").css("color", "black");
	console.log(resId, questionId, scoredMarks, totalMarks, level, startBirdInfo, label);
	if (Utils.mobileDeviceFlag)
		Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startBirdInfo, label);

	BirdInfo.scoreFlag = false;
	BirdInfo.attemptFlag = false;
	BirdInfo.firstAttemptFlag = true;
	if (BirdInfo.questionCtr < 5) {

		BirdInfo.initializeVariables();
		BirdInfo.setQuestion();
	}
	else {
		$("#gamePage").hide();
		$("#endPage").show();
	}
}
BirdInfo.createAcknowledgementDivs = function () {
	var i = 0, div;
	for (i = 0; i < Object.keys(BirdInfo.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(BirdInfo.AcknowledgementPageData)[i] + " : " + Object.values(BirdInfo.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 = BirdInfo.hardCodedData.createdOn;
	div.style.marginBottom = "2vh";
	acknowledgementDiv.appendChild(div);
}

BirdInfo.showAcknowledgement = function () {
	$("#acknowledgementDiv").show();

}

BirdInfo.playAgain = function () {
	BirdInfo.questionArr = [];
	$("#acknowledgementDiv").hide();
	$("#endPage").hide();
	$("#gamePage").show();
	BirdInfo.questionCtr = 0;
	BirdInfo.initializeVariables();
	BirdInfo.setQuestion();
}




BirdInfo.callHome = function () {
	if (BirdInfo.attemptFlag) {
		console.log(resId, questionId, scoredMarks, totalMarks, level, startBirdInfo, label);
		if (Utils.mobileDeviceFlag)
			Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startBirdInfo, label);
	}
	location.reload();
}
