var MultiplicationChart = {}; // namespace name
var Utils = {};
Utils.path = '';
Utils.mobileDeviceFlag = false;
var selectdNumber = 0,
    topFlag = false,
    leftFlag = false,
    id, data, count = 1;
	MultiplicationChart.soundFlag=false;
	var getduration=0;
var questionId = 0,
    startTime = 0,
    scoredMarks = 0,
    totalMarks = 0,
    resId = 0,
    label = "",
    level = 1;
	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() {
        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 = 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");
				document.getElementById("div" + (j) + (i)).setAttribute("sound",soundArray[cnt]);
				cnt++;
            }
            if (i == 1 && j > 0) {
                document.getElementById("div" + (j) + (i)).innerHTML = counter1;
                document.getElementById("div" + (j) + (i)).style.backgroundColor = "rgb(230, 227, 182)";
				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", "MultiplicationChart.getData(this)");
                document.getElementById("div" + (j) + (i)).style.backgroundColor = "white";
				document.getElementById("div" + (j) + (i)).setAttribute("sound",soundArray[counter-1]);
                counter++;
            }
            if (j >= 1 && 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--;
            }
            if (j > 1 && i >= 1) {
                document.getElementById("div" + (j) + (i)).innerHTML = counter2;
                document.getElementById("div" + (j) + (i)).style.backgroundColor = "rgb(230, 227, 182)";
				document.getElementById("div" + (j) + (i)).setAttribute("sound",soundArray[counter2-1]);
                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/Sentence_1.mp3";
        document.getElementById("playSound").play();
    }
function getDuration(src) {
	var duration = 0;
	if (MultiplicationChart.soundFlag) {
		$('#playSound').on("loadedmetadata", function () {
			MultiplicationChart.soundFlag = false;
			duration = $('#playSound').prop('duration');
			$('#playSound').unbind("loadedmetadata");
			getduration=duration;
				//AdditionChart.getData();
		});
		$('#playSound').prop('src', src);
			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;
    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 (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
		 MultiplicationChart.soundFlag = true;
		getDuration(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");
        var timer = setTimeout(function() {
			 MultiplicationChart.soundFlag = true;
            getDuration(Utils.path + 'sounds/instruction/Sentence_2.mp3');
           // document.getElementById("playSound").play();
        }, getduration*200);
        document.getElementById("div" + (3)).innerHTML = parseInt(number);
        setTimeout(function() {
			 MultiplicationChart.soundFlag = true;
		    getDuration(Utils.path + 'sounds/' + document.getElementById(object.id).getAttribute("sound")+ '.mp3');
           // document.getElementById("playSound").play();
        }, getduration*1000);
        setTimeout(function() {
			 MultiplicationChart.soundFlag = true;
            getDuration(Utils.path + 'sounds/instruction/jod_2.mp3');
            //document.getElementById("playSound").play();
        }, getduration*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).innerHTML == selectdNumber) {
                    $("#questions").css("pointer-Events", "none");
                    $("#soundbtnid").css("pointer-Events", "none");
                    $("#outerDiv1").css("pointer-Events", "none");
                    setTimeout(function() {
						MultiplicationChart.soundFlag = true;
                        document.getElementById("div" + i + no1).style.backgroundColor = "rgb(139, 216, 84)";
                        //$("#playSound").prop("src", Utils.path + 'sounds/' + document.getElementById("div" + i + no1).innerHTML + '.mp3');
						getDuration(Utils.path + 'sounds/' + document.getElementById("div" + i + no1).getAttribute("sound")+ '.mp3');
                       // document.getElementById("playSound").play();
                        document.getElementById("div" + (5)).innerHTML = selectdNumber;
                        console.log(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
                        if (Utils.mobileDeviceFlag) {
                            Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
                        }
						setTimeout(function() {
                        $("#questions").css("pointer-Events", "auto");
                        $("#soundbtnid").css("pointer-Events", "auto");
                        $("#resetbtnid").css("pointer-Events", "auto");
						}, getduration*1000);
                    }, getduration*2700);
                    data = "";
                    break;
                }
            } else {
                if (document.getElementById("div" + no + i).innerHTML == selectdNumber) {
                    $("#questions").css("pointer-Events", "none");
                    $("#soundbtnid").css("pointer-Events", "none");
                    $("#outerDiv1").css("pointer-Events", "none");
                    setTimeout(function() {
						MultiplicationChart.soundFlag = true;
                        document.getElementById("div" + no + i).style.backgroundColor = "rgb(139, 216, 84)";
                       // $("#playSound").prop("src", Utils.path + 'sounds/' + document.getElementById("div" + no + i).innerHTML + '.mp3');
						getDuration(Utils.path + 'sounds/' + document.getElementById("div" + no + i).getAttribute("sound")+ '.mp3');
                       // document.getElementById("playSound").play();
                        document.getElementById("div" + (5)).innerHTML = selectdNumber;
                        console.log(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
                        if (Utils.mobileDeviceFlag) {
                            Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime, label);
                        }
						setTimeout(function() {
                        $("#questions").css("pointer-Events", "auto");
                        $("#soundbtnid").css("pointer-Events", "auto");
                        $("#resetbtnid").css("pointer-Events", "auto");
						}, getduration*1000);
                    }, getduration*2700);
                    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);
}