var MultiplicationChart = {}; // namespace name
var Utils = {};
Utils.path = '';
Utils.mobileDeviceFlag = false;
var selectdNumber = 0,
    topFlag = false,
    leftFlag = false,
    id, data, count = 1;
var questionId = 0,
    startTime = 0,
    scoredMarks = 0,
    totalMarks = 0,
    resId = 0,
    label = "",
    level = 1;
	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() {
        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 = 11;
    var counter2 = 11;
	var cnt=10;
    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 = 10;
        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[counter1-1];
                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");
				document.getElementById("div" + (j) + (i)).setAttribute("sound",soundArray[cnt]);
				document.getElementById("div" + (j) + (i)).setAttribute("setData",counter1);
				cnt++;
            }
            if (i == 1 && j > 0) {
                document.getElementById("div" + (j) + (i)).innerHTML = numberArray[counter1-1];
                document.getElementById("div" + (j) + (i)).style.backgroundColor = "rgb(230, 227, 182)";
				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", "MultiplicationChart.getData(this)");
                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 >= 1 && 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--;
            }
            if (j > 1 && i >= 1) {
                document.getElementById("div" + (j) + (i)).innerHTML = numberArray[counter2-1];
                document.getElementById("div" + (j) + (i)).style.backgroundColor = "rgb(230, 227, 182)";
				document.getElementById("div" + (j) + (i)).setAttribute("sound",soundArray[counter2-1]);
				document.getElementById("div" + (j) + (i)).setAttribute("setData",counter2);
                counter2--;
            }
        }
        counter2 = 10 + j;
        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 = "=";
    $("#outerDiv1").css("pointer-Events", "none");
}
MultiplicationChart.playInstructions = function() {
        document.getElementById("playSound").pause();
        document.getElementById('playSound').src = Utils.path + "sounds/instruction/instructionsg.mp3";
        document.getElementById("playSound").play();
    }
    //get selected number
MultiplicationChart.getData = function(object) {
    if (topFlag == true && leftFlag == true) {
        selectdNumber = 0;
        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 = 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 (leftFlag == true)
        $("#outerDiv1").css("pointer-Events", "auto");
    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');
		$("#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");
        var timer = setTimeout(function() {
            $("#playSound").prop("src", Utils.path + 'sounds/instruction/Do_AS.mp3');
            document.getElementById("playSound").play();
        }, 2);
        document.getElementById("div" + (3)).innerHTML = document.getElementById(object.id).innerHTML;
        setTimeout(function() {
           // $("#playSound").prop("src", Utils.path + 'sounds/' + document.getElementById(object.id).innerHTML + '.mp3');
		    $("#playSound").prop("src", Utils.path + 'sounds/' + document.getElementById(object.id).getAttribute("sound")+ '.mp3');
            document.getElementById("playSound").play();
        }, 1700);
        setTimeout(function() {
            $("#playSound").prop("src", Utils.path + 'sounds/instruction/Do_AS1.mp3');
            document.getElementById("playSound").play();
        }, 2700);
    }
    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");
                    $("#outerDiv1").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, questionId, scoredMarks, totalMarks, level, startTime, label);
                        if (Utils.mobileDeviceFlag) {
                            Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
                        }
                        $("#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).getAttribute("setData") == selectdNumber) {
                    $("#questions").css("pointer-Events", "none");
                    $("#soundbtnid").css("pointer-Events", "none");
                    $("#outerDiv1").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, questionId, scoredMarks, totalMarks, level, startTime, label);
                        if (Utils.mobileDeviceFlag) {
                            Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
                        }
                        $("#questions").css("pointer-Events", "auto");
                        $("#soundbtnid").css("pointer-Events", "auto");
                        $("#resetbtnid").css("pointer-Events", "auto");
                    }, 4500);
                    data = "";
                    break;
                }
            }
        }
    }
}
MultiplicationChart.reset = function() {
    selectdNumber = 0;
    topFlag = false, leftFlag = false, id = "", data = "";
    document.getElementById('questions').innerHTML = "";
    document.getElementById('numberpad').innerHTML = "";
    MultiplicationChart.selectMatrix();
}
MultiplicationChart.callHome = function() {
    location.reload(true);
}