var Laddoo={};
Laddoo.i=0;
Laddoo.ans=0;
var gameId=null;
Laddoo.startLaddooGame=function(id)
{
	gameId=id;
	$(".se-pre-con").fadeOut("slow");
	setTimeout(function()
	{
		document.getElementById('endPage').style.display="none";
		Laddoo.setImage();
		Laddoo.createOptionDivs();
		Laddoo.playGame();
 	},200);
}
Laddoo.playGame=function()
{
	document.body.style.backgroundImage="url('images/BG.png')";
	document.getElementById('sweetGamePage').style.display="block";
	$('.handEvent').css({'pointer-events':'none'});
}
// set Images//
Laddoo.setImage=function()
{
	document.getElementById('hand1img').src="images/hand2.png";
	document.getElementById('hand2img').src="images/hand2.png";
	document.getElementById('hand3img').src="images/hand2.png";
	document.getElementById('orderHereImg').src="images/order.png";
	document.getElementById('trayimg').src="images/tray.png";
	document.getElementById('bagimg').src="images/bag.png";
	document.getElementById('checkimg').src="images/done.png";
	document.getElementById('laddooImg').src="images/laddoo.png";
	document.getElementById('bagLaddoo').src="images/bag.png";
	document.getElementById('UndooLaddooImg').src="images/UndooLaddoo.png";
}
Laddoo.createOptionDivs=function()
{
	var divElement,paraElement,optionDivElement;
	document.getElementById('laddooOrder').style.display="none";
	for(var i=1;i<=5;i++)
	{
		optionDivElement=document.createElement('span');
		optionDivElement.setAttribute('id','para'+i)
		optionDivElement.setAttribute('class','bordercls fontclass noPaddingcls ');
		optionDiv.append(optionDivElement);
	}
	if((document.getElementById('para1').innerHTML)=='')
	{
		$('#UndooLaddooImg').css({'pointer-events':'none'});
		$('#checkimg').css({'pointer-events':'none'});
		Laddoo.ans=0;
	}
}
Laddoo.generateOrder=function(obj)
{
	var randomNumber=0;
	document.getElementById('laddooOrder').style.display="block";
	$('.handEvent').css({'pointer-events':'auto'});
	$('#orderHereImg').css({'pointer-events':'none'});
	randomNumber=Math.floor(Math.random() * (Laddoo.noOfLaddoos.length));	
	document.getElementById('laddooOrder').innerHTML=Laddoo.noOfLaddoos[randomNumber];
}
Laddoo.calculation=function(obj)
{
	Laddoo.i++;
	$('#UndooLaddooImg').css({'pointer-events':'auto'} );
	$('#checkimg').css({'pointer-events':'auto'});
	if(Laddoo.i<6)
	{
		var index=0,num=0,data=0;
		data=obj.getAttribute("data");
		if(data==15)
			num= Math.floor(Math.random()*(17-13+1)+13);
		else if(data==10)
			num=Math.floor(Math.random()*(12-8+1)+8);
		else if(data==5)
			num=Math.floor(Math.random()*(7-3+1)+3);
		Laddoo.calculateNoOfLaddoos(num);
	}
	else
		alert("limit exeeds");
	if(document.getElementById('para1').innerHTML=='')
	{
		$('#UndooLaddooImg').css({'pointer-events':'none'});
		$('#checkimg').css({'pointer-events':'none'});
		Laddoo.ans=0;
	}
}
Laddoo.calculateNoOfLaddoos=function(num)
{
	var leftpos=0,rightelem='',myvar=0;
	$('.handEvent').css({'pointer-events':'none'});
	rightelem=document.getElementById('flowingLaddoo');
	rightelem.style.left ='0%';
	rightelem.style.top='0%';
	document.getElementById('para'+Laddoo.i).innerHTML=num+"+";
	Laddoo.ans=Laddoo.ans+num;
	document.getElementById('flowingLaddoo').style.visibility="visible";
	myvar = setInterval(function()
	{
		leftpos++;
		rightelem.style.left = leftpos + '%';
		if(leftpos ==35)
		{
			clearInterval(myvar);
			rightelem.style.top ='55px';
			$('#hand1').addClass('slideClass');
			$('.handEvent').css({'pointer-events':'auto'});
		}
	},30);
}
Laddoo.checkAns=function(obj)
{
	num=0;
	$('.handEvent').css({'pointer-events':'none'});
	num=Number(document.getElementById('laddooOrder').innerHTML);
	document.getElementById('sweetGamePage').style.display="none";
	document.getElementById('endPage').style.display="block";
	document.getElementById('givenOrderLaddoo').innerHTML="Ordered:";
	document.getElementById('yourOrderLaddoo').innerHTML=num;
	document.getElementById('filledLaddoo').innerHTML="Filled:";
	document.getElementById('youFilledOrderLaddoo').innerHTML=Laddoo.ans;
	if(num - 1 == Laddoo.ans || num - 2 == Laddoo.ans || num + 1 == Laddoo.ans || num + 2 == Laddoo.ans || num == Laddoo.ans)
	{
		document.getElementById('answerLaddoo').src="images/rightAns.png";
		document.getElementById('paymentBtnLaddoo').style.display="block";
	}
	else
	{
		document.getElementById('answerLaddoo').src="images/wrongAns.png";
		document.getElementById('paymentBtnLaddoo').style.display="none";
		setTimeout(function()
		{
			document.getElementById('payment').style.display="none";
			document.getElementById('endPage').style.display="none";
			document.getElementById('sweetGamePage').style.display="block";
			Laddoo.reset();
		},4000);
	}
}
Laddoo.reset=function()
{
	Laddoo.i=0;
	Laddoo.ans=0;
	Shop.checkLaddooShopFlag=false;
	optionDiv.innerHTML='';
	document.getElementById('laddooOrder').innerHTML="";
	document.getElementById('flowingLaddoo').style.left ='0%';
	$('#orderHereImg').css({'pointer-events':'auto'});
	Laddoo.createOptionDivs();
}
Laddoo.paymentPage=function()
{
	var actualPayment=0;
	Shop.checkLaddooShopFlag=true;
	actualPayment=Laddoo.ans*5;
	document.getElementById('endPage').style.display="none";
	document.getElementById('payment').style.display="block";
	//document.body.style.backgroundImage="url('images/sweet_mart_cash_counter.png')";
	document.getElementById("paymentSide1").style.backgroundImage="url('images/sweet_mart_cash_counter.png')";
	document.getElementById("paymentSide2").style.backgroundImage="url('images/pyBG2.png')";
	document.getElementById('custImg').src="images/custImg.png";
	EstimationPayment.setPayment(actualPayment,gameId);
}
Laddoo.undooWord=function()
{
	var undooNum=0,word='',num='';
	
	if(Laddoo.i==6)
		Laddoo.i--;
	undooNum=document.getElementById('para'+Laddoo.i).innerHTML;
	num=undooNum.split('');
	num.splice(num.length-1,1);
	num=num.join('');
	num=parseInt(num);
	Laddoo.ans=Laddoo.ans-num;
	document.getElementById('para'+Laddoo.i).innerHTML='';
	Laddoo.i--;
	if((document.getElementById('para1').innerHTML)=='')
		$('#UndooLaddooImg').css({'pointer-events':'none'});
}
