// 22/10/2018

var BhalooKeAkshar={};
BhalooKeAkshar.slideCount=0;
var Utils={};
Utils.Path='';
Utils.mobileDeviceFlag=false;
var elePosition=null,leftPos=0,askQueFlag=false,queSoundSrc,scoreFlag=true,label;
var questionId=0,startTime=0,scoredMarks=0,totalMarks=10,resId=0;
BhalooKeAkshar.init=function()
{
	//document.body.style.backgroundImage = "url("+Utils.Path+"images/"+BhalooKeAkshar.hardCodedData.cover_page+")";// add if animation
	document.body.style.backgroundImage = "url("+Utils.Path+"images/"+BhalooKeAkshar.hardCodedData.cover_page+")";//remove if animation
	if(navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i))
		Utils.mobileDeviceFlag=true;
	if(Utils.mobileDeviceFlag)
	{
		//Utils.Path=Android.getMediaPath('Rhyme N Show');
	}	
	BhalooKeAkshar.setVariables();
	BhalooKeAkshar.setImages();
}
//set variable
BhalooKeAkshar.setVariables=function()
{
	BhalooKeAkshar.bhalooTimeInterval=0;
	BhalooKeAkshar.bhalooPos=0;
	BhalooKeAkshar.WordsArray=[];
	BhalooKeAkshar.loadmetaDataFlag=false;
	BhalooKeAkshar.checkStatus=null;
	BhalooKeAkshar.ansString="";
	BhalooKeAkshar.queSequenceArray=[];
	BhalooKeAkshar.queCount=0;
	BhalooKeAkshar.answerWord=0;
	BhalooKeAkshar.count=1;
	BhalooKeAkshar.suffixIndex=0;
	BhalooKeAkshar.slideCount++;
	BhalooKeAkshar.homeClickFlag=true;
}

//set images
BhalooKeAkshar.setImages=function()
{	
	//document.getElementById("runBhaloo").src =Utils.Path+"images/BhalooRun.gif";//add
	//document.getElementById("playImage").src =Utils.Path+"images/blankBanner.png";//add
	document.getElementById("playImage").src =Utils.Path+"images/play.png";//remove
	document.getElementById("bhalooImage").src =Utils.Path+"images/panda.png";
	document.getElementById("playSoundImg").src =Utils.Path+"images/Sound.png";
	document.getElementById("home").src =Utils.Path+"images/Home.png";
	document.getElementById("logo").src =Utils.Path+"images/logo.png";
	document.getElementById("nextImg").src =Utils.Path+"images/Next.png";
	playAgainImg.src=Utils.Path+"images/playAgain.png";
	creditsImg.src=Utils.Path+"images/credits.png";
	$("#playSoundImg").css('pointer-events', 'auto');
	$("#playSoundImg").css('opacity', '0.4');
	//BhalooKeAkshar.bhalooMoveForPlay();//add
}
//loader animation of bhaloo and show the play image 
BhalooKeAkshar.bhalooMoveForPlay=function()
{
	if(BhalooKeAkshar.bhalooTimeInterval)
	{
		clearTimeout(BhalooKeAkshar.bhalooTimeInterval);
		BhalooKeAkshar.bhalooTimeInterval=null;
	}
	BhalooKeAkshar.bhalooTimeInterval=setInterval(function()
	{
		BhalooKeAkshar.bhalooPos=BhalooKeAkshar.bhalooPos+4;	
		document.getElementById("runBhaloo").style.left=BhalooKeAkshar.bhalooPos+"%";	
		
		if(BhalooKeAkshar.bhalooPos>=60)
		{
			$("#playImgDiv").addClass('animated playImgAnimation');						
		}
		
		if(BhalooKeAkshar.bhalooPos>90)
		{
			clearInterval(BhalooKeAkshar.bhalooTimeInterval);
			document.getElementById("playImage").src =Utils.Path+"images/blankBannerPlay.png";
			$("#playImgDiv").removeClass('animated playImgAnimation');
			$("#playImage").addClass('animated playImgAfterAni');
			$("#playImage").css('pointer-events', 'auto');
			document.getElementById("runBhaloo").style.display="none";
			return;
		}
		
	},300);
}

