//August 2019

var RabbitGame={};
var Utils={};
Utils.Path='';
Utils.mobileDeviceFlag=false;
var questionId=0,startTime=0,scoredMarks=0,totalMarks=10,resId=0;
var firstAttempt=true,finalMarks=0;
RabbitGame.init=function()
{
	if(navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i))
		Utils.mobileDeviceFlag=true;
	
	/* if(Utils.mobileDeviceFlag)
		Utils.Path=Android.getMediaPath("Daud"); */
		
	document.body.style.backgroundImage = "url("+Utils.Path+"images/"+RabbitGame.hardCodedData.cover_page+")";
	RabbitGame.setVariables();	
	RabbitGame.setImages();
}

RabbitGame.setImages=function()
{
	document.getElementById("home").src =Utils.Path+"images/Home.png";
	document.getElementById("credits").src =Utils.Path+"images/credits.png";
	document.getElementById("logo").src =Utils.Path+"images/logo.png";
	document.getElementById("playImage").src =Utils.Path+"images/Play.png";
	document.getElementById("soundImage").src =Utils.Path+"images/sound.png";
	optionDiv.style.backgroundImage = "url("+Utils.Path+"images/greenboard.png"+")";
	
}
//set all variable
RabbitGame.setVariables=function()
{
	RabbitGame.tortoisTimeInterval=0;
	RabbitGame.tortoisPos=0;
	RabbitGame.rabbitTimeInterval=0;
	RabbitGame.rabbitPos=0;
	RabbitGame.checkAnsValue=0;
	RabbitGame.queCount=1;
	RabbitGame.optionsArray=[];
	RabbitGame.loadmetaDataFlag=false;
	RabbitGame.playCarrotFlag=true;
	RabbitGame.playSoundTimeOut=null;
	RabbitGame.homeFlag=true;
	RabbitGame.creditsFlag=true;
	RabbitGame.leftPositionDiffValue=1.5;
	RabbitGame.onCanPlayThroughFlag=true;
}

//play click
RabbitGame.playgame=function()
{
	RabbitGame.homeFlag=true;
	document.body.style.backgroundImage = "url("+Utils.Path+"images/"+RabbitGame.hardCodedData.bg+")";
	var BGMP3=Utils.Path+"sounds/"+RabbitGame.BGSound;
	$('#bgSound').prop('src',BGMP3);
	if(RabbitGame.onCanPlayThroughFlag)
	{
		$('#bgSound').on("canplaythrough", function()
		{
			console.log("hjgfhj");
			RabbitGame.onCanPlayThroughFlag=false;
			$('#bgSound').unbind("canplaythrough");
			document.getElementById("bgSound").play();
			document.getElementById("startPage").style.display="none";
			document.getElementById("home").style.display="block";
			document.getElementById("gamePage").style.display="block";
			document.getElementById("tortoisAnimationDiv").style.display="block";
			document.getElementById("rabbitAnimationDiv").style.display="block";
			document.getElementById("destinationImg").src =Utils.Path+"images/destination.png";
			document.getElementById("destination").style.display="block";
			document.getElementById("soundDiv").style.display="block";
			document.getElementById("rabbitAnimationImg").src =Utils.Path+"images/rabbitStart.png";
			document.getElementById("tortoisAnimationImg").src =Utils.Path+"images/Tortois-walk.gif";
			RabbitGame.startGame();
			RabbitGame.animateTortois();
		});
	}
	else
	{
		$('#bgSound').unbind("canplaythrough");
	}
}

//Tortois animation
RabbitGame.animateTortois=function()
{
	if(RabbitGame.tortoisTimeInterval)
	{
		clearTimeout(RabbitGame.tortoisTimeInterval);
		RabbitGame.tortoisTimeInterval=null;
	}
	RabbitGame.tortoisTimeInterval=setInterval(function()
	{
		RabbitGame.tortoisPos=RabbitGame.tortoisPos+0.3;
		document.getElementById("tortoisAnimationDiv").style.left=RabbitGame.tortoisPos+"%";
		if(RabbitGame.tortoisPos>=85)
		{
			clearInterval(RabbitGame.tortoisTimeInterval);
			RabbitGame.youLose();
		}
	},150);
}

