NadiKePaar = {};
var Utils = {};
Utils.Path = '';
Utils.mobileDeviceFlag = false;
var questionId = 0, startTime = 0, totalMarks = 10, resId = 0, scoredMarks = 0;
NadiKePaar.noOfTimeQuestionAnswered = 0;
NadiKePaar.totalDistance = 60;
NadiKePaar.attemptFlag = false;
NadiKePaar.goBack = null;
NadiKePaar.muteFlag=false;
//function for loader
$(document).ready(function () {
	$('title').text(NadiKePaar.gameTitle);
	$(".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("ChitraVichitra");  
		$(".tooltiptext").css("display", "none");
	}
	setTimeout(function () {
		document.body.style.backgroundImage = "url(" + Utils.Path + "images/Cover.png)";
		document.getElementById('coverpage').style.display = "block";
		document.getElementById('middlepage').style.display = "none";
		document.getElementById('home').style.display = "none";
		document.getElementById('gamapage').style.display = "none";
		document.getElementById('help').style.display = "none";
		$("#muteAudioDiv").css("visibility","hidden");
		NadiKePaar.setImages();
		NadiKePaar.movingBoat();
		for (var i = 0; i < 5; i++) {
			NadiKePaar.createLevelWiseDivs(timerDiv, 1, NadiKePaar.showTimeRequire.imgname + i + NadiKePaar.showTimeRequire.imgtype, "noPadding travelCntClass", "travelCnt" + i, 4);
		}
		NadiKePaar.createLevelWiseDivs(timerDiv, 1, "", "noPadding", "timer", "");
		$("#cloudDiv").show();
		document.getElementsByTagName("marquee")[0].start();
	}, 300);
});
//function to set images
NadiKePaar.setImages = function () {
	document.getElementById('logo').src = Utils.Path + "images/logo.png";
	document.getElementById('help').src = Utils.Path + "images/help.png";
	document.getElementById('play').src = Utils.Path + "images/Play.png";
	document.getElementById('easy').src = Utils.Path + "images/easy.png";
	document.getElementById('mid').src = Utils.Path + "images/mid.png";
	document.getElementById('high').src = Utils.Path + "images/high.png";
	document.getElementById('highest').src = Utils.Path + "images/highest.png";
	document.getElementById('boat').src = Utils.Path + "images/boat.png";
	document.getElementById('home').src = Utils.Path + "images/Home.png";
	document.getElementById('replayImage').src = Utils.Path + "images/playAgain.png";
	document.getElementById('credit').src = Utils.Path + "images/credits.png";
	document.getElementById('movingboatImg').src = Utils.Path + "images/boat.png";
	document.getElementById("cloud").src = Utils.Path + "images/skyClouds.png";
	
}
//this function will execute on click of play button
NadiKePaar.playGame = function () {
	NadiKePaar.goBack = null;
	NadiKePaar.noOfTimeQuestionAnswered = 0;
	$("#muteAudioDiv").css("visibility","hidden");
	NadiKePaar.muteFlag=false;
	document.getElementById("muteAudioImg").src = Utils.Path + "images/sound.png";
	document.getElementById("playSoundInLoop").src = Utils.Path + "sound/BG_track.mp3";
	document.getElementById("playSoundInLoop").loop = true;
	$("#divhuman").remove();
	$("#divobjectDiv1").remove();
	$("#divobjectDiv2").remove();
	document.getElementById("playSound").pause();
	document.getElementById("playSoundInLoop").pause();
	document.getElementById("playSoundOfMotor").pause();
	document.getElementById('help').style.display = "none";
	document.getElementById('endpage').style.display = "none";
	document.getElementById("myModal").style.display = "none";
	document.body.style.backgroundImage = "url('" + Utils.Path + "images/river.png')";
	document.getElementById('gamapage').style.display = "none";
	document.getElementById('middlepage').style.display = "block";
	document.getElementById('home').style.display = "block";
	document.getElementById('coverpage').style.display = "none";
	console.log(NadiKePaar.levelNumber);
	NadiKePaar.randomIndex = Math.floor(Math.random() * NadiKePaar.combination[0].list.length);
}
//function to create Level Wise Divs
NadiKePaar.createLevelWiseDivs = function (parentDiv, numberOfDivs, img, className, idName, levelNumber, numText) {
	var temp, newDiv, newImage, para, subDiv;
	if (idName == "human")
		temp = 4;
	else if (parentDiv == leftDiv || parentDiv == rightDiv)
		temp = 12;
	else if (parentDiv == timerDiv)
		temp = 1;
	for (var i = 1; i <= numberOfDivs; i++) {
		if (parentDiv == leftDiv || parentDiv == rightDiv) {
			subDiv = document.createElement("div");
			subDiv.setAttribute("id", "subDiv" + idName);
			subDiv.setAttribute("class", className + " heightClass");
			parentDiv.appendChild(subDiv);
		}
		if (parentDiv != rightDiv) {
			newDiv = document.createElement("div");
			newDiv.setAttribute("id", "div" + idName);
			if (idName == "objectDiv2" || idName == "objectDiv1")
				newDiv.setAttribute("class", className + " noPadding  clickClass positionClass ");
			else if (idName == "human" || parentDiv == timerDiv)
				newDiv.setAttribute("class", className + " col-xs-" + temp + " col-md-" + temp + " col-lg-" + temp + " col-sm-" + temp);
			else
				newDiv.setAttribute("class", className + " heightClass");
			if (idName != "human" && idName != "objectDiv2" && idName != "objectDiv1")
				newDiv.setAttribute("onclick", "NadiKePaar.pushObjectsIntoLevel(this);");
			if (parentDiv != leftDiv && parentDiv != rightDiv)
				parentDiv.appendChild(newDiv);
			else
				subDiv.appendChild(newDiv);
		}
		if (parentDiv == leftDiv || idName == "human" || parentDiv == timerDiv && idName != "timer") {
			newImage = document.createElement("img");
			newImage.setAttribute("id", "img" + idName);
			if (idName == "human")
				newImage.setAttribute("class", "img-responsive col-lg-12 col-md-12 col-xs-12 col-sm-12 noPadding");
			else if (parentDiv == timerDiv)
				newImage.setAttribute("class", "img-responsive noPadding col-lg-12 col-md-12 col-xs-12 col-sm-12");
			else
				newImage.setAttribute("class", "img-responsive noPadding imgClass");
			newImage.setAttribute("src", Utils.Path + img);
			newDiv.appendChild(newImage);
		}
	}
	if (parentDiv == timerDiv)
		$("#timerDiv").children().css("pointer-events", "none");
}
//set conditions to initial
NadiKePaar.setToInitial = function () {
	var boatDiv;
	document.getElementById('endpage').style.display = "none";
	var d = new Date();
	startTime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
	clearInterval(NadiKePaar.myVar);
	document.getElementById('middlepage').style.display = "none";
	document.body.style.backgroundImage = "url('" + Utils.Path + "images/river.png')";
	document.getElementById('gamapage').style.display = "block";
	NadiKePaar.flag = true;
	NadiKePaar.left = 0;
	$("#rightDiv").children().css("pointer-events", "auto");
	$("#leftDiv").children().css("pointer-events", "auto");
	$(".clickClass").css("pointer-events", "auto");
	document.getElementById("leftDiv").innerHTML = "";
	document.getElementById("rightDiv").innerHTML = "";
	boatDiv = document.getElementById("boatDiv");
	$("#boatDiv").removeClass("rotate");
	$("#boatDiv").addClass("rotateX");
}
// push/pop animals onto boat
NadiKePaar.pushObjectsIntoLevel = function (element) {
	document.getElementById("playSound").pause();
	document.getElementById("playSound").src = Utils.Path + "sound/ting.mp3";
	document.getElementById("playSound").play();
	NadiKePaar.attemptFlag = true;
	if (document.getElementById("leftDiv").contains(element) || document.getElementById("rightDiv").contains(element)) {
		$(element).removeClass("animated rollIn");
		$("#boat").css("pointer-events", "auto");
		if ((document.getElementById("divobjectDiv1").children).length == 0)
			$("#divobjectDiv1").append(element);
		else if (NadiKePaar.levelNumber != 1 && (document.getElementById("divobjectDiv2").children).length == 0)
			$("#divobjectDiv2").append(element);
		$(element).addClass("animated rollIn");
		if ($("#divhuman").children().length != 0)
			$(element).parent().height($(element).height());
		if (($("#mainDiv").children().children().length) == 3) {
			$("#leftDiv").children().css("pointer-events", "none");
			$("#rightDiv").children().css("pointer-events", "none");
		}
		else if ((NadiKePaar.levelNumber == 1 || NadiKePaar.levelNumber == 3 || NadiKePaar.levelNumber == 4) && ($("#mainDiv").children().children().length) == 2) {
			$("#leftDiv").children().css("pointer-events", "none");
			$("#rightDiv").children().css("pointer-events", "none");
		}
	}
	else {
		if (NadiKePaar.flag == true) {
			$("#subDiv" + $(element).data("pos")).append(element);
			$("#leftDiv").children().css("pointer-events", "auto");
		}
		else {
			$("#subDivright" + $(element).data("pos")).append(element);
			$("#rightDiv").children().css("pointer-events", "auto");
		}
		$(element).css("top", "0vh");
	}
	if ((NadiKePaar.levelNumber == 1 && $("#rightDiv").children().children().length == 3) || (NadiKePaar.levelNumber == 3 && $("#rightDiv").children().children().length == 6) || ((NadiKePaar.levelNumber == 2 || (NadiKePaar.levelNumber == 4)) && $("#rightDiv").children().children().length == 5))
		NadiKePaar.checkAnswer(NadiKePaar.levelNumber);
}
//starts the boat
NadiKePaar.startBoat = function () {
	NadiKePaar.speed = 0;
	if (NadiKePaar.myVar || NadiKePaar.counter) {
		clearInterval(NadiKePaar.myVar);
		clearInterval(NadiKePaar.counter);
		NadiKePaar.counter = null;
		NadiKePaar.myVar = null;
	}
	if (NadiKePaar.levelNumber == 4) {
		for (var i = 0; i < $("#mainDiv").children().children().length; i++) {
			if ($("#mainDiv").children().children().length == 1 || $("#" + $("#mainDiv").children().children()[i].id).data("answer") > $("#" + $("#mainDiv").children().children()[i + 1].id).data("answer"))
				NadiKePaar.speed = $("#" + $("#mainDiv").children().children()[i].id).data("answer");
			else
				NadiKePaar.speed = $("#" + $("#mainDiv").children().children()[i + 1].id).data("answer");
			break;
		}
	}
	if ($("#mainDiv").children().children().length != 0) {
		if (NadiKePaar.levelNumber == 4) {
			NadiKePaar.myVar = setInterval(myTimer, ((NadiKePaar.speed * 1000) / NadiKePaar.totalDistance) + 1);
			NadiKePaar.counter = setInterval(myCounter, 1000);
		}
		else
			NadiKePaar.myVar = setInterval(myTimer, 50);
		document.getElementById("playSound").pause();
		document.getElementById("playSoundOfMotor").src = Utils.Path + "sound/motor.mp3";
		document.getElementById("playSoundOfMotor").loop = true;
		document.getElementById("playSoundOfMotor").play();
	}
	function myCounter() {
		document.getElementById("divtimer").innerHTML = --NadiKePaar.counterVar;
		if (NadiKePaar.counterVar == 0) {
			NadiKePaar.checkHighestLevel();
			clearInterval(NadiKePaar.counter);
		}
	}
	function myTimer() {
		$(".clickClass").css("pointer-events", "none");
		$("#leftDiv").children().css("pointer-events", "none");
		$("#rightDiv").children().css("pointer-events", "none");
		if (NadiKePaar.flag == true) {
			boatDiv.style.left = (NadiKePaar.left + 1) + "%";
			NadiKePaar.left++;
			NadiKePaar.nightana = true;
			NadiKePaar.pochlyavr = false;
			if (NadiKePaar.left == NadiKePaar.totalDistance) {
				clearInterval(NadiKePaar.myVar);
				clearInterval(NadiKePaar.counter);
				document.getElementById("playSound").pause();
				document.getElementById("playSoundOfMotor").pause();
				NadiKePaar.nightana = false;
				NadiKePaar.pochlyavr = true;
				NadiKePaar.flag = false;
				$("#rightDiv").children().css("pointer-events", "auto");
				$(".clickClass").css("pointer-events", "auto");
				NadiKePaar.checkAnswer(NadiKePaar.levelNumber);
			}
		}
		else {
			boatDiv.style.left = (NadiKePaar.left - 1) + "%";
			NadiKePaar.left--;
			$("#boatDiv").removeClass("rotateX");
			$("#boatDiv").addClass("rotate");

			NadiKePaar.nightana = true;
			NadiKePaar.pochlyavr = false;
			if (NadiKePaar.left == 0) {
				clearInterval(NadiKePaar.myVar);
				clearInterval(NadiKePaar.counter);
				document.getElementById("playSound").pause();
				document.getElementById("playSoundOfMotor").pause();
				NadiKePaar.flag = true;
				NadiKePaar.pochlyavr = true;
				NadiKePaar.nightana = false;
				$("#leftDiv").children().css("pointer-events", "auto");
				$(".clickClass").css("pointer-events", "auto");
				$("#boatDiv").removeClass("rotate");
				$("#boatDiv").addClass("rotateX");
				NadiKePaar.checkAnswer(NadiKePaar.levelNumber);
			}
		}
	}
	NadiKePaar.nightana = true;
	NadiKePaar.pochlyavr = false;
	if (($("#mainDiv").children().children().length == 0) && (NadiKePaar.levelNumber == 3 || NadiKePaar.levelNumber == 4))
		$("#boat").css("pointer-events", "none");
	else
		NadiKePaar.checkAnswer(NadiKePaar.levelNumber);
}
//check Answer
NadiKePaar.checkAnswer = function (level) {
	var leftSide = [], rightSide = [];
	NadiKePaar.arrayOfFunction = [NadiKePaar.checkEasyLevel, NadiKePaar.checkMidLevel, NadiKePaar.checkHighLevel, NadiKePaar.checkHighestLevel];
	for (var i = 0; i < $("#leftDiv").children().children().length; i++)
		leftSide.push($("#leftDiv").children().children().eq(i).attr('id'));
	for (var i = 0; i < $("#rightDiv").children().children().length; i++)
		rightSide.push($("#rightDiv").children().children().eq(i).attr('id'));
	if (level == 1) {
		if (NadiKePaar.left == 0)
			NadiKePaar.arrayOfFunction[0](leftSide, $("#leftDiv"));
		else
			NadiKePaar.arrayOfFunction[0](rightSide, $("#rightDiv"));
	}
	else if (level == 3) {
		if (NadiKePaar.left == 0)
			NadiKePaar.arrayOfFunction[1](leftSide, $("#leftDiv"));
		else
			NadiKePaar.arrayOfFunction[1](rightSide, $("#rightDiv"));
	}
	else if (level == 2) {
		if (NadiKePaar.left == 0)
			NadiKePaar.arrayOfFunction[2](leftSide, $("#leftDiv"));
		else
			NadiKePaar.arrayOfFunction[2](rightSide, $("#rightDiv"));
	}
	else
		NadiKePaar.arrayOfFunction[3](rightSide, $("#rightDiv"));
}
//shows next level sequence
NadiKePaar.showNextSequence = function (counter) {
	$('#closeDiv').hide();
	$('#myModal').show();
	$('#showHelpMessage').show();
	clearInterval(NadiKePaar.myVar);
	document.getElementById("playSoundOfMotor").pause();
	document.getElementById("modalDiv").style.display = "block";
	document.getElementById("acknowledgementDiv").innerHTML = "";
	$('#showHelpMessage').css("fontSize", "7vw");
	$('#showHelpMessage').css("margin-top", "6vh");
	$('#myModal').addClass('overlay');
	document.getElementById('answerImg').src = Utils.Path + NadiKePaar.ansMessages[counter]['img'];
	document.getElementById('showHelpMessage').innerHTML = NadiKePaar.ansMessages[counter]['message'];
	NadiKePaar.playSounds(counter);
	setTimeout(function () {
		if (scoredMarks == 0)
			NadiKePaar.showNextGame(NadiKePaar.levelNumber);
		else
			NadiKePaar.showLastPage();
	}, 2000);
}
NadiKePaar.showNextGame = function (level) {
	//clearInterval(NadiKePaar.myVar);
	NadiKePaar.attemptFlag = false;
	NadiKePaar.muteFlag=false;
	document.getElementById("muteAudioImg").src = Utils.Path + "images/sound.png";
	setTimeout(function () {
		$('#myModal').hide();
		$('#showHelpMessage').css("fontSize", "2vw");
		$('#showHelpMessage').css("margin-top", "0%");
		$('#showHelpMessage').css("margin-left", " 8.33333333%%");
		if (level == 1)
			NadiKePaar.createEasyLevelObjects();
		else if (level == 2)
			NadiKePaar.createMidLevelObjects();
		else if (level == 3)
			NadiKePaar.createHighLevelObjects();
		else
			NadiKePaar.createHighestLevelObjects();
	}, 2000);
}
NadiKePaar.playSounds = function (counter) {
	document.getElementById("playSound").pause();
	document.getElementById("playSound").src = Utils.Path + "sound/" + NadiKePaar.Sounds[counter].sound;
	document.getElementById("playSound").play();
}
NadiKePaar.showHelp = function () {
	var sentence, i = 0;
	$('#closeDiv').show();
	$('#showHelpMessage').css("fontSize", "2vw");
	document.getElementById("modalDiv").style.display = "none";
	$('#myModal').show();
	$("#msgDiv").scrollTop(0);
	$('#myModal').addClass('overlay');
	sentence = NadiKePaar.combination[NadiKePaar.levelNumber - 1].help;
	if (sentence.indexOf('@') != -1)
		sentence = NadiKePaar.combination[NadiKePaar.levelNumber - 1].help.replace(/@/gi, (NadiKePaar.combination[NadiKePaar.levelNumber - 1].list[NadiKePaar.randomIndex].obj[i]));
	if (sentence.indexOf('%') != -1)
		sentence = sentence.replace(/%/gi, (NadiKePaar.combination[NadiKePaar.levelNumber - 1].list[NadiKePaar.randomIndex].obj[++i]));
	if (sentence.indexOf('#') != -1)
		sentence = sentence.replace(/#/gi, (NadiKePaar.combination[NadiKePaar.levelNumber - 1].list[NadiKePaar.randomIndex].obj[++i]));
	if (sentence.indexOf('$') != -1)
		sentence = sentence.replace('$', (NadiKePaar.combination[NadiKePaar.levelNumber - 1].list[NadiKePaar.randomIndex].obj[++i]));
	if (sentence.indexOf('^') != -1)
		sentence = sentence.replace('^', (NadiKePaar.combination[NadiKePaar.levelNumber - 1].list[NadiKePaar.randomIndex].obj[++i]));
	document.getElementById('showHelpMessage').innerHTML = sentence;
}
NadiKePaar.muteAudio=function()
{
	if(NadiKePaar.muteFlag==false)
	{
		NadiKePaar.muteFlag=true;
		document.getElementById("muteAudioImg").src = Utils.Path + "images/muteSound.png";
		document.getElementById("playSoundInLoop").pause();
	}
	
	else
	{
		NadiKePaar.muteFlag=false;
		document.getElementById("muteAudioImg").src = Utils.Path + "images/sound.png";
		document.getElementById("playSoundInLoop").play();
	}
}
NadiKePaar.closeModal = function () {
	$('#myModal').hide();
}
NadiKePaar.callHome = function () {
	if (NadiKePaar.attemptFlag) {
		scoredMarks = 0;
		NadiKePaar.attemptFlag = false;
		NadiKePaar.noOfTimeQuestionAnswered = 0;
		NadiKePaar.noOfTimeQuestionAnswered++;
		NadiKePaar.calculateScore();
	}
	if (NadiKePaar.goBack == "levelMenu")
		NadiKePaar.playGame();
	else
		location.reload();
}
NadiKePaar.playAgain = function () {
	NadiKePaar.noOfTimeQuestionAnswered = 0;
	NadiKePaar.showNextGame(NadiKePaar.levelNumber);
}
NadiKePaar.showLastPage = function () {
	setTimeout(function () {
		$('#gamapage,#help').hide();
		$('#endpage').show();
		document.getElementById('message').innerHTML = NadiKePaar.hardcoded.gameOver;
		document.getElementById("message").style.visibility = "visible";
		$("#muteAudioDiv").css("visibility","hidden");
		NadiKePaar.createHardcodeddivs();
		document.getElementById("acknowledgementDiv").style.display = "none";
	}, 2000);
}
NadiKePaar.createHardcodeddivs = function () {
	var ackDiv;
	for (var i = 0; i < Object.keys(NadiKePaar.acknowledgementData).length; i++) {
		ackDiv = document.createElement("div");
		ackDiv.setAttribute("id", "ack" + i);
		ackDiv.setAttribute("class", "noPadding firstLetter text-center fontClass col-sm-12 col-lg-12 col-md-12 col-xs-12");
		ackDiv.innerHTML = Object.keys(NadiKePaar.acknowledgementData)[i] + " : " + Object.values(NadiKePaar.acknowledgementData)[i];
		document.getElementById("acknowledgementDiv").appendChild(ackDiv);
	}
	div = document.createElement('div');
	div.className = "noPadding fontClass firstLetter text-center col-xs-12 col-sm-12 col-md-12 col-lg-12";
	div.innerHTML = NadiKePaar.hardcoded['createdOn'];
	document.getElementById("acknowledgementDiv").appendChild(div);
}
NadiKePaar.showCredit = function () {
	document.getElementById("acknowledgementDiv").style.display = "block";
}
NadiKePaar.calculateScore = function () {
	//if(NadiKePaar.noOfTimeQuestionAnswered==1)
	//{
	console.log(resId, questionId, scoredMarks, totalMarks, NadiKePaar.levelNumber, startTime);
	if (Utils.mobileDeviceFlag)
		Android.addScore(resId, questionId, scoredMarks, totalMarks, NadiKePaar.levelNumber, startTime);
	//}
}
NadiKePaar.movingBoat = function () {
	var movingBoatDiv = $("#movingBoatDiv");
	waterFloat(movingBoatDiv, 1000, 1, 5);				//(target element,duration,degree,vertical offset)
}