var DivisionChart = {}; // namespace name
var Utils = {};
Utils.path = '';
Utils.mobileDeviceFlag = false;
var questionId = 0,startTime = 0,scoredMarks = 0, totalMarks = 0,resId = 0,label = "",level = 1;
DivisionChart.storeId;
DivisionChart.queCounter = 0;
DivisionChart.Numerator;
DivisionChart.denomnatorCounter = 0;
DivisionChart.denomnatorId;
DivisionChart.instructionTimer;
DivisionChart.denomnator;
DivisionChart.timer;
DivisionChart.barabartimer;
$(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.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() {
        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   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);
            document.getElementById("text" + (j + 1)).setAttribute("onclick", "DivisionChart.getNumber(this)");
            document.getElementById("text" + (j + 1)).setAttribute("class", " chooseNumberText");
            document.getElementById("text" + (j + 1)).setAttribute("getNumber", (j * 10) + 1);
        }
        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')";
            else
                document.getElementById("window" + (j + 1)).style.backgroundImage = "url('images/window.png')";
        }
        for (var j = 1; j <= 6; 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('thirdBuilding').appendChild(range1);
            document.getElementById("greenwindow" + (j + 1)).style.backgroundImage = "url('images/window2.png')";
        }
        DivisionChart.selectMatrix();
        document.getElementById("instruction").innerHTML = DivisionChart.hardcodedData.Instruction;
        DivisionChart.playInstructions();
    }
    //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("numberpad").style.backgroundImage = "url('images/b3 window.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)";
            }
        }
    }
    for (j = 0; j < 5; j++) {
        var div = document.createElement("div");
        div.setAttribute("id", "div" + (j + 1));
        div.setAttribute("class", " nopadding displaynumbers col-sm-1 col-lg-1 col-md-1 col-xs-1");
        document.getElementById('numberpad').appendChild(div);
    }
    document.getElementById("div" + (2)).innerHTML = "  ÷";
    document.getElementById("div" + (4)).innerHTML = "  =";
}
DivisionChart.playInstructions = function() {
    document.getElementById("playSound").pause();
    document.getElementById('playSound').src = Utils.path + "sounds/instruction/" + DivisionChart.hardcodedData.InstructionAudio;
    document.getElementById("playSound").play();
}
DivisionChart.getNumber = function(obj) {
    document.getElementById("playSound").pause();
    if (DivisionChart.instructionTimer) {
        clearTimeout(DivisionChart.instructionTimer);
        DivisionChart.instructionTimer = null;
    }
    if (DivisionChart.timer) {
        clearTimeout(DivisionChart.timer);
        DivisionChart.timer = null;
    }
    if (DivisionChart.barabartimer) {
        clearTimeout(DivisionChart.barabartimer);
        DivisionChart.barabartimer = null;
    }
    $('#questions').css('pointer-events', 'auto');
    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";
            }
        }
    }
    document.getElementById("div" + (1)).innerHTML = "";
    document.getElementById("div" + (3)).innerHTML = "";
    document.getElementById("div" + (5)).innerHTML = "";
    DivisionChart.removeNumber();
    if (DivisionChart.queCounter > 0) {
        document.getElementById(DivisionChart.storeId).style.backgroundColor = "white";
    }
    var number = document.getElementById(obj.id).getAttribute("getNumber");
    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;
                document.getElementById("div" + (j) + (i)).setAttribute("onclick", "DivisionChart.getData(this)");
                number++;
            }
        }
    }

}
DivisionChart.getData = function(obj) {
    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");
            }
        }
    }
    document.getElementById("div" + (1)).innerHTML = "";
    document.getElementById("div" + (3)).innerHTML = "";
    document.getElementById("div" + (5)).innerHTML = "";
    DivisionChart.removeNumber();
    if (DivisionChart.queCounter > 0) {
        document.getElementById(DivisionChart.storeId).style.backgroundColor = "white";
    }
    if (DivisionChart.denomnatorCounter > 0) {
        document.getElementById(DivisionChart.denomnatorId).style.backgroundColor = "white";
    }
    $("#playSound").prop("src", Utils.path + 'sounds/' + document.getElementById(obj.id).innerHTML + '.mp3');
    document.getElementById("playSound").play();
    document.getElementById(obj.id).style.backgroundColor = "yellow";
    DivisionChart.denomnatorCounter = 0;
    DivisionChart.queCounter++;
    DivisionChart.storeId = obj.id;
    var factorsArray = [];
    DivisionChart.Numerator = document.getElementById(obj.id).innerHTML;
    document.getElementById("div" + (1)).innerHTML = parseInt(DivisionChart.Numerator);
    for (var k = 1; k <= DivisionChart.Numerator; k++) {
        if (DivisionChart.Numerator % k == 0) {
            factorsArray.push(k);
        }
    }
    for (j = 0; j < 1; j++) {
        for (var i = 0; i <= factorsArray.length; i++) {
            if (j == 0 && i > 0) {
                document.getElementById("div" + (j) + (i)).innerHTML = factorsArray[i - 1];
                //$("#div"+j+i).css("pointer-Events","auto");
                document.getElementById("div" + (j) + (i)).setAttribute("onclick", "DivisionChart.showAnswer(this)");
            }
        }
    }
}
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)";
                document.getElementById("div" + (j) + (i)).innerHTML = "";
            }
        }
    }
}

