var Estimation={};

Estimation.calibrationValue=100,Estimation.temp=0;
var canvas=document.getElementById('myCanvas');
var context=canvas.getContext('2d');
var x=null,countWT=20,count=0;
var time=3000;
var gameId=null;

Estimation.startWeightGame=function(id)
{
	gameId=id;
	Estimation.createDiv();
	Estimation.weightCount();	
}
Estimation.setQuestion=function()
{
	Estimation.random=Math.floor(Math.random()*Estimation.Question.length);
	document.getElementById('question').innerHTML=Estimation.Question[Estimation.random].question;
}
Estimation.weightCount=function()
{
	var value=20,weight=0,count=0,bha=0;
	time=3000;
	document.getElementById('div2').classList.remove("moveHand");
	//document.getElementById("servedGrainsOrder").innerHTML="";
	//Estimation.temp+=1;
	context.fillStyle = "black";
	context.fillRect(10,20,18,320);
	canvas.style.borderRadius="20px";
	context.lineWidth=5;
	if(Estimation.temp>2)
	{
		//var random=Math.floor(Math.random()*weightData.length);
		Estimation.calibrationValue=weightData[1].Estimation.calibrationValue;
	}
	var y=setInterval(function ()
	{
		count+=100;
		bha+=33.333;
		if(count%Estimation.calibrationValue==0)
		{
			context.font ="25px Arial";
			context.fillText(count,45,bha+7);
		}	
			context.moveTo(28,bha);
			context.lineTo(42,bha);
			context.strokeStyle="white";
			context.stroke();
		if(count>=1000)
		{
			clearInterval(y);
			context.beginPath();
			context.moveTo(10,20);
			context.lineTo(28,20);
			context.strokeStyle="red";
			context.stroke();
		}					
	},5);
	//Estimation.createDiv();
	Estimation.setQuestion();
}

Estimation.createDiv=function()
{
	for(var i=0;i<2;i++)
	{
		var div=document.createElement('div');
		div.setAttribute("id","imgDiv"+(i+1));
		div.setAttribute("class","noPadding col-xs-2 col-sm-2 col-lg-2 col-md-2");
		
		var img=document.createElement('img');
		img.setAttribute('id','img'+(i+1));
		img.setAttribute('class','img-responsive col-xs-12 col-sm-12 col-lg-12 col-md-12');
		div.appendChild(img);
		innerDiv.appendChild(div);
		
		var imgDiv=document.createElement('div');
		imgDiv.setAttribute("id","div"+(i+1));
		imgDiv.setAttribute("class","grainPick noPadding col-xs-3 col-sm-3 col-lg-3 col-md-3 col-xs-offset-2 col-sm-offset-2 col-lg-offset-2 col-md-offset-2");
		
		var image=document.createElement('img');
		image.setAttribute('id','images'+(i+1));
		image.setAttribute('class','img-responsive col-xs-12 col-sm-12 col-lg-12 col-md-12 noPadding');
		
		imgDiv.appendChild(image);
		container.appendChild(imgDiv);
		
		var bagDiv=document.createElement('div');
		bagDiv.setAttribute("id","bagDiv"+(i+1));
		bagDiv.setAttribute("class","noPadding col-xs-2 col-sm-2 col-lg-2 col-md-2");
		
		var imageBag=document.createElement('img');
		imageBag.setAttribute('id','bagImg'+(i+1));
		imageBag.setAttribute('class','img-responsive col-xs-12 col-sm-12 col-lg-12 col-md-12 noPadding');
	
		bagDiv.appendChild(imageBag);
		bagContainer.appendChild(bagDiv);
	}
	
	Estimation.setImages();
}

