//starting Date:11-01-2022
var FractionPieces = {};
var Utils = {};
Utils.Path = '';
Utils.mobileDeviceFlag = false;
var questionId = 0, startTime = 0, scoredMarks = 0, totalMarks = 10, resId = 0, level = 1, label = '';
var ctx, canvas, ctx2, optionCanvas;
var startAngle = -Math.Pi / 2, rno, numerator = 0, denomenator = 0; startPoint = 0;
var optionHeight, optionWidth;
FractionPieces.ansArr = [];
FractionPieces.numArr = [2, 3, 4, 5, 6, 8, 10, 12]
FractionPieces.ansString = "";
FractionPieces.CakeMakinFlag = false;
FractionPieces.sameDenomFlag = true;
FractionPieces.attemptFlag = false;
ans = 0;
startPoint = 0;
ctr = 0;
biketop = 0;
bikeLeft = 0;
busTop = 0;
busLeft = 0;
topPos = 0, leftPos = 0, imgWidth = 0, imgHeight = 0, bikeHeight = 0, bikeWidth = 0, busHeight = 0, busWidth = 0;

$(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("FractionPieces"); 
	setTimeout(function () {
		FractionPieces.init();
	}, 100);
});
//shows coverPage
FractionPieces.init = function () {
	document.title = Object.values(FractionPieces.AcknowledgementPageData)[0];

	$("#coverPage").show();
	$("#gamePage").hide();
	$("#endPage").hide();
	$("#instrPage").hide();
	$(".header").not("#logo").css("visibility", "hidden");
	document.body.style.backgroundImage = "url(" + Utils.Path + "images/skin/cover.gif";

	FractionPieces.setAllImages();
	FractionPieces.createOptionDivs();
	FractionPieces.initializeVariables();
	// FractionPieces.generateFractions();
	FractionPieces.createAcknowledgementDivs();
	//FractionPieces.createAcknowledgementDivs();
}

FractionPieces.initializeVariables = function () {
	FractionPieces.ansArr = [];
	FractionPieces.arcArr = [];
	FractionPieces.fractionArr = [];
	FractionPieces.numerArr = [];
	FractionPieces.denomArr = [];
	FractionPieces.questionArr = [];
	FractionPieces.addition = 0;
	FractionPieces.questionCtr = 0;
}

FractionPieces.playBackgroundSound=function(){
    clearInterval( FractionPieces.backgroundSoundTimer);
    // document.getElementById("playsound1").pause();
    document.getElementById("playSound1").src = Utils.Path + "sounds/Skylines.mp3";
    document.getElementById("playSound1").play(); 
    FractionPieces.backgroundSoundTimer= setTimeout(function(){
        FractionPieces.playBackgroundSound();
    },150000);
}


//set sources to images
FractionPieces.setAllImages = function () {
	document.getElementById("homeImage").src = Utils.Path + "images/skin/Home.png";
	document.getElementById("logoImage").src = Utils.Path + "images/skin/logo.png";
	document.getElementById("playImg1").src = Utils.Path + "images/skin/Play.png";
	document.getElementById("playImg2").src = Utils.Path + "images/skin/Play.png";
	// document.getElementById("reloadImg").src = Utils.Path + "images/skin/refresh.png";
	document.getElementById("creditImg").src = Utils.Path + "images/skin/credits.png";
	document.getElementById("replayImg").src = Utils.Path + "images/skin/Replay.png";
	document.getElementById("questionImg").src = Utils.Path + "images/skin/Track_1.png";
	document.getElementById("nextImg").src = Utils.Path + "images/skin/next.png";
	//document.getElementById("dontUseImg").src = Utils.Path + "images/skin/dontUse.png";
	//document.getElementById("swapImg").src = Utils.Path + "images/skin/swap.png";
	// document.getElementById("questionText").style.backgroundImage = "url(" + Utils.Path + "images/skin/QuestionBoard.png";
	// document.getElementById("questionDiv").style.backgroundImage = "url(" + Utils.Path + "images/skin/board.png";
	document.getElementById("gameOver").innerHTML = FractionPieces.hardcodedData.gameOver;
	document.getElementById("instrTitle").innerHTML = FractionPieces.hardcodedData.instrTitle;
	document.getElementById("instr1").innerHTML = FractionPieces.hardcodedData.instr1;
	document.getElementById("instr2").innerHTML = FractionPieces.hardcodedData.instr2;
	document.getElementById("instr3").innerHTML = FractionPieces.hardcodedData.instr3;

}

