var MultiplicationChart = {}; // namespace name
var Utils = {};
Utils.path = '';
Utils.mobileDeviceFlag = false;
var selectdNumber = 1,
    topFlag = false,
    leftFlag = false,
    id, data = "",
    count = 1
var startTime = 0,
    resId = 0;
//var soundArray=["GJ_01","GJ_02","GJ_03","GJ_04","GJ_05","GJ_06","GJ_07","GJ_08","GJ_09","GJ_10","GJ_11","GJ_12","GJ_13","GJ_14","GJ_15","GJ_16","GJ_17","GJ_18","GJ_19","GJ_20"];
$(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.getElementById("homeid").style.visibility = "hidden";
        $("#playbtnid").prop("src", "images/Play.png");
        $("#checkbtn").prop("src", "images/Done.png");
        $("#reloadbtn").prop("src", "images/Undo.png");
        $("#homeid").prop("src", "images/Home.png");
        $("#logoid").prop("src", "images/logo.png");
        $("#soundbtnid").prop("src", "images/sound.png");
        $("#resetbtnid").prop("src", "images/Undo.png");
    }
    //create options
MultiplicationChart.selectMatrix = function() {
    document.body.style.backgroundImage = "url('images/BG.jpg')";
    document.getElementById("coverpage").style.display = "none";
    document.getElementById("MainDiv").style.display = "block";
    document.body.style.backgroundImage = "url('images/BG.jpg')";
    document.getElementById("homeid").style.visibility = "visible";
    document.getElementById("instruction").innerHTML = MultiplicationChart.hardcodedData.Instruction;
    if (count == 1)
        MultiplicationChart.playInstructions();
    count++;
    var counter = 1;
    var counter1 = 1;
    var counter2 = 4;
    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);
        if (j == 1)
            counter = 1;
        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 = "X";
                document.getElementById("div" + (j) + (i)).style.backgroundColor = "rgb(139, 216, 84)";
            }
            if (i == 0 && j > 0) {
                document.getElementById("div" + (j) + (i)).innerHTML = counter1;
                document.getElementById("div" + (j) + (i)).setAttribute("onclick", "MultiplicationChart.getData(this)");
                document.getElementById("div" + (j) + (i)).style.backgroundColor = "white";
                document.getElementById("div" + (j) + (i)).setAttribute("data", "Left");
            }
            if (i == 1 && j > 0) {
                document.getElementById("div" + (j) + (i)).innerHTML = counter1;
                document.getElementById("div" + (j) + (i)).style.backgroundColor = "rgb(230, 227, 182)";
                counter1++;
            }
            if (j == 0 && i > 0) {
                document.getElementById("div" + (j) + (i)).innerHTML = counter;
                document.getElementById("div" + (j) + (i)).setAttribute("onclick", "MultiplicationChart.getData(this)");
                document.getElementById("div" + (j) + (i)).style.backgroundColor = "white";
                counter++;
            }
            if (j == 1 && i > 0) {
                document.getElementById("div" + (j) + (i)).innerHTML = counter;
                document.getElementById("div" + (j) + (i)).style.backgroundColor = "rgb(230, 227, 182)";
                counter++;
            }
            if (j > 1 && i > 1) {
                document.getElementById("div" + (j) + (i)).innerHTML = counter2;
                document.getElementById("div" + (j) + (i)).style.backgroundColor = "rgb(230, 227, 182)";
                counter2 = counter2 + j;
            }
        }
        counter2 = 2 * (j + 1);
        counter = 1 + j + 1;
    }
    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 = "X";
    document.getElementById("div" + (4)).innerHTML = "=";
}
MultiplicationChart.playInstructions = function() {
        document.getElementById("playSound").pause();
        document.getElementById('playSound').src = Utils.path + "sounds/instruction/MultiplicationchartInstruction2.mp3";
        document.getElementById("playSound").play();
    }
    //get selected number
