var Appletree={};
var Utils={};
Utils.Path='';
Utils.mobileDeviceFlag=false;
var questionId=0,startTime=0,scoredMarks=0,totalMarks=10,resId=0;
Appletree.slotsArray=[];
Appletree.mangoCnt=0;
Appletree.attemptCount=0;
Appletree.moveDown='';
Appletree.fruitClickedFlag=false;
Appletree.checkAnswerFlag=false;
Appletree.fruitTimeout='';
$(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("Nishanebaaz");  
	
	setTimeout(function()
	{
		if(Appletree.hardcodedData['languageFont']!=null)
			setFontFamilyForLang("fonts/"+Appletree.hardcodedData['languageFont']);
		document.body.style.backgroundImage="url("+Utils.Path+"images/"+Appletree.hardcodedData.coverImage+")";
		document.getElementById('coverpage').style.display="block";
		document.getElementById('gamepage').style.display="none";
		document.getElementById('nextDiv').style.display="none";
		document.getElementById('home').style.visibility="hidden";
		document.getElementById('boyDiv').style.display="none";
		document.getElementById('soundDiv').style.visibility="hidden";
		Appletree.setImage();
	},200);
});
setFontFamilyForLang=function(fontFilePath){
  var newStyle = "<style>"+
            "@font-face {  "+
              "font-family: 'CustomFontFamily';"  +
                "src: url('"+fontFilePath+"') format('truetype');"+
                "}  </style>";
    $("head").append(newStyle);
    $("body").css("font-family","CustomFontFamily"); 
}
Appletree.showInstruction=function()
{
	document.getElementById('headerId').innerHTML=Appletree.hardcodedData.header;
	document.getElementById('instructionDiv').style.display="block";
	document.getElementById('instructionPara').innerHTML=Appletree.hardcodedData.instruction;
	document.getElementById('soundDiv').style.visibility="hidden";
	document.getElementById('nextDiv').style.display="none";
	document.getElementById('gamepage').style.display="none";
	document.getElementById('playGame').style.display="none";
	document.getElementById('skip').src=Utils.Path+"images/play.png";
	document.getElementById('soundInstructImg').src=Utils.Path+"images/sound.png";
	document.body.style.backgroundImage="url("+Utils.Path+"images/"+Appletree.hardcodedData.bgImage+")";
	
}
Appletree.playGame=function()
{
	document.getElementById('playsound').pause();
	document.body.style.backgroundImage="url("+Utils.Path+"images/"+Appletree.hardcodedData.bgImage+")";
	document.getElementById('instructionDiv').style.display="none";
	document.getElementById('gamepage').style.display="block";
	document.getElementById('coverpage').style.display="none";
	document.getElementById('submitDiv').style.pointerEvents="none";
	document.getElementById('home').style.visibility="visible";
	document.getElementById('nextDiv').style.display="block";
	document.getElementById('boyDiv').style.display="block";
	document.getElementById('soundDiv').style.visibility="visible";
	document.getElementById("playSoundBack").src=Utils.Path+"sounds/Appletreebg.mp3";
	document.getElementById("playSoundBack").loop=true;
	document.getElementById("playSoundBack").play();
	Appletree.createmangoDivs();
	Appletree.getRandomSlots();
}
// set Images//
Appletree.setImage=function()
{
	document.getElementById('logo').src=Utils.Path+"images/logo.png";
	document.getElementById('play').src=Utils.Path+"images/play.png";
	document.getElementById('home').src=Utils.Path+"images/home.png";
	document.getElementById('next').src=Utils.Path+"images/Next.png";
	document.getElementById('questionImg').src=Utils.Path+"images/board.png";
	document.getElementById('boyImg').src=Utils.Path+"images/standingBoy.png";
	document.getElementById('submitImg').src=Utils.Path+"images/correct.png";
	document.getElementById('playAgain').src=Utils.Path+"images/replay.png";
	document.getElementById('credit').src=Utils.Path+"images/credits.png";
	document.getElementById('soundImg').src=Utils.Path+"images/sound.png";
	document.getElementById('cloudImg').src=Utils.Path+"images/cloud.png";
	document.getElementById('treeDiv').style.backgroundImage="url("+Utils.Path+"images/tree.png)";
}
Appletree.playInstruction=function()
{
	document.getElementById('playsound').src=Utils.Path+"sounds/AmazingBrain_H_Pathee.mp3";
	document.getElementById('playsound').play();
}
Appletree.createmangoDivs=function()
{
	var cnt=0;
	for(var i=1;i<=3;i++)
	{
		parentElement=document.createElement('div');
		parentElement.setAttribute('id','parentDiv'+i);
		
		 if(i==2)
			parentElement.setAttribute('class','col-xs-10 col-sm-10 col-lg-10 col-md-10 col-sm-offset-2 col-lg-offset-2 col-md-offset-2 col-xs-offset-2');
		else 
			parentElement.setAttribute('class','col-xs-10 col-sm-10 col-lg-10 col-md-10');
		for(var j=1;j<=5;j++)	
		{		
		 	divDragElm=document.createElement('div');
			divDragElm.setAttribute('id','divDrag'+cnt);
			divDragElm.setAttribute('class','pointerCls noPaddingcls col-sm-2 col-lg-2 col-md-2 col-xs-2 ');
			imgDragElm=document.createElement('img');
			imgDragElm.setAttribute('id','imgDrag'+cnt);
			imgDragElm.setAttribute('class','noPaddingcls mango  img-responsive col-sm-8 col-lg-8 col-md-8 col-xs-8');
			divDragElm.append(imgDragElm);		
			parentElement.append(divDragElm);
			mangoDiv.append(parentElement);
			cnt++;
		}
	}
}
// get random 10 divs //
Appletree.getRandomSlots=function()
{
	var i=0,randomNumber=0;
	var currentLimit=0;
	Appletree.slotsArray.length=0;
	for(var i=0;i<10;)
	{	
		randomNumber=Math.floor(Math.random() *15);
		while($.inArray(randomNumber,Appletree.slotsArray)!=-1)
		{
			randomNumber=Math.floor(Math.random() *15);
		}
		Appletree.slotsArray.push(randomNumber);
		i++;
	}
	Appletree.setAppleImages();
	console.log(currentLimit+" "+Appletree.slotsArray);
	Appletree.setQuestion();
}