//set carrot for count
RabbitGame.startGame=function()
{	
	var startSound;
	var d=new Date();
	$('.onlyImg').css({pointerEvents: "none"});
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	RabbitGame.createOption();
	RabbitGame.setOptionImages();
	startSound=Utils.Path+"sounds/"+RabbitGame.giveCarrotSound;
	$('#playSound').prop('src',startSound);
	RabbitGame.onCanPlayThroughFlag=true;
	if(RabbitGame.onCanPlayThroughFlag)
	{
		$('#playSound').on("canplaythrough", function()
		{
			console.log($('#playSound'));
			RabbitGame.onCanPlayThroughFlag=false;
			RabbitGame.loadmetaDataFlag=true;
			RabbitGame.getDuration(startSound);
			$('#playSound').unbind("canplaythrough");
		});
	}
	else
	{
		$('#playSound').unbind("canplaythrough");
	}
}

RabbitGame.nextQue=function()
{	
	var startSound;
	var d=new Date();
	$('.onlyImg').css({pointerEvents: "none"});
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	RabbitGame.createOption();
	RabbitGame.setOptionImages();
	RabbitGame.playOneTwoThreeSound();
}
 

//create options values
RabbitGame.createOption=function()
{
	var i=0,num;
	for(i=0;i<=3;i++)
	{
		num=(Math.floor(Math.random() *7 ) + 1);
		if(RabbitGame.optionsArray.includes(num))
		{
			i--;
			continue;
		}
		else
			RabbitGame.optionsArray[i]=num;
	}	
}
//set option images
RabbitGame.setOptionImages=function()
{
	var i=0,j=0,value1=0,value=0,temp=[],num,divId,innrDiv,img;
	for(i=0;i<3;i++)
	{
		num=(Math.floor(Math.random()*3)+1);
		console.log(num);
		if(temp.includes(num))
		{
			i--;
			continue;
		}
		else
		{
			temp[i]=num;
			divId=document.getElementById("optionImg"+num);
			divId.setAttribute("dataValue",RabbitGame.optionsArray[i]);
			for(j=0;j<RabbitGame.optionsArray[i];j++)
			{
				innrDiv=document.createElement('div');
				innrDiv.setAttribute("class","optionAllImg img-responsive nopadding");
				img=document.createElement('img');
				img.setAttribute("class","optionImageClass img-responsive nopadding");
				img.setAttribute("src", Utils.Path+"images/carrot.png");
				divId.appendChild(innrDiv);
				innrDiv.appendChild(img);
			}
		}
	}
}

//get answer
RabbitGame.getAnswer=function(answerID)
{
	var c;
	RabbitGame.checkAnsValue=document.getElementById(answerID).getAttribute("dataValue");
	RabbitGame.checkAnswer(answerID);
}

// check answer onclick of ok 
RabbitGame.checkAnswer=function(answerID)
{
	$('.onlyImg').css({pointerEvents: "none"});
	var checkPos=0;
	questionId=RabbitGame.optionsArray[0];
	if(RabbitGame.optionsArray[0]==RabbitGame.checkAnsValue)
	{
		RabbitGame.bucketAnimation();
		$("#"+answerID).addClass('animated rubberBand');
		setTimeout(function()
		{
			$('#playSound').prop('src',Utils.Path+"sounds/Rabbit_Running.mp3");
			document.getElementById("playSound").play();
			finalMarks=finalMarks+10;
			scoredMarks=10;
			RabbitGame.scoreFun();
			document.getElementById("rabbitAnimationImg").src =Utils.Path+"images/runRabbit.gif";
			if(RabbitGame.rabbitTimeInterval)
			{
				clearTimeout(RabbitGame.rabbitTimeInterval);
				RabbitGame.rabbitTimeInterval=null;
			}
			RabbitGame.rabbitTimeInterval=setInterval(function()
			{
				RabbitGame.rabbitPos=RabbitGame.rabbitPos+RabbitGame.leftPositionDiffValue;
				checkPos=checkPos+1;
				document.getElementById("rabbitAnimationDiv").style.left=RabbitGame.rabbitPos+"%";
						
				if(checkPos==10)
				{
					$("#"+answerID).removeClass('animated rubberBand');
					clearInterval(RabbitGame.rabbitTimeInterval);
					$('.onlyImg').css({pointerEvents: "auto"});
					document.getElementById("rabbitAnimationImg").src =Utils.Path+"images/standRabbit.gif";
					RabbitGame.reset();
				}
				if(RabbitGame.rabbitPos>=85)
				{
					bucketDiv.innerHTML="";
					$("#"+answerID).removeClass('animated rubberBand');
					RabbitGame.youWin();
				}
			},30);	
		},1000);	
	}
	else
	{
		$("#"+answerID).addClass('animated shake');
		firstAttempt=false;
		setTimeout(function()
		{
			RabbitGame.scoreFun();
			$("#"+answerID).removeClass('animated shake');
			RabbitGame.reset();
			
		},700);
	}
}