//play game
BhalooKeAkshar.playgame=function()
{
	document.body.style.backgroundImage = "url("+Utils.Path+"images/"+BhalooKeAkshar.hardCodedData.backGround+")";
	document.getElementById("gamePage").style.display="block";
	document.getElementById("home").style.display="block";
	document.getElementById("bhalooImageDiv").style.display="block";
	document.getElementById("startPage").style.display="none";
	$("#playSoundImg").css('pointer-events', 'none');
	//document.getElementById("playSoundImg").style.visibility="hidden";
	$(".clickClass").css('pointer-events', 'none');
	$("#nextImg").css('pointer-events', 'none');
	$("#nextImg").css('opacity', '0.4');
	//document.body.style.backgroundImage = "url("+Utils.Path+"images/"+BhalooKeAkshar.hardCodedData.cover_page_2+")";
	var d=new Date();
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	BhalooKeAkshar.selectSuffix();
	BhalooKeAkshar.setWordsToSpan();
}

//select randome one suffix
BhalooKeAkshar.selectSuffix=function()
{
	BhalooKeAkshar.suffixIndex=(Math.floor(Math.random()*(BhalooKeAkshar.wordsData.length)));
	console.log(BhalooKeAkshar.suffixIndex);
	BhalooKeAkshar.selectWords();
}

//select words to show
BhalooKeAkshar.selectWords=function()
{
	var i=0,wordIndex;
	for(i=0;i<3;i++)
	{
		wordIndex=(Math.floor(Math.random()*(BhalooKeAkshar.wordsData[BhalooKeAkshar.suffixIndex].list.length)));
		if(BhalooKeAkshar.WordsArray.includes(wordIndex))
		{
			i--;
			continue;
		}
		else
		{
			BhalooKeAkshar.WordsArray[i]=wordIndex;
		}
	}
	
	
	console.log(BhalooKeAkshar.WordsArray);
}

//set word to span
BhalooKeAkshar.setWordsToSpan=function()
{
	var i=0;
	for(i=1;i<=BhalooKeAkshar.WordsArray.length;i++)
		document.getElementById("word"+i).innerHTML=BhalooKeAkshar.wordsData[BhalooKeAkshar.suffixIndex].list[BhalooKeAkshar.WordsArray[i-1]].word;	
	BhalooKeAkshar.redWords();
}

//bhaloo read the word
BhalooKeAkshar.redWords=function()
{
	var wordSound;
	wordSound=Utils.Path+"sounds/"+BhalooKeAkshar.wordsData[BhalooKeAkshar.suffixIndex].list[BhalooKeAkshar.WordsArray[BhalooKeAkshar.count-1]].sound;
	BhalooKeAkshar.loadmetaDataFlag=true;
	BhalooKeAkshar.getWordsDuration(wordSound);
	
}
// get duration function
BhalooKeAkshar.getWordsDuration=function(sound)
{
	var duration=0;
	//BhaluGame.loadmetaDataFlag=true;
	$('#playSound').prop('src',sound);
	if(BhalooKeAkshar.loadmetaDataFlag)
	{
		$('#playSound').on("loadedmetadata", function(){
			duration=$('#playSound').prop('duration');
			BhalooKeAkshar.loadmetaDataFlag=false;
			$('#playSound').unbind("loadedmetadata");
			BhalooKeAkshar.WordSound(duration);
		});
	}
	else
		$('#playSound').unbind("loadedmetadata");
}

//bhaloo read the word
BhalooKeAkshar.WordSound=function(duration)
{
	console.log(duration);
	document.getElementById("playSound").play();
	if(askQueFlag==true)
	{
		
		setTimeout(function()
		{
			$('#playSound').prop('src',queSoundSrc);
			document.getElementById("playSound").play();
			setTimeout(function()
			{
				$(".clickClass").css('pointer-events', 'auto');
			},1000);
			$("#nextImg").css('pointer-events', 'auto');
			$("#playSoundImg").css('pointer-events', 'auto');
			$("#nextImg").css('opacity', '1');
			$("#playSoundImg").css('opacity', '1');
			
		},duration*1600);
		
	}
	else
	{
		elePosition=$('#word'+BhalooKeAkshar.count).offset();
		$('#bhalooImageDiv').offset({left:elePosition.left-170});
		if(BhalooKeAkshar.playSoundTimeOut)
		{
			clearTimeout(BhalooKeAkshar.playSoundTimeOut);
			BhalooKeAkshar.playSoundTimeOut=null;
		}
		BhalooKeAkshar.playSoundTimeOut=setTimeout(function()
		{
			BhalooKeAkshar.count++;
			if(BhalooKeAkshar.count!=4)
			{
				clearTimeout(BhalooKeAkshar.playSoundTimeOut);
				BhalooKeAkshar.playSoundTimeOut=null;
				BhalooKeAkshar.redWords();
			}
			else
			{
				clearTimeout(BhalooKeAkshar.playSoundTimeOut);
				BhalooKeAkshar.playSoundTimeOut=null;
				setTimeout(function()
				{
					$('#bhalooImageDiv').offset({left:0});
					//document.getElementById("bhalooImage").src =Utils.Path+"images/panda2.png";
					BhalooKeAkshar.selectQuestionSequence();
				},1000);
			}
		},duration*1500);
	}
}

