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 numberArray=["১","২","৩","৪","৫","৬","৭","৮","৯","১০","১১","১২","১৩","১৪","১৫","১৬","১৭","১৮","১৯","২০"];
	var soundArray=["1_As","2_As","3_As","4_As","5_As","6_As","7_As","8_As","9_As","10_As","11_As","12_As","13_As","14_As","15_As","16_As","17_As","18_As","19_As","20_As"];
$(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 = numberArray[counter-2];
                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]);
				document.getElementById("div" + (j) + (i)).setAttribute("setData",counter1);
                counter1++;
            }
            if (j == 0 && i > 0) {
                document.getElementById("div" + (j) + (i)).innerHTML = numberArray[counter-1];
                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]);
				document.getElementById("div" + (j) + (i)).setAttribute("setData",counter);
                counter++;
            }
            if (j > 0 && i > 0) {
                document.getElementById("div" + (j) + (i)).innerHTML = numberArray[counter-1];
                document.getElementById("div" + (j) + (i)).style.backgroundColor = "rgb(230, 227, 182)";
				document.getElementById("div" + (j) + (i)).setAttribute("sound",soundArray[counter-1]);
				document.getElementById("div" + (j) + (i)).setAttribute("setData",counter);
                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/instructions.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 =document.getElementById(object.id).getAttribute("setData");
    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 = document.getElementById(object.id).innerHTML;
    } else {
        $("#resetbtnid").css("pointer-Events", "none");
        setTimeout(function() {
            $("#playSound").prop("src", Utils.path + 'sounds/instruction/Aek_AS.mp3');
            document.getElementById("playSound").play();
        }, 5);
        document.getElementById("div" + (3)).innerHTML =document.getElementById(object.id).innerHTML;
        setTimeout(function() {
            $("#playSound").prop("src", Utils.path + 'sounds/' + document.getElementById(object.id).getAttribute("sound")+ '.mp3');
            document.getElementById("playSound").play();
        }, 800);
        setTimeout(function() {
            $("#playSound").prop("src", Utils.path + 'sounds/instruction/Do_AS.mp3');
            document.getElementById("playSound").play();
        }, 1700);
    }
    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).getAttribute("setData") == 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 = document.getElementById("div" + i + no1).innerHTML;
                        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");
                    }, 2500);
                    data = "";
                    break;
                }
            } else {
                if (document.getElementById("div" + no + i).getAttribute("setData") == 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 = document.getElementById("div" + no + i).innerHTML;
                        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");
                    }, 2500);
                    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);
}