var MultiplicationChart = {}; // namespace name
var Utils = {};
Utils.path = '';
Utils.mobileDeviceFlag = false;
var flag=true,correctcounter=0,PerimeterNumber=0,perimeterArray=[12,6,8,10,12,14],checkFlag=false,oldID,currentId,QueCounter=0,firstClickFlag=true,cnt=0;
var questionId = 0,startTime = 0,scoredMarks = 0,totalMarks = 0,resId = 0,label = "",level = 1;
$(document).ready(function() {
    if ((navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) && !(window.location.href.includes('http')))
        Utils.mobileDeviceFlag = true;
    $(".se-pre-con").fadeOut("slow");
	setTimeout(function() {
        MultiplicationChart.setImages();
    }, 100);
}, 200);
MultiplicationChart.setImages = function() {
        //document.body.style.backgroundImage = "url(" + Utils.path + MultiplicationChart.coverImage + ")";
		 document.body.style.backgroundImage = "url('images/Perimeter Game Cover_1.gif')";
        document.getElementById("homeid").style.visibility = "hidden";
        $("#playbtnid").prop("src", "images/Play-Button.png");
        $("#checkBtn").prop("src", "images/Wright-Button.png");
		//$("#restartBtn").prop("src", "images/undo-Button.png");
        $("#homeid").prop("src", "images/Home-Button.png");
        $("#logoid").prop("src", "images/logo.png");
		$("#nextbtnid").prop("src", "images/Next-Button.png");
		$("#nextbtn").prop("src", "images/Next-Button.png");
		document.getElementById("creditImg").src = Utils.path + "images/Credits-Button.png";
		document.getElementById("replayImg").src = Utils.path + "images/replayt-Button.png";
		document.getElementById("endPage").style.display = "none";
    }
MultiplicationChart.showInstruction = function() {
	$('#playBackground').prop('src', 'sounds/WhenYoureNotLooking.mp3');
	document.getElementById('playBackground').play();
	document.getElementById("coverpage").style.display = "none";
	document.body.style.backgroundImage = "url('images/Bg.png')";
    document.getElementById("Instructionpage").style.display = "block";
	    document.getElementById("homeid").style.visibility = "visible";
			document.getElementById("endPage").style.display = "none";
	document.getElementById("Instruction").style.backgroundImage = "url('images/Game-Screen.png')";
	for (j = 0; j <3; j++) {
        div = document.createElement("div");
        div.setAttribute("id", "ins" + (j + 1));
        div.setAttribute("class", " nopadding col-sm-12 col-lg-12 col-md-12 col-xs-12");
        document.getElementById('Instruction').appendChild(div);
		div.innerHTML=(j+1)+".  "+  MultiplicationChart.hardcodedData.Instruction[j];
    }
	}
    //create options