//shuffle the word and red the question word
BhalooKeAkshar.selectQuestionSequence=function()
{
	var num;
	for(i=0;i<3;i++)
	{
		num=(Math.floor(Math.random()*3)+1);
		if(BhalooKeAkshar.queSequenceArray.includes(num))
		{
			i--;
			continue;
		}
		else
		{
			BhalooKeAkshar.queSequenceArray[i]=num;
		}
	}
	setTimeout(function()
	{
		BhalooKeAkshar.askQuestion();
	},1000);
}

//ask question 
BhalooKeAkshar.askQuestion=function()
{
	var que=0,sound=null;
	var d=new Date();
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	$(".clickClass").css('color', 'black');
	if(BhalooKeAkshar.queCount==3)
	{
		$(".clickClass").css('pointer-events', 'none');
		if(BhalooKeAkshar.slideCount==5)
		{
			document.getElementById("gamePage").style.display="none";
			BhalooKeAkshar.homeClickFlag=true;
			BhalooKeAkshar.AcknowledgementPage();
		}
		else
		{
			askQueFlag=false;
			scoreFlag=true;
			BhalooKeAkshar.nextSlideQuestions();
		}
	}
	else
	{
		que=BhalooKeAkshar.queSequenceArray[BhalooKeAkshar.queCount];
		BhalooKeAkshar.answerWord=document.getElementById('word'+que).innerHTML;
		sound=Utils.Path+"sounds/where_is.mp3";									//Can you read this?
		console.log(sound);
		queSoundSrc=Utils.Path+"sounds/"+BhalooKeAkshar.wordsData[BhalooKeAkshar.suffixIndex].list[BhalooKeAkshar.WordsArray[BhalooKeAkshar.queSequenceArray[BhalooKeAkshar.queCount]-1]].sound;
		askQueFlag=true;
		questionId=Number(BhalooKeAkshar.wordsData[BhalooKeAkshar.suffixIndex].list[BhalooKeAkshar.WordsArray[BhalooKeAkshar.queSequenceArray[BhalooKeAkshar.queCount]-1]].questionId);
		label=BhalooKeAkshar.wordsData[BhalooKeAkshar.suffixIndex].list[BhalooKeAkshar.WordsArray[BhalooKeAkshar.queSequenceArray[BhalooKeAkshar.queCount]-1]].word;
		BhalooKeAkshar.loadmetaDataFlag=true;
		BhalooKeAkshar.getWordsDuration(sound);
		BhalooKeAkshar.queCount++;
	}
}

//onclick of next 
BhalooKeAkshar.nextQue=function()
{
	$("#playSoundImg").css('opacity', '0.4');
	$("#playSoundImg").css('pointer-events', 'none');
	document.getElementById("bhalooImage").src =Utils.Path+"images/panda.png";
	BhalooKeAkshar.queCount=3;
	
	if(scoreFlag==false)
		scoredMarks=0;
	else
		scoredMarks=-1;
	BhalooKeAkshar.scoreFun();
	BhalooKeAkshar.askQuestion();
}

//called after three que. completed and load next slide of three que.
BhalooKeAkshar.nextSlideQuestions=function()
{
	$("#nextImg").css('pointer-events', 'none');
	$("#nextImg").css('opacity', '0.4');
	$("#playSoundImg").css('pointer-events', 'none');
	$("#playSoundImg").css('opacity', '0.4');
	BhalooKeAkshar.setVariables();
	BhalooKeAkshar.selectSuffix();
	BhalooKeAkshar.setWordsToSpan();
}


