var DivisionChart = {}; // namespace name
var Utils = {};
Utils.path = '';
Utils.mobileDeviceFlag = false;
var selectdNumber = 0,
    topFlag = false,
    leftFlag = false;
var startTime = 0,
    resId = 0;
$(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() {
        DivisionChart.setImages();
    }, 100);
}, 200);
DivisionChart.globalVariables = function() {
    DivisionChart.storeId;
    DivisionChart.queCounter = 0;
    DivisionChart.Numerator;
    DivisionChart.denomnatorCounter = 0;
    DivisionChart.denomnatorId;
    DivisionChart.Number = 11;
    DivisionChart.QueCounter = 1;
    DivisionChart.nonPrimeNumberArray = [];
    DivisionChart.factorsArray = [];
    DivisionChart.fieldId;
    DivisionChart.Answer;
    DivisionChart.currentText;
    DivisionChart.checkFlag = false;
    DivisionChart.randomNumber = 0;
    DivisionChart.Timer;
    DivisionChart.rightAnswerTimer;
}
DivisionChart.setImages = function() {
    document.body.style.backgroundImage = "url(" + Utils.path + DivisionChart.coverImage + ")";
    document.getElementById("homeid").style.visibility = "hidden";
    $("#playbtnid").prop("src", "images/Play.png");
    $("#homeid").prop("src", "images/Home.png");
    $("#logoid").prop("src", "images/logo.png");
    $("#soundbtnid").prop("src", "images/sound.png");
}
DivisionChart.createNumberRange = function() {
    DivisionChart.globalVariables();
    document.getElementById("endpage").style.display = "none";
    document.getElementById("range").style.visibility = "visible";
    document.getElementById("questions").innerHTML = "";
    document.getElementById("numberSystemDiv").innerHTML = "";
    document.getElementById("range").innerHTML = "";
    document.getElementById("soundbtnid").innerHTML = "";
    document.getElementById("nextbtnid").innerHTML = "";
    document.getElementById("acknowledgementDiv").innerHTML = "";
    document.getElementById("instruction").innerHTML = DivisionChart.instruction.Instruction;
    for (var j = 1; j <= 4; j++) {
        var range = document.createElement("div");
        range.setAttribute("id", "range" + (j + 1));
        range.setAttribute("class", " nopadding chooseNumber col-sm-2 col-lg-2 col-md-2 col-xs-2");
        document.getElementById('range').appendChild(range);
        document.getElementById("range" + (j + 1)).style.backgroundImage = "url(" + Utils.path + "images/" + j + ".png)";

        var text = document.createElement("div");
        text.setAttribute("id", "text" + (j + 1));
        text.setAttribute("class", " nopadding  chooseNumberText col-sm-12 col-lg-12 col-md-12 col-xs-12");
        document.getElementById("range" + (j + 1)).appendChild(text);
        document.getElementById("text" + (j + 1)).innerHTML = ((j * 10) + 1) + " - " + ((j * 10) + 10);
    }
    for (var j = 1; j <= 4; j++) {
        var range = document.createElement("div");
        range.setAttribute("id", "window" + (j + 1));
        range.setAttribute("class", " nopadding instruction col-sm-3 col-lg-3 col-md-3 col-xs-3");
        document.getElementById('soundbtnid').appendChild(range);
        if (j == 4) {
            document.getElementById("window" + (j + 1)).style.backgroundImage = "url('images/sound.png')";
            document.getElementById("window" + (j + 1)).setAttribute("onclick", "DivisionChart.playInstructions()");
        } else
            document.getElementById("window" + (j + 1)).style.backgroundImage = "url('images/window.png')";
    }
    for (var j = 1; j <= 4; j++) {
        var range1 = document.createElement("div");
        range1.setAttribute("id", "greenwindow" + (j + 1));
        range1.setAttribute("class", " nopadding window col-sm-3 col-lg-3 col-md-3 col-xs-3");
        document.getElementById('nextbtnid').appendChild(range1);
        if (j == 4) {
            document.getElementById("greenwindow" + (j + 1)).style.backgroundImage = "url('images/next.png')";
            document.getElementById("greenwindow" + (j + 1)).setAttribute("onclick", "DivisionChart.NextQuestion()");
        } else
            document.getElementById("greenwindow" + (j + 1)).style.backgroundImage = "url('images/window2.png')";
    }
    DivisionChart.selectMatrix();
    document.getElementById("instruction").innerHTML = DivisionChart.instruction.Instruction;
    DivisionChart.playInstructions();
}
DivisionChart.playInstructions = function() {
        document.getElementById("playSound").pause();
        document.getElementById('playSound').src = Utils.path + "sounds/instruction/" + DivisionChart.instruction.InstructionAudio;
        document.getElementById("playSound").play();
    }
    //create options
