Balloons = {};
var Utils = {};
Utils.Path = '';
Utils.mobileDeviceFlag = false;
var questionId = 0, startTime = 0, totalMarks = 1, resId = 0, indexOfletter = 0, type = "letter";
Balloons.attemptedCnt = 0;
Balloons.balloonImageArr = [];
Balloons.index = 0;
Balloons.no = 5;
Balloons.cnt = 0;
Balloons.starId = 0;
Balloons.totalCnt = 0;
Balloons.scoredMarks = 0;
Balloons.todaysMulAkshare = [];
Balloons.soundFlag = true;
Balloons.goBackFlag = false;
Balloons.homeFlag = false;
//function for loader
$(document).ready(function () {
	$(".se-pre-con").fadeOut("slow");
	if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i))
		Utils.mobileDeviceFlag = true;

	/* if(Utils.mobileDeviceFlag)
	{
		Utils.Path=Android.getMediaPath("Balloon_game"); 
		Balloons.todaysMulAkshare=(Android.getTodaysLetters()).split(',');
	} */

	console.log(Balloons.todaysMulAkshare);
	Balloons.setImages();
	$('#endpage,#firstPage,#gamepage').hide();
	$("#homeDiv").css("visibility", "hidden");
	$('#coverPage').show();


});
Balloons.playGame = function () {
	$('#coverPage').hide();
	$("#homeDiv").css("visibility", "visible");
	getTodaysLetters();
	Balloons.createStars();
	Balloons.searchAndGetIndex(Balloons.mulAkshare, Balloons.todaysMulAkshare);
	document.getElementById('nextImage').style = "visibility:hidden";
	Balloons.showFirstLetter();
}
function getTodaysLetters() {
	var randomIndex;
	while (Balloons.todaysMulAkshare.length !== 2) {
		randomIndex = Math.floor(Math.random() * Balloons.mulAkshare.length);
		if ($.inArray(Balloons.mulAkshare[randomIndex].word, Balloons.todaysMulAkshare) === -1)
			Balloons.todaysMulAkshare.push(Balloons.mulAkshare[randomIndex].word);
	}
}
Balloons.searchAndGetIndex = function (mulAkshare, todaysMulAkshare) {
	Balloons.indexArr = [];
	for (var i = 0; i < todaysMulAkshare.length; i++) {
		for (var j = 0; j < mulAkshare.length; j++) {
			if (mulAkshare[j]['word'] == todaysMulAkshare[i]) {
				Balloons.indexArr.push(j);
				break;
			}
		}
	}
}

Balloons.showFirstLetter = function () {
	Balloons.timer = 0;
	Balloons.attemptedCnt = 0;
	Balloons.homeFlag = false;
	document.body.style.backgroundImage = "url(" + Utils.Path + "images/BG.jpg)";
	document.getElementById('firstPage').style.display = "block";
	document.getElementById('gamepage').style.display = "none";
	document.getElementById('text').innerHTML = Balloons.todaysMulAkshare[Balloons.index];
	$("#soundDiv1").css("pointer-events", "none");
	Balloons.soundFlag = true;
	getDuration(Utils.Path + "sound/" + Balloons.playSound[0]['sound'])
}
function getDuration(src) {
	var duration = 0;
	if (Balloons.soundFlag) {
		$('#playSound').on("loadedmetadata", function () {
			Balloons.soundFlag = false;
			duration = $('#playSound').prop('duration');
			$('#playSound').unbind("loadedmetadata");
			Balloons.playFirstSound(duration);
		});
		$('#playSound').prop('src', src);
	}
}
Balloons.playFirstSound = function (duration) {
	console.log(duration);
	document.getElementById('playSound').play();
	if (Balloons.timer) {
		clearTimeout(Balloons.timer);
		Balloons.timer = null;
	}
	Balloons.timer = setTimeout(function () {
		document.getElementById('playSound').pause();
		document.getElementById('playSound').src = Utils.Path + "sound/" + Balloons.mulAkshare[Balloons.indexArr[Balloons.index]]['sound'];
		document.getElementById('playSound').play();
		$("#soundDiv1").css("pointer-events", "auto");
		document.getElementById('nextImage').style = "visibility:visible";
	}, duration * 1000);
}