MultiplicationChart.selectMatrix = function() {
	QueCounter++;
    document.body.style.backgroundImage = "url('images/BG.png')";
    document.getElementById("coverpage").style.display = "none";
	document.getElementById("Instructionpage").style.display = "none";
    document.getElementById("MainDiv").style.display = "block";
	document.getElementById("questionsImg").style.backgroundImage = "url('images/Number-button.png')";
	document.getElementById("questionsText").style.backgroundImage = "url('images/Number-button.png')";
	document.getElementById("questionsOuterDiv").style.backgroundImage = "url('images/Game-Screen.png')";
	document.getElementById("showPerimeterImg").style.backgroundImage = "url('images/Number-button.png')";
	//PerimeterNumber=Math.floor(Math.random() * perimeterArray.length);
	document.getElementById("questionsText").innerHTML= MultiplicationChart.hardcodedData.QuestionText;
	document.getElementById("showPerimeter1").innerHTML="1";
	document.getElementById("showPerimeter2").innerHTML="1";
	document.getElementById("questionsImg").innerHTML=perimeterArray[PerimeterNumber];
	MultiplicationChart.createGrid();
}
MultiplicationChart.createGrid=function()
{
	var counter=1;
	for (j = 0; j < 10; j++) {
        div = document.createElement("div");
        div.setAttribute("id", "outerDiv" + (j + 1));
        div.setAttribute("class", " nopadding col-sm-12 col-lg-12 col-md-12 col-xs-12");
        document.getElementById('questions').appendChild(div);
        for (var i = 0; i < 10; i++) {
            Div = document.createElement("div");
            Div.setAttribute("id", ""+counter);
            Div.setAttribute("class", " nopadding quetions quetions1 text-center col-sm-1 col-lg-1 col-md-1 col-xs-1");
			Div.setAttribute("onclick", "MultiplicationChart.setImage(this)");
            div.appendChild(Div);
		   // Div.style.height = "7.5vh";
            //Div.style.width = "4.5vw";
            document.getElementById("" +counter).style.backgroundColor = "rgb(230, 227, 182)"; 
			Div.setAttribute("data", "false");
			counter++;
	}}
}
MultiplicationChart.setImage=function(object)
{
	var ClickFlag=false;
	if(firstClickFlag==false){
		var checkFlag=false,counter=1;
		for (j = 0; j < 10; j++) {
		for ( i = 0; i < 10; i++) {
		if(document.getElementById(""+counter).getAttribute("data")=="true"){
		checkFlag=true;}
		counter++;}}
		if(checkFlag==false){firstClickFlag=true;}
	}
	if(firstClickFlag==false)
	{
		counter=object.id;
		currentId=parseInt(""+counter);
		var id=object.parentNode.id;
		var currentposition=parseInt(""+counter);
		var a=[];
		a=String(currentposition).split('');
		if(currentId<91)
		{
			if(currentId!="100"){
			if(document.getElementById(""+(currentId+1)).getAttribute("data")=="true"){
			ClickFlag=true;}}
			if(id!="outerDiv10"){
			if(document.getElementById(""+(currentId+10)).getAttribute("data")=="true"){
			ClickFlag=true;}}
			if(a[0]==0 || currentposition!=1){
			if(document.getElementById(""+(currentId-1)).getAttribute("data")=="true" ){
			ClickFlag=true;}}
		}
		if(currentId >10)
		{
			if(document.getElementById(""+(currentId-1)).getAttribute("data")=="true" ){
			ClickFlag=true;}
			if(id!="outerDiv1"){
			if(document.getElementById(""+(currentId-10)).getAttribute("data")=="true"){
			ClickFlag=true;}}
			if(a[1]==1 || id=="outerDiv10"){
			if(document.getElementById(""+(currentId+1)).getAttribute("data")=="true"){
			ClickFlag=true;}}
		}
		if(ClickFlag==false)
		{
			ClickFlag=true;
			document.getElementById(object.id).style.backgroundImage = "url('images/Number-button.png')";
			setTimeout(function() {
			document.getElementById(object.id).style.backgroundImage="";
			document.getElementById(object.id).setAttribute("data", "false");
				},300);
		}
		else
		{
			if(document.getElementById(object.id).getAttribute("data")=="true")
			{
				document.getElementById(object.id).style.backgroundImage="";
				document.getElementById(object.id).setAttribute("data", "false");
			}else{
			document.getElementById(object.id).style.backgroundImage = "url('images/Number-button.png')";
			flag=false;
			document.getElementById(object.id).setAttribute("data", "true");
			}
		}
	}
	if(firstClickFlag==true)
	{
		firstClickFlag=false;
		if(document.getElementById(object.id).getAttribute("data")=="true")
		{
			document.getElementById(object.id).style.backgroundImage="";
			document.getElementById(object.id).setAttribute("data", "false");		
		}else{
		document.getElementById(object.id).style.backgroundImage = "url('images/Number-button.png')";
		flag=false;
		document.getElementById(object.id).setAttribute("data", "true");
		}	
	}
}
MultiplicationChart.checkAnser=function(object)
{
	var currentId="";
	var correctcounter=0,cnt=1;
		for(var k=1;k<=10;k++)
		{
			for(var p=1;p<=10;p++)
			{
				if(document.getElementById(""+cnt).getAttribute("data")=="true")
				{
					currentId=parseInt(""+cnt);
					if(document.getElementById(""+(currentId-1))==null || document.getElementById(""+(currentId-1)).getAttribute("data")=="false" ){
					correctcounter=correctcounter+1;}
					if(document.getElementById(""+(currentId+1))==null || document.getElementById(""+(currentId+1)).getAttribute("data")=="false"){
					correctcounter=correctcounter+1;}
					if(document.getElementById(""+(currentId-10))==null || document.getElementById(""+(currentId-10)).getAttribute("data")=="false" ){
					correctcounter=correctcounter+1;}
					if(document.getElementById(""+(currentId+10))==null || document.getElementById(""+(currentId+10)).getAttribute("data")=="false" ){
					correctcounter=correctcounter+1;}
				}
				cnt++;
			}
		}
	if(flag==false)
	{
		console.log(perimeterArray[PerimeterNumber],correctcounter);
		if(perimeterArray[PerimeterNumber]==correctcounter)
		{
			document.getElementById("playSound").src = Utils.path + "sounds/thats_right.mp3";
			document.getElementById("playSound").play();
			document.getElementById("nextbtnid").classList.add('blink_me'); 
		}
		else{
			document.getElementById("playSound").src = Utils.path + "sounds/Try_Again!_Aditi.mp3";
			document.getElementById("playSound").play();
			setTimeout(function() {
			document.getElementById('questions').innerHTML="";
			MultiplicationChart.createGrid();
			MultiplicationChart.restart();
			}, 2000);
		}
		cnt++;
	}
}
MultiplicationChart.restart=function()
{
	document.getElementById('questions').innerHTML="";
	firstClickFlag=true;
	MultiplicationChart.createGrid();
}
MultiplicationChart.next=function()
{
	document.getElementById("nextbtnid").classList.remove('blink_me');
	firstClickFlag=true;
	if(QueCounter<5){
	var flag=true,correctcounter=0,checkFlag=false;
	document.getElementById('questions').innerHTML="";
	PerimeterNumber++;
	MultiplicationChart.selectMatrix();
	}else{
		MultiplicationChart.endPage();
	}
}
MultiplicationChart.endPage = function () {
    document.getElementById("MainDiv").style.display = "none";
    document.getElementById("endPage").style.display = "block";
	document.getElementById("gameOver").innerHTML = MultiplicationChart.hardcodedData.gameOver;
	document.getElementById('playBackground').pause();
 }
MultiplicationChart.createAcknowledgementDivs = function () {
	document.getElementById("credit").style.pointerEvents = "none";
	document.getElementById("acknowledgementDiv").innerHTML = "";
		document.getElementById("acknowledgementDiv").style.visibility = "visible";
    var i = 0, div;
    for (i = 0; i < Object.keys(MultiplicationChart.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(MultiplicationChart.AcknowledgementPageData)[i] + " : " + Object.values(MultiplicationChart.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";
	div.innerHTML = MultiplicationChart.hardcodedData['CreatedOn'];
    acknowledgementDiv.appendChild(div);
}
MultiplicationChart.showAcknowledgement = function () {
	MultiplicationChart.createAcknowledgementDivs();
}
MultiplicationChart.replayGame=function()
{
	flag=true,correctcounter=0,checkFlag=false,QueCounter=0,PerimeterNumber=0;
	document.getElementById('questions').innerHTML="";
		document.getElementById('Instruction').innerHTML="";
	document.getElementById("acknowledgementDiv").style.visibility = "hidden";
		document.getElementById("credit").style.pointerEvents = "auto";
	MultiplicationChart.showInstruction();
}
MultiplicationChart.callHome = function() {
    location.reload(true);
}