var Fraction = {};
var Utils = {};
Utils.Path = '';
Utils.mobileDeviceFlag = false;
var questionId = 0, startTime = 0, scoredMarks = 0, totalMarks = 10, resId = 0, level = 1, label = '',AttemptFlag=false,cnt=0,currentLevel,counter=0,noOfblock,currentNumbers,selectedNumber;
$(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("Fraction");  */
	setTimeout(function () {
		Fraction.init();
	}, 100);
});

Fraction.init = function () {
	$("#coverPage").show();
	$("#gamePage").hide();
	$("#endPage").hide();
	$(".header").not("#logo").css("visibility", "hidden");
	document.body.style.backgroundImage = "url(" + Utils.Path + "images/skin/" + Fraction.hardcodedData.coverPage + ")";
	Fraction.setAllImages();
	Fraction.makeNumberPad();
}
Fraction.initializeVariables = function () { 
	Fraction.questionArray=[];
	Fraction.Answer ;
	Fraction.questionCounter=0;
	Fraction.BlinkCounter=0;
	Fraction.wrongAns;
	 Fraction.correctAns;
	Fraction.divider;
	Fraction.attemptFlag=false;
	Fraction.correctAnsFlag=false;
	Fraction.counter=0;
	Fraction.clickedAnsDiv="";
	Fraction.queCounter=0;
	Fraction.DivCounter=0;
	Fraction.commonNumberArray=[];
	Fraction.clickNumbers;
	Fraction.screenFlag=false;
	Fraction.helpFlag=false;
	Fraction.nextFlag;
	Fraction.scoreFlag=false;
}
Fraction.setAllImages = function () {
	document.getElementById("homeImage").src = Utils.Path + "images/skin/Home.png";
	document.getElementById("next").src = Utils.Path + "images/skin/Next.png";
	document.getElementById("logoImage").src = Utils.Path + "images/skin/logo.png";
		document.getElementById("help").src = Utils.Path + "images/skin/Help.png";
	document.getElementById("playImg").src = Utils.Path + "images/skin/Play.png";
	document.getElementById("questionDiv").style.backgroundImage = "url(" + Utils.Path + "images/skin/B.png)";
	document.getElementById("selectNumbers").style.backgroundImage = "url(" + Utils.Path + "images/skin/B1.png)";
	document.getElementById("acknowledgementDiv").style.backgroundImage = "url(" + Utils.Path + "images/skin/C.png)";
	document.getElementById("numberPad").style.backgroundImage = "url(" + Utils.Path + "images/skin/D.png)";
		document.getElementById("gameOver").style.backgroundImage = "url(" + Utils.Path + "images/skin/B.png)";
	document.getElementById("creditImg").src = Utils.Path + "images/skin/Credits.png";
	document.getElementById("replayImg").src = Utils.Path + "images/skin/Replay.png";
	
}
Fraction.playGame = function () {
	Fraction.initializeVariables();
	document.getElementById("Text").innerHTML=Fraction.hardcodedData.GunankhadInstructions[Fraction.counter];
	document.body.style.backgroundImage = "url(" + Utils.Path + "images/skin/BG.jpg)";
	Fraction.questionCtr = 0;
	$("#coverPage,#SmallNumberDivs").hide();
	$("#gamePage,#nextDiv").show();
   $("#screen2").hide();
    $("#screen5,#numPad").hide();
   $("#homeButton").css("visibility","visible");
   	$("#helpDiv").css("visibility", "hidden");
	Fraction.showthirdScreen();
}
Fraction.showthirdScreen=function(object)
{
	document.getElementById("nextDiv").style.visibility = "visible";
	document.getElementById("questionScreen").style.backgroundImage = "url(" + Utils.Path + "images/skin/C.png)";
	$("#numPad,#screen6,#nextDiv,#screen4").hide();
		$("#helpDiv").css("visibility", "hidden");
	$("#screen3,#gamePage").show();Fraction.counter=1;
	document.getElementById("Text").innerHTML=Fraction.hardcodedData.GunankhadInstructions[Fraction.counter];
	Fraction.counter++;
	if(Fraction.screenFlag==false){
	Fraction.calculation();
	var cnt=0;
	for(var i=0;i<2;i++)
	{
	var outerDiv = document.createElement("div");
     outerDiv.setAttribute("id","Div"+i);
     outerDiv.setAttribute("class","nopadding  col-sm-12 col-xs-12 col-md-12 col-lg-12");
     document.getElementById("selectNumbers").appendChild(outerDiv);
	 for(var j=0;j<2;j++)
	{
	 var innerDiv = document.createElement("div");
     innerDiv.setAttribute("id","innerDiv"+i+j);
     innerDiv.setAttribute("class","nopadding cls col-sm-6 col-xs-6 col-md-6 col-lg-6");
     outerDiv.appendChild(innerDiv);
	 
	  var innerDiv1 = document.createElement("div");
     innerDiv1.setAttribute("id","innerDiv1"+i+j);
     innerDiv1.setAttribute("class","nopadding cll col-sm-9 col-xs-9 col-md-9 col-lg-9 col-xs-offset-1 col-md-offset-1 col-lg-offset-1 col-sm-offset-1");
     innerDiv.appendChild(innerDiv1);
	 innerDiv1.setAttribute("onclick","Fraction.showFourthScreen(this)");
	 innerDiv1.setAttribute("data",cnt);
	 innerDiv1.innerHTML=Fraction.questionArray[cnt][0]+" , "+Fraction.questionArray[cnt][1];

	var imageElement=document.createElement('img');
	imageElement.setAttribute("id","image"+cnt);
	imageElement.setAttribute("class","cl img-responsive col-xs-12 col-sm-12 col-lg-12 col-md-12 nopadding");
	innerDiv.appendChild(imageElement);
	imageElement.src=Utils.Path+"images/"+Fraction.hardcodedData.imageArr[cnt];
	cnt++;
	}
	}}
}
Fraction.calculation = function(){
 var upperVal , lowerVal ,randomArry=[],min=2,max=20,firstNumberArray=[],secondNumberArray=[],commonNumberArray=[];
	while(Fraction.questionArray.length < 4){
		//for(var i=0;i<2;i++)
		while(randomArry.length < 2)
		 {
			var upperVal = Math.floor(Math.random()*(max-min)+min);
			var flag=true;
			 for(var i = 2; i <= upperVal - 1; i++){
                if (upperVal % i == 0) {
                    flag = false;
					break;
			 } }
			 if(flag==false)
				randomArry.push(upperVal);
			 //randomArry.push(upperVal);
		 }
			var cnt1=1,array=[],array1=[],num;
			for(var i=1;i<=randomArry[0];i++){
				if((randomArry[0])%cnt1==0){
					num=cnt1;
					array.push(num);}
					cnt1++;}
				for(var i=0;i<6;i++)
				{
					if(i<array.length)
						firstNumberArray.push(array[i]);
					if(cnt1==(randomArry[0]+1))
						break;
				}
				var cnt1=1,num;
				for(var i=1;i<=randomArry[1];i++){
					if((randomArry[1])%cnt1==0){
						num=cnt1;
					array1.push(num);}cnt1++;}
				for(var i=0;i<6;i++)
				{
					if(i<array1.length)
						secondNumberArray.push(array1[i]);
					if(cnt1==(randomArry[1]+1))
						break;
				}
			for(var i=0;i<firstNumberArray.length;i++){
			for(var j=0;j<secondNumberArray.length;j++){
			if(firstNumberArray[i]==secondNumberArray[j])
				commonNumberArray.push(firstNumberArray[j]);
			}}
	 if(randomArry[0]<randomArry[1] && commonNumberArray.length>0)
	 {
		 if(Fraction.questionArray.length>0){
			 var flag=false,cnt=0;
			 for(var i=0;i<Fraction.questionArray.length;i++){
			 if(Fraction.questionArray[i].includes(randomArry[0])){flag=true;} 
			 }
			 if(flag==false)
				Fraction.questionArray.push(randomArry);
		 }else{
		 Fraction.questionArray.push(randomArry);
		 }
	 }
	// if(Fraction.questionArray.length==4)
	//	 break;
    randomArry=[],firstNumberArray=[],secondNumberArray=[],commonNumberArray=[];
 }
  console.log(Fraction.questionArray);
}
function noOfBlocks(randomArry)
{
	var cnt1=2,array=[],array1=[],num,firstNumberArray=[],secondNumberArray=[];
			for(var i=1;i<=randomArry[0];i++){
				if((randomArry[0])%cnt1==0){
					num=cnt1;
					array.push(num);}
					cnt1++;}
				for(var i=0;i<6;i++)
				{
					if(i<array.length)
						firstNumberArray.push(array[i]);
					if(cnt1==(randomArry[0]+1))
						break;
				}
				var cnt1=2,num;
				for(var i=1;i<=randomArry[1];i++){
					if((randomArry[1])%cnt1==0){
						num=cnt1;
					array1.push(num);}cnt1++;}
				for(var i=0;i<6;i++)
				{
					if(i<array1.length)
						secondNumberArray.push(array1[i]);
					if(cnt1==(randomArry[1]+1))
						break;
				}
				noOfblock=Math.max(firstNumberArray.length,secondNumberArray.length);
				noOfblock=noOfblock+1;
				console.log(noOfblock,firstNumberArray.length,secondNumberArray.length);
}
Fraction.showFourthScreen=function(object)
{
	d = new Date(); //date object
    startTime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds(); //to get the current date
	Fraction.questionCounter++;
	Fraction.screenFlag=false;
	document.getElementById(object.id).style.pointerEvents = "none";
	document.getElementById(object.id).style.opacity = "0.5";
	document.getElementById("answerDivs").innerHTML="";
	document.getElementById("FirstNumberDivs").innerHTML="";
	document.getElementById("SecondNumberDivs").innerHTML="";
	document.getElementById("SmallNumberDivs").innerHTML="";
	var num=document.getElementById(object.id).getAttribute("data");
	selectedNumber=document.getElementById(object.id).getAttribute("data");
	Fraction.nextFlag=document.getElementById(object.id);
	$("#screen3").hide();
	$("#helpDiv").css("visibility", "visible");
	$("#numPad,#screen4,#SmallNumberDivs,#nextDiv").show();
	var firstNumberArray=[],secondNumberArray=[],commonNumberArray=[];
	var number=document.getElementById(object.id).innerHTML.split(",");
	label=document.getElementById(object.id).innerHTML;
		currentNumbers=document.getElementById(object.id).innerHTML.split(",");
	Fraction.clickNumbers=document.getElementById(object.id).innerHTML.split(",");
		document.getElementById("answer").innerHTML=number[0]+" "+Fraction.hardcodedData.GunankhadInstructions[Fraction.counter][0]+" "+number[1]+" "+Fraction.hardcodedData.GunankhadInstructions[Fraction.counter][1]+"  :-";
		document.getElementById("FirstNumber").innerHTML=number[0]+" "+Fraction.hardcodedData.que1+"  :-";
		document.getElementById("SecondNumber").innerHTML=number[1]+" "+Fraction.hardcodedData.que1+"  :-";
		noOfBlocks(number);
		var cnt1=1,array=[];//cnt1=1
		for(var i=1;i<=number[0];i++){
			if((number[0])%cnt1==0){
				num=cnt1;
				array.push(num);
			}
			cnt1++;}//array.push(number[0]);
	for(var i=0;i<noOfblock;i++)
	{
	var first = document.createElement("div");
     first.setAttribute("id","firstDiv"+cnt);
     first.setAttribute("class","nopadding num col-sm-1 col-xs-1 col-md-1 col-lg-1");
     document.getElementById("FirstNumberDivs").appendChild(first);
	 		if(i<array.length){
			first.setAttribute("data",array[i]);
			firstNumberArray.push(array[i]);}
			cnt++;
		if(cnt1==(number[0]+1))
				break;
	}
		var cnt1=1,array=[];//cnt1=1
		//num=cnt1;
		for(var i=1;i<=number[1];i++){
			if((number[1])%cnt1==0){
				num=cnt1;
				array.push(num);
			}
			cnt1++;}//array.push(number[1]);
	for(var i=0;i<noOfblock;i++)
	{
	var second = document.createElement("div");
     second.setAttribute("id","firstDiv"+cnt);
     second.setAttribute("class","nopadding num col-sm-1 col-xs-1 col-md-1 col-lg-1");
     document.getElementById("SecondNumberDivs").appendChild(second);
			if(i<array.length){
			second.setAttribute("data",array[i]);
			secondNumberArray.push(array[i]);}
					cnt++;
		if(cnt1==(number[1]+1))
				break;	
	}
	for(var i=0;i<firstNumberArray.length;i++){
		for(var j=0;j<secondNumberArray.length;j++){
			if(firstNumberArray[i]==secondNumberArray[j]){
				commonNumberArray.push(secondNumberArray[j]);
					Fraction.commonNumberArray.push(secondNumberArray[j]);
			}
		}
	}
	console.log(firstNumberArray,secondNumberArray,commonNumberArray);
	for(var i=0;i<commonNumberArray.length;i++)
	{
	var third = document.createElement("div");
     third.setAttribute("id","firstDiv"+cnt);
     third.setAttribute("class","nopadding num col-sm-1 col-xs-1 col-md-1 col-lg-1");
	third.setAttribute("data",commonNumberArray[i]);
     document.getElementById("answerDivs").appendChild(third);
	 cnt++;
	}
	Fraction.DivCounter=cnt;
	Fraction.clickedAnsDiv="firstDiv"+Fraction.queCounter;
	document.getElementById(Fraction.clickedAnsDiv).classList.add('blink_me'); 
	Fraction.counter++;
	document.getElementById("Text").innerHTML=number[0]+" "+Fraction.hardcodedData.GunankhadInstructions[Fraction.counter][0]+" "+number[1]+" "+Fraction.hardcodedData.GunankhadInstructions[Fraction.counter][1];
	document.getElementById("SmallNumber").innerHTML=(Fraction.clickNumbers[0]).trim(" ")+" "+Fraction.hardcodedData.GunankhadInstructions[Fraction.counter][0]+" "+(Fraction.clickNumbers[1]).trim(" ")+" "+Fraction.hardcodedData.GunankhadInstructions[Fraction.counter][1]+"  :-";
	var i = 0;var smallestNumber = Fraction.commonNumberArray[0];
	for(i = 0; i < Fraction.commonNumberArray.length; i++) {
		if(Fraction.commonNumberArray[i] < smallestNumber) 
			smallestNumber = Fraction.commonNumberArray[i];// choose smallest number
	}
	for(i = 0; i <1; i++) {
			var third = document.createElement("div");
	        third.setAttribute("id","firstDiv"+cnt);
			third.setAttribute("class","nopadding num1 col-sm-12 col-xs-12 col-md-12 col-lg-12");
			document.getElementById("SmallNumberDivs").appendChild(third);}
	var Num=Fraction.commonNumberArray[Fraction.commonNumberArray.length-1];
	document.getElementById("firstDiv"+cnt).setAttribute("data",Num);
	Fraction.DivCounter=cnt;
	Fraction.clickedAnsDiv="firstDiv"+Fraction.queCounter;
	document.getElementById(Fraction.clickedAnsDiv).classList.add('blink_me'); 
}
Fraction.clickedButton = function(clickedNumberId)  //Click on number pad button.
{
	Fraction.scoreFlag=true;
    var clickedBtn='';
    document.getElementById(clickedNumberId).style.color ='rgb(24,56,132)'; 
    setTimeout(function()
    { 
        document.getElementById(clickedNumberId).style.color ='white';  
    }, 150);
  	Fraction.attemptFlag = true;
		document.getElementById(Fraction.clickedAnsDiv).style.color ='rgb(24,56,132)'; 
        clickedBtn = document.getElementById(clickedNumberId).getAttribute("data");
        document.getElementById(Fraction.clickedAnsDiv).append(clickedBtn);
        Fraction.answer = document.getElementById(Fraction.clickedAnsDiv).innerHTML;
		var num=(document.getElementById(Fraction.clickedAnsDiv).getAttribute("data")).trim(" ");
		if(Fraction.answer.length == num.length){
                Fraction.checkAnswer();
			}
}
Fraction.checkAnswer=function()
{
	var num=(document.getElementById(Fraction.clickedAnsDiv).getAttribute("data")).trim(" ");
	document.getElementById("numPad").style.pointerEvents = "none";
	if(Fraction.answer==num)
	{
		document.getElementById(Fraction.clickedAnsDiv).style.color ='green'; 
		 if(Fraction.DivCounter==(Fraction.queCounter) &&	Fraction.questionCounter==4)
		{
					setTimeout(function(){
			Fraction.gameOver();
						}, 2000);
		}
		else if(Fraction.DivCounter==(Fraction.queCounter) && 	Fraction.screenFlag==false)//+1
		{
			scoredMarks=10;
			Fraction.scoreFunction();
			Fraction.nextFlag.style.display = "none";
			document.getElementById("nextDiv").style.visibility = "hidden";
			console.log(selectedNumber);
			var a=num-1;
			document.getElementById("image"+selectedNumber).style.display = "block";
			document.getElementById("playSound").src = Utils.Path + "sounds/instructions/Well_done__Aditi.mp3";
			document.getElementById("playSound").play();
		setTimeout(function(){
			document.getElementById("playSound").pause();
			cnt=0,Fraction.queCounter=0,counter=0,Fraction.DivCounter=0,Fraction.commonNumberArray=[];
			document.getElementById("firstDiv"+cnt).innerHTML="";
			Fraction.screenFlag=true;
			document.getElementById("numPad").style.pointerEvents = "auto";
		Fraction.showthirdScreen();
			}, 3000);
		}
		else{
		setTimeout(function()       
		{
		document.getElementById("numPad").style.pointerEvents = "auto";
		document.getElementById(Fraction.clickedAnsDiv).style.color ='rgb(24,56,132)'; 
		document.getElementById(Fraction.clickedAnsDiv).classList.remove('blink_me');
		Fraction.queCounter++;
		Fraction.clickedAnsDiv="firstDiv"+Fraction.queCounter;
		if(document.getElementById(Fraction.clickedAnsDiv).getAttribute("data")==null){
			var num=counter+noOfblock;
			Fraction.queCounter=num;
			Fraction.clickedAnsDiv="firstDiv"+Fraction.queCounter;
			document.getElementById(Fraction.clickedAnsDiv).classList.add('blink_me');
			counter=num;
		}
		else
		document.getElementById(Fraction.clickedAnsDiv).classList.add('blink_me'); 
		}, 500);}
		console.log(Fraction.DivCounter,Fraction.queCounter,Fraction.questionCounter);
	}	
	else
	{
		document.getElementById(Fraction.clickedAnsDiv).style.color ='red'; 
		setTimeout(function()       
		{
		document.getElementById(Fraction.clickedAnsDiv).style.color ='rgb(24,56,132)'; 
		document.getElementById(Fraction.clickedAnsDiv).innerHTML=""; 
				document.getElementById("numPad").style.pointerEvents = "auto";
		}, 500);
	}
}
Fraction.nextQuestion = function(){
	if(Fraction.scoreFlag==false)
	{
		scoredMarks=-1;
		Fraction.scoreFunction();
	}
	else{
		scoredMarks=0;
		Fraction.scoreFunction();
	}
	if(	Fraction.questionCounter==4)
	{
		Fraction.gameOver();
	}
	else{
	    Fraction.counter++;
				Fraction.scoreFlag=false;
		if(Fraction.questionCounter>0)
		{
			cnt=0,Fraction.queCounter=0,counter=0,Fraction.DivCounter=0,Fraction.commonNumberArray=[];
			document.getElementById("firstDiv"+cnt).innerHTML="";
			Fraction.screenFlag=true;
			document.getElementById("numPad").style.pointerEvents = "auto";
		}
		Fraction.showthirdScreen();
		}
}
Fraction.scoreFunction = function() {

        console.log(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
        if (Utils.mobileDeviceFlag) {
            Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
        }
    }

Fraction.gameOver = function () {
    document.getElementById("gamePage").style.display = "none";
    document.getElementById("endPage").style.display = "block";
		document.getElementById("gameOver").innerHTML = Fraction.hardcodedData.gameOver;
 }
Fraction.createAcknowledgementDivs = function () {
	document.getElementById("credit").style.pointerEvents = "none";
	document.getElementById("acknowledgementDiv").innerHTML = "";
    var i = 0, div;
    for (i = 0; i < Object.keys(Fraction.AcknowledgementPageData).length; i++) {
        div = document.createElement('div');
        div.className = "fontClass firstLetter col-xs-12 col-sm-12 col-md-12 col-lg-12";
        div.innerHTML = Object.keys(Fraction.AcknowledgementPageData)[i] + " : " + Object.values(Fraction.AcknowledgementPageData)[i];
        acknowledgementDiv.appendChild(div);
    }
    div = document.createElement('div');
    div.className = "fontClass firstLetter col-xs-12 col-sm-12 col-md-12 col-lg-12";
    div.style.marginBottom = "2vh";
    acknowledgementDiv.appendChild(div);
}
Fraction.showAcknowledgement = function () {
   // $("#acknowledgementDiv").show();
	document.getElementById("acknowledgementDiv").style.visibility = "visible";
	Fraction.createAcknowledgementDivs();
}
Fraction.replayGame = function(){
    document.getElementById("playSound").pause();
    document.getElementById("endPage").style.display = "none";
    document.getElementById("gameOver").innerHTML ="";
	document.getElementById("acknowledgementDiv").style.visibility = "hidden";
	document.getElementById("credit").style.pointerEvents = "auto";
	 AttemptFlag = false;
	 Fraction.screenFlag=false;
	document.getElementById("FirstNumberDivs").innerHTML="";
	document.getElementById("selectNumbers").innerHTML="";
	document.getElementById("SecondNumberDivs").innerHTML="";
	document.getElementById("SmallNumberDivs").innerHTML="";
	cnt=0,Fraction.queCounter=0,counter=0,Fraction.DivCounter=0,Fraction.commonNumberArray=[];
	Fraction.helpFlag=false;
	document.getElementById("numPad").style.pointerEvents = "auto";
	Fraction.initializeVariables();
	Fraction.showthirdScreen();
  }
  Fraction.goHome = function(){
    document.getElementById("playSound").pause();
	Fraction.scoreFunction();
    location.reload();
}
Fraction.help=function()
{
	if(	Fraction.helpFlag==false){
	Fraction.helpFlag=true;
	$("#nextDiv").hide();
   document.getElementById("numPad").style.display = "none";
     document.getElementById("questionScreen").style.display = "none";
   document.getElementById("questionDiv").style.display = "none";
	document.getElementById("MainDiv").style.display = "block";
	document.getElementById("questions").innerHTML ="";
	selectMatrix(currentNumbers[0],currentNumbers[1]);
	}
	else{
		$("#nextDiv").show();
		Fraction.helpFlag=false;
	document.getElementById("numPad").style.display = "block";
     document.getElementById("questionScreen").style.display = "block";
   	document.getElementById("questionDiv").style.display = "block";
		document.getElementById("questionDiv").style.display = "flex";
	document.getElementById("MainDiv").style.display = "none";
	}
}