var DirectProportion = {};
var Utils = {};
Utils.path = "";
var questionId = 0, startTime = 0, scoredMarks = 0, totalMarks = 10, resId = 0, label, scoreFlag = false;

$(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(navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i))
		Utils.mobileDeviceFlag=true; */
	else
		Utils.mobileDeviceFlag=false;
    setTimeout(function () {
        DirectProportion.init();
    });
}, 200);

DirectProportion.init = function () {
    $("#home").hide();
    $("#gamePage").hide();
    $("#finalPage").hide();
	document.title = DirectProportion.hardcodedData.nameOfGame;
	DirectProportion.setImages();
	DirectProportion.initVariables();
	DirectProportion.makeNumberPad();
	DirectProportion.makeQuestionLayout();
	DirectProportion.createAcknowledgementDivs();
}

DirectProportion.setImages = function () {
    document.body.style.backgroundImage = Utils.path + "url('images/skin/lastBG.png')";
    document.getElementById("home").src = Utils.path + "images/skin/Home.png";
    document.getElementById("logo").src = Utils.path + "images/skin/logo.png";
    document.getElementById("playbutton").src = Utils.path + "images/skin/Play.png";
    document.getElementById("soundImage").src = Utils.path + "images/skin/Sound.png";
	document.getElementById("nextImage").src = Utils.path + "images/skin/Next.png";
	document.getElementById("correctImage").src = Utils.path + "images/skin/Check.png";
    document.getElementById("playAgainBtn").src = Utils.path + "images/skin/replay.png";
    document.getElementById("creditbtn").src = Utils.path + "images/skin/Credits.png";
	document.getElementById("gameOver").innerHTML = DirectProportion.hardcodedData.message2;
}

DirectProportion.initVariables = function () 
{
	DirectProportion.answer = '';
	DirectProportion.questionCnt = 0;
	DirectProportion.nextQueTimer = 0;
	DirectProportion.constantArray = [];
	DirectProportion.questionArray = [];
	DirectProportion.answerArray = []; 
	DirectProportion.finalAnsArray = [];
}

DirectProportion.playGame = function () {
    document.body.style.backgroundImage = Utils.path + "url('images/skin/FullBG.jpg')";
    $("#home").show();
	$("#gamePage").show();
	$("#numPad").show();
    $("#coverPage").hide();
	$("#finalPage").hide();
	$('#correctBtn').hide();
	$('#wordProblemDiv').hide();
    
	if(DirectProportion.questionCnt != 5)
		DirectProportion.makeQuestion();
	else
		DirectProportion.gameOver();    
}

DirectProportion.makeQuestion = function()
{
	var	jsonObj = {}, nonPrimeNoArray = [];
	var highArray = [], tableArray = [], subQueArray = []; 
	var low = 0, high = 0, ans = 0, constant = 0, subQuestionCnt = 0;
	
	//Only non-prime number can be asked in question.
	nonPrimeNoArray[k++] = 1;	//add First non-prime no 1 into array.
	for(i=1; i<100; i++){		//Find all non-prime number from 1 to 99.	Total non-Prime numbers are 74.
		for(j=2; j<i; j++){
			if(i%j == 0){
				nonPrimeNoArray[k++] = i;
				break;
			}
		}
	}
	//console.log('no of elements in nonPrimeNoArray');
	//console.log(nonPrimeNoArray);
	
	constant = Math.floor(Math.random() * 9)+2;		// Check for unique constant.
	while(DirectProportion.constantArray.includes(constant))
		constant = Math.floor(Math.random() * 9)+2;		
	DirectProportion.constantArray.push(constant);
	for(i=1; i<=10; i++){
		ans = constant * i;
		tableArray[i-1] = ans;
	}
	low = Math.floor(Math.random() * 7)+1;

	//Check for next two question. always N1 & N2 should be greater than previous N1 & N2 and all questions ans should be same after  division.
	while(subQuestionCnt != 3)
	{
		jsonObj['N1'] = tableArray[low] / constant;
		jsonObj['N2'] = tableArray[low];
		jsonObj['Ans'] = constant;
		subQueArray[subQuestionCnt] = jsonObj;
		jsonObj = {}; 
		subQuestionCnt++;
		low += 1;
	}	
	DirectProportion.questionArray[DirectProportion.questionCnt] = subQueArray;
		
	for(i=1; i<4; i++)		//add question numbers to Div.
    {
		for(k=0; k<3; k++)
		{		 
			if(k == 0){
				document.getElementById('div'+(i+1)+(k+1)).innerHTML = DirectProportion.questionArray[DirectProportion.questionCnt][i-1].N1;
			}				
			if(k == 1){
				document.getElementById('div'+(i+1)+(k+1)).innerHTML = DirectProportion.questionArray[DirectProportion.questionCnt][i-1].N2;
				if(i == 3){
					document.getElementById('div'+(i+1)+(k+1)).style.color = '#f01111';
					document.getElementById('div'+(i+1)+(k+1)).innerHTML = DirectProportion.hardcodedData.questionMark;
					document.getElementById('div'+(i+1)+(k+1)).classList.add('blink_me');
				}
			}
			if(k == 2){
				document.getElementById('div'+(i+1)+(k+1)).innerHTML = DirectProportion.questionArray[DirectProportion.questionCnt][i-1].Ans;
			}
		}    
    }    		
	//DirectProportion.questionCnt++;
	subQueArray = [];

	//console.log('DirectProportion.constantArray');
	//console.log(DirectProportion.constantArray);
	console.log('DirectProportion.questionArray');
	console.log(DirectProportion.questionArray);
}

