var Laddoo={};
Laddoo.i=0;
Laddoo.ans=0;
$(document).ready(function()
{
	$(".se-pre-con").fadeOut("slow");
	setTimeout(function()
	{
		Laddoo.setImage();
		Laddoo.playGame();
		Laddoo.createHandDivs();
		Laddoo.createOptionDivs();
		
 	},200);
});
Laddoo.playGame=function()
{
	document.body.style.backgroundImage="url('images/sweetmartcloseup.png')";
	setTimeout(function()
	{
		document.getElementById('firstPage').style.display='none';
		document.body.style.backgroundImage="url('images/BG.png')";
		Laddoo.showSecondPage();
 	},2000);
}
// 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('bagGrain').src="images/bag.png"; */
	document.getElementById('customerImg').src="images/customer1standig.png";
	document.getElementById('laddooTrayImg').src="images/laddoos.png";
	document.getElementById('movingLaddoImg').src="images/handwithtenladdoos.png";
	document.getElementById('customerOrderImg').src="images/customer1lookingright.png";
	document.getElementById('emptyTrayImg').src="images/mithaibox.png";
	document.getElementById('fullTrayImg').src="images/laddoos.png";
	document.getElementById('serverImg').src="images/assistantlookingup.png";
	document.getElementById('checkImg').src="images/tickbutton.png";
	document.getElementById('UndooLaddooImg').src="images/resetbutton.png";
	document.getElementById('bCheckImg').src="images/tickbutton.png";
	
}
Laddoo.showSecondPage=function()
{
	var randomNumber=0
	document.getElementById('orderPage').style.display='block';
	document.getElementById('checkDiv').style.display='block';
	document.getElementById('boardDiv').style.display='block';
	document.getElementById('trayDiv').style.display='block';
	document.getElementById('lowerDiv').style.display='block';
	document.getElementById('orderPage').style.display='block';
	document.getElementById('boardHandDiv').style.display='none';
	document.getElementById('fullTrayDiv').style.display='none';
	$('#UndooLaddooImg').css({'pointer-events':'none'});
	document.getElementById('boardDiv').style.backgroundImage="url('images/orderboard.png')";
	document.getElementById('boardHandDiv').style.backgroundImage="url('images/handBoard.png')";
	document.getElementById('orderTextDiv').innerHTML=Laddoo.hardcodedData.orderText;
	document.getElementById('noOfLaddoo').innerHTML=Laddoo.hardcodedData.laddooText;
	randomNumber=Math.floor(Math.random() * (Laddoo.noOfLaddoos.length));	
	document.getElementById('noOfOrder').innerHTML=Laddoo.noOfLaddoos[randomNumber];
}
Laddoo.createHandDivs=function()
{
	var divElement,imgElement,paraElement;
	for(var i=1;i<=3;i++)
	{
		divElement=document.createElement('div');
		divElement.setAttribute('id','handDiv'+i);
		divElement.setAttribute('class','handMargin noPaddingcls col-sm-3 col-lg-3 col-md-3 col-xs-3 col-sm-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-offset-1')
		imgElement=document.createElement('img');
		imgElement.setAttribute('id','handImg'+i);
		imgElement.setAttribute('src',Laddoo.handImages[i-1]);
		imgElement.setAttribute('class','handEvent noPaddingcls col-sm-12 col-lg-12 col-md-12 col-xs-12')
		imgElement.setAttribute('onclick','Laddoo.calculation(this)');
		imgElement.setAttribute('data',Laddoo.handNumbers[i-1]);
		paraElement=document.createElement('p');
		paraElement.setAttribute('id','para'+i);
		paraElement.setAttribute('class','noPaddingcls col-sm-12 col-lg-12 col-md-12 col-xs-12')
		paraElement.innerHTML="~"+Laddoo.handNumbers[i-1];
		divElement.append(imgElement);
		divElement.append(paraElement);
		boardHandDiv.append(divElement)
	}
	
}
Laddoo.createOptionDivs=function()
{
	var divElement,paraElement,optionDivElement;
	for(var i=0;i<5;i++)
	{
		optionDivElement=document.createElement('span');
		optionDivElement.setAttribute('id','paraOpt'+i)
		optionDivElement.setAttribute('class','bordercls fontClass noPaddingcls ');
		additionDiv.append(optionDivElement);
	}
}