DivisionChart.selectMatrix = function() {
    document.getElementById("coverpage").style.display = "none";
    document.getElementById("MainDiv").style.display = "block";
    document.body.style.backgroundImage = "url('images/bg.png')";
    document.getElementById("instructionDiv").style.backgroundImage = "url('images/b 1.png')";
    document.getElementById("instruction").style.backgroundImage = "url('images/b1 text window.png')";
    document.getElementById("thirdBuilding").style.backgroundImage = "url('images/b 3.png')";
    document.getElementById("secondBuilding").style.backgroundImage = "url('images/b 2.png')";
    document.getElementById("homeid").style.visibility = "visible";
    for (j = 0; j < 11; 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 < 11; i++) {
            Div = document.createElement("div");
            Div.setAttribute("id", "div" + (j) + (i));
            Div.setAttribute("class", " nopadding quetions text-center col-sm-1 col-lg-1 col-md-1 col-xs-1");
            div.appendChild(Div);
            Div.style.height = "6.7vh";
            Div.style.width = "4vw";
            if (i == 0 && j == 0) {
                document.getElementById("div" + (j) + (i)).innerHTML = "÷";
                document.getElementById("div" + (j) + (i)).style.backgroundColor = "rgb(139, 216, 84)";
            }
            if (i == 0 && j > 0 || j == 0 && i > 0) {
                document.getElementById("div" + (j) + (i)).style.backgroundColor = "white";
            }
            if (i == 1 && j > 0 || j >= 1 && i > 0 || j > 1 && i >= 1) {
                document.getElementById("div" + (j) + (i)).style.backgroundColor = "rgb(230, 227, 182)";
            }
        }
    }
    var buttonCount = 1;
    DivisionChart.createNumberDivs(numberSystemDiv, buttonCount);
}
DivisionChart.createNumberDivs = function(parentDiv, buttonCount) {
    var i = 0,
        j = 0,
        row, span, newbutton;
    for (i = 1; i <= 4; i++) {
        row = document.createElement('div');
        row.setAttribute("id", "row" + i);
        row.setAttribute("class", "rowClass col-xs-11 col-sm-11 col-md-11 col-lg-11 nopadding");
        for (j = 1; j <= 3; j++) {
            if (buttonCount < 11) {
                newbutton = document.createElement('BUTTON');
                newbutton.setAttribute("id", "buttonValue" + buttonCount);
                newbutton.setAttribute("class", "calValuesClass noPadding col-sm-offset-1 col-xs-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-3 col-sm-3 col-md-3 col-lg-3 nopadding");
                if (buttonCount == 10)
                    $(newbutton).text(0);
                else
                    $(newbutton).text(buttonCount);
                newbutton.setAttribute("onclick", "DivisionChart.appendNumbertoText(this.id)");
            } else {
                if (buttonCount == 11) {
                    newbutton = document.createElement('BUTTON');
                    newbutton.setAttribute("id", "buttonValue" + buttonCount);
                    newbutton.setAttribute("onclick", "DivisionChart.clearText()");
                    newbutton.setAttribute("class", "noPadding glyphicon glyphicon-erase glyphiColor calButtonClass col-sm-offset-1 col-xs-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-3 col-sm-3 col-md-3 col-lg-3 nopadding");
                }
            }
            row.append(newbutton);
            parentDiv.append(row);
            buttonCount++;
        }
    }
    DivisionChart.getNumber();
};
DivisionChart.getNumber = function() {
        for (j = 0; j < 1; j++) {
            for (var i = 0; i < 11; i++) {
                if (j == 0 && i > 0) {
                    document.getElementById("div" + (j) + (i)).innerHTML = "";
                    document.getElementById("div" + (j) + (i)).style.backgroundColor = "white";
                    $("#div" + j + i).css("pointer-Events", "none");
                }
            }
        }
        DivisionChart.removeNumber();
        var number = DivisionChart.Number;
        if (DivisionChart.queCounter > 0) {
            document.getElementById(DivisionChart.storeId).style.backgroundColor = "white";
        }
        for (j = 0; j < 11; j++) {
            for (var i = 0; i < 11; i++) {
                if (i == 0 && j > 0) {
                    document.getElementById("div" + (j) + (i)).innerHTML = number;
                    var cnt = 0;
                    for (var k = 2; k < number; k++) {
                        if (number % k == 0)
                            cnt++;
                    }
                    if (cnt != 0)
                        DivisionChart.nonPrimeNumberArray.push("div" + (j) + (i));
                    number++;
                }
            }
        }
        shuffleArray(DivisionChart.nonPrimeNumberArray);
        DivisionChart.getData();
    }
    //suffle the array