DirectProportion.makeQuestionLayout = function()
{
    var i, queRow, numberBtn;

    for(i=0; i<4; i++)
    {
		queRow = document.createElement('div');
        document.getElementById('queDiv').appendChild(queRow);
        queRow.setAttribute('id','row'+(i+1));   
        queRow.setAttribute('class','nopadding col-xs-11 col-sm-11 col-md-11 col-lg-11'); 
        
		for(k=0; k<3; k++)
		{		 
			numberBtn = document.createElement('div');
			document.getElementById('row'+(i+1)).appendChild(numberBtn);
			numberBtn.setAttribute('id','div'+(i+1)+(k+1));   
			
			if(i == 0 && k == 0){
				numberBtn.setAttribute('class','nopadding notationCss col-xs-3 col-sm-3 col-md-3 col-lg-3 col-xs-offset-1 col-sm-offset-1 col-md-offset-1 col-lg-offset-1'); 
				document.getElementById('div'+(i+1)+(k+1)).innerHTML = DirectProportion.hardcodedData.number1;
			}				
			else if(i == 0 && k == 1){
				numberBtn.setAttribute('class','nopadding notationCss col-xs-3 col-sm-3 col-md-3 col-lg-3 col-xs-offset-1 col-sm-offset-1 col-md-offset-1 col-lg-offset-1'); 
				document.getElementById('div'+(i+1)+(k+1)).innerHTML = DirectProportion.hardcodedData.number2;
			}
			else if(i == 0 && k == 2){
				numberBtn.setAttribute('class','nopadding notationCss col-xs-3 col-sm-3 col-md-3 col-lg-3 col-xs-offset-1 col-sm-offset-1 col-md-offset-1 col-lg-offset-1'); 
				//document.getElementById('div'+(i+1)+(k+1)).innerHTML = "K";
				document.getElementById('div'+(i+1)+(k+1)).innerHTML = DirectProportion.hardcodedData.formula;
			}	
			else{
				numberBtn.setAttribute('class','nopadding questionCss col-xs-3 col-sm-3 col-md-3 col-lg-3 col-xs-offset-1 col-sm-offset-1 col-md-offset-1 col-lg-offset-1'); 
			}
		}     
    }    
}