//function to set images
Balloons.setImages = function () {
	document.body.style.backgroundImage = "url(" + Utils.Path + Balloons.coverImage + ")";
	document.getElementById('logo').src = Utils.Path + "images/logo.png";
	document.getElementById('sound').src = Utils.Path + "images/Sound.png";
	document.getElementById('sound1').src = Utils.Path + "images/Sound.png";
	document.getElementById('nextImage').src = Utils.Path + "images/Next.png";
	document.getElementById('playImg').src = Utils.Path + "images/Play.png";
	document.getElementById('homeImg').src = Utils.Path + "images/Home.png";

}
Balloons.showFirstPage = function () {
	Balloons.homeFlag = true;
	document.body.style.backgroundImage = "url('" + Utils.Path + "images/BG.jpg')";
	clearTimeout(Balloons.timer);
	document.getElementById('gamepage').style.display = "block";
	document.getElementById('firstPage').style.display = "none";
	document.getElementById('logoDiv').style.display = "block";
	Balloons.createBalloons();
}
//create divs
Balloons.createDivs = function (parentDiv, numberOfDivs, img, classname, word) {
	var temp = 2, newDiv, newImage, para;
	for (var i = 1; i <= numberOfDivs; i++) {
		newDiv = document.createElement("div");
		newDiv.setAttribute("id", word);
		newDiv.setAttribute("class", "" + classname + " col-xs-" + temp + " col-md-" + temp + " col-lg-" + temp + " col-sm-" + temp);
		newDiv.setAttribute("onclick", "Balloons.checkAnswer(this);");
		parentDiv.appendChild(newDiv);

		newImage = document.createElement("img");
		newImage.setAttribute("id", "img" + word);
		newImage.setAttribute("class", "img-responsive noPadding");
		newImage.setAttribute("src", img);
		newDiv.appendChild(newImage);

		para = document.createElement("p");
		para.setAttribute("id", "para" + word);
		para.setAttribute("class", "akshar text-center");
		para.innerHTML = word;
		newDiv.appendChild(para);
	}
}
//to create balloons
Balloons.createBalloons = function () {
	var d, balloonDiv, noOfBalloons = 5;
	$("#balloonDiv").css("pointer-events", "auto");
	Balloons.mulAkshareArr = [];
	d = new Date();
	startTime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
	$('#myModal').hide();
	document.getElementById("balloonDiv").innerHTML = "";
	balloonDiv = document.getElementById("balloonDiv");
	Balloons.balloonImageArr = populate(Balloons.balloonImages, Balloons.balloonImageArr, Balloons.no);
	Balloons.mulAkshareArr.push(Balloons.mulAkshare[Balloons.indexArr[Balloons.index]]);
	if (Balloons.totalCnt == 0 || Balloons.goBackFlag == true)
		Balloons.readText();
	Balloons.mulAkshareArr = populate(Balloons.mulAkshare, Balloons.mulAkshareArr, Balloons.no);
	Balloons.mulAkshareArr = shuffleArray(Balloons.mulAkshareArr);
	for (var i = 0; i < noOfBalloons; i++) {
		Balloons.createDivs(balloonDiv, 1, Balloons.balloonImageArr[i].img, 'paddingClass', Balloons.mulAkshareArr[i].word);
		var div = Balloons.mulAkshareArr[i].word;
		Balloons.animate(div);
	}
}
Balloons.findIndex = function (array, letter) {
	for (var i = 0; i < array.length; i++) {
		if (array[i].word == letter)
			return i;
	}
}
Balloons.createStars = function () {
	var temp, newImage, newDiv, i, numberOfStars = 2;
	for (i = 1; i <= numberOfStars; i++) {
		newDiv = document.createElement("div");
		newDiv.setAttribute("id", "starDiv" + i);
		newDiv.setAttribute("class", "noPadding stars col-xs-2 col-md-2 col-lg-2 col-sm-2 col-xs-offset-1 col-sm-offset-1 col-md-offset-1 col-lg-offset-1");

		newImage = document.createElement("img");
		newImage.setAttribute("id", "starImg" + i);
		newImage.setAttribute("class", "img-responsive");
		newImage.setAttribute("src", Utils.Path + "images/star1.png");
		$(newDiv).append(newImage);
		$("#starDiv").append(newDiv);
	}
};
Balloons.giveStars = function (id) {
	$("#starImg" + id).prop("src", Utils.Path + "images/star2.png");
};
//set animation for balloons
Balloons.animate = function (div) {
	var speed = 0;
	speed = Math.floor((Math.random() * 20000) + 6000);
	$("#" + div).css({ 'top': $('#balloonDiv').height() });
	$("#" + div).animate(
		{
			'top': '-=' + ($(document).height() + ($("#" + div).outerHeight()))
		}, speed, function () {
			$(this).stop();
			id = $(this).prop('id');
			Balloons.animate(id);
		});
}
//this function will execute on click of sound button
Balloons.readText = function () {
	var cnt = 0;
	Balloons.soundArr = [Balloons.mulAkshare[Balloons.indexArr[Balloons.index]].sound, Balloons.playSound[1].sound];
	$("#soundDiv").css("pointer-events", "none");
	$("#balloonDiv").css("pointer-events", "none");
	Balloons.playSoundInARow(cnt);
	questionId = Balloons.mulAkshare[Balloons.indexArr[Balloons.index]].id;
}