function getDuration(src) {
    var duration = 0;
    $('#playSound').on("loadedmetadata", function() {
        duration = $('#playSound').prop('duration');
        $('#playSound').unbind("loadedmetadata");
        DivisionChart.playNextSound(duration);
    });
    $('#playSound').prop('src', src);
}
DivisionChart.playNextSound = function(duration) //function for instruction
    {
        document.getElementById('playSound').play();
        if (DivisionChart.instructionTimer) {
            clearTimeout(DivisionChart.instructionTimer);
            DivisionChart.instructionTimer = null;
        }
        DivisionChart.instructionTimer = setTimeout(function() {
            $("#playSound").prop("src", Utils.path + 'sounds/' + DivisionChart.denomnator + '.mp3');
            document.getElementById("playSound").play();
        }, duration * 1000);
    }
DivisionChart.showAnswer = function(obj) {
    $('#questions').css('pointer-events', 'none');
    var d = new Date(); //date object
    startTime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds(); //to get the current date
    DivisionChart.denomnator = document.getElementById(obj.id).innerHTML;
    document.getElementById("div" + (3)).innerHTML = parseInt(DivisionChart.denomnator);
    if (DivisionChart.denomnatorCounter > 0) {
        document.getElementById(DivisionChart.denomnatorId).style.backgroundColor = "white";
        DivisionChart.denomnatorId = "";
        document.getElementById("div" + (5)).innerHTML = "";
        $("#playSound").prop("src", Utils.path + 'sounds/' + DivisionChart.Numerator + '.mp3');
        document.getElementById("playSound").play();
    }
    DivisionChart.removeNumber();
    document.getElementById(obj.id).style.backgroundColor = "yellow";
    setTimeout(function() {
        getDuration(Utils.path + 'sounds/instruction/MultiplicationchartInstruction1.mp3');
    }, 800);
    DivisionChart.barabartimer = setTimeout(function() {
        $("#playSound").prop("src", Utils.path + 'sounds/instruction/MultiplicationchartInstruction3.mp3');
        document.getElementById("playSound").play();
    }, 2500);
    DivisionChart.denomnatorCounter++;
    DivisionChart.denomnatorId = obj.id;
    var Answer = DivisionChart.Numerator / DivisionChart.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) {
            DivisionChart.timer = setTimeout(function() {
                document.getElementById("div" + no + no1).style.backgroundColor = "rgb(139, 216, 84)";
                document.getElementById("div" + no + no1).innerHTML = Answer;
                document.getElementById("div" + (5)).innerHTML = parseInt(Answer);
                $("#playSound").prop("src", Utils.path + 'sounds/' + document.getElementById("div" + no + no1).innerHTML + '.mp3');
                document.getElementById("playSound").play();
                console.log(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
                if (Utils.mobileDeviceFlag) {
                    Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
                }
                $('#questions').css('pointer-events', 'auto');
            }, 3500);
        }
    }
}
DivisionChart.callHome = function() {
    location.reload(true);
}