var bucketGame = {};
var Utils = {};
Utils.Path = '';
bucketGame.answerIsFalse = false, bucketGame.attempt = 0;
bucketGame.scoreObject= {"score" : 0, "correctAnswers" : 0, "wrongAnswers" : 0, "scoredMarks" : 0, "totalMarks" : 10};

bucketGame.selectedCategories = {};

bucketGame.selectedQuestions = [];

bucketGame.questionCount = 0;

bucketGame.questionId = 0;

bucketGame.startTime = "";

bucketGame.countOfBuckets,bucketGame.newQuestionObject,bucketGame.randomQuestion;

$(document).ready(function() {
    $(".se-pre-con").fadeOut("slow");
    if ((navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) && !(window.location.href.includes('http')))
        Utils.mobileDeviceFlag = true;
    /* if(Utils.mobileDeviceFlag)
    	Utils.Path=Android.getMediaPath("Aksharkhadi");  */
    setTimeout(function() {
        bucketGame.setWelcomeImage();
    }, 300);
});
bucketGame.setWelcomeImage = function()
{
	document.getElementById('homeImage').style.visibility='hidden';
	$('body').css('background-image', 'url("' + background + '")');
	$('#homeImage').attr('src',Utils.Path +'resources/Home.png');
	$('#playImage').attr('src',Utils.Path +'resources/Play.png');
	$('#logoImage').attr('src',Utils.Path +'resources/logo.png');
	$('#playAgain').attr('src',Utils.Path +'resources/playAgain.png');
	$('.heading').find('h2').text(gameName);
	$('.welcome,#playGame').show();
	$('.game-screen').hide();
	$('.end-screen').hide();
	$('.btn-play-now').text(play);
};

bucketGame.StartGame = function(){
	$("#playGame").on('click', function(event) {
		$('body').css('background-image', 'url("resources/BG.jpg")');
		$('.game-screen').show();
		document.getElementById('homeImage').style.visibility='visible';
		bucketGame.showNewQuestion();
		bucketGame.getInitialData();
	});
};

bucketGame.allQuestions=JSONData.slice();

bucketGame.getInitialData=function()
{
	document.getElementById('score').innerHTML=scoreLabel;
}

bucketGame.resetElement = function(){
	bucketGame.selectedCategories = {};
	bucketGame.selectedQuestions = [];
	$('#score').text(scoreLabel);
	$('.all-buckets').empty();
};

bucketGame.showNewQuestion=function()
{
	$('.para').css({'pointer-events':'auto'}); 
	bucketGame.questionCount++;
	bucketGame.answerIsFalse = false;
	bucketGame.resetElement();
	var d=new Date();
	bucketGame.startTime=d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	$('#playGame').hide();
	bucketGame.randomQuestion=Math.floor(Math.random()*bucketGame.allQuestions.length);
	bucketGame.newQuestionObject=bucketGame.allQuestions[bucketGame.randomQuestion];
	document.getElementById('showQuestion').innerHTML=bucketGame.newQuestionObject['question'];
	bucketGame.questionId=bucketGame.newQuestionObject['questionNumber'];
	bucketGame.countOfBuckets=bucketGame.allQuestions[bucketGame.randomQuestion]['options'].length;
	bucketGame.populateBucketsOnscrren(bucketGame.countOfBuckets);
	bucketGame.attachLabelNamesToBuckets();
	bucketGame.answerOfQuestion=bucketGame.newQuestionObject['answerKey'];
	bucketGame.allQuestions.splice(bucketGame.randomQuestion,1);
}

bucketGame.populateBucketsOnscrren=function(countOfBuckets)
{
	var newBucket, i=1;
	for(i=1;i<=bucketGame.countOfBuckets;i++)
	{
			newBucket=document.createElement('div'); //numberdivs created
			newBucket.id='bucket'+i;	
			newBucket.className='para nopadding col-md-4 col-lg-4 col-sm-4 col-xs-4  bucketPicture';
			$(newBucket).on('click','bucketGame.showResult(this.id)');
			newBucket.setAttribute('onclick','bucketGame.showResult(this.id)');
			newBucket.style.backgroundImage='url(resources/'+i+'.png)';
			$(".all-buckets").append(newBucket);
	}
}