Balloons.playSoundInARow = function (cnt) {
	var duration = 0;
	document.getElementById('playSound').pause();
	document.getElementById('playSound').src = Utils.Path + "sound/" + Balloons.soundArr[cnt];
	document.getElementById('playSound').play();
	setTimeout(function () {
		duration = document.getElementById('playSound').duration;
		if (Balloons.Timer) {
			clearTimeout(Balloons.Timer);
			Balloons.Timer = null;
		}
		Balloons.Timer = setTimeout(function () {
			cnt++;
			if (cnt <= 1)
				Balloons.playSoundInARow(cnt);
			else {
				$("#soundDiv").css("pointer-events", "auto");
				$("#balloonDiv").css("pointer-events", "auto");
			}
		}, duration * 1000);
	}, 500);
}
//function to check answer
Balloons.checkAnswer = function (element) {
	var counter = 1, totalMarks = 1, duration = 0, flag = false;
	//Balloons.homeFlag = false;
	Balloons.goBackFlag = false;
	$(".balloonDiv").stop();
	$("#balloonDiv").css("pointer-events", "none");
	$("#soundDiv1").css("pointer-events", "none");
	if (element.id == Balloons.mulAkshare[Balloons.indexArr[Balloons.index]].word.trim()) {
		$(element).hide();
		Balloons.cnt++;
		Balloons.attemptedCnt++;
		counter = 0;
		Balloons.scoredMarks = 1;
	}
	else {
		Balloons.cnt = 0;
		Balloons.scoredMarks = 0;
	}
	Balloons.totalCnt++;
	setTimeout(function () {
		$('#myModal').show();
		$('#myModal').addClass('overlay');
		document.getElementById('answerImg').src = Utils.Path + Balloons.ansMessages[counter]['img'];
		document.getElementById('showMessage').innerHTML = Balloons.ansMessages[counter]['message'];
	}, 500);
	if (counter == 0) {
		/* console.log(Balloons.mulAkshare[Balloons.indexArr[Balloons.index]].word, Balloons.mulAkshare.length, type);
		if (Utils.mobileDeviceFlag)
			Android.addLearntData(Balloons.mulAkshare[Balloons.indexArr[Balloons.index]].word, Balloons.mulAkshare.length, type); */
		document.getElementById('playSound').pause();
		document.getElementById('playSound').src = Utils.Path + "sound/" + Balloons.ansMessages[counter]['sound'];
		document.getElementById('playSound').play();
		Balloons.callThisAfterEveryQ();
	}
	else {
		document.getElementById('playSound').pause();
		document.getElementById('playSound').src = Utils.Path + "sound/" + Balloons.mulAkshare[Balloons.indexArr[Balloons.index]].sound;
		Balloons.goBackFlag = true;
		if (Balloons.intervl) {
			clearInterval(Balloons.intervl);
			Balloons.intervl = null;
		}
		Balloons.intervl = setInterval(function () {
			duration = document.getElementById("playSound").duration;
			if (duration !== NaN || duration == 0) {
				clearInterval(Balloons.intervl);
				console.log(duration);
				if (Balloons.totalCnt < 9)
					document.getElementById('playSound').play();
				if (Balloons.newTimer) {
					clearTimeout(Balloons.newTimer);
					Balloons.newTimer = null;
				}
				Balloons.newTimer = setTimeout(function () {
					if (Balloons.totalCnt < 9) {
						document.getElementById('playSound').pause();
						document.getElementById('playSound').src = Utils.Path + "sound/" + Balloons.ansMessages[counter]['sound'];
						document.getElementById('playSound').play();

					}

					Balloons.callThisAfterEveryQ();
				}, duration * 1000);
			}
		}, 300);
	}
	console.log(resId, questionId, Balloons.scoredMarks, totalMarks, 1, startTime, Balloons.mulAkshare[Balloons.indexArr[Balloons.index]].word);
	if (Utils.mobileDeviceFlag)
		Android.addScore(resId, questionId, Balloons.scoredMarks, totalMarks, 1, startTime, Balloons.mulAkshare[Balloons.indexArr[Balloons.index]].word);
}

