var Kahaniyaan = {};
var Utils = {};
Utils.Path = '';
Utils.mobileDeviceFlag = false;
var questionId = 0,
    startTime = 0,
    scoredMarks = 0,
    totalMarks = 10,
    resId = 0,
    startLesson = 6;
Kahaniyaan.nextPage = 0;
Kahaniyaan.storyReadCnt = 0;
Kahaniyaan.goBackFlag = null;
Kahaniyaan.playPauseFlag = false;
$(document).ready(function() {
    $(".se-pre-con").fadeOut("slow");
    $('title').text(Kahaniyaan.gameTitle);
    if ((navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) && !(window.location.href.includes('http')))
        Utils.mobileDeviceFlag = true;
    setTimeout(function() {
        if (screen.width < 450) {
            $('#imgDiv').removeClass('bgClass noPadding col-lg-4 col-md-4 col-xs-6 col-sm-6');
            $('#imgDiv').addClass('bgClass noPadding col-lg-4 col-md-4 col-xs-12 col-sm-12');
        } else {
            $('#imgDiv').removeClass('bgClass noPadding col-lg-4 col-md-4 col-xs-12 col-sm-12');
            $('#imgDiv').addClass('bgClass noPadding col-lg-4 col-md-4 col-xs-6 col-sm-6');
        }
        $("#coverpage").show();
        $("#showMenu,#storyPage,#endpage,#home,#levelPage").hide();
        Kahaniyaan.setImages();
        Kahaniyaan.init();
        Kahaniyaan.showFromUrl();
        Kahaniyaan.populateMenu(scrollBar);
    }, 300);
});
/* Get into full screen */
function GoInFullscreen(element) {
    if (element.requestFullscreen)
        element.requestFullscreen();
    else if (element.mozRequestFullScreen)
        element.mozRequestFullScreen();
    else if (element.webkitRequestFullscreen)
        element.webkitRequestFullscreen();
    else if (element.msRequestFullscreen)
        element.msRequestFullscreen();
    screen.orientation.lock("landscape-primary")
        .then(function() {})
        .catch(function(error) {});
}
$(window).on("orientationchange", function(event) {
    if (screen.width < 450) {
        $('#imgDiv').removeClass('bgClass noPadding col-lg-4 col-md-4 col-xs-6 col-sm-6');
        $('#imgDiv').addClass('bgClass noPadding col-lg-4 col-md-4 col-xs-12 col-sm-12');
    } else {
        $('#imgDiv').removeClass('bgClass noPadding col-lg-4 col-md-4 col-xs-12 col-sm-12');
        $('#imgDiv').addClass('bgClass noPadding col-lg-4 col-md-4 col-xs-6 col-sm-6');
    }
});