DirectProportion.clickedButton = function(clickedBtn)	//Clicked on Number pad.
{
	var clickedNo = '', finalAns='';
	
	if(clickedBtn == 'button42')		//If clicked on Clear Button of Number Pad
		document.getElementById('div42').innerHTML ='';
	else								//If clicked on Other Button of Number Pad
	{
		clickedNo = document.getElementById(clickedBtn).innerHTML;
		if(DirectProportion.answer.length == 0)
			document.getElementById('div42').innerHTML ='';
		document.getElementById('div42').append(clickedNo);
		
		DirectProportion.answer = document.getElementById('div42').innerHTML;
		DirectProportion.answerArray = DirectProportion.answer.split('');
		//DirectProportion.answer = parseInt(DirectProportion.answer);
		finalAns = DirectProportion.questionArray[DirectProportion.questionCnt][2].N2;
		finalAns = finalAns.toString();
		DirectProportion.finalAnsArray = finalAns.split('');

		if(DirectProportion.answerArray.length == DirectProportion.finalAnsArray.length)
		{
			if(DirectProportion.answer == finalAns)		//For Correct answer
			{
				$('#div42').css('color','green');
				$('#numberPad').css('pointerEvents','none');
				document.getElementById('div42').classList.remove('blink_me');
				document.getElementById('div42').classList.add('animated','flash');
				document.getElementById("playSound").src = Utils.path + "sounds/instructions/thats_right.mp3";
				document.getElementById("playSound").play();
			
				if(DirectProportion.nextQueTimer){					//For showing next question.
					clearTimeout(DirectProportion.nextQueTimer);
					DirectProportion.nextQueTimer = 0;
				}
				DirectProportion.nextQueTimer = setTimeout(function(){
					DirectProportion.answer = '';
					$('#div42').css('color','red');
					$('#numberPad').css('pointerEvents','auto');
					document.getElementById('div42').classList.remove('animated','flash');
					DirectProportion.showWordProblem();
					DirectProportion.questionCnt++;
					//DirectProportion.playGame();
				},1500);	
			}
			else{									//For wrong answer
				DirectProportion.showFormula();
				$('#div42').css('color','red');
				document.getElementById("playSound").src = Utils.path + "sounds/instructions/wrong_answer.mp3";
				document.getElementById("playSound").play();
				setTimeout(function(){
					DirectProportion.answer = '';
					document.getElementById('div42').innerHTML ='?';
					document.getElementById('numberPad').innerHTML ='';
					document.getElementById('div42').classList.add('blink_me');
					DirectProportion.makeNumberPad();
				},2000);
			}
		}
	}	
}

DirectProportion.showWordProblem = function()	//If answer is correct show Word problem.
{
	var statement = '', innerQueCnt = 0, wordProblemCnt = 0;
	$('#widges').hide();
	$('#questionDiv').hide();
	$('#correctBtn').show();
	$('#wordProblemDiv').show();
	document.getElementById('wordProblemDiv').innerHTML = '';

	var randomNo = Math.floor(Math.random() * DirectProportion.data.length);
	for(i=0; i<6; i++)
	{
		div = document.createElement("div");
		document.getElementById('wordProblemDiv').appendChild(div);
		$(div).prop('id','problemDiv'+(i+1));
		$(div).prop('class','nopadding wordProblemCss col-xs-12 col-sm-12 col-md-12 col-lg-12');
		if(i%2 == 0){
			$('#problemDiv'+(i+1)).css('color','black');
			statement = DirectProportion.data[randomNo].question[wordProblemCnt];
			statement = statement.replace('@', DirectProportion.questionArray[DirectProportion.questionCnt][wordProblemCnt].N1);
			statement = statement.replace('!', DirectProportion.questionArray[DirectProportion.questionCnt][wordProblemCnt].N2);
			$('#problemDiv'+(i+1)).text(statement);
			wordProblemCnt++;
		}
		else{
			$('#problemDiv'+(i+1)).css('color','red');
			number1 = DirectProportion.questionArray[DirectProportion.questionCnt][innerQueCnt].N1;
			number2 = DirectProportion.questionArray[DirectProportion.questionCnt][innerQueCnt].N2;
			ans = DirectProportion.questionArray[DirectProportion.questionCnt][innerQueCnt].Ans;
			$('#problemDiv'+(i+1)).text(DirectProportion.hardcodedData.constant +" "+ number2 + DirectProportion.hardcodedData.divideSign + number1 +" "+ DirectProportion.hardcodedData.equalSign + ans);
			innerQueCnt++;
		}
	}
}