function shuffleArray(array) {
    var i, j, temp;
    for (i = array.length - 1; i > 0; i--) {
        j = Math.floor(Math.random() * (i + 1));
        temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
    return array;
}
DivisionChart.getData = function() {
    DivisionChart.randomNumber++;
    for (j = 0; j < 1; j++) {
        for (var i = 0; i < 11; i++) {
            if (j == 0 && i > 0) {
                document.getElementById("div" + (j) + (i)).innerHTML = "";
                $("#div" + j + i).css("pointer-Events", "none");
            }
        }
    }
    DivisionChart.removeNumber();
    if (DivisionChart.queCounter > 0) {
        document.getElementById(DivisionChart.storeId).style.backgroundColor = "white";
    }
    if (DivisionChart.denomnatorCounter > 0) {
        document.getElementById(DivisionChart.denomnatorId).style.backgroundColor = "white";
    }
    DivisionChart.denomnatorCounter = 0;
    DivisionChart.queCounter++;
    DivisionChart.factorsArray = [];
    //var randomNumber=Math.floor(Math.random()*DivisionChart.nonPrimeNumberArray.length);
    document.getElementById(DivisionChart.nonPrimeNumberArray[DivisionChart.randomNumber]).style.backgroundColor = "yellow";
    DivisionChart.Numerator = document.getElementById(DivisionChart.nonPrimeNumberArray[DivisionChart.randomNumber]).innerHTML;
    DivisionChart.storeId = DivisionChart.nonPrimeNumberArray[DivisionChart.randomNumber];
    for (var k = 2; k <= DivisionChart.Numerator / 2; k++) {
        if (DivisionChart.Numerator % k == 0) {
            DivisionChart.factorsArray.push(k);
        }
    }
    for (j = 0; j < 1; j++) {
        for (var i = 0; i <= DivisionChart.factorsArray.length; i++) {
            if (j == 0 && i > 0) {
                document.getElementById("div" + (j) + (i)).innerHTML = DivisionChart.factorsArray[i - 1];
                $("#div" + j + i).css("pointer-Events", "auto");
            }
        }
    }
    DivisionChart.showAnswer();
}
DivisionChart.showAnswer = function() {
    if (DivisionChart.denomnatorCounter > 0) {
        document.getElementById(DivisionChart.denomnatorId).style.backgroundColor = "white";
        DivisionChart.denomnatorId = "";
    }
    DivisionChart.removeNumber();
    var randomNumber = Math.floor(Math.random() * (DivisionChart.factorsArray.length - 0) + 1);
    var denomnator = document.getElementById("div" + (0) + (randomNumber)).innerHTML;
    document.getElementById("div" + (0) + (randomNumber)).style.backgroundColor = "yellow";
    DivisionChart.denomnatorCounter++;
    DivisionChart.denomnatorId = "div" + (0) + (randomNumber);
    DivisionChart.Answer = DivisionChart.Numerator / denomnator;
    for (var i = 1; i <= 10; i++) {
        var no = DivisionChart.storeId;
        no = no.match(/(\d+)/)[0];
        if (no == 100)
            no = no[0] + no[1];
        else
            no = no[0];
        no = parseInt(no);
        var no1 = DivisionChart.denomnatorId;
        no1 = no1.match(/(\d+)/)[0];
        if (no1.length == 3)
            no1 = 10;
        else
            no1 = (no1 % 10);
        no1 = parseInt(no1);
        if (i == no1) {
            setTimeout(function() {
                DivisionChart.fieldId = "div" + no + no1;
                document.getElementById("div" + no + no1).style.backgroundColor = "rgb(132, 189, 247)";
                $("#div" + no + no1).css("border", "2px solid gray");
            }, 500);
        }
    }
}
DivisionChart.appendNumbertoText = function(obj) {
    document.getElementById("playSound").pause();
    if (DivisionChart.checkFlag == true) {
        $("#" + DivisionChart.fieldId).css("background-color", "rgb(132, 189, 247)");
        $("#" + DivisionChart.fieldId).text("");
    }
    DivisionChart.checkFlag = false;
    $("#" + DivisionChart.fieldId).css("color", "black");
    $("#" + DivisionChart.fieldId).css("background-color", "rgb(132, 189, 247)");
    num = Number($("#" + DivisionChart.fieldId).text());
    if (num.toString().length < 3)
        $("#" + DivisionChart.fieldId).append(document.getElementById(obj).innerHTML);
    clearTimeout(DivisionChart.Timer);
    DivisionChart.Timer = setTimeout(function() {
        DivisionChart.currentText = document.getElementById(DivisionChart.fieldId).innerHTML;
        if (DivisionChart.Answer == DivisionChart.currentText && DivisionChart.Answer.toString().length == DivisionChart.currentText.toString().length) {
            $("#" + DivisionChart.fieldId).css("background-color", "rgb(139, 216, 84)");
            document.getElementById('playSound').src = Utils.path + "sounds/instruction/Right_answer.mp3";
             document.getElementById("playSound").play();
            clearTimeout(DivisionChart.rightAnswerTimer);
            DivisionChart.rightAnswerTimer = setTimeout(function() {
                if (DivisionChart.queCounter < 2) {
                    $('#numberSystemDiv').css('pointer-events', 'auto');
                    DivisionChart.currentText = "";
                    DivisionChart.getNumber();
                } else {
                    DivisionChart.NextQuestion();
                }
            }, 1000);
        } else {
            $('#numberSystemDiv').css('pointer-events', 'auto');
            DivisionChart.checkFlag = true;
            $("#" + DivisionChart.fieldId).css("background-color", "red");
            document.getElementById('playSound').src = Utils.path + "sounds/instruction/wrong_ans.mp3";
             document.getElementById("playSound").play();
            DivisionChart.currentText = "";
        }
    }, 1000);
    setTimeout(function(){
        DivisionChart.clearText ();
    }, 1800);
}
DivisionChart.clearText = function() {
    $("#" + DivisionChart.fieldId).css("background-color", "rgb(132, 189, 247)");
    $("#" + DivisionChart.fieldId).text("");
};
DivisionChart.removeNumber = function() {
    for (j = 0; j < 11; j++) {
        for (var i = 0; i < 11; i++) {
            if (i == 1 && j > 0 || j >= 1 && i > 0 || j > 1 && i >= 1) {
                document.getElementById("div" + (j) + (i)).style.backgroundColor = "rgb(230, 227, 182)";
                $("#div" + (j) + (i)).css("border", "1px solid gray");
                document.getElementById("div" + (j) + (i)).innerHTML = "";
            }
        }
    }
}
DivisionChart.NextQuestion = function() {
    DivisionChart.removeNumber();
    document.getElementById("playSound").pause();
    DivisionChart.Number = DivisionChart.Number + 10;
    DivisionChart.QueCounter++;
    DivisionChart.queCounter = 0;
    DivisionChart.checkFlag = false;
    DivisionChart.randomNumber = 0;
    document.getElementById(DivisionChart.storeId).style.backgroundColor = "white";
    DivisionChart.nonPrimeNumberArray = [];
    $('#numberSystemDiv').css('pointer-events', 'auto');
    if (DivisionChart.QueCounter < 5)
        DivisionChart.getNumber();
    else
        DivisionChart.gameOver();
}
DivisionChart.callHome = function() {
        location.reload(true);
    }
    //credit 
DivisionChart.showCredits = function() {
        document.getElementById("acknowledgementDiv").style.display = "block";
    }
    //display game over page
DivisionChart.gameOver = function() {
    document.getElementById("coverpage").style.display = "none";
    document.getElementById("MainDiv").style.display = "none";
    document.getElementById("range").style.visibility = "hidden";
    document.getElementById("endpage").style.display = "block";
    document.getElementById("acknowledgementDiv").style.display = "none";
    document.getElementById("resetbtnid").src = Utils.path + "images/replay.png";
    document.getElementById("creditbtnid").src = Utils.path + "images/credits.png";
    document.getElementById("gameover").innerHTML = DivisionChart.hardcodedData.gameover;
    for (i = 0; i < Object.keys(DivisionChart.AcknowledgementPageData).length; i++) {
        div = document.createElement('div');
        div.className = "fontClass nopadding col-xs-12 col-sm-12 col-md-12 col-lg-12";
        div.innerHTML = Object.keys(DivisionChart.AcknowledgementPageData)[i] + " : " + Object.values(DivisionChart.AcknowledgementPageData)[i];
        acknowledgementDiv.appendChild(div);
    }
    div = document.createElement('div');
    div.className = "fontClass col-xs-12 col-sm-12 col-md-12 col-lg-12";
    div.innerHTML = DivisionChart.hardcodedData['createdOn'];
    acknowledgementDiv.appendChild(div);
}