// set images to divs //
Appletree.setAppleImages=function()
{
	var randomNumber=0; 
	randomNumber=Math.floor(Math.random() * (Appletree.imageNames.length))
	for(var i=0;i<Appletree.slotsArray.length;i++)
	{	
		$('#imgDrag'+Appletree.slotsArray[i]).prop('src',Utils.Path+'images/'+Appletree.imageNames[randomNumber]);
		document.getElementById('imgDrag'+Appletree.slotsArray[i]).setAttribute('onclick','Appletree.checkElements(this)');
	}	
}
// set question on board //
Appletree.setQuestion=function()
{
	var num=0;
	var d=new Date();
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	Appletree.question=Math.floor(Math.random() *(Appletree.numbers.length));
	lNum=Appletree.numbers[Appletree.question].Lnumber;
	num=Appletree.numbers[Appletree.question].number;
	questionId=Appletree.numbers[Appletree.question].number;
	$("#paraId").text(lNum);
	document.getElementById('playsound').src=Utils.Path+"sounds/"+Appletree.numbers[Appletree.question].sound;
	document.getElementById('playsound').play();
}
Appletree.playSoundAgain=function()
{
	document.getElementById('playsound').src=Utils.Path+"sounds/"+Appletree.numbers[Appletree.question].sound;
	document.getElementById('playsound').play();
}
//gets call when clicked on fruits//
Appletree.checkElements=function(obj)
{
	Appletree.fruitClickedFlag=true;
	var moveNum=0,parentid='',objId='',pParentid='';
	document.getElementById('boyImg').src=Utils.Path+"images/boy.gif";
	document.getElementById('submitDiv').style.pointerEvents="none";
	Appletree.mangoCnt++;
	document.getElementById('playsound').src=Utils.Path+"sounds/throwStone.mp3";
	document.getElementById('playsound').play();
	objId=$(obj).prop('id');
	parentid=$(obj).parent().prop('id');
	pParentid=$('#'+parentid).parent().prop('id');
	$('.pointerCls').css('pointer-events','none');
	$('#'+objId).removeClass('mango');
	Appletree.fruitTimeout=setTimeout(function()
	{
		document.getElementById('playsound').src=Utils.Path+"sounds/Falling.mp3";
		document.getElementById('playsound').play(); 
		Appletree.moveDown=setInterval(function()
		{
			moveNum++;
			document.getElementById(parentid).style.top = moveNum + 'vh';
			if(moveNum == 50)
			{
				document.getElementById('boyImg').src=Utils.Path+"images/standingBoy.png";
				if(Appletree.mangoCnt == 10)
					document.getElementById('playsound').src=Utils.Path+"sounds/10_BG.mp3";

				else
					document.getElementById('playsound').src=Utils.Path+"sounds/"+Appletree.numbers[Appletree.mangoCnt-1].sound;
				document.getElementById('playsound').play();
				document.getElementById('submitDiv').style.pointerEvents="auto";
				$('.pointerCls').css('pointer-events','auto');
				$('#'+parentid).css('pointer-events','none');
				$('#'+parentid).removeClass('pointerCls');
				if( pParentid=="parentDiv2")
				{
					document.getElementById(parentid).style.top = '65vh';
					$('#'+parentid).addClass('rotatefruitz2');
				}
				else if(pParentid=="parentDiv1")
				{
					document.getElementById(parentid).style.top = '76vh';
					$('#'+parentid).addClass('rotatefruitz1');
				}
				else if(pParentid=="parentDiv3")
				{
					
					$('#'+parentid).addClass('rotatefruitz3');
				}
				
				if(Appletree.moveDown)
				{
					clearInterval(Appletree.moveDown);
					Appletree.moveDown=null;
				}
				if(Appletree.fruitTimeout)
				{
					clearTimeout(Appletree.fruitTimeout)
					Appletree.fruitTimeout=null;
				}
				
			}
		},20);
	},1200);
}
Appletree.checkAnswer=function()
{
	Appletree.attemptCount++;
	Appletree.checkAnswerFlag=true;
	document.getElementById('submitDiv').style.pointerEvents="none";
	document.getElementById('nextDiv').style.pointerEvents="none";
	$('.pointerCls').css('pointer-events','none');
	if(Appletree.mangoCnt==Appletree.numbers[Appletree.question].number)
	{
		scoredMarks=10;
		document.getElementById('boyImg').src=Utils.Path+"images/boy-jump.gif";
		document.getElementById('playsound').src=Utils.Path+"sounds/Hureyyy.mp3";
		document.getElementById('playsound').play();
	}	
	else
	{
		scoredMarks=0;
		document.getElementById('boyImg').src=Utils.Path+"images/boy-sad.gif";
		document.getElementById('playsound').src=Utils.Path+"sounds/ohhno.mp3";
		document.getElementById('playsound').play();
	}
	
	console.log(resId,questionId,scoredMarks,10,1,startTime);
	if(Utils.mobileDeviceFlag)
		Android.addScore(resId,questionId,scoredMarks,10,1,startTime);	
	setTimeout(function()
	{
		Appletree.nextQuestion();
	},2000);		
}
Appletree.nextQuestion=function()
{
	document.getElementById('playsound').pause();
	if(Appletree.fruitTimeout)
	{
		clearTimeout(Appletree.fruitTimeout)
		Appletree.fruitTimeout=null;
	}
	if(Appletree.moveDown)
	{
		clearInterval(Appletree.moveDown);
		Appletree.moveDown=null;
	}
	document.getElementById('nextDiv').style.pointerEvents="auto";
	if(Appletree.fruitClickedFlag && Appletree.checkAnswerFlag != true)
	{
		Appletree.fruitClickedFlag=false;
		console.log(resId,questionId,0,10,1,startTime);
		if(Utils.mobileDeviceFlag)
			Android.addScore(resId,questionId,0,10,1,startTime);	
	}
	else if(Appletree.checkAnswerFlag == false)
	{
		console.log(resId,questionId,-2,10,1,startTime);
		if(Utils.mobileDeviceFlag)
			Android.addScore(resId,questionId,-2,10,1,startTime);	
	}
	if(Appletree.attemptCount<5)
	{
		Appletree.fruitClickedFlag=false;
		Appletree.mangoCnt=0;
		Appletree.checkAnswerFlag=false;
		document.getElementById('boyImg').src=Utils.Path+"images/standingBoy.png";
		document.getElementById('mangoDiv').innerHTML='';
		Appletree.createmangoDivs();
		
		Appletree.getRandomSlots();
		document.getElementById('submitDiv').style.pointerEvents="none";
		$('.pointerCls').css('pointer-events','auto');
	}
	else
	{
		Appletree.fruitClickedFlag=false;
		Appletree.showLastPage();
	}
}
// show last page //
Appletree.showLastPage=function()
{
	document.getElementById('soundDiv').style.display="none";
	document.getElementById('buttonDiv').style.display="none";
	document.getElementById('treeDiv').style.display="none";
	document.getElementById('nextDiv').style.display="none";
	document.getElementById("endpage").style.display="block";
	document.getElementById("gamepage").style.display="none";
	document.getElementById('gameOver').innerHTML=Appletree.hardcodedData.message2;
	for(i=0;i<Object.keys(Appletree.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(Appletree.AcknowledgementPageData)[i]+" : "+Object.values(Appletree.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=Appletree.hardcodedData['createdOn'];
	acknowledgementDiv.appendChild(div);
	document.getElementById("acknowledgementDiv").style.display="none";
} 
//gets call on home //
Appletree.HomePage=function()
{
	if(Appletree.fruitClickedFlag && Appletree.checkAnswerFlag != true)
	{
		console.log(resId,questionId,0,10,1,startTime);
		if(Utils.mobileDeviceFlag)
			Android.addScore(resId,questionId,0,10,1,startTime);	
	}
	location.reload(true);
}
Appletree.showCredits=function()
{
	document.getElementById("acknowledgementDiv").style.display="block";
}
Appletree.playAgain=function()
{
	Appletree.slotsArray=[];
	Appletree.mangoCnt=0;
	Appletree.attemptCount=0;
	Appletree.moveDown='';
	Appletree.fruitClickedFlag=false;
	Appletree.checkAnswerFlag=false;
	document.getElementById('soundDiv').style.display="block";
	document.getElementById('buttonDiv').style.display="block";
	document.getElementById('treeDiv').style.display="block";
	document.getElementById('nextDiv').style.display="block";
	document.getElementById("endpage").style.display="none";
	document.getElementById("gamepage").style.display="block";
	acknowledgementDiv.innerHTML='';
	document.getElementById('boyImg').src=Utils.Path+"images/standingBoy.png";
	document.getElementById('mangoDiv').innerHTML='';
	document.getElementById('submitDiv').style.pointerEvents="none";
	$('.pointerCls').css('pointer-events','auto');
	Appletree.createmangoDivs();
	Appletree.getRandomSlots();
}