Kahaniyaan.init = function() {
    if (Kahaniyaan.hardcoded['languageFont'] != null)
        setFontFamilyForLang("fonts/" + Kahaniyaan.hardcoded['languageFont']);
}
setFontFamilyForLang = function(fontFilePath) {
    var newStyle = "<style>" +
        "@font-face {  " +
        "font-family: 'CustomFontFamily';" +
        "src: url('" + fontFilePath + "') format('truetype');" +
        "}  </style>";
    $("head").append(newStyle);
    $("body").css("font-family", "CustomFontFamily");
}
Kahaniyaan.showFromUrl = function() {
    //var url = 'file:///D:/Sample_Story/index.html/?standard=8',
    var url = $(location).attr('href');
    parts = url.split("="),
        last_part = Number(parts[parts.length - 1]);
    if ($.isNumeric(last_part))
        Kahaniyaan.lessonNum = last_part - startLesson;
    else
        Kahaniyaan.lessonNum = Math.floor(Math.random() * Kahaniyaan.storyData.length);

    document.body.style.backgroundImage = "url(" + Utils.Path + "images/skin/" + Kahaniyaan.coverImage.imgName + (Kahaniyaan.lessonNum + startLesson) + Kahaniyaan.coverImage.imgType + ")";
}
Kahaniyaan.setImages = function() {

    $('#home').prop("src", Utils.Path + "images/skin/Home.png");
    $('#logo').prop("src", Utils.Path + "images/skin/logo.png");
    $('#play').prop("src", Utils.Path + "images/skin/Play.png");
    $('#prev').prop("src", Utils.Path + "images/skin/Pre.png");
    $('#next').prop("src", Utils.Path + "images/skin/Next.png");
    $('#playAgain').prop("src", Utils.Path + "images/skin/playAgain.png");
    $('#googleImg').prop("src", Utils.Path + "images/skin/Google.png");
    $('#sarvaMangalImg').prop("src", Utils.Path + "images/skin/sarva-mangal.png");
    $('#stopImg').prop("src", Utils.Path + "images/skin/stop.png");
    $('#viewAllImg').prop("src", Utils.Path + "images/skin/view-all.png");
    $('.bluePatch').css("background-image", "url(" + Utils.Path + "images/skin/title.png)");
    $('#imgPart').css("background-image", "url(" + Utils.Path + "images/skin/tape.png)");
    $('#imgDiv').css("background-image", "url(" + Utils.Path + "images/skin/image_space.png)");
    $('#textPart').css("background-image", "url(" + Utils.Path + "images/skin/text_space.png)");
}
Kahaniyaan.populateMenu = function(parentDiv) { //creates story menu
    var mainDiv, outerDiv, cassetteDiv, newDiv, newImage, para, imgCnt = 0;
    for (var i = 0; i < Math.ceil(Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList.length / 3); i++) {
        mainDiv = document.createElement("div");
        mainDiv.setAttribute("id", "mainDiv" + i);
        mainDiv.setAttribute("class", "mainDivClass col-lg-12 col-md-12 col-xs-12 col-sm-12 noPadding");
        parentDiv.appendChild(mainDiv);
        for (var j = 0; j < 3; j++) {
            if (imgCnt < Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList.length) {
                outerDiv = document.createElement("div");
                outerDiv.setAttribute("id", "outerDiv" + imgCnt);
                outerDiv.setAttribute("class", "bgClass divClass col-lg-3 col-md-3 col-xs-3 col-sm-3 col-sm-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-offset-1 noPadding");
                $(outerDiv).css("position", "relative");
                mainDiv.appendChild(outerDiv);

                newDiv = document.createElement("div");
                newDiv.setAttribute("id", "div" + imgCnt);
                newDiv.setAttribute("class", "bgClass divClass col-lg-9 col-md-9 col-xs-9 col-sm-9 noPadding");
                newDiv.setAttribute("onclick", "Kahaniyaan.showlevel(this)");
                newDiv.style.backgroundImage = "url(" + Utils.Path + "images/skin/cassette_cover.png)";
                outerDiv.appendChild(newDiv);

                newImage = document.createElement("img");
                newImage.setAttribute("id", "img" + imgCnt);
                newImage.setAttribute("class", "img-responsive borderClass col-lg-12 col-md-12 col-xs-12 col-sm-12");
                newImage.setAttribute("src", Utils.Path + "images/class_" + (Kahaniyaan.lessonNum + startLesson) + "/" + Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[imgCnt].icon);
                newDiv.appendChild(newImage);

                para = document.createElement("div");
                para.setAttribute("id", "para" + imgCnt);
                para.setAttribute("class", "menuNames widthCls text-center col-lg-12 col-md-12 col-xs-12 col-sm-12");
                para.innerHTML = Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[imgCnt].languageTitle;
                newDiv.appendChild(para);
                $("#div" + imgCnt).data("storyNo", imgCnt);

                cassetteDiv = document.createElement("div");
                cassetteDiv.setAttribute("id", "cassetteDiv" + imgCnt);
                cassetteDiv.setAttribute("class", "bgClass cassetteCls col-lg-3 col-md-3 col-xs-3 col-sm-3 col-sm-offset-9 col-lg-offset-9 col-md-offset-9 col-xs-offset-9 noPadding");
                var randomCassette = Math.floor(Math.random() * 4);
                $(cassetteDiv).css("background-image", "url(" + Utils.Path + "images/skin/halfcassett_" + randomCassette + ".png)");
                outerDiv.appendChild(cassetteDiv);
                if ($(para).text().length > 10)
                    $("#para" + imgCnt).addClass('marqueCls');
                else
                    $("#para" + imgCnt).removeClass('widthCls');
                imgCnt++;
            }
        }
    }
}