FractionPieces.createAcknowledgementDivs = function () {
	var i = 0, div;
	for (i = 0; i < Object.keys(FractionPieces.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(FractionPieces.AcknowledgementPageData)[i] + " : " + Object.values(FractionPieces.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 = FractionPieces.hardcodedData['createdOn'];
	div.style.marginBottom = "2vh";
	acknowledgementDiv.appendChild(div);
}


FractionPieces.createOptionDivs = function () {
	parentDiv = document.getElementById("optionDiv");
	for (i = 0; i <= 2; i++) {
		option = document.createElement("DIV");
		option.setAttribute("id", "option" + i);
		option.setAttribute("class", "nopadding options col-xs-12 col-md-12 col-lg-12 col-sm-12");
		option.setAttribute("onclick", "FractionPieces.checkAnswer(this.id)");
		parentDiv.appendChild(option);

	}
}
FractionPieces.showInstr = function () {
	document.body.style.backgroundImage = "url(" + Utils.Path + "images/skin/background2.png";
	$("#coverPage").hide();
	$("#instrPage").show();
	$(".header").css("visibility", "visible");
	FractionPieces.playBackgroundSound();	
}

FractionPieces.playGame = function () {
	$("#instrPage").hide();
	$("#gamePage").show();
	$(".header").css("visibility", "visible");
	// document.body.style.backgroundImage = "";
	document.body.style.backgroundColor = "white";
	scoredMarks = 0;
	d = new Date();
	startTime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
	FindPositions();
	FractionPieces.generateDistance();
	FractionPieces.setOptions();
}
FindPositions = function () {
	document.getElementById("questionText").innerHTML = FractionPieces.hardcodedData.questionText1;
	document.getElementById("questionImg").src = Utils.Path + "images/skin/Track_1.png";
	document.getElementById("bikeImg").src = Utils.Path + "images/skin/bike.png";
	document.getElementById("distanceDiv1").innerHTML = 1;
	document.getElementById("distanceDiv2").innerHTML = 1;
	bike = document.getElementById("bikeDiv");
	bikeHeight = bike.offsetWidth;
	bikeWidth = bike.offsetHeight;

	element = document.getElementById("questionImg");
	imgWidth = element.offsetWidth;
	imgHeight = element.offsetHeight;
	console.log(imgWidth, imgHeight);
	position = $('#questionImg').offset();
	topPos = position.top;
	leftPos = position.left;
	bikeTop = topPos + imgHeight - bikeHeight;
	bikeLeft = leftPos;

	//document.getElementById("questionText").innerHTML = "";
	document.getElementById("questionImg").src = Utils.Path + "images/skin/Track_2.png";
	document.getElementById("bikeImg").src = Utils.Path + "images/skin/bus.png";
	bus = document.getElementById("bikeDiv");
	busHeight = bus.offsetWidth;
	busWidth = bus.offsetHeight;

	element = document.getElementById("questionImg");
	img2Width = element.offsetWidth;
	img2Height = element.offsetHeight;
	position = $('#questionImg').offset();
	topPos2 = position.top;
	leftPos2 = position.left;
	busTop = topPos2 + img2Height - busHeight;
	busLeft = leftPos2;
}
FractionPieces.generateDistance = function () {
	// document.getElementById("playSound").pause();
	// document.getElementById("playSound1").src = Utils.Path + "sounds/Skylines.mp3";
	// document.getElementById("playSound1").play();
	if (FractionPieces.questionCtr < 3)
		// randTrack = Math.floor(Math.random() * 2);
		randTrack = 0;
	else
		randTrack = 1;
	if (randTrack == 0) {

		d1 = Math.floor(Math.random() * 15 + 1);
		d2 = Math.floor(Math.random() * 15 + 1);
		while (d1 == d2)
			d2 = Math.floor(Math.random() * 15 + 1);

		if (d1 > d2) {
			max = d1;
			d1 = d2;
			d2 = max;
		}
		FractionPieces.Answer = d1 * 2 + d2 * 2;
		document.body.style.backgroundImage = "url(" + Utils.Path + "images/skin/background.png";
		document.getElementById("questionImg").src = Utils.Path + "images/skin/Track_1.png";
		document.getElementById("bikeImg").src = Utils.Path + "images/skin/bike.png";
		bikeDiv.style.transform = "rotate(270deg)";
		document.getElementById("questionText").innerHTML = FractionPieces.hardcodedData.questionText1;
		document.getElementById("distanceDiv1").innerHTML = d2;
		document.getElementById("distanceDiv2").innerHTML = d1;
		document.getElementById("pumpImg").src = Utils.Path + "images/skin/pump.png";
		document.getElementById("houseImg").src = Utils.Path + "images/skin/house.png";
		$("#pumpDiv").css("margin-top", "-15vh");
		// $("#questionDiv").css("height", "38vh");
		document.getElementById("distanceDiv3").innerHTML = "";
		document.getElementById("distanceDiv4").innerHTML = "";
		document.getElementById("distanceDiv5").innerHTML = "";
		$("#pumpImg").removeClass("nopadding img-responsive col-xs-6 col-md-6 col-lg-6 col-sm-6");
		$("#pumpImg").addClass("nopadding img-responsive col-xs-12 col-md-12 col-lg-12 col-sm-12");

		FractionPieces.moveUp();

	}
	else {
		d1 = Math.floor(Math.random() * 10 + 5);
		d2 = Math.floor(Math.random() * 10 + 5);
		if (d1 > d2) {
			max = d1;
			d1 = d2;
			d2 = max;
		}
		d3 = Math.floor(Math.random() * (d1 - 1) + 1);
		d4 = d1 - d3;
		if (d4 > d3) {
			max1 = d4;
			d4 = d3
			d3 = max1
		}
		d5 = Math.floor(Math.random() * (d2 - Math.ceil(d2 / 2)) + Math.ceil(d2 / 2)) + 1;
		while (d5 == d2 || (d2 - d5) > d4)
			d5 = Math.floor(Math.random() * (d2 - Math.ceil(d2 / 2)) + Math.ceil(d2 / 2)) + 1;
		FractionPieces.Answer = d1 + d2 + d3 + d4 + d5 + (d2 - d5);
		document.body.style.backgroundImage = "url(" + Utils.Path + "images/skin/background2.png";

		document.getElementById("questionImg").src = Utils.Path + "images/skin/Track_2.png";
		document.getElementById("bikeImg").src = Utils.Path + "images/skin/bus.png";
		bikeDiv.style.transform = "rotate(270deg)";
		document.getElementById("questionText").innerHTML = FractionPieces.hardcodedData.questionText2;
		document.getElementById("distanceDiv1").innerHTML = d2;
		document.getElementById("distanceDiv2").innerHTML = d1;
		document.getElementById("distanceDiv3").innerHTML = d3;
		document.getElementById("distanceDiv4").innerHTML = d4;
		document.getElementById("distanceDiv5").innerHTML = d5;
		document.getElementById("pumpImg").src = Utils.Path + "images/skin/student.png";
		document.getElementById("houseImg").src = Utils.Path + "images/skin/school.png";
		$("#pumpImg").remove("nopadding img-responsive col-xs-12 col-md-12 col-lg-12 col-sm-12");
		$("#pumpImg").addClass("nopadding img-responsive col-xs-6 col-md-6 col-lg-6 col-sm-6");
		$("#pumpDiv").css("margin-top", "-30vh");
		// $("#questionDiv").css("height", "42vh");

		FractionPieces.moveBusUp();

	}
	FractionPieces.setOptions();
}

FractionPieces.setOptions = function () {
	FractionPieces.optionArr = [];
	randOption = Math.floor(Math.random() * 3);
	for (i = 0; i <= 2; i++) {
		if (i == randOption) {
			rno = FractionPieces.Answer;
		}
		else {
			rno = Math.floor(Math.random() * 40 + 11);
			while (FractionPieces.optionArr.includes(rno) || rno == FractionPieces.Answer)
				rno = Math.floor(Math.random() * 40 + 11);
		}
		FractionPieces.optionArr.push(rno);
		document.getElementById("option" + i).innerHTML = rno;
	}

}

FractionPieces.moveUp = function () {
	FractionPieces.rideBike = setInterval(function () {
		if ((topPos) < bikeTop) {
			$('#bikeDiv').offset({ top: bikeTop, left: leftPos });
			bikeTop--;
		}
		else {
			clearInterval(FractionPieces.rideBike);
			bikeDiv.style.transform = "rotate(0deg)";
			FractionPieces.moveRight();
		}
	}, 20)
}

FractionPieces.moveRight = function () {
	FractionPieces.rideBike = setInterval(function () {
		if ((leftPos + imgWidth - (bikeHeight * 0.4)) > bikeLeft) {
			$('#bikeDiv').offset({ top: bikeTop, left: bikeLeft });
			bikeLeft++;
		}
		else {
			clearInterval(FractionPieces.rideBike);
			FractionPieces.rideBike = null;
			bikeDiv.style.transform = "rotate(90deg)";
			// alert("stop")
			FractionPieces.moveDown();
		}
	}, 20)
}

FractionPieces.moveDown = function () {
	FractionPieces.rideBike = setInterval(function () {
		if ((topPos + imgHeight - (bikeHeight * 0.4)) > bikeTop) {
			$('#bikeDiv').offset({ top: bikeTop, left: bikeLeft });
			bikeTop++;
		}
		else {
			clearInterval(FractionPieces.rideBike);
			bikeDiv.style.transform = "rotate(180deg)";
			FractionPieces.moveLeft();
		}
	}, 20)
}

FractionPieces.moveLeft = function () {
	FractionPieces.rideBike = setInterval(function () {
		if ((leftPos) < bikeLeft + (bikeHeight * 0.4)) {
			$('#bikeDiv').offset({ top: bikeTop, left: bikeLeft });
			bikeLeft--;
		}
		else {
			//$('#bikeDiv').offset({ top: bikeTop, left: position.left + bikeWidth });
			clearInterval(FractionPieces.rideBike);
			bikeDiv.style.transform = "rotate(270deg)";
			if (!FractionPieces.attemptFlag) {
				document.getElementById("playSound").src = Utils.Path + "sounds/wrongSoundAksharkhadi.mp3";
				document.getElementById("playSound").play();
			}
		}
	}, 20)
}

FractionPieces.moveBusUp = function () {
	FractionPieces.rideBike = setInterval(function () {
		if ((topPos) < busTop) {
			$('#bikeDiv').offset({ top: busTop, left: leftPos });
			busTop--;
		}
		else {
			clearInterval(FractionPieces.rideBike);
			bikeDiv.style.transform = "rotate(0deg)";
			FractionPieces.moveBusRight();
		}
	}, 20)
}

FractionPieces.moveBusRight = function () {
	FractionPieces.rideBike = setInterval(function () {
		if ((leftPos + imgWidth - (busHeight * 0.4)) > busLeft) {
			$('#bikeDiv').offset({ top: busTop, left: busLeft });
			busLeft++;
		}
		else {
			clearInterval(FractionPieces.rideBike);
			FractionPieces.rideBike = null;
			bikeDiv.style.transform = "rotate(90deg)";
			FractionPieces.moveBusDown();
		}
	}, 20)
}

FractionPieces.moveBusDown = function () {
	FractionPieces.rideBike = setInterval(function () {
		if (topPos + (imgHeight * 0.6) > busTop) {
			$('#bikeDiv').offset({ top: busTop, left: busLeft });
			busTop++;
		}
		else {
			clearInterval(FractionPieces.rideBike);
			bikeDiv.style.transform = "rotate(180deg)";
			// document.getElementById("pumpDiv").style.visibility = "hidden";
			FractionPieces.moveBusLeft();
		}
	}, 20)
}

FractionPieces.moveBusLeft = function () {
	FractionPieces.rideBike = setInterval(function () {
		if ((leftPos + (imgWidth * 0.8)) < busLeft) {
			$('#bikeDiv').offset({ top: busTop, left: busLeft });
			busLeft--;
		}
		else {
			busLeft = leftPos + (imgWidth * 0.8)
			$('#bikeDiv').offset({ top: busTop, left: busLeft });

			clearInterval(FractionPieces.rideBike);
			bikeDiv.style.transform = "rotate(90deg)";
			FractionPieces.moveBusDownAgain();

		}
	}, 20)
}

FractionPieces.moveBusDownAgain = function () {
	FractionPieces.rideBike = setInterval(function () {
		if ((topPos + imgHeight - (busHeight * 0.4) > busTop)) {
			$('#bikeDiv').offset({ top: busTop, left: busLeft });
			busTop++;
		}
		else {
			clearInterval(FractionPieces.rideBike);
			bikeDiv.style.transform = "rotate(180deg)";
			FractionPieces.moveBusleftAgain();

		}
	}, 20)
}

FractionPieces.moveBusleftAgain = function () {
	FractionPieces.rideBike = setInterval(function () {
		if ((leftPos) < busLeft + (busHeight * 0.4)) {
			$('#bikeDiv').offset({ top: busTop, left: busLeft });
			busLeft--;
		}
		else {
			clearInterval(FractionPieces.rideBike);
			bikeDiv.style.transform = "rotate(270deg)";
			if (!FractionPieces.attemptFlag) {
				document.getElementById("playSound").src = Utils.Path + "sounds/wrongSoundAksharkhadi.mp3";
				document.getElementById("playSound").play();
			}
		}
	}, 20)
}


FractionPieces.checkAnswer = function (ele) {
	//clearInterval(FractionPieces.rideBike);
	// FractionPieces.rideBike = null;
	FractionPieces.attemptFlag = true;
	if (document.getElementById(ele).innerHTML == FractionPieces.Answer) {
		clearInterval(FractionPieces.rideBike);
		scoredMarks = 10;
		$("#optionDiv").css("pointer-events", "none");
		$("#nextDiv").addClass("blink_me");
		document.getElementById("playSound").src = Utils.Path + "sounds/thats_right.mp3";
		document.getElementById("playSound").play();
		$("#nextDiv").addClass("blink_me");

	}
	else {
		scoredMarks = 0;
		document.getElementById("playSound").src = Utils.Path + "sounds/Try_again.mp3";
		document.getElementById("playSound").play();
	}
}
FractionPieces.nextQuestion = function () {
	document.getElementById("playSound").pause();
	// document.getElementById("playSound1").pause();

	$("#nextDiv").removeClass("blink_me");
	clearInterval(FractionPieces.rideBike);
	FractionPieces.rideBike = null;
	bikeDiv.style.transform = "rotate(0deg)";
	bikeTop = topPos + imgHeight - bikeHeight;
	bikeLeft = leftPos;

	busTop = topPos2 + img2Height - busHeight;
	busLeft = leftPos2;

	if (!FractionPieces.attemptFlag)
		scoredMarks = -1;
	console.log(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
	if (Utils.mobileDeviceFlag) {
		Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
	}
	FractionPieces.attemptFlag = false;
	$("#nextDiv").removeClass("blink_me");
	// document.getElementById("pumpDiv").style.visibility = "visible";

	FractionPieces.questionCtr++;
	$("#optionDiv").css("pointer-events", "auto");
	if (FractionPieces.questionCtr < 5) {
		$('#bikeDiv').offset({ top: topPos, left: leftPos });

		FractionPieces.generateDistance();
	}
	else {
		document.getElementById("playSound1").pause();
		clearInterval(FractionPieces.backgroundSoundTimer);
		$("#gamePage").hide();
		$("#endPage").show();
	}
}

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

FractionPieces.playAgain = function () {
	$("#nextDiv").removeClass("blink_me");
	$("#gamePage").show();
	$("#endPage").hide();
	FractionPieces.questionCtr = 0;
	FractionPieces.initializeVariables();
	FractionPieces.playGame();
	FractionPieces.playBackgroundSound();
}

FractionPieces.callhome = function () {
	if (FractionPieces.attemptFlag) {
		console.log(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
		if (Utils.mobileDeviceFlag) {
			Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
		}
	}
	location.reload();
}
/************ */


FractionPieces.makeOptionCanvas = function () {
	element = document.getElementById("optionDiv");
	optionCanvas = document.getElementById("optionCanvas");
	ctx2 = optionCanvas.getContext("2d");
	optionCanvas.style.width = "100%";
	optionCanvas.width = element.offsetWidth;
	optionCanvas.height = element.offsetHeight;
	optionHeight = element.offsetHeight / 2;
	optionWidth = element.offsetWidth / 2;
	radius = optionWidth / 3;
	ctx2.clearRect(0, 0, optionCanvas.width, optionCanvas.height)
	ctx2.beginPath();
	ctx2.strokeStyle = "black";
	ctx2.lineWidth = 3;
	rno = Math.floor(Math.random() * 7) + 2;
	while (rno == 5 || rno == 7)
		rno = Math.floor(Math.random() * 7) + 2;
	console.log(rno);
	var arc = Math.PI * 2 / 12;
	ctx2.lineTo(optionWidth, optionHeight / 2);
	ctx2.arc(optionWidth, optionHeight / 2, radius, 0, rno * arc);
	ctx2.lineTo(optionWidth, optionHeight / 2);

	var coordinateX = (radius * 3.5 / 5) * Math.cos(rno * arc / 2);
	//console.log(coordinateX);
	var coordinateY = (radius * 3.5 / 5) * Math.sin(rno * arc / 2);
	//console.log(coordinateY);
	ctx2.font = "25px Arial black";
	for (i = 1; i <= rno; i++) {
		if (rno % i == 0 && 12 % i == 0)
			gcd = i;
	}
	numerator = rno / gcd;
	denomenator = 12 / gcd;
	ctx2.fillText(numerator + "/" + denomenator, optionWidth + coordinateX - 15, optionHeight / 2 + coordinateY);
	ctx2.stroke();
}


