var Countinggame=[];
Countinggame.rNo=0;
Countinggame.counter=0;
Countinggame.shiftImg=0;
Countinggame.interval;
Countinggame.timeout;

//runs @onload of body.sets the homepage of game. 
Countinggame.init=function()
{
	document.getElementById("home").style.visibility = "hidden";
	document.getElementById("gamePage").style.display="none";
	document.body.style.backgroundImage = "url(images/BG_Flowers.png)";
	Countinggame.setAllImage();
	Countinggame.createNumbers();
}
//called from Countinggame.init function,sets sources to all the buttons and images.	
Countinggame.setAllImage=function()
{
	document.getElementById("homeLogo").src="images/Home.png";
	document.getElementById("logo").src="images/logo.png";
	document.getElementById("go").src="images/Next.png";
	document.getElementById("next").src="images/Next.png";
	document.getElementById("tom").src="images/tom.png";
	document.getElementById("tom").style.marginLeft="0%";
	
}
//runs @ onclick of next button on homepage sets gamepage of game.  
Countinggame.playGame=function()
{
	document.body.style.backgroundImage = "url(images/BG123.png)";
	document.getElementById("home").style.visibility = "visible";
	document.getElementById("gamePage").style.display="block";
	document.getElementById("buttonDiv").style.display="none";	
	
	Countinggame.createDiv();
}
//called from Countinggame.playgame and  Countinggame.reset function.creates dynamic divs for the images
Countinggame.createDiv=function()
{	
	var imgDiv,img,i,array=[];
	var imgNo=Math.floor(Math.random()*Countinggame.imageData.length); //to get image from json
	var parentDiv=document.getElementById("parentDiv");
	Countinggame.rNo=Math.floor(Math.random()*9+1);
	
	$("#parentDiv").empty();
	
	for(i=0;i<36;i++)
	{
		imgDiv=document.createElement("DIV");
		imgDiv.setAttribute("class","objects zeroPadding col-lg-1 col-md-1 col-sm-1 col-xs-1 col-xs-offset-1 col-sm-offset-1 col-md-offset-1 col-lg-offset-1");
		imgDiv.setAttribute("id","imgDiv"+(i+1));
		
		parentDiv.appendChild(imgDiv);
	}
	for(j=0;j<Countinggame.rNo;j++)
	{
		var position=Math.floor(Math.random()*36+1);;
		if(array.includes(position))
		{
			j--;
			continue;
		}
		else
		{
			array.push(position);
			img=document.createElement("IMG");
			img.setAttribute("id","img"+(j+1));
			img.setAttribute("class","img-responsive images");
			img.setAttribute("onclick","Countinggame.countNumber(this.id)");
			img.setAttribute("src","images/"+Countinggame.imageData[imgNo]["img"]);
			var a=document.getElementById("imgDiv"+position);
			a.appendChild(img);
		}	
	}
}

//called from Countinggame.init function. creates the nmberline.
Countinggame.createNumbers=function()
{
	var parentDiv,i=0,imgDiv,img1,para;
	
	parentDiv=document.getElementById("numSystem"); 
	for(i=0;i<10;i++)
	{
		imgDiv=document.createElement("DIV");
		imgDiv.setAttribute("class","numDiv zeroPadding col-lg-1 col-md-1 col-sm-1 col-xs-1");
		imgDiv.setAttribute("id","numDiv"+i);
		
		img1=document.createElement("IMG");
		img1.setAttribute("id","button"+i);
		img1.setAttribute("class","button zeroPadding img-responsive col-lg-12 col-md-12 col-sm-12 col-xs-12");
		img1.setAttribute("src","images/Button.png");
		
		para=document.createElement("span");
		para.innerHTML=i;	
		para.setAttribute("class","number");
		para.setAttribute("id","number"+i);
			
		parentDiv.appendChild(imgDiv);
		imgDiv.appendChild(img1);
		imgDiv.appendChild(para);
	}
}
//runs @ onclick of images dispalayed on page.plays the sound of numbers when images are clicked.		
Countinggame.countNumber=function(imgId)
{
	document.getElementById(imgId).style.pointerEvents="none";
	document.getElementById(imgId).style.zIndex="0"; 
	document.getElementById(imgId).style.opacity="0.5"; 
	document.getElementById("audio").src="sounds/"+Countinggame.soundData[Countinggame.counter]["sound"];
	document.getElementById("audio").play();
	Countinggame.counter++;
	Countinggame.moveTom();
	
}
//called from Countinggame.countNumber function,moves the tom through the numberline by one digit when images are clicked one by one.	
Countinggame.moveTom=function()
 {
	var initial=$("#numDiv"+Countinggame.counter).offset();
	var pos=initial.left;
	if(Countinggame.interval)
	{
		clearInterval(Countinggame.interval);
		Countinggame.interval=null;
	}
	document.getElementById("tom").src="images/hase3.gif";
	Countinggame.interval=setInterval(function()
	{
		Countinggame.shiftImg++;
		document.getElementById("tomDiv").style.left=(Countinggame.shiftImg)+"px";
		if(Countinggame.shiftImg>=(pos-50))
		{
			clearInterval(Countinggame.interval);
			document.getElementById("tom").src="images/tom.png";
			if(Countinggame.counter==Countinggame.rNo)
			{
				document.getElementById("tom").classList.add("animation");
				setTimeout(function()
				{
					document.getElementById("audio").src="sounds/Hureyyy.mp3";
					document.getElementById("audio").play();
				},500);
			
				
				
				Countinggame.timeout=setTimeout(function()
				{
					Countinggame.reset();
				},2000);
			}
		}	
	},8);
}	
//runs @ onclick of home.loads the homepage.
Countinggame.callHome=function()
{
	location.reload();
}
//runs @ onclick of next button on gamepage.resets the gamepage.
Countinggame.reset=function()
{
	clearTimeout(Countinggame.timeout);
	document.getElementById("audio").pause();
	document.getElementById("tom").src="images/tom.png";
	clearInterval(Countinggame.interval);
	Countinggame.shiftImg=0;
	Countinggame.counter=0;
	document.getElementById("tomDiv").style.left=Countinggame.shiftImg+"px";
	$("#tom").removeClass("animation");
	
	Countinggame.createDiv();
}