Kahaniyaan.showlevel = function(element) {
    Kahaniyaan.resetAll();
    Kahaniyaan.nextPage = 0;
    Kahaniyaan.currentlevel = '';
    Kahaniyaan.goBackFlag = 'storylevels';
    $('#home').prop("src", Utils.Path + "images/skin/Pre.png");
    $("#scrollBar").scrollTop(0);
    $("#txtDiv").scrollTop(0);
    document.body.style.backgroundImage = "url(" + Utils.Path + "images/skin/levelBg.png)";
    if (element != undefined)
        Kahaniyaan.currentStoryNumber = $(element).data("storyNo"); //gets story number
    $("#Stories").text(Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].languageTitle);
    $("#levelPage").show();
    $("#coverpage,#playPauseDiv,#storyPage,#endpage,#showMenu").hide();
    $("#Stories,.bluePatch").css('visibility', 'visible');
    $("#pageNo").css('visibility', 'hidden');
    Kahaniyaan.levelDivs();
}
Kahaniyaan.levelDivs = function() {
    var levelDiv, levelImg, rowCnt, cnt = 0;
    $('#levels').text('');
    rowCnt = Math.ceil(Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level.length / 2);
    for (var i = 0; i < rowCnt; i++) {
        row = document.createElement('div');
        $(row).prop("id", "row" + i);
        $(row).prop("class", "noPadding rowClass col-lg-11 col-md-11 col-xs-11 col-sm-11");
        $("#levels").append(row);
        for (var j = 0; j < 2; j++) {
            if (cnt < Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level.length) {
                levelDiv = document.createElement("div");
                levelDiv.setAttribute("id", "levelDiv" + cnt);
                levelDiv.setAttribute("class", "noPadding col-xs-5 col-sm-5 col-md-5 col-lg-5  col-sm-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-offset-1");
                levelDiv.setAttribute("onclick", "Kahaniyaan.openStory(this)");

                $(row).append(levelDiv);
                $("#levelDiv" + cnt).data("levelNo", cnt);
                levelImg = document.createElement("img");
                levelImg.setAttribute("id", "levelImg" + cnt);
                levelImg.setAttribute("class", "noPadding img-responsive borderClass col-lg-12 col-md-12 col-xs-12 col-sm-12");
                levelImg.setAttribute("src", Utils.Path + "images/skin/" + Kahaniyaan.levelImg.imgName + (cnt + 1) + Kahaniyaan.levelImg.imgType);
                levelDiv.appendChild(levelImg);
                cnt++;
            } else
                break;
        }
    }
    $("#levels").scrollTop(0);

}
Kahaniyaan.showStoryMenu = function() //shows story menu
    {
        Kahaniyaan.resetAll();
        if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i))
            GoInFullscreen($("#element").get(0));
        Kahaniyaan.nextPage = 0;
        Kahaniyaan.currentlevel = '';
        $('#home').prop("src", Utils.Path + "images/skin/Home.png");
        document.body.style.backgroundImage = "url(" + Utils.Path + "images/skin/levelBg.png)";
        $("#txtDiv").scrollTop(0);
        Kahaniyaan.goBackFlag = "storyMenu";
        $("#pageNo,#Stories,.bluePatch").css('visibility', 'hidden');
        $("#showMenu,#home").show();
        var index = 0;
        for (var i = 0; i < Math.ceil(Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList.length / 3); i++) {
            for (var j = 0; j < 3; j++) {
                if (index < Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList.length) {
                    $("#cassetteDiv" + index).css('height', Math.ceil($("#outerDiv" + index).height() / 2) + 20);
                    index++;
                }
            }
        }
        $("#coverpage,#playPauseDiv,#storyPage,#endpage,#levelPage").hide();
    }
