//20June 2018
var CountingInKitchen={};
var Utils = {};
Utils.Path='';
Utils.mobileDeviceFlag=false;
var questionId=0,startTime=0,scoredMarks=0,totalMarks=10,resId=0;
$(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("CountingInTheKitchen");     
	setTimeout(function()
	{
		CountingInKitchen.init();
	},100);
});

CountingInKitchen.init=function()
{
	CountingInKitchen.width=100;
	CountingInKitchen.showEndPage=0;
	CountingInKitchen.showNextQuextionTimer=0;
	CountingInKitchen.randomQuestion=0;
	CountingInKitchen.orderedItem=0;
	CountingInKitchen.orderCounter=0;
	CountingInKitchen.loadmetaDataFlag=false;
	CountingInKitchen.clickFlag=false;
	CountingInKitchen.number=0;
	document.body.style.backgroundImage="url("+Utils.Path+"images/"+CountingInKitchen.hardCodeddata.cover+")";
	$('#gamePage,#endPage').hide();
	CountingInKitchen.setImages();
	CountingInKitchen.setHardcodedData();
	
	
};
//set harcoded data
CountingInKitchen.setHardcodedData=function()
{
	$('#conceptBy').text(CountingInKitchen.hardCodeddata.concept_by);
	$('#artWork').text(CountingInKitchen.hardCodeddata.art_work);
	$('#audioEditing').text(CountingInKitchen.hardCodeddata.audio_editing);
	$('#animation').text(CountingInKitchen.hardCodeddata.animation);
	$('#music').text(CountingInKitchen.hardCodeddata.music);
	$('#developerName').text(CountingInKitchen.hardCodeddata.developed_by);
	$('#date').text(CountingInKitchen.hardCodeddata.date);
};
// set Images//
CountingInKitchen.setImages=function()
{
	document.getElementById('question').style.backgroundImage="url("+Utils.Path+"images/board.png)";
	document.getElementById('otaDiv').style.backgroundImage="url("+Utils.Path+"images/kadappa.png)";
	document.getElementById('play').src=Utils.Path+"images/Play_button.png";
	document.getElementById('homeImage').src=Utils.Path+"images/Home_button.png";
	document.getElementById('logoImage').src=Utils.Path+"images/logo.png";
	document.getElementById('chefimg').src=Utils.Path+"images/Cook.png";
	//document.getElementById('timerImg').src=Utils.Path+"images/stopwatch.png";
	document.getElementById('replayImg').src=Utils.Path+"images/Refresh_button.png";
	document.getElementById('creditImg').src=Utils.Path+"images/credits.png";
	$('#ordersDiv,#questionDiv,#chefDiv').css('visibility','visible');
}
CountingInKitchen.playGame=function()
{
	document.body.style.backgroundImage="url("+Utils.Path+"images/backgrd.png)";
	document.getElementById('backgroundMusic').src=Utils.Path+"sounds/backgroundMusic.mp3";
	document.getElementById('backgroundMusic').loop=true;
	document.getElementById('backgroundMusic').play();
	$('#coverPage').hide();
	$('#timerDiv').css('visibility','visible');
	$('#homeImage,#gamePage').show();
	for(var i=0;i<4;i++)
	{
		CountingInKitchen.createMuffinDivs((i+1));
	}
	CountingInKitchen.showQuestion();
};
CountingInKitchen.showQuestion=function()
{
	var d=new Date();
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	CountingInKitchen.noOfTimeQuestionAnswered=0;	
	$('.clickClass').removeAttr('onclick');
	//$('#questionDiv').removeClass('animated slideInRight');
	$('.plates').css('pointer-events','auto');
	//$('.clickClass').attr('onclick','');
	$('.muffinimg').prop('src',"");
	CountingInKitchen.startTimer();
	CountingInKitchen.orderedItem=Math.floor(Math.random()*CountingInKitchen.items.length);
	CountingInKitchen.setOptions();
}
CountingInKitchen.startTimer=function()
{
	var sound;
	if(CountingInKitchen.progressBarTimer)
	{
		clearInterval(CountingInKitchen.progressBarTimer);
		CountingInKitchen.progressBarTimer=null;
	}	
	CountingInKitchen.progressBarTimer=setInterval(function() 
	{
		CountingInKitchen.countDown();
		if(CountingInKitchen.width<=1)
		{
			document.getElementById('playSound').pause();
			clearInterval(CountingInKitchen.progressBarTimer);
			$('.plates').css('pointer-events','none');
			$("#questionDiv").css("visibility","hidden");
			$("#plate1,#plate2,#plate3,#plate4").css('visibility','hidden');
			document.getElementById("playSound").src=Utils.Path+"sounds/finish.mp3";
			document.getElementById("playSound").play();
			if(CountingInKitchen.showOrderCount)
			{
				clearTimeout(CountingInKitchen.showOrderCount);
				CountingInKitchen.showOrderCount=null;
			}	
			CountingInKitchen.showOrderCount=setTimeout(function()
			{	
				sound=Utils.Path+"sounds/"+CountingInKitchen.data[CountingInKitchen.number]["sound"];
				console.log(sound);
				CountingInKitchen.loadmetaDataFlag=true;
				CountingInKitchen.getDuration(sound);
			},2000);
		}
		if(CountingInKitchen.width==20)
			$('#progressBar').addClass("progress-bar-danger");
	},300);	
}
// function which decrements the timer 
CountingInKitchen.countDown=function()
{
	//$('#questionDiv').removeClass('animated slideInRight');
	$('#chefDiv').removeClass('animated flipInY');
	CountingInKitchen.width-=1; 
    document.getElementById("progressBar").style.width = CountingInKitchen.width + '%'; 
}
//calls to createplates and setOptions functions
CountingInKitchen.setOptions=function()
{
	var optionsArr=[],randomNumber=0;
	CountingInKitchen.randomQuestion=Math.floor(Math.random()*9)+1;
	$('#numberDiv').text(CountingInKitchen.data[CountingInKitchen.randomQuestion-1].no);
	document.getElementById('playSound').src=Utils.Path+"sounds/"+CountingInKitchen.data[CountingInKitchen.randomQuestion-1].sound;
	document.getElementById('playSound').play();
	optionsArr.push(CountingInKitchen.randomQuestion);
	for(var i=0;i<3;i++)
	{
		randomNumber=Math.floor(Math.random()*9)+1;
		while($.inArray(randomNumber,optionsArr)!=-1)
			randomNumber=Math.floor(Math.random()*9)+1;
		optionsArr.push(randomNumber);	
	}
	shuffleArray(optionsArr);
	for(var i=0;i<4;i++)
		CountingInKitchen.setMuffinImages((i+1),optionsArr[i]);
	$('#chefDiv').addClass('animated flipInY');
	$('.muffins').addClass('animated bounceInDown');
}
//setting mufffins images to divs
CountingInKitchen.setMuffinImages=function(parentDiv,count)
{
	for(var i=9;i>9-count;i--)
	{
		$("#"+parentDiv+'muffins'+i).parent().addClass('clickClass');
		$("#"+parentDiv+'muffins'+i).prop("src",Utils.Path+'images/'+CountingInKitchen.items[CountingInKitchen.orderedItem]);
		$("#"+parentDiv+'muffins'+i).parent().data('answer',count);
		var a=$("#"+parentDiv+'muffins'+i).parent().prop('id');
		document.getElementById(a).setAttribute('onclick','CountingInKitchen.checkAnswer(this.id)');
	}
	$("#itemImg").prop("src",Utils.Path+'images/'+CountingInKitchen.items[CountingInKitchen.orderedItem]);
	//$('#questionDiv').addClass('animated slideInRight');
}
//creating muffinsDivs
 CountingInKitchen.createMuffinDivs=function(parentDiv)
{
	var plateDiv,Div,img,muffinsDiv,plateImg;
	
	for(var i=0;i<10;i++)
	{	
		muffinsDiv=document.createElement('div');
		muffinsDiv.setAttribute('id',parentDiv+"muffinsDiv"+i);
		if(i%2==1)
			muffinsDiv.setAttribute('class'," nopadding muffins col-xs-3 col-md-3 col-lg-3 col-sm-3 ");
		else
			muffinsDiv.setAttribute('class'," nopadding muffins col-xs-3 col-md-3 col-lg-3 col-sm-3 col-xs-offset-3 col-md-offset-3 col-lg-offset-3 col-sm-offset-3");
		img=document.createElement('img');
		img.setAttribute('id',parentDiv+"muffins"+i);
		img.setAttribute('class',"nopadding muffinimg img-responsive  col-xs-12 col-md-12 col-lg-12 col-sm-12");
		muffinsDiv.appendChild(img);
		document.getElementById('plate'+parentDiv).appendChild(muffinsDiv);
	}
	//creating plateDivs
	plateDiv=document.createElement('div');
	plateDiv.setAttribute('class',"plateImg nopadding col-xs-12 col-md-12 col-lg-12 col-sm-12");
	plateImg=document.createElement('img');
	plateImg.setAttribute('class',"img-responsive col-xs-12 col-md-12 col-lg-12 col-sm-12");
	plateImg.setAttribute('src',Utils.Path+'images/plateimg.png');
	plateImg.style.zIndex=-1;
	plateDiv.appendChild(plateImg);
	document.getElementById('plate'+parentDiv).appendChild(plateDiv);
}
CountingInKitchen.checkAnswer=function(obj)
{
	CountingInKitchen.clickFlag=true;
	$('.plates').css('pointer-events','none');
	$('.muffins').removeClass('animated bounceInDown');
	CountingInKitchen.noOfTimeQuestionAnswered++;
	setTimeout(function()
	{
		if(($('#'+obj).data('answer'))==CountingInKitchen.randomQuestion)
		{	
			scoredMarks=10;
			clearInterval(CountingInKitchen.progressBarTimer);
			CountingInKitchen.addCompletedOrder();
			$('#'+obj).parent().children().not('.plateImg').addClass('animated bounceIn');
			document.getElementById('playSound').src=Utils.Path+"sounds/thatsright.mp3";
			if(CountingInKitchen.showNextQuextionTimer)
			{
				clearTimeout(CountingInKitchen.showNextQuextionTimer);
				CountingInKitchen.showNextQuextionTimer=null;
			}	
			CountingInKitchen.showNextQuextionTimer=setTimeout(function(){
				$('#'+obj).parent().children().not('.plateImg').removeClass('animated bounceIn');
				if(CountingInKitchen.progressBarTimer)
					CountingInKitchen.showQuestion();
			},1300);
		}
		else
		{
			scoredMarks=0;
			document.getElementById('playSound').src=Utils.Path+"sounds/ohhno.mp3";
			$('#'+obj).parent().children().not('.plateImg').addClass('animated shake');
			setTimeout(function()
			{
				$('#'+obj).parent().children().not('.plateImg').removeClass('animated shake');
			},500);
			$('.plates').css('pointer-events','auto');
		}
			document.getElementById('playSound').play();	
			if(CountingInKitchen.noOfTimeQuestionAnswered==1)
			{
				console.log(resId,parseInt(CountingInKitchen.data[CountingInKitchen.randomQuestion-1].num),scoredMarks,totalMarks,1,startTime);
				 if(Utils.mobileDeviceFlag)
					Android.addScore(resId,parseInt(CountingInKitchen.data[CountingInKitchen.randomQuestion-1].num),scoredMarks,totalMarks,1,startTime); 
			}			
	},300);	
}
// creates the item order divs on right answer 
CountingInKitchen.addCompletedOrder=function()
{
	var itemDiv,item;
	itemDiv=document.createElement('div');
	itemDiv.setAttribute('class',"orderitem nopadding rotate30 col-xs-2 col-md-2 col-lg-2 col-sm-2");
	itemDiv.setAttribute('id',"item"+CountingInKitchen.orderCounter);
	$(itemDiv).css('backgroundColor','white');
	item=document.createElement('img');
	item.setAttribute('class',"items nopadding img-responsive col-xs-8 col-md-8 col-lg-8 col-sm-8 col-xs-offset-2 col-md-offset-2 col-lg-offset-2 col-sm-offset-2");
	item.setAttribute('src',Utils.Path+'images/'+CountingInKitchen.items[CountingInKitchen.orderedItem]);
	itemDiv.appendChild(item);
	document.getElementById('ordersDiv').appendChild(itemDiv);
	CountingInKitchen.orderCounter++;
}
//function which executes after counting the orders
CountingInKitchen.countCompletedOrders=function(duration)
{
	$("#item"+CountingInKitchen.number).addClass("animated pulse");
	console.log(CountingInKitchen.orderCounter);
	if(CountingInKitchen.orderCounter)
		document.getElementById("playSound").play();
	clearTimeout(CountingInKitchen.showNextQuextionTimer);	
	if(CountingInKitchen.countOrderTimer)
	{
		clearTimeout(CountingInKitchen.countOrderTimer);
		CountingInKitchen.countOrderTimer=null;
	}	
	CountingInKitchen.countOrderTimer=setTimeout(function() 
	{
		
		if(CountingInKitchen.number==CountingInKitchen.orderCounter-1 || CountingInKitchen.orderCounter==0)
		{
			clearTimeout(CountingInKitchen.countOrderTimer);
			$("#completedOrder").css("visibility","visible");
			if((CountingInKitchen.orderCounter)<=0)
				document.getElementById("completedOrder").innerHTML="0";
			else
				document.getElementById("completedOrder").innerHTML=CountingInKitchen.data[CountingInKitchen.orderCounter-1].no;
			$("#completedOrder").addClass("animated zoomIn");
			if(CountingInKitchen.showEndPage)
			{
				clearTimeout(CountingInKitchen.showEndPage);
				CountingInKitchen.showEndPage=null;
			}
			CountingInKitchen.showEndPage=setTimeout(function()
			{
				$('#gamePage').hide();
				CountingInKitchen.width=100;
				document.getElementById("progressBar").style.width = CountingInKitchen.width + '%';  
				$('#timerDiv').css('visibility','hidden');
				document.body.style.backgroundImage="url("+Utils.Path+"images/plain.jpg)";
				$('#endPage').show();
				CountingInKitchen.displayMessage();
			},3000); 
		}
		else
		{	
			CountingInKitchen.number++
			sound=Utils.Path+"sounds/"+CountingInKitchen.data[CountingInKitchen.number]["sound"];
			CountingInKitchen.loadmetaDataFlag=true;
			CountingInKitchen.getDuration(sound);
		}
	},duration*1000);
}
//function which executes on home button click		 
CountingInKitchen.goToHome=function()
{
	location.reload(true);
}
//function which executes on replay button click
CountingInKitchen.replay=function()
{
	document.getElementById('playSound').pause();
	$('#gamePage').show();
	$('#responseBoxDiv,#endPage,#acknowledgementDiv').hide();
	$("#completedOrder").css("visibility","hidden");
	$("#ordersDiv").empty();
	$('.muffinimg').prop('src',"");
	$('.muffins').removeClass('animated bounceInDown');
	$("#completedOrder").removeClass("animated zoomIn");
	$('#progressBar').removeClass("progress-bar-danger");
	$('#progressBar').addClass("progress-bar-success");
	$("#plate1,#plate2,#plate3,#plate4").css('visibility','visible');
	//$('#questionDiv').removeClass('animated slideInRight');
	$("#questionDiv,#timerDiv").css("visibility","visible");
	$('#timerDiv').css('visibility','visible');
	document.body.style.backgroundImage="url("+Utils.Path+"images/backgrd.png)";
	CountingInKitchen.orderCounter=0;
	CountingInKitchen.number=0;
	CountingInKitchen.showQuestion();
}
//function which generates messages according to score
CountingInKitchen.displayMessage=function()
{	var message;

	if(CountingInKitchen.orderCounter==0)
	{	
		message=CountingInKitchen.message.tryAgain;
		document.getElementById('playSound').src=Utils.Path+"sounds/"+CountingInKitchen.hardCodeddata.sound0;
		document.getElementById('playSound').play();
	}	
	if(CountingInKitchen.orderCounter>=1 && CountingInKitchen.orderCounter<=3)
	{	
		message=CountingInKitchen.message.keepGoing;
		document.getElementById('playSound').src=Utils.Path+"sounds/"+CountingInKitchen.hardCodeddata.sound1;
		document.getElementById('playSound').play();
	}	
	if(CountingInKitchen.orderCounter<=7 && CountingInKitchen.orderCounter>3)
	{	
		message=CountingInKitchen.message.niceOne;
		document.getElementById('playSound').src=Utils.Path+"sounds/"+CountingInKitchen.hardCodeddata.sound2;
		document.getElementById('playSound').play();
	}	
	if(CountingInKitchen.orderCounter<=10 && CountingInKitchen.orderCounter>7)
	{	
		message=CountingInKitchen.message.awesomeJob;
		document.getElementById('playSound').src=Utils.Path+"sounds/"+CountingInKitchen.hardCodeddata.sound3;
		document.getElementById('playSound').play();
	}
	if(CountingInKitchen.orderCounter>10)
	{	
		message=CountingInKitchen.message.amazingBrain;
		document.getElementById('playSound').src=Utils.Path+"sounds/"+CountingInKitchen.hardCodeddata.sound4;
		document.getElementById('playSound').play();
	}	
	$("#modalMsg").text(message);
	if((CountingInKitchen.orderCounter)<=0)
		$('#scoreMsg').text("");
	else
		$('#scoreMsg').text(CountingInKitchen.hardCodeddata.totalOrders+' '+CountingInKitchen.data[CountingInKitchen.orderCounter-1].no+' '+CountingInKitchen.hardCodeddata.orders);
}
//function which gets duration of audio file
CountingInKitchen.getDuration=function(sound)
{
	var duration=0;
	$('#playSound').prop('src',sound);
	if(CountingInKitchen.loadmetaDataFlag)
	{
		$('#playSound').on("loadedmetadata", function(){
			CountingInKitchen.loadmetaDataFlag=false;
			$('#playSound').unbind("loadedmetadata");
			duration=$('#playSound').prop('duration');
			
			CountingInKitchen.countCompletedOrders(duration);
			}	
		);
    }
    else
		$('#playSound').unbind("loadedmetadata");
}
CountingInKitchen.showCredits=function()
{
	//$("#GameOverMsg").hide();
	$("#acknowledgementDiv").show();
};