//July 2nd 2019
var FortuneWheel = {};
var Utils = {};
Utils.Path = '';
Utils.mobileDeviceFlag = false;
FortuneWheel.loadmetaDataFlag = false;
FortuneWheel.colors = ["#f31919", "#e4e410c2", "#008000b3", "#0000ffc7", "#e88812", "#800080d1", "white", "#000000cf", "#800fd4c7"];
FortuneWheel.names = ["red", "yellow", "green", "blue", "orange", "purple", "white", "black", "indigo"];
FortuneWheel.noOfPlayers = 3;
var questionId = 0, startTime = 0, scoredMarks = 0, resId = 0, totalMarks = 10;
$(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("FortuneWheel");
	setTimeout(function () {
		FortuneWheel.init();
		FortuneWheel.setImages();
		$("#selectText").text("Select no of players");
	}, 100);
}, 200);
FortuneWheel.init = function () {
	$('#gamePage,#endPage').hide();
};
// set Images//
FortuneWheel.setImages = function () {
	document.getElementById('play').src = Utils.Path + "images/Play.png";
	document.getElementById('homeImage').src = Utils.Path + "images/Home.png";
	document.getElementById('logoImage').src = Utils.Path + "images/logo.png";
	//document.getElementById('soundImg').src=Utils.Path+"images/audio.png";
	//document.getElementById('nextImg').src=Utils.Path+"images/Next.png";
	document.body.style.backgroundImage = "url(" + Utils.Path + "images/cover.jpg)";
};
//function which executes on play button click
FortuneWheel.playGame = function () {
	d = new Date();
	startTime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
	$('#coverPage').hide();
	$('#homeImage').css('visibility', 'visible');
	$('#logoImage,#gamePage,#dropDownDiv').show();
	document.body.style.backgroundImage = "url(" + Utils.Path + "images/background.png)";
	FortuneWheel.drawWheel();
};
FortuneWheel.getNoOfPlayers = function (obj) {
	$("#questionDiv").text("");
	$("#myCanvas").css('pointer-events', 'auto');
	FortuneWheel.noOfPlayers = obj.value;
	FortuneWheel.drawWheel();
};
FortuneWheel.drawWheel = function () {
	c = document.getElementById("myCanvas");
	ctx = c.getContext("2d");
	ctx.arc(200, 200, 180, 0, 2 * Math.PI);
	ctx.lineWidth = 4;
	ctx.shadowBlur = "15";
	ctx.shadowColor = "black";
	ctx.stroke();
	FortuneWheel.drawMarker();
	FortuneWheel.drawSlices();
	//FortuneWheel.spinWheel();
};
function deg2rad(deg) {
	return deg * Math.PI / 180;
};
FortuneWheel.drawMarker = function () {
	var c = document.getElementById("Canvas");
	c.style.width = '100%';
	c.width = c.offsetWidth;
	var ctx1 = c.getContext("2d");
	ctx1.beginPath();
	ctx1.fillStyle = "black";
	ctx1.moveTo(160, 60);
	ctx1.lineTo(200, 140);
	ctx1.lineTo(240, 60);
	ctx1.fill();
};
FortuneWheel.spinWheel = function () {
	var cnt = 0, degree = 0, itertions = 0;
	iterations = Math.floor(Math.random() * (25 - 15) + 15);
	if (FortuneWheel.spinInterval) {
		clearInterval(FortuneWheel.spinInterval);
		FortuneWheel.spinInterval = null;
	}
	FortuneWheel.spinInterval = setInterval(function () {
		cnt++;
		if (cnt == iterations)
		{
			clearInterval(FortuneWheel.spinInterval);
			FortuneWheel.showQuestion();
		}
		else 
		{
			degree = degree+10;
			$("#myCanvas").css('transform', "rotate(" + degree + "deg)");
		}
	}, 200);
	$("#myCanvas").css('pointer-events', 'none');
	
};
FortuneWheel.drawSlices = function () {
	var degree = 0;
	ctx.clearRect(0, 0, 500, 500);
	for (var i = 0; i < FortuneWheel.noOfPlayers; i++) {
		ctx.beginPath();
		ctx.fillStyle = FortuneWheel.colors[i];
		ctx.moveTo(200, 200);
		ctx.arc(200, 200, 180, deg2rad(degree), deg2rad(degree + (360 / FortuneWheel.noOfPlayers)));
		ctx.lineTo(200, 200);
		ctx.fill();
		FortuneWheel.drawText(degree + (360 / FortuneWheel.noOfPlayers) / 2, FortuneWheel.names[i]);
		degree = degree + (360 / FortuneWheel.noOfPlayers);
	}
};
FortuneWheel.drawText = function (deg, text) {
	ctx.save();
	ctx.translate(200, 200);
	ctx.rotate(deg2rad(deg));
	ctx.textAlign = "right";
	ctx.fillStyle = "#3ABBF2";
	ctx.font = 'bold 30px sans-serif';
	ctx.fillText(text, 130, 10);
	ctx.restore();
};
FortuneWheel.showQuestion=function(){
var ran=Math.floor(Math.random()*FortuneWheel.questionsCategory.length);
	$("#questionDiv").text(FortuneWheel.questionsCategory[ran].clue);
};
//function which executes on home button click
FortuneWheel.goToHome = function () {
	location.reload(true);
};	