Kahaniyaan.openStory = function(element) //opens perticular story
    {
        var d = new Date();
        startTime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
        document.body.style.backgroundImage = "url(" + Utils.Path + "images/skin/game_bg.png)";
        $("#pageNo,#Stories,.bluePatch").css('visibility', 'visible');
        if (Kahaniyaan.currentlevel === '')
            Kahaniyaan.currentlevel = $(element).data("levelNo");
        scoredMarks = 10;
        Kahaniyaan.storyReadCnt = 0;
        $(".marginClass").css("pointer-events", "none");
        $("#storyBookDiv").show();
        Kahaniyaan.goBackFlag = "storyOpened";
        $(".marginClass").css("pointer-events", "none");
        Kahaniyaan.goBackFlag = "storyOpened";
        $("#showMenu,#levelPage").hide();


        $("#pageNo").text(Kahaniyaan.nextPage + 1 + '/' + Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level[Kahaniyaan.currentlevel]['pageList'].length);
        $("#storyPage,#textPart").show();
        $(".marginClass").css("pointer-events", "auto");
        Kahaniyaan.nextStoryPage(element);
    }

Kahaniyaan.nextStoryPage = function(element) //display story pages , calls on onclick of pre & next button
    {
        var spanEle;
        Kahaniyaan.wordNo = 0;
        Kahaniyaan.playPauseFlag = true;
        //$('#musicIcon').hide();
        $("#next").css("visibility", "visible");
        clearTimeout(Kahaniyaan.stopHighlighting);
        clearTimeout(Kahaniyaan.playWord);
        document.getElementById("playPause").src = Utils.Path + "images/skin/playStory.png";
        document.getElementById("playSound").pause();
        clearTimeout(Kahaniyaan.playStoryPage);
        $("#txtDiv").text('');
        $("#playPauseDiv").show();
        if (element.id == "prev" && Kahaniyaan.nextPage > 0) //if prev button clicked
            Kahaniyaan.nextPage--;
        else if (element.id == "next" && Kahaniyaan.nextPage < Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level[Kahaniyaan.currentlevel].pageList.length) {
            Kahaniyaan.nextPage++; //if next button clicked
            Kahaniyaan.storyReadCnt++;
        }
        if (Kahaniyaan.nextPage < Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level[Kahaniyaan.currentlevel].pageList.length) //shows story page
        {
            for (var i = 0; i < Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level[Kahaniyaan.currentlevel].pageList[Kahaniyaan.nextPage].readList.length; i++) {
                spanEle = document.createElement("span");
                spanEle.setAttribute("id", "span" + i);
                spanEle.setAttribute("class", "spans");
                spanEle.setAttribute("onclick", "Kahaniyaan.playSelectedWord(this)");
                spanEle.innerHTML = " " + Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level[Kahaniyaan.currentlevel].pageList[Kahaniyaan.nextPage].readList[i].word;
                document.getElementById("txtDiv").appendChild(spanEle);
            }
            var pageImage = Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level[Kahaniyaan.currentlevel].pageList[Kahaniyaan.nextPage].pageImage;
            if (pageImage != '')
            {
                $("#imgDiv").show();
                $('#textPart').prop('class', 'bgClass noPadding col-lg-8 col-md-8 col-xs-12 col-sm-12');
                 if (pageImage.includes('StoriesCover'))
                pageImage = pageImage.replace('StoriesCover', Kahaniyaan.storyData[Kahaniyaan.lessonNum].folderName)
                else
                pageImage = Kahaniyaan.storyData[Kahaniyaan.lessonNum].folderName + '/'.concat(pageImage);
            }
            else
            {
                $("#imgDiv").hide();
                $('#textPart').prop('class', 'bgClass noPadding col-lg-12 col-md-12 col-xs-12 col-sm-12');

            }
            document.getElementById("storyImg").src = Utils.Path + "images/" + pageImage;
            var readPageAudio = Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level[Kahaniyaan.currentlevel].pageList[Kahaniyaan.nextPage].readPageAudio;
            if (readPageAudio.includes('StoriesAudio'))
                readPageAudio = readPageAudio.replace('StoriesAudio', Kahaniyaan.storyData[Kahaniyaan.lessonNum].folderName)
            else
            readPageAudio=Kahaniyaan.storyData[Kahaniyaan.lessonNum].folderName + '/'.concat(readPageAudio)
            document.getElementById("playSound").src = Utils.Path + "sounds/" + readPageAudio;
            document.getElementById('playSound').load();
            $("#pageNo").text(Kahaniyaan.nextPage + 1 + '/' + Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level[Kahaniyaan.currentlevel]['pageList'].length);
        }
        if ($("#txtDiv").children().length < 50)
            $("#txtDiv").css("font-size", "2.5vw");
        else
            $("#txtDiv").css("font-size", "2vw");

        if (Kahaniyaan.nextPage == Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level[Kahaniyaan.currentlevel].pageList.length) //story ends
        {
            Kahaniyaan.resetAll();
            $("#storyPage,#playPauseDiv").hide();
            $('#endpage').show();
            $("#pageNo,#Stories,.bluePatch").css('visibility', 'hidden');
            $("#gameOverDiv").text(Kahaniyaan.hardcoded.gameOver);
        }
        if (Kahaniyaan.storyReadCnt == 1) {
            console.log(resId, questionId, scoredMarks, totalMarks, 1, startTime);
            if (Utils.mobileDeviceFlag)
                Android.addScore(resId, questionId, scoredMarks, totalMarks, 1, startTime);
        }
    }