//stop sound
BhalooKeAkshar.stopSound=function()
{
	document.getElementById("playSound").pause();
}
// check Answer
BhalooKeAkshar.checkAnswer=function(clickId)
{
	var correctWrongSoundSrc;
	answerString=document.getElementById(clickId).innerHTML;
	//BhalooKeAkshar.answerWord=BhalooKeAkshar.answerWord.toLowerCase();
	console.log(BhalooKeAkshar.answerWord);
	console.log(answerString);
	$('#'+clickId).css({pointerEvents:'none'});
	if(BhalooKeAkshar.answerWord==answerString)
	{
		if(scoreFlag==true)
			scoredMarks=10;
		BhalooKeAkshar.scoreFun();
		correctWrongSoundSrc=sound=Utils.Path+"sounds/CorrectSound.mp3";
		$('#playSound').prop('src',correctWrongSoundSrc);
		BhalooKeAkshar.playSoundFunction();
		document.getElementById("correctWrongImage").src=Utils.Path+"images/correct.png";
		document.getElementById("responseBoxDiv").style.display="block";
		setTimeout(function()
		{
			document.getElementById("responseBoxDiv").style.display="none";
			document.getElementById("bhalooImage").src =Utils.Path+"images/panda.png";
			BhalooKeAkshar.queCount=3;
			BhalooKeAkshar.askQuestion();
		},1800);
		
	}
	else
	{
		if(scoreFlag==true)
			scoredMarks=5;
		else
			scoredMarks=1;
		scoreFlag=false;
		BhalooKeAkshar.homeClickFlag=false;
		document.getElementById(clickId).style.color="red";
		correctWrongSoundSrc=Utils.Path+"sounds/wrong_ans.mp3";
		$('#playSound').prop('src',correctWrongSoundSrc);
		BhalooKeAkshar.playSoundFunction();
		document.getElementById("correctWrongImage").src=Utils.Path+"images/wrong.png";
		document.getElementById("responseBoxDiv").style.display="block";
		setTimeout(function()
		{
			document.getElementById("responseBoxDiv").style.display="none";
			document.getElementById("playSound").src=Utils.Path+"sounds/Try_again.mp3";
			document.getElementById("playSound").play();
			$("#playSoundImg").css('opacity', '1');
		},1800);
	}
}
 
//play sound
BhalooKeAkshar.playSoundFunction=function()
{
	document.getElementById("playSound").play();
}

//play question sound
BhalooKeAkshar.playQueSound=function()
{
	document.getElementById("playSound").src=queSoundSrc;
	document.getElementById("playSound").play();
}

//Add_score function 
BhalooKeAkshar.scoreFun=function()
{
	if(Utils.mobileDeviceFlag)
		Android.addScore(resId,questionId,scoredMarks,totalMarks,1,startTime,label);
	else
		console.log(resId,questionId,scoredMarks,totalMarks,1,startTime,label);
}

//Acknowledgement page
BhalooKeAkshar.AcknowledgementPage=function()
{
	document.getElementById("playSound").pause();
	document.getElementById("endPage").style.display="block";
	document.getElementById("bhalooImage").style.display="none";
	document.getElementById("lastPageBtn").style.display="block";
	acknowledgementDiv.innerHTML="";
	gameOver.innerHTML=BhalooKeAkshar.hardCodedData.the_end;
	console.log(Object.keys(BhalooKeAkshar.AcknowledgementPageData).length)
	for(i=0;i<Object.keys(BhalooKeAkshar.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(BhalooKeAkshar.AcknowledgementPageData)[i]+" : "+Object.values(BhalooKeAkshar.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=BhalooKeAkshar.hardCodedData['createdOn'];
	acknowledgementDiv.appendChild(div);
}

//play again
BhalooKeAkshar.playAgain=function()
{
	BhalooKeAkshar.setVariables();
	BhalooKeAkshar.slideCount=0;
	elePosition=null;
	leftPos=0;
	askQueFlag=false
	queSoundSrc=0;
	scoreFlag=true;
	document.getElementById("gamePage").style.display="block";
	document.getElementById("bhalooImage").style.display="block";
	document.getElementById("endPage").style.display="none";
	$(".clickClass").css('pointer-events', 'auto');
	$("#nextImg").css('pointer-events', 'none');
	$("#nextImg").css('opacity', '0.4');
	$("#playSoundImg").css('pointer-events', 'none');
	$("#playSoundImg").css('opacity', '0.4');
	BhalooKeAkshar.playgame();
}

//home click
BhalooKeAkshar.homeClick=function()
{
	if(BhalooKeAkshar.homeClickFlag==false)
	{
		scoredMarks=0;
		BhalooKeAkshar.scoreFun();
		location.reload();
	}
	else
	{
		location.reload();
	}
	
}

//show Acknowledgement Page
BhalooKeAkshar.showcCredits=function()
{
	document.getElementById("acknowledgementDiv").style.display="block";
}