//reset function
RabbitGame.reset=function()
{
	firstAttempt=true;
	scoredMarks=0;
	RabbitGame.playCarrotFlag=true;
	RabbitGame.optionsArray=[];
	$( ".optionAllImg" ).remove();
	RabbitGame.queCount++;
	if(RabbitGame.rabbitPos<=76)
	{
		RabbitGame.nextQue();
	}
	else
	{
		document.getElementById("optionDiv").style.display="none";
	}
}

//after win
RabbitGame.youWin=function()
{
	document.getElementById("winnerDiv").style.display="block";
	document.getElementById("credits").style.display="block";
	$("#credits").addClass('magictime boingInUp');
	scoredMarks=100;
	totalMarks=100;
	RabbitGame.scoreFun();
	clearInterval(RabbitGame.tortoisTimeInterval);
	clearInterval(RabbitGame.rabbitTimeInterval);
	document.getElementById("endGame").style.display="block";
	document.getElementById("optionsInnerDiv").style.display="none";	
	document.getElementById("soundDiv").style.display="none";
	document.getElementById("rabbitAnimationImg").src =Utils.Path+"images/rabbitWin.gif";	
	document.getElementById("tortoisAnimationImg").src =Utils.Path+"images/Tortoi_sad.gif";
	$('#playSound').prop('src',Utils.Path+"sounds/C4.mp3");
	RabbitGame.playSound();
	document.getElementById("winnerImg").src =Utils.Path+"images/rabbitWin.png";
	document.getElementById("rabbitAnimationDiv").style.left="86%";	
	RabbitGame.AcknowledgementPage();
}
//after loss
RabbitGame.youLose=function()
{
	document.getElementById("winnerDiv").style.display="block";
	document.getElementById("credits").style.display="block";
	$("#credits").addClass('magictime boingInUp');
	scoredMarks=0;
	totalMarks=100;
	RabbitGame.scoreFun();
	clearInterval(RabbitGame.tortoisTimeInterval);
	clearInterval(RabbitGame.rabbitTimeInterval);
	document.getElementById("endGame").style.display="block";
	document.getElementById("optionsInnerDiv").style.display="none";
	document.getElementById("soundDiv").style.display="none";
	document.getElementById("tortoisAnimationImg").src =Utils.Path+"images/Tortois_happy.gif";
	document.getElementById("tortoisAnimationImg").style.height="22vh";
	document.getElementById("rabbitAnimationImg").src =Utils.Path+"images/rabbit_sad.gif";
	document.getElementById("winnerImg").src =Utils.Path+"images/TortoisWin.png";
	$('#playSound').prop('src',Utils.Path+"sounds/wrong_beat_1.mp3");
	RabbitGame.playSound();
	document.getElementById("tortoisAnimationDiv").style.left="90%";
	RabbitGame.AcknowledgementPage();
}

//get duration and play sound
RabbitGame.getDuration=function(soundFile)
{
	var duration=0;
	$('#playSound').prop('src',soundFile);
	if(RabbitGame.loadmetaDataFlag)
	{
		console.log("idfhggggggggg");
		$('#playSound').on("loadedmetadata", function(){
			console.log("idfhggggggggg");
			duration=$('#playSound').prop('duration');
			RabbitGame.loadmetaDataFlag=false;
			$('#playSound').unbind("loadedmetadata");
			if(RabbitGame.playCarrotFlag==true)
			{
				RabbitGame.playCarrotFlag=false;
				RabbitGame.playGiveCarrotSound(duration);
			}
			else
			{
				$('.onlyImg').css({pointerEvents: "auto"});
				RabbitGame.playSound();
			}
		});
	}
	else
		$('#playSound').unbind("loadedmetadata");
}