bucketGame.attachLabelNamesToBuckets=function()
{
	var temp=bucketGame.newQuestionObject['options'].length;
	var k=1,picRandomName,label;
	var length=bucketGame.newQuestionObject['options'].length;
	while(k<=length)
	{
		temp=bucketGame.newQuestionObject['options'].length;
		picRandomName=Math.floor((Math.random()*(temp-1))+0);
		document.getElementById('bucket'+k).innerHTML=bucketGame.newQuestionObject['options'][picRandomName]['value'];
		$('#bucket'+k).data("keyValue",bucketGame.newQuestionObject['options'][picRandomName]['key']);
		bucketGame.newQuestionObject['options'].splice(picRandomName,1);
		k++;
	}
}

bucketGame.showResult = function(bucketId)
{
		var classToAdd, randomCheer, randomMsg, audio;
		var $levelDiv = $(this).find(".level");
		$question = $("#dvSource");
		$cheerMessageDiv = $("#messageDiv");
		var keyOfLabel=$("#"+bucketId).data('keyValue');
		if(bucketGame.answerOfQuestion===keyOfLabel)
		{
			$('.para').css({'pointer-events':'none'}); 
			if(bucketGame.answerIsFalse == false){

				bucketGame.scoreObject.scoredMarks=10;
				bucketGame.scoreObject.score+=10;
			}
			bucketGame.scoreObject.scoredMarks=10;
			if($("#"+bucketId).hasClass("1")){
				classToAdd = "moveBtn";
				$question.addClass(classToAdd);
			}
			else if($("#"+bucketId).hasClass("2")){
				classToAdd = "moveBtnMiddle"; 
				$question.addClass(classToAdd);
			}
			else if($("#"+bucketId).hasClass("3")){
				classToAdd = "moveBtnLast"
				$question.addClass(classToAdd);
			}
			randomCheer = cheering[Math.floor(Math.random()*cheering.length)];
			$cheerMessageDiv.addClass('magictime puffIn');
			$cheerMessageDiv.text(randomCheer);
			$levelDiv.animate({
				height: $levelDiv.height() + 8
			},500);
			bucketGame.playSound("resources/soundFiles/For_correct.mp3");
			setTimeout(function(){
				$cheerMessageDiv.removeClass('magictime puffIn');
				$cheerMessageDiv.text("");
			},1200);
			setTimeout(function()
			{
				$question.removeClass(classToAdd);
				/*if(bucketGame.questionCount < bucketGame.allQuestions.length)
				{
					bucketGame.showNewQuestion();
				}*/
				if(bucketGame.questionCount < 10)
				{
					bucketGame.showNewQuestion();
				}
				else
				{
					$('.welcome').hide();
					$('.game-screen').hide();
					document.getElementById('homeButton').style.visibility="hidden";
					$('.end-screen').show();
					$(".end-screen").find("h2").text(endingMessage);
					$(".btn-replay").text(playAgain);
				}		
			},1500);
			$('.score').addClass('magictime puffIn');
				setTimeout(function(){
					$('.score').removeClass('magictime puffIn');
				},1500);
			$('.score').text(bucketGame.scoreObject.score);	
		}
		else{  //answer is false
			bucketGame.attempt ++;
			bucketGame.answerIsFalse = true;
			bucketGame.scoreObject.scoredMarks=0;
			randomMsg = wrongAnswerMsgs[Math.floor(Math.random()*wrongAnswerMsgs.length)];
			$('#messageDiv').addClass('magictime puffIn');
			$("#messageDiv").text(randomMsg);
			bucketGame.playSound("resources/soundFiles/For_wrong1.wav");
			setTimeout(function(){
				$('#messageDiv').removeClass('magictime puffIn');
				$("#messageDiv").text("");
			},1200);
			$("#"+bucketId).addClass("shake shake-crazy");
			setTimeout(function(){
				$("#"+bucketId).removeClass("shake shake-crazy");
			},400);
		}
};
bucketGame.playSound = function(soundfile) {
	document.getElementById('audio').src = soundfile;
	document.getElementById('audio').play();
 }
bucketGame.goToHome=function()
{
  location.reload();
}