Kahaniyaan.playStory = function() //calculates total pages ,first page play time & highlights them
    {
        var totalPages;
        $('.spans').css({ 'background-color': 'transparent' });
        //$('#musicIcon').hide();
        document.getElementById("playPause").src = Utils.Path + "images/skin/pause.png";
        totalPages = Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level[Kahaniyaan.currentlevel].pageList[Kahaniyaan.nextPage].readList.length - 1;
        Kahaniyaan.currentTime = Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level[Kahaniyaan.currentlevel].pageList[Kahaniyaan.nextPage].readList[Kahaniyaan.wordNo].wordFrom;

        while (Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level[Kahaniyaan.currentlevel].pageList[Kahaniyaan.nextPage].readList[totalPages].word.trim() == '<br>')
            totalPages -= 1;

        Kahaniyaan.playTime = Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level[Kahaniyaan.currentlevel].pageList[Kahaniyaan.nextPage].readList[totalPages].wordTo - Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level[Kahaniyaan.currentlevel].pageList[Kahaniyaan.nextPage].readList[Kahaniyaan.wordNo].wordFrom;
        document.getElementById("playSound").currentTime = Kahaniyaan.currentTime;
        document.getElementById("playSound").play();
        var audio = document.getElementById('playSound');
        while (!audio.paused) {
            Kahaniyaan.highLight();
            break;
        }
        if (Kahaniyaan.playStoryPage) {
            clearTimeout(Kahaniyaan.playStoryPage);
            Kahaniyaan.playStoryPage = null;
        }
        Kahaniyaan.playStoryPage = setTimeout(function() {
            document.getElementById("playSound").pause();

        }, Kahaniyaan.playTime * 1000);
    }
Kahaniyaan.highLight = function() {
    var getDuration = Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level[Kahaniyaan.currentlevel].pageList[Kahaniyaan.nextPage].readList[Kahaniyaan.wordNo].wordDuration; //duration of word
    $("#span" + Kahaniyaan.wordNo).css({ 'color': 'yellow' });
    document.getElementById('txtDiv').scrollTop = document.getElementById('span' + Kahaniyaan.wordNo).offsetTop;
    if (Kahaniyaan.stopHighlighting) {
        clearTimeout(Kahaniyaan.stopHighlighting);
        Kahaniyaan.stopHighlighting = null;
    }
    Kahaniyaan.stopHighlighting = setTimeout(function() {
        if (Kahaniyaan.wordNo < Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level[Kahaniyaan.currentlevel].pageList[Kahaniyaan.nextPage].readList.length - 1) //plays one by one word
        {
            Kahaniyaan.wordNo++;
            $('.spans').css({ 'color': 'black' });
            Kahaniyaan.highLight();
        } else //stops highlighting
        {
            Kahaniyaan.playPauseFlag = true;
            document.getElementById("playPause").src = Utils.Path + "images/skin/playStory.png";
            Kahaniyaan.wordNo = 0;
            $('.spans').css({ 'color': 'black' });
        }
    }, getDuration * 1000);
}