//paly sound
RabbitGame.playSound=function()
{
	document.getElementById("playSound").play();
	
}
RabbitGame.playGiveCarrotSound=function(duration)
{
	var soundFile;
	document.getElementById("playSound").play();
	if(RabbitGame.playSoundTimeOut)
	{
		clearTimeout(RabbitGame.playSoundTimeOut);
		RabbitGame.playSoundTimeOut=null;
	}
	RabbitGame.playSoundTimeOut=setTimeout(function()
	{
		RabbitGame.playOneTwoThreeSound();
	},duration*1000);
}

//one two three sound
RabbitGame.playOneTwoThreeSound=function()
{
	var soundFile;
	soundFile=Utils.Path+"sounds/"+RabbitGame.oneTwoThreeSounds[(RabbitGame.optionsArray[0])-1];
	$('#playSound').prop('src',soundFile);
	$('.onlyImg').css({pointerEvents: "auto"});
	RabbitGame.loadmetaDataFlag=true;
	RabbitGame.playSound();	
}

//Add_score function 
RabbitGame.scoreFun=function()
{
	questionId=RabbitGame.optionsArray[0];
	if(Utils.mobileDeviceFlag)
		Android.addScore(resId,questionId,scoredMarks,totalMarks,1,startTime);
	else
		console.log(resId,questionId,scoredMarks,totalMarks,1,startTime);
}

//Acknowledgement page
RabbitGame.AcknowledgementPage=function()
{
	var i=0,div;
	RabbitGame.homeFlag=false;
	document.getElementById("bgSound").pause();
	acknowledgementDiv.innerHTML="";
	gameOver.innerHTML=RabbitGame.hardCodedData['game_Over'];
	document.getElementById("gameOverDiv").style.display="block";
	console.log(Object.keys(RabbitGame.AcknowledgementPageData).length)
	for(i=0;i<Object.keys(RabbitGame.AcknowledgementPageData).length;i++)
	{
		div=document.createElement('div');
		div.className="nopadding fontClass firstLetter col-xs-12 col-sm-12 col-md-12 col-lg-12";
		div.innerHTML=Object.keys(RabbitGame.AcknowledgementPageData)[i]+" : "+Object.values(RabbitGame.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=RabbitGame.hardCodedData['createdOn'];
	acknowledgementDiv.appendChild(div);
}

//hide and show credits 
RabbitGame.creditsHideShow=function()
{
	if(RabbitGame.creditsFlag==true)
	{
		RabbitGame.creditsFlag=false;
		document.getElementById("acknowledgementDiv").style.display="block";
		document.getElementById("winnerDiv").style.display="none";
	}
	else
	{
		RabbitGame.creditsFlag=true;
		document.getElementById("acknowledgementDiv").style.display="none";
		document.getElementById("winnerDiv").style.display="block";
	}
}
//
RabbitGame.homeClickFun=function()
{
	document.getElementById("tortoisAnimationImg").style.height="18vh";
	document.getElementById("acknowledgementDiv").style.display="none";
	document.getElementById("winnerDiv").style.display="none";
	if(RabbitGame.homeFlag)
	{
		location.reload();
	}
	else
	{
		firstAttempt=true;
		scoredMarks=0;
		totalMarks=10;
		RabbitGame.playCarrotFlag=true;
		RabbitGame.optionsArray=[];
		$( ".optionAllImg" ).remove();
		document.getElementById("gameOverDiv").style.display="none";
		document.getElementById("acknowledgementDiv").style.display="none";
		document.getElementById("optionsInnerDiv").style.display="block";	
		document.getElementById("credits").style.display="none";
		RabbitGame.setVariables();
		document.getElementById("rabbitAnimationDiv").style.left=RabbitGame.rabbitPos+"%";
		RabbitGame.playgame();
	}
}

RabbitGame.bucketAnimation=function()
{
	var Div
	bucketDiv.innerHTML="";
	Div=document.createElement('div');
	Div.setAttribute("id","bucket"+RabbitGame.rabbitPos);
	Div.setAttribute("class","bucketClass img-responsive nopadding");
	img=document.createElement('img');
	img.setAttribute("class","img-responsive nopadding");
	img.setAttribute("src", Utils.Path+"images/bucket.png");
	bucketDiv.appendChild(Div);
	Div.appendChild(img);
	Div.style.left=RabbitGame.rabbitPos+(RabbitGame.leftPositionDiffValue*15)+"%";
	$("#bucket"+RabbitGame.rabbitPos).addClass('magictime fadeInDownBig');
}
