var AdditionChart = {}; // namespace name
var Utils = {};
Utils.path = '';
Utils.mobileDeviceFlag = false;
var selectdNumber = 0,
    topFlag = false,
    leftFlag = false,
    id, data, count = 1;
var startTime = 0,
    resId = 0;
var soundArray=["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","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() {
        AdditionChart.setImages();
    }, 100);
}, 200);
AdditionChart.setImages = function() {
        document.body.style.backgroundImage = "url(" + Utils.path + AdditionChart.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");
        $("#resetbtnid").prop("src", "images/Undo.png");
    }
    //create options
AdditionChart.selectMatrix = function() {
    document.body.style.backgroundImage = "url('images/BG.jpg')";
    document.getElementById("coverpage").style.display = "none";
    document.body.style.backgroundImage = "url('images/BG.jpg')";
    document.getElementById("MainDiv").style.display = "block";
    document.getElementById("homeid").style.visibility = "visible";
    document.getElementById("instruction").style.visibility = "visible";
    document.getElementById("instruction").innerHTML = AdditionChart.hardcodedData.Instruction;
    if (count == 1)
        AdditionChart.playInstructions();
    count++;
    var counter = 1;
    var counter1 = 1;
    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) {
                document.getElementById("div" + (j) + (i)).innerHTML = counter1;
                document.getElementById("div" + (j) + (i)).setAttribute("onclick", "AdditionChart.getData(this)");
                document.getElementById("div" + (j) + (i)).style.backgroundColor = "white";
                document.getElementById("div" + (j) + (i)).setAttribute("data", "Left");
				document.getElementById("div" + (j) + (i)).setAttribute("sound",soundArray[counter1-1]);
                counter1++;
            }
            if (j == 0 && i > 0) {
                document.getElementById("div" + (j) + (i)).innerHTML = counter;
                document.getElementById("div" + (j) + (i)).setAttribute("onclick", "AdditionChart.getData(this)");
                document.getElementById("div" + (j) + (i)).setAttribute("data", "Top");
                document.getElementById("div" + (j) + (i)).style.backgroundColor = "white";
				document.getElementById("div" + (j) + (i)).setAttribute("sound",soundArray[counter-1]);
                counter++;
            }
            if (j > 0 && i > 0) {
                document.getElementById("div" + (j) + (i)).innerHTML = counter;
                document.getElementById("div" + (j) + (i)).style.backgroundColor = "rgb(230, 227, 182)";
				document.getElementById("div" + (j) + (i)).setAttribute("sound",soundArray[counter-1]);
                counter++;
            }
        }
        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 = "+";
    document.getElementById("div" + (4)).innerHTML = "=";
}
AdditionChart.playInstructions = function() {
        document.getElementById("playSound").pause();
        document.getElementById('playSound').src = Utils.path + "sounds/instruction/km_Jod chart-3_1080p.mp3";
        document.getElementById("playSound").play();
    }
    //get selected number
AdditionChart.getData = function(object) {
    if (topFlag == true && leftFlag == true) {
        selectdNumber = 0;
        topFlag = false, leftFlag = false, id = "";
        document.getElementById('questions').innerHTML = "";
        document.getElementById('numberpad').innerHTML = "";
        AdditionChart.selectMatrix();
    }
    var data1 = document.getElementById(object.id).getAttribute("data");
    if (data == data1) {
        selectdNumber = 0;
        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).getAttribute("sound") + '.mp3');
        document.getElementById("playSound").play();
        document.getElementById("div" + (1)).innerHTML = parseInt(number);
    } else {
        $("#resetbtnid").css("pointer-Events", "none");
        setTimeout(function() {
            $("#playSound").prop("src", Utils.path + 'sounds/instruction/km_Jod chart-1_1080p.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).getAttribute("sound")+ '.mp3');
            document.getElementById("playSound").play();
        }, 2000);
        setTimeout(function() {
            $("#playSound").prop("src", Utils.path + 'sounds/instruction/km_Jod chart-2_1080p.mp3');
            document.getElementById("playSound").play();
        }, 3000);
    }
    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');
						$("#playSound").prop("src", Utils.path + 'sounds/' + document.getElementById("div" + i + no1).getAttribute("sound")+ '.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");
                    }, 4500);
                    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');
					    $("#playSound").prop("src", Utils.path + 'sounds/' + document.getElementById("div" + no + i).getAttribute("sound")+ '.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");
                    }, 4500);
                    data = "";
                    break;
                }
            }
        }
    }
}
AdditionChart.reset = function() {
    selectdNumber = 0;
    topFlag = false, leftFlag = false, id = "", data = "";
    document.getElementById('questions').innerHTML = "";
    document.getElementById('numberpad').innerHTML = "";
    AdditionChart.selectMatrix();
}
AdditionChart.callHome = function() {
    location.reload(true);
}