//24-09-2018

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("Rabbit_Game");
		
	document.body.style.backgroundImage = "url("+Utils.Path+"images/"+RabbitGame.hardCodedData.cover_page+")";
	RabbitGame.setVariables();

	
}
//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;
}

//play click
RabbitGame.playgame=function()
{
	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/standRabbit.png";
	document.getElementById("tortoisAnimationImg").src =Utils.Path+"images/Tortois-walk.gif";
	RabbitGame.startGame();
	RabbitGame.animateTortois();
	
	
}

//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>=88)
		{
			clearInterval(RabbitGame.tortoisTimeInterval);
			RabbitGame.youLose();
		}
	},200);
}

//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;
	RabbitGame.loadmetaDataFlag=true;
	RabbitGame.getDuration(startSound);
}
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() *6 ) + 2);
		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;
	//document.getElementsByClssName("onlyImg").style.pointerEvents = "none";
	
	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;
	if(RabbitGame.optionsArray[0]==RabbitGame.checkAnsValue)
	{
		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+1.5;
			checkPos=checkPos+1;
			document.getElementById("rabbitAnimationDiv").style.left=RabbitGame.rabbitPos+"%";
					
			if(checkPos==10)
			{
				clearInterval(RabbitGame.rabbitTimeInterval);
				//document.getElementsByClssName("onlyImg").style.pointerEvents = "auto";
				$('.onlyImg').css({pointerEvents: "auto"});
				document.getElementById("rabbitAnimationImg").src =Utils.Path+"images/standRabbit.png";
				RabbitGame.reset();
			}
			if(RabbitGame.rabbitPos>=88)
			{
				RabbitGame.youWin();
			}
		},300);
		
		
		
	}
	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()
{
	scoredMarks=100;
	totalMarks=100;
	RabbitGame.scoreFun();
	clearInterval(RabbitGame.tortoisTimeInterval);
	clearInterval(RabbitGame.rabbitTimeInterval);
	//document.getElementById("result").innerHTML=RabbitGame.hardCodedData.you_win;
	//document.getElementById("endText").innerHTML=RabbitGame.hardCodedData.the_end;
	document.getElementById("endGame").style.display="block";
	document.getElementById("gamePage").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.png";
	document.getElementById("rabbitAnimationDiv").style.left="88%";	
	RabbitGame.AcknowledgementPage();
}
//after loss
RabbitGame.youLose=function()
{
	scoredMarks=0;
	totalMarks=100;
	RabbitGame.scoreFun();
	clearInterval(RabbitGame.tortoisTimeInterval);
	clearInterval(RabbitGame.rabbitTimeInterval);
	//document.getElementById("result").innerHTML=RabbitGame.hardCodedData.you_loss;
	//document.getElementById("endText").innerHTML=RabbitGame.hardCodedData.the_end;
	document.getElementById("endGame").style.display="block";
	document.getElementById("gamePage").style.display="none";
	document.getElementById("soundDiv").style.display="none";
	document.getElementById("tortoisAnimationImg").src =Utils.Path+"images/Tortois_happy.gif";
	document.getElementById("rabbitAnimationImg").src =Utils.Path+"images/rabbit_sad.gif";
	document.getElementById("tortoisAnimationDiv").style.left="91%";
	RabbitGame.AcknowledgementPage();
}

//get duration and play sound
RabbitGame.getDuration=function(soundFile)
{
	var duration=0;
	$('#playSound').prop('src',soundFile);
	if(RabbitGame.loadmetaDataFlag)
	{
		$('#playSound').on("loadedmetadata", function(){
			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()
{
	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()
{
	document.getElementById("acknowledgementDiv").style.display="block";
	conceptBy.innerHTML=RabbitGame.AcknowledgementPageData.concept_by;
	artWork.innerHTML=RabbitGame.AcknowledgementPageData.art_work;
	audioRecording.innerHTML=RabbitGame.AcknowledgementPageData.audio_recording;
	music.innerHTML=RabbitGame.AcknowledgementPageData.music;
	developerName.innerHTML=RabbitGame.AcknowledgementPageData.developed_by;
}