DirectProportion.showFormula = function()	//If answer is wrong show formula
{
	var div;
	document.getElementById('numberPad').innerHTML = '';
		
	div = document.createElement('div');
    document.getElementById('numberPad').appendChild(div);
    div.setAttribute('id','formulaDiv');   
	div.setAttribute('class','nopadding formulaCss col-xs-12 col-sm-12 col-md-12 col-lg-12'); 
	
	div = document.createElement('div');						//Div for constant K = 
    document.getElementById('formulaDiv').appendChild(div);
    div.setAttribute('id','constantDiv');   
	div.setAttribute('class','nopadding col-xs-5 col-sm-5 col-md-5 col-lg-5 col-sm-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-offset-1'); 
	document.getElementById('constantDiv').innerHTML = DirectProportion.hardcodedData.constant;
	
	div = document.createElement('div');						//Div for N1 & N2
    document.getElementById('formulaDiv').appendChild(div);
    div.setAttribute('id','numberDiv');   
	div.setAttribute('class','nopadding col-xs-6 col-sm-6 col-md-6 col-lg-6'); 

	div = document.createElement('div');						//Div for N2
    document.getElementById('numberDiv').appendChild(div);
    div.setAttribute('id','numberDiv1');   
	div.setAttribute('class','nopadding borderBottom col-xs-5 col-sm-5 col-md-5 col-lg-5 col-sm-offset-2 col-lg-offset-2 col-md-offset-2 col-xs-offset-2'); 
	document.getElementById('numberDiv1').innerHTML = DirectProportion.hardcodedData.number2;

	div = document.createElement('div');						//Div for N1
    document.getElementById('numberDiv').appendChild(div);
    div.setAttribute('id','numberDiv2');   
	div.setAttribute('class','nopadding col-xs-5 col-sm-5 col-md-5 col-lg-5 col-sm-offset-2 col-lg-offset-2 col-md-offset-2 col-xs-offset-2'); 
	document.getElementById('numberDiv2').innerHTML = DirectProportion.hardcodedData.number1;
}

DirectProportion.callNext = function()
{
	DirectProportion.questionCnt++;
	DirectProportion.playGame();
}

DirectProportion.callOkBtn = function()
{
	$('#correctBtn').hide();
	$('#wordProblemDiv').hide();
	$('#widges').show();
	$('#questionDiv').show();
	document.getElementById('wordProblemDiv').innerHTML = '';
	DirectProportion.playGame();
}

DirectProportion.callSound = function()
{
	document.getElementById("playSound").src = Utils.path + "sounds/instructions/please_ans_the_ques.mp3";
	document.getElementById("playSound").play();	
}

DirectProportion.gameOver = function()
{
	$("#gamePage").hide();
    $("#finalPage").show();
	document.body.style.backgroundImage = Utils.path + "url('images/skin/lastBG.png')";
	
	DirectProportion.questionCnt = 0;
	DirectProportion.questionArray = [];
	DirectProportion.constantArray = [];
}

DirectProportion.callHome = function()
{
	location.reload();
}

DirectProportion.createAcknowledgementDivs = function() {
	var i = 0, div;
	for (i = 0; i < Object.keys(DirectProportion.AcknowledgementPageData).length; i++) {
		div = document.createElement('div');
		div.className = "nopadding fontClass firstLetter col-xs-12 col-sm-12 col-md-12 col-lg-12";
		div.innerHTML = Object.keys(DirectProportion.AcknowledgementPageData)[i] + " : " + Object.values(DirectProportion.AcknowledgementPageData)[i];
		acknowledgementDiv.appendChild(div);
	}
	div = document.createElement('div');
	div.className = "nopadding fontClass firstLetter col-xs-12 col-sm-12 col-md-12 col-lg-12";
	div.innerHTML = DirectProportion.hardcodedData.createdOn;
	div.style.marginBottom = "2vh";
	acknowledgementDiv.appendChild(div);
}

DirectProportion.showAcknowledgement = function () {
	$("#acknowledgementDiv").show();
}