Balloons.callThisAfterEveryQ = function () {
	setTimeout(function () {
		if (Balloons.cnt == 3 || Balloons.totalCnt == 9) {
			if (Balloons.cnt == 3) {
				Balloons.starId++;
				Balloons.giveStars(Balloons.starId);
			}
			//if(Utils.mobileDeviceFlag)
			//Android.StudentLevelDetection(Balloons.attemptedCnt,Balloons.totalCnt);
			console.log(Balloons.attemptedCnt, Balloons.totalCnt);
			Balloons.index++;
			Balloons.cnt = 0;
			Balloons.totalCnt = 0;
			if (Balloons.index <= 1)
				Balloons.showFirstLetter();
			else {
				$('#endpage').show();
				Balloons.homeFlag = false;
				document.getElementById('gamepage').innerHTML = "";
				document.getElementById('message').innerHTML = Balloons.endGame[0].message;
				document.getElementById('nextImage').style = "visibility:hidden";
			}
		}
		else
			Balloons.showNextBalloons();
	}, 1000);
}

Balloons.showNextBalloons = function () {
	clearTimeout(Balloons.tempTimer);
	setTimeout(function () {
		Balloons.mulAkshareArr = [];
		$('#myModal').hide();
		$(".balloonDiv").stop();
		$('#balloonDiv').empty();
		if (Balloons.goBackFlag == false)
			Balloons.createBalloons();
		else
			Balloons.showFirstLetter();

	}, 1000);
}
Balloons.callHome = function () {
	clearTimeout(Balloons.timer);
	clearTimeout(Balloons.newTimer);
	clearTimeout(Balloons.Timer);
	clearTimeout(Balloons.tempTimer);
	if (Balloons.homeFlag == true)
		Balloons.showFirstLetter();
	else
		location.reload(true);
}