Kahaniyaan.playPause = function() //change play pause image,stops highlighting accordingly
    {
        if (Kahaniyaan.playPauseFlag == false) {
            Kahaniyaan.playPauseFlag = true;
            clearTimeout(Kahaniyaan.stopHighlighting);
            document.getElementById("playPause").src = Utils.Path + "images/skin/playStory.png";
            document.getElementById("playSound").pause();
            Kahaniyaan.currentTime = Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level[Kahaniyaan.currentlevel].pageList[Kahaniyaan.nextPage].readList[Kahaniyaan.wordNo].wordDuration;
        } else {
            Kahaniyaan.playPauseFlag = false;
            clearTimeout(Kahaniyaan.stopHighlighting);
            Kahaniyaan.playStory();
        }
    }
Kahaniyaan.stopPlayBack = function() {
    Kahaniyaan.resetAll();
    Kahaniyaan.currentTime = Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level[Kahaniyaan.currentlevel].pageList[Kahaniyaan.nextPage].readList[Kahaniyaan.wordNo].wordFrom;
};
Kahaniyaan.displaySound = function(element) {
    Kahaniyaan.stopPlayBack();
    Kahaniyaan.selectedWord = Number(element.id.match(/(\d+)/)[0]);
    $('.spans').css({ 'background-color': 'transparent' });
    $(element).css('background-color', '#337ab7');
    //$('#musicIcon').css({'top':$(element).position().top-20,'left':$(element).offset().left+16});
    //$('#musicIcon').show();
}
Kahaniyaan.playSelectedWord = function(element) {
    Kahaniyaan.stopPlayBack();
    Kahaniyaan.selectedWord = Number(element.id.match(/(\d+)/)[0]);
    $('.spans').css({ 'background-color': 'transparent' });
    $(element).css('background-color', '#337ab7');
    var playTime = Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level[Kahaniyaan.currentlevel].pageList[Kahaniyaan.nextPage].readList[Kahaniyaan.selectedWord].wordTo - Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level[Kahaniyaan.currentlevel].pageList[Kahaniyaan.nextPage].readList[Kahaniyaan.selectedWord].wordFrom;
    var currentTime = Kahaniyaan.storyData[Kahaniyaan.lessonNum].storyList[Kahaniyaan.currentStoryNumber].level[Kahaniyaan.currentlevel].pageList[Kahaniyaan.nextPage].readList[Kahaniyaan.selectedWord].wordFrom;
    document.getElementById("playSound").currentTime = currentTime;
    document.getElementById("playSound").play();
    if (Kahaniyaan.playWord) {
        clearTimeout(Kahaniyaan.playWord);
        Kahaniyaan.playWord = null;
    }
    Kahaniyaan.playWord = setTimeout(function() {
        document.getElementById("playSound").pause();

    }, playTime * 1000);
}

Kahaniyaan.resetAll = function() {
    //$('#musicIcon').hide();
    document.getElementById("playSound").pause();
    clearTimeout(Kahaniyaan.stopHighlighting);
    clearTimeout(Kahaniyaan.playStoryPage);
    clearTimeout(Kahaniyaan.playWord);
    Kahaniyaan.playPauseFlag = true;
    document.getElementById("playPause").src = Utils.Path + "images/skin/playStory.png";
    $('.spans').css({ 'color': 'black' });
    Kahaniyaan.wordNo = 0;
}
Kahaniyaan.playAgain = function() {
    $('#endpage').hide();
    Kahaniyaan.nextPage = 0;
    Kahaniyaan.openStory(Kahaniyaan.currentlevel);
};
Kahaniyaan.callHome = function() {
    clearTimeout(Kahaniyaan.stopHighlighting);
    clearTimeout(Kahaniyaan.playStoryPage);
    clearTimeout(Kahaniyaan.playWord);
    document.getElementById("playSound").pause();
    if (Kahaniyaan.goBackFlag == "storyOpened")
        Kahaniyaan.showlevel();
    else if (Kahaniyaan.goBackFlag == "storylevels")
        Kahaniyaan.showStoryMenu();
    else if (Kahaniyaan.goBackFlag == "storyMenu")
        location.reload();
}