MultiplicationChart.getData = function(object) {
    if (topFlag == true && leftFlag == true) {
        selectdNumber = 1;
        topFlag = false, leftFlag = false, id = "";
        document.getElementById('questions').innerHTML = "";
        document.getElementById('numberpad').innerHTML = "";
        $("#resetbtnid").css("pointer-Events", "auto");
        MultiplicationChart.selectMatrix();
    }
    var data1 = document.getElementById(object.id).getAttribute("data");
    if (data == data1) {
        selectdNumber = 1;
        document.getElementById(id).style.backgroundColor = "white";
        id = "";
    }
    id = object.id;
    var no = object.id;
    no = no.match(/(\d+)/)[0]; // give digits from string
    no = no[0];
    no = parseInt(no); //convert string to digit 
    var number;
    number = document.getElementById(object.id).innerHTML;

    selectdNumber = parseInt(selectdNumber) * parseInt(number);
    document.getElementById(object.id).style.backgroundColor = "yellow";
    if (no == 0)
        topFlag = true;
    else if (no != 0)
        leftFlag = true;
    if (document.getElementById("div" + (1)).innerHTML == "" || data == data1) {
        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
        $("#playSound").prop("src", Utils.path + 'sounds/' + document.getElementById(object.id).innerHTML + '.mp3');
        document.getElementById("playSound").play();
        document.getElementById("div" + (1)).innerHTML = parseInt(number);
    } else {
        $("#resetbtnid").css("pointer-Events", "none");
        var timer = setTimeout(function() {
            $("#playSound").prop("src", Utils.path + 'sounds/instruction/MultiplicationchartInstruction1.mp3');
            document.getElementById("playSound").play();
        }, 5);
        document.getElementById("div" + (3)).innerHTML = parseInt(number);
        setTimeout(function() {
            $("#playSound").prop("src", Utils.path + 'sounds/' + document.getElementById(object.id).innerHTML + '.mp3');
            document.getElementById("playSound").play();
        }, 1500);
        setTimeout(function() {
            $("#playSound").prop("src", Utils.path + 'sounds/instruction/MultiplicationchartInstruction3.mp3');
            document.getElementById("playSound").play();
        }, 2500);
    }
    data = document.getElementById(object.id).getAttribute("data");
    var no = object.id;
    no = no.match(/(\d+)/)[0];
    if (no == 100)
        no = no[0] + no[1];
    else
        no = no[0];
    no = parseInt(no);

    if (topFlag == true && leftFlag == true) {
        for (var i = 1; i <= 10; i++) {
            var no1 = object.id;
            no1 = no1.match(/(\d+)/)[0];
            if (no1.length == 3)
                no1 = 10;
            else
                no1 = (no1 % 10);
            no1 = parseInt(no1);
            if (no == 0) {
                if (document.getElementById("div" + i + no1).innerHTML == selectdNumber) {
                    $("#questions").css("pointer-Events", "none");
                    $("#soundbtnid").css("pointer-Events", "none");
                    setTimeout(function() {
                        document.getElementById("div" + i + no1).style.backgroundColor = "rgb(139, 216, 84)";
                        $("#playSound").prop("src", Utils.path + 'sounds/' + document.getElementById("div" + i + no1).innerHTML + '.mp3');
                        document.getElementById("playSound").play();
                        document.getElementById("div" + (5)).innerHTML = selectdNumber;
                        console.log(resId, 0, 0, 0, 0, startTime, 0);
                        if (Utils.mobileDeviceFlag) {
                            Android.addScore(resId, 0, 0, 0, 0, startTime, 0);
                        }
                        $("#questions").css("pointer-Events", "auto");
                        $("#soundbtnid").css("pointer-Events", "auto");
                        $("#resetbtnid").css("pointer-Events", "auto");
                    }, 4000);
                    data = "";
                    break;
                }
            } else {
                if (document.getElementById("div" + no + i).innerHTML == selectdNumber) {
                    $("#questions").css("pointer-Events", "none");
                    $("#soundbtnid").css("pointer-Events", "none");
                    setTimeout(function() {
                        document.getElementById("div" + no + i).style.backgroundColor = "rgb(139, 216, 84)";
                        $("#playSound").prop("src", Utils.path + 'sounds/' + document.getElementById("div" + no + i).innerHTML + '.mp3');
                        document.getElementById("playSound").play();
                        document.getElementById("div" + (5)).innerHTML = selectdNumber;
                        console.log(resId, 0, 0, 0, 0, startTime, 0);
                        if (Utils.mobileDeviceFlag) {
                            Android.addScore(resId, 0, 0, 0, 0, startTime, 0);
                        }
                        $("#questions").css("pointer-Events", "auto");
                        $("#soundbtnid").css("pointer-Events", "auto");
                        $("#resetbtnid").css("pointer-Events", "auto");
                    }, 4000);
                    data = "";
                    break;
                }
            }
        }
    }
}
MultiplicationChart.reset = function() {
    selectdNumber = 1;
    topFlag = false, leftFlag = false, id = "", data = "";
    document.getElementById('questions').innerHTML = "";
    document.getElementById('numberpad').innerHTML = "";
    MultiplicationChart.selectMatrix();
}
MultiplicationChart.callHome = function() {
    location.reload(true);
}