Laddoo.showHandDiv=function()
{
	document.getElementById('fullTrayDiv').style.display='block';
	document.getElementById('boardHandDiv').style.display='block';
	document.getElementById('checkDiv').style.display='none';
}
Laddoo.calculation=function(obj)
{
		document.getElementById('checkDiv').style.display='block';
		document.getElementById('checkImg').src='images/stopbutton.png';
		document.getElementById('checkImg').setAttribute('onclick','Laddoo.checkAns(this)')
		document.getElementById('movingLaddoNAddition').style.display='block';
	document.getElementById('movingLaddoDiv').style.visibility="visible";
		document.getElementById('additionDiv').style.display='block';

	if(Laddoo.i<5)
	{
		var index=0,num=0;
		var data=$(obj).attr("data");
		if(data==15)
		{
			num= Math.floor(Math.random()*(17-13+1)+13);
			document.getElementById('movingLaddoImg').src=Laddoo.ImageData[0];
		}
		else if(data==10)
		{
			num=Math.floor(Math.random()*(12-8+1)+8);
			document.getElementById('movingLaddoImg').src=Laddoo.ImageData[1];

		}
		else if(data==5)
		{
			num=Math.floor(Math.random()*(7-3+1)+3);
			document.getElementById('movingLaddoImg').src=Laddoo.ImageData[2];

		}
		
		Laddoo.animationOfLaddoosAndCalculatAns(num);
		
	}
	
	//justrightladdoosmithaibox
	else
		alert("limit exeeds");
}
Laddoo.animationOfLaddoosAndCalculatAns=function(num)
{
	var leftpos=0,rightelem='';
	$('#UndooLaddooImg').css({'pointer-events':'auto'});
	rightelem=document.getElementById('movingLaddoImg');
	rightelem.style.right ='0%';
	rightelem.style.top='0%';
	document.getElementById('paraOpt'+Laddoo.i).innerHTML=num+"+";
	Laddoo.ans=Laddoo.ans+num;
	document.getElementById('movingLaddoImg').style.display="block";
	Laddoo.i++;
	var myvar = setInterval(function()
	{
		leftpos++;
		rightelem.style.right = leftpos + '%';
		if(leftpos ==135)
		{
			clearInterval(myvar);
			rightelem.style.top ='55px';
			document.getElementById('movingLaddoImg').style.display='none';
			document.getElementById('emptyTrayImg').src='images/justrightladdoosmithaibox.png';
			$('#hand1').addClass('slideClass');
		}
	},30);
	
}
Laddoo.checkAns=function(obj)
{
	num=0;
	$('.handEvent').css({'pointer-events':'none'});
	document.getElementById('checkDiv').style.display='none';
	document.getElementById('movingLaddoDiv').style.visibility="hidden";
	num=Number(document.getElementById('noOfOrder').innerHTML);
	/* document.getElementById('gamepage').style.display="none";
	document.getElementById('endPage').style.display="block"; */
	if(num - 1 == Laddoo.ans || num + 1 == Laddoo.ans  || num == Laddoo.ans)
	{
		document.getElementById('customerOrderImg').src="images/customer1happy.png";
		document.getElementById('serverImg').src="images/assistanthappy.png";
		setTimeout(function()
		{
			document.getElementById('lowerDiv').style.display='none';
			document.getElementById('trayDiv').style.display='none';
			document.getElementById('movingLaddoNAddition').style.display='none';
			document.getElementById('orderPage').style.display='none';
			document.getElementById('firstPage').style.display='none';
			Laddoo.showRateCard();
		},500);
	}
	else if(Laddoo.ans < num - 1 )
	{
		document.getElementById('customerOrderImg').src="images/customer1sad.png";
		document.getElementById('serverImg').src="images/assistantunhappy.png";
		document.getElementById('emptyTrayImg').src="images/lessladdoosmithaibox.png";
		setTimeout(function()
		{
			$('#UndooLaddooImg').css({'pointer-events':'none'});
			Laddoo.reset();
		},4000); 
	}
		
	else
	{
		document.getElementById('customerOrderImg').src="images/customer1sad.png";
		document.getElementById('serverImg').src="images/assistantunhappy.png";
		document.getElementById('emptyTrayImg').src="images/moreladdoosmithaibox.png";
		setTimeout(function()
		{
			$('#UndooLaddooImg').css({'pointer-events':'none'});
			Laddoo.reset();
		},4000); 
	}
	document.getElementById('emptyTrayDiv').style.bottom="1vh";
	$('#customerOrderDiv').addClass('col-sm-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-offset-1')
	 $('#serverDiv').removeClass('col-sm-2 col-lg-2 col-md-2 col-xs-2 col-sm-offset-8 col-lg-offset-8 col-md-offset-8 col-xs-offset-8')
	 $('#serverDiv').addClass('col-sm-2 col-lg-2 col-md-2 col-xs-2 col-sm-offset-7 col-lg-offset-7 col-md-offset-7 col-xs-offset-7')
	 $('#emptyTrayDiv').removeClass('col-sm-2 col-lg-2 col-md-2 col-xs-2 col-sm-offset-3 col-lg-offset-3 col-md-offset-3 col-xs-offset-3')
	$('#emptyTrayDiv').addClass('col-sm-3 col-lg-3 col-md-3 col-xs-3 col-sm-offset-3 col-lg-offset-3 col-md-offset-3 col-xs-offset-3') 
	/* $('#emptyTrayImg').removeProp("bottom"); */
}
Laddoo.undooWord=function()
{
	var undooNum=0,word='',num='';
	if((document.getElementById('paraOpt0').innerHTML)=='')
		$('#UndooLaddooImg').css({'pointer-events':'none'});
	else
	{
		Laddoo.i--;
		undooNum=document.getElementById('paraOpt'+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('paraOpt'+Laddoo.i).innerHTML='';
	}
}

Laddoo.showRateCard=function()
{
	var row,column;
	document.getElementById('bRate').innerHTML="Rate";
	document.getElementById('bOrder').innerHTML="Order";
	document.getElementById('billDiv').style.display='block';
	document.getElementById('bBoard').style.backgroundImage="url('images/ratedisplayboard.png')";
	document.body.style.backgroundImage="url('images/sweetmartcashcounter.png')";

	
	//document.getElementById('bItem').innerHTML=EstimationGrain.ingredient[EstimationGrain.randomIngredient].ingrdientName;
	//document.getElementById('bNOItem').innerHTML=EstimationGrain.Question[EstimationGrain.randomQue].question;
	document.getElementById('totalAmount').innerHTML="Rs. "+Laddoo.ans *Laddoo.itemData[0].itemPrice;
	for(var i=0;i<4;i++)
	{
		row=document.createElement('tr');
		row.setAttribute("id","row"+(i+1));
		row.setAttribute("class","ingredientsRows bfontClass text-center ");
		for(var j=1;j<=2;j++)
		{
			column=document.createElement('td');
			column.setAttribute("id","column"+(i+1)+j);
			column.setAttribute("class","bfontClass text-center");
			row.appendChild(column);
			if(j==1)
				column.innerHTML=Laddoo.itemData[i].itemName;
			else
				column.innerHTML='Rs.'+Laddoo.itemData[i].itemPrice +'/1';
		}
		ItemRateDiv.appendChild(row);
	}
}
Laddoo.reset=function()
{
	Laddoo.i=0;
	Laddoo.ans=0;
	additionDiv.innerHTML='';
	document.getElementById('emptyTrayDiv').style.bottom="19vh";
	$('#customerOrderDiv').removeClass('col-sm-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-offset-1')
	$('#serverDiv').removeClass('col-sm-2 col-lg-2 col-md-2 col-xs-2 col-sm-offset-7 col-lg-offset-7 col-md-offset-7 col-xs-offset-7')
	$('#serverDiv').addClass('col-sm-2 col-lg-2 col-md-2 col-xs-2 col-sm-offset-8 col-lg-offset-8 col-md-offset-8 col-xs-offset-8')
	$('#emptyTrayDiv').removeClass('col-sm-3 col-lg-3 col-md-3 col-xs-3 col-sm-offset-3 col-lg-offset-3 col-md-offset-3 col-xs-offset-3') 
	$('#emptyTrayDiv').addClass('col-sm-2 col-lg-2 col-md-2 col-xs-2 col-sm-offset-3 col-lg-offset-3 col-md-offset-3 col-xs-offset-3')
	document.getElementById('emptyTrayImg').src="images/mithaibox.png";
	document.getElementById('checkImg').setAttribute('onclick','Laddoo.showHandDiv(this)')
	$('.handEvent').css({'pointer-events':'auto'});
	$('.UndooLaddooImg').css({'pointer-events':'none'});
	document.getElementById('additionDiv').style.display='none';
	Laddoo.setImage();
	Laddoo.createOptionDivs();
	Laddoo.showSecondPage();
}