Estimation.setImages=function()
{
	document.body.style.backgroundImage="url('images/BG.png')";
	document.getElementById('img1').src="images/hooks.png";
	document.getElementById('img2').src="images/grainPour.png";
	document.getElementById('images1').src="images/scoop.png";
	document.getElementById('images2').src="images/hand.png";
	document.getElementById('bagImg1').src="images/bag.png";
	document.getElementById('bagImg2').src="images/Grain.png";
	document.getElementById('bagGrain').src="images/bagWithGrains.png";
	document.getElementById('grainsOrder').src="images/orderGrain.png";

	document.getElementById('div2').setAttribute("onclick","Estimation.handClicked()");
	document.getElementById('div2').style.pointerEvents="none";
	document.getElementById('imgDiv2').setAttribute("onclick","Estimation.stopPouring()");
}
Estimation.grainsOrderHere=function()
{
	document.getElementById('question').style.visibility="visible";
	document.getElementById('div2').style.pointerEvents="auto";
}
Estimation.handClicked=function()
{
	document.getElementById('div2').classList.add("moveHand");
	
	setTimeout(function()
	{
		document.getElementById('imgDiv2').style.display="block";	
		document.getElementById('images1').style.visibility="hidden";
		document.getElementById('images2').style.visibility="hidden";
		document.getElementById('bagDiv2').style.visibility="visible";
		time=0;
		Estimation.myfun();
	},time);
	//document.getElementById('div2').classList.remove("moveHand");
}

Estimation.myfun=function()
{
	x=setInterval(function ()
	{
		context.beginPath();
		context.moveTo(10,countWT);
		context.lineTo(28,countWT);
		context.strokeStyle="red";
		context.stroke();
		countWT+=1;
		//document.getElementById("served").innerHTML="You filled"+countWT*3;
		if(countWT>333)
			clearInterval(x);
	},100);
}

Estimation.stopPouring=function()
{
	clearInterval(x);
	document.getElementById('images1').style.visibility="visible";
	document.getElementById('images2').style.visibility="visible";
	document.getElementById('imgDiv2').style.display="none";
	//document.getElementById("served").innerHTML="You filled"+countWT*3;
	setTimeout(function()
	{
		Estimation.checkAnswer();
	},1000);
	
}

Estimation.checkAnswer=function()
{
	var calculatedWt=countWT*3;
	document.getElementById('grainsGamePage').style.display="none";
	document.getElementById('checkAnswer').style.display="block";
	document.getElementById('givenOrder').innerHTML="Ordered:";
	document.getElementById('yourOrder').innerHTML=Estimation.Question[Estimation.random].question;
	document.getElementById('youPour').innerHTML="Filled:";
	document.getElementById('youFilledOrder').innerHTML=calculatedWt+"gms of wheat";
	
	if((Estimation.Question[Estimation.random].weight+15)>=calculatedWt && (Estimation.Question[Estimation.random].weight-15)<=calculatedWt)
	{	
		document.getElementById('paymentBtnGrains').style.display="block";
		document.getElementById('answer').src="images/rightAns.png";
	}
	else
	{
		document.getElementById('paymentBtnGrains').style.display="none";
		document.getElementById('answer').src="images/wrongAns.png";
		setTimeout(function()
		{
			document.getElementById('payment').style.display="none";
			document.getElementById('checkAnswer').style.display="none";
			document.getElementById('grainsGamePage').style.display="block";
			Estimation.next();
	
		},4000);
	}
}

Estimation.next=function()
{
	document.getElementById('question').style.visibility="hidden";
	document.getElementById('bagDiv2').style.visibility="hidden";
	document.getElementById('div2').style.pointerEvents="none";
	context.clearRect(0, 0,100,400);
	countWT=20;
	Shop.checkGrainsShopFlag=false;
	clearInterval(x);
	x=null;
	context.beginPath();
	context.moveTo(10,20);
	context.lineTo(28,20);
	context.strokeStyle="red";
	context.stroke();
	Estimation.weightCount();
}

Estimation.paymentPage=function()
{
	var actualWeight=0,cost1GM=0,actualPayment=0;
	actualWeight=Estimation.Question[Estimation.random].weight;
	cost1GM=40/1000;
	actualPayment=actualWeight*cost1GM;
	document.getElementById('checkAnswer').style.display="none";
	payment.style.display="block";
	Shop.checkGrainsShopFlag=true;
	EstimationPayment.setPayment(actualPayment,gameId);
}





	
