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,replayFlag=false,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.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("playImg").src = Utils.Path + "images/skin/Play.png";
	document.getElementById("questionDiv").style.backgroundImage = "url(" + Utils.Path + "images/skin/Cloud.png)";
	document.getElementById("selectNumbers").style.backgroundImage = "url(" + Utils.Path + "images/skin/A.png)";
	document.getElementById("acknowledgementDiv").style.backgroundImage = "url(" + Utils.Path + "images/skin/B.png)";
	document.getElementById("numberPad").style.backgroundImage = "url(" + Utils.Path + "images/skin/C.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.GunajInstructions[Fraction.counter];
	document.body.style.backgroundImage = "url(" + Utils.Path + "images/skin/BG.jpg)";
	Fraction.questionCtr = 0;
	$("#coverPage,#numPad").hide();
	$("#gamePage,#nextDiv").show();
      $("#screen2,#screen4").hide();
   $("#homeButton").css("visibility","visible");
   Fraction.showthirdScreen();
}
Fraction.showthirdScreen=function(object)
{
	document.getElementById("nextDiv").style.visibility = "visible";
	document.getElementById("questionScreen").style.backgroundImage = "url(" + Utils.Path + "images/skin/B.png)";
	$("#screen4,#numPad,#nextDiv").hide();
	$("#screen3,#gamePage").show();Fraction.counter=1;
	document.getElementById("Text").innerHTML=Fraction.hardcodedData.GunajInstructions[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);
	 //innerDiv.innerHTML=Fraction.questionArray[cnt][0]+" , "+Fraction.questionArray[cnt][1];
	 
	  var innerDiv1 = document.createElement("div");
     innerDiv1.setAttribute("id","innerDiv1"+i+j);
     innerDiv1.setAttribute("class","nopadding  col-sm-6 col-xs-6 col-md-6 col-lg-6");
     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-8 col-sm-8 col-lg-8 col-md-8 nopadding");
	innerDiv.appendChild(imageElement);
	imageElement.src=Utils.Path+"images/"+Fraction.hardcodedData.imageArr[cnt];
	cnt++;
	}
	}}
}
Fraction.calculation = function(){
 var upperVal , lowerVal ,randomArry=[],min=2,max=9,firstNumberArray=[],secondNumberArray=[],commonNumberArray=[],flag1=true;
	while(Fraction.questionArray.length < 4){
	 for(var i=0;i<2;i++)
	 {
		upperVal = Math.floor(Math.random()*(max-min)+min);
		randomArry.push(upperVal);
	 }
		 firstNumberArray.push(randomArry[0]);secondNumberArray.push(randomArry[1]); var cnt1=2;
		 for(var i=0;i<5;i++)
		 {
			var num=randomArry[0]*(cnt1);
			firstNumberArray.push(num);
			var num=randomArry[1]*(cnt1);
			secondNumberArray.push(num);
			cnt1++;	 }
			var num=Math.min(firstNumberArray.length,secondNumberArray.length);
			var num1=Math.max(firstNumberArray.length,secondNumberArray.length);
			console.log(num,num1);
	 for(var i=0;i<num1;i++){
			for(var j=0;j<num;j++){
			if(firstNumberArray[i]==secondNumberArray[j])
				commonNumberArray.push(secondNumberArray[j]);
			}}
	 if(randomArry[0]!=randomArry[1] && commonNumberArray.length>0)//randomArry[0]<randomArry[1] &&
	 {
		 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=[],flag1=true;
 }
  console.log(Fraction.questionArray);
}
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";
	var num=document.getElementById(object.id).getAttribute("data");
		selectedNumber=document.getElementById(object.id).getAttribute("data");
			Fraction.nextFlag=document.getElementById(object.id);
	//document.getElementById(object.id).style.display = "none";
	//document.getElementById("image"+num).style.display = "block";
	$("#screen3").hide();
	$("#numPad,#screen4,#nextDiv").show();//,#SmallNumberDivs
	document.getElementById("answerDivs").innerHTML="";
	document.getElementById("FirstNumberDivs").innerHTML="";
	document.getElementById("SecondNumberDivs").innerHTML="";
	if(replayFlag==false)
		document.getElementById("SmallNumberDivs").innerHTML="";
	var firstNumberArray=[],secondNumberArray=[],commonNumberArray=[];
	var number=document.getElementById(object.id).innerHTML.split(",");
		label=document.getElementById(object.id).innerHTML;
	Fraction.clickNumbers=document.getElementById(object.id).innerHTML.split(",");
	document.getElementById("FirstNumber").innerHTML=number[0]+" "+Fraction.hardcodedData.que+"  :-";
	document.getElementById("SecondNumber").innerHTML=number[1]+" "+Fraction.hardcodedData.que+"  :-";
	document.getElementById("answer").innerHTML=number[0]+" "+Fraction.hardcodedData.GunajInstructions[Fraction.counter][0]+" "+number[1]+" "+Fraction.hardcodedData.GunajInstructions[Fraction.counter][1]+"  :-";
	var cnt1=2, num=number[0].trim(" ");
	for(var i=0;i<6;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);
		first.setAttribute("data",num);
		firstNumberArray.push(num);
		num=number[0]*(cnt1);
		cnt1++,cnt++;
		if(cnt1==(number[0]+1))
				break;
	}
	var cnt1=2, num=number[1].trim(" ");
	for(var i=0;i<6;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);
		second.setAttribute("data",num);
		secondNumberArray.push(num);
		num=number[1]*(cnt1);
		cnt1++,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.GunajInstructions[Fraction.counter][0]+" "+number[1]+" "+Fraction.hardcodedData.GunajInstructions[Fraction.counter][1];
	document.getElementById("SmallNumber").innerHTML=(Fraction.clickNumbers[0]).trim(" ")+" "+Fraction.hardcodedData.GunajInstructions[Fraction.counter][0]+" "+(Fraction.clickNumbers[1]).trim(" ")+" "+Fraction.hardcodedData.GunajInstructions[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("SmallNumberDivs").setAttribute("id","firstDiv"+cnt);
	document.getElementById("firstDiv"+cnt).setAttribute("data",smallestNumber);
	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)
		{
			document.getElementById("responseBoxDiv").style.display="block";
			document.getElementById("questionMsg").innerHTML="Congratulations, You win!"
			document.getElementById("numPad").style.pointerEvents = "auto";
					setTimeout(function(){
			Fraction.gameOver();
						}, 3000);
		}
		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";
			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(){
			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();
			}, 2000);
		}
		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){
			Fraction.queCounter=counter+6;
			Fraction.clickedAnsDiv="firstDiv"+Fraction.queCounter;
			document.getElementById(Fraction.clickedAnsDiv).classList.add('blink_me');
			counter=counter+6;
		}
		else
		document.getElementById(Fraction.clickedAnsDiv).classList.add('blink_me'); 
		}, 500);}
		console.log(Fraction.DivCounter,Fraction.queCounter);
	}	
	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("responseBoxDiv").style.display="none";
				document.getElementById("credit").style.display="auto";
		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 ="";
	$("#acknowledgementDiv").hide();
	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=[];
	document.getElementById("numPad").style.pointerEvents = "auto";
	Fraction.initializeVariables();
	Fraction.showthirdScreen();
	}
Fraction.goHome = function(){
    document.getElementById("playSound").pause();
	Fraction.scoreFunction();
    location.reload();
}
