var Aksharkhadi = {};
Utils={};

Utils.Path = " ";
Aksharkhadi.colors=['#ff9933','#006600','#ffcccc','#ff9999','#ff66ff','#ffff00','#33ccff','#99ff33','#40bf80']
Aksharkhadi.Option=[];
Aksharkhadi.numbers=[];
var Chances=1;
Aksharkhadi.Clicked_Chances=0;
Aksharkhadi.Sound_played="";
var rightAnswer=0;
var next=0;
var x;
$(document).ready(function () {
    $(".se-pre-con").fadeOut("slow");
    setTimeout(Aksharkhadi.init, 200);
});

Aksharkhadi.init= function() {
    $('body').css('background-image', "url("+Aksharkhadi.hardCodeddata.coverPage+")");
}

Aksharkhadi.Play= function(){   
    $('body').css('background-image', "url("+Aksharkhadi.hardCodeddata.BG+")");
    $('#playButton').css('display','none');
    $('#home').css('display','block');
    $('#replay').css('display','none');
    $('#skip').css('display','block');
    Aksharkhadi.createDiv();
    
}
Aksharkhadi.createDiv= function(){
    shuffle(Aksharkhadi.colors);
    Aksharkhadi.numbers=[];
    Aksharkhadi.Option=[];
    while(Aksharkhadi.numbers.length<9){
        var num=Math.floor(Math.random()*Aksharkhadi.AksharKhadi_wordsData.length);
        if((Aksharkhadi.numbers).includes(num)!=true){
            if(Aksharkhadi.Option.length<6){
                Aksharkhadi.Option.push(num)
            }
            Aksharkhadi.numbers.push(num)
        }

    }
    console.log(Aksharkhadi.numbers);
    shuffle(Aksharkhadi.Option);
    shuffle(Aksharkhadi.numbers);
    for(var i=0;i<9;i++){
        var childDiv=document.createElement("div");
        childDiv.setAttribute("class","nopadding childDiv col-sm-4 col-xs-4 col-md-4 col-lg-4");
        childDiv.setAttribute("id",("child")+i);
        childDiv.setAttribute("onclick","Aksharkhadi.setSound("+i+")" );
        var img=document.createElement("img");
        img.setAttribute("id",("Imgchild")+i);
        img.setAttribute("class","nopadding AnswerImage");
        img.setAttribute("src","");
        childDiv.append(img);
        
        var Text=document.createElement("p");
        Text.setAttribute("id",("Wordchild")+i)
        Text.innerText=Aksharkhadi.AksharKhadi_wordsData[Aksharkhadi.numbers[i]].word;
        childDiv.append(Text);
        childDiv.style.backgroundColor=Aksharkhadi.colors[i];
        document.getElementById("gamePage").appendChild(childDiv);

    }
    
}
Aksharkhadi.reset= function(){
    $(".se-pre-con").fadeOut("slow");
    document.getElementById("PlayAudio").pause();
    clearTimeout(x);
    Chances=1;
    rightAnswer=0;
    next=0;  
    $(".childDiv").css('pointer-events', 'auto');
    $('#replay').css('display','none');
    $('#sound').css('display','none');
    $('#next').css('display','none');
    $('#skip').css('display','none');
    $('#home').css('display','none');
    $('#logo').css('display','block');
    document.getElementById("gamePage").innerHTML="";
    $('#playButton').css('display','block');
    $('body').css('background-image', "url("+Aksharkhadi.hardCodeddata.coverPage+")");
}
Aksharkhadi.remove=function(){
    for(i=0;i<9;i++){
        document.getElementById("child"+i).classList.remove("img");
    }
}
Aksharkhadi.next= function(){
    document.getElementById("PlayAudio").pause();
    Aksharkhadi.remove();
    Aksharkhadi.Clicked_Chances=0;   
    if(Chances<5){
        x=setTimeout(Aksharkhadi.Playsound, 100);
        $(".childDiv").css('pointer-events', 'auto');
        Chances++;
    }
    else{
        
        if(next<5)
        $(".childDiv").css('pointer-events', 'none');
        document.getElementById("next").classList.add("moveup");
        Chances++;
    }

   
   
}
Aksharkhadi.Playsound= function(){
    document.getElementById("PlayAudio").src="sounds/"+Aksharkhadi.AksharKhadi_wordsData[Aksharkhadi.Option[Chances]].sound;
    Aksharkhadi.Sound_played=Aksharkhadi.AksharKhadi_wordsData[Aksharkhadi.Option[Chances]].word;
    document.getElementById("PlayAudio").play();
}

Aksharkhadi.CheckAsnwer= function(Clicked_Div){    
    txt= document.getElementById("Word"+Clicked_Div).innerText;
    $(".childDiv").css('pointer-events', 'none');
    if (Aksharkhadi.Clicked_Chances==0){
        if(txt==Aksharkhadi.Sound_played){
            document.getElementById(Clicked_Div).style.pointerEvents = 'none'; 
            document.getElementById("PlayAudio").src="sounds/instructions/Right_answer__Aditi.mp3"
            document.getElementById("Word"+Clicked_Div).style.display="none";
            document.getElementById("Img"+Clicked_Div).src="Images/withFace2.jpg";
            document.getElementById("Img"+Clicked_Div).style.display="block";
            rightAnswer+=1;
        }
        else{
            
            document.getElementById(Clicked_Div).classList.add("img");
            Aksharkhadi.WrongAsnwer();
            document.getElementById("PlayAudio").src="sounds/instructions/wrong_answer!_Aditi.mp3"   
        }
        
        Aksharkhadi.Clicked_Chances=1;
        document.getElementById("PlayAudio").play();
        x=setTimeout(Aksharkhadi.next, 3000);
}

}
Aksharkhadi.WrongAsnwer= function(){
    
    for(i=0;i<9;i++){
        txt=document.getElementById("Wordchild"+i).innerText;
        if(txt==Aksharkhadi.Sound_played){
            document.getElementById("child"+i).style.pointerEvents = 'none';
            document.getElementById("Wordchild"+i).style.display="none";
            document.getElementById("Imgchild"+i).src="Images/Wrong_01.png";
            document.getElementById("Imgchild"+i).style.display="block";
        }
    }

}
Aksharkhadi.setSound= function(no){
    document.getElementById("PlayAudio").src="sounds/"+Aksharkhadi.AksharKhadi_wordsData[Aksharkhadi.numbers[no]].sound;
    document.getElementById("PlayAudio").play();
}
Aksharkhadi.skip= function(no){
    document.getElementById("PlayAudio").pause();
    clearTimeout(x);
    document.getElementById("next").classList.remove("moveup");
    $(".childDiv").css('pointer-events', 'auto');
    $('#skip').css('display','none');
    $('#sound').css('display','block');
    $('#next').css('display','block');  
    for(i=0;i<9;i++){
        childDiv="child"+i;
        document.getElementById(childDiv).setAttribute("onclick","Aksharkhadi.CheckAsnwer(this.id)" );
    }
   x=setTimeout(Aksharkhadi.Playsound, 800);
}
Aksharkhadi.newquestion= function(){
    document.getElementById("PlayAudio").pause();
    clearTimeout(x);
    document.getElementById("next").classList.remove("moveup");
    
    if (next<5){
    Chances=1;
    rightAnswer=0;
    Aksharkhadi.Clicked_Chances=0;
    document.getElementById("gamePage").innerHTML="";
    document.getElementById("PlayAudio").pause();
    $('#sound').css('display','none');
    $('#next').css('display','none');
    Aksharkhadi.Play();
    }
    else if(next==5){
        $('#sound').css('display','none');
        $('#next').css('display','none');
        $('#replay').css('display','block');
        document.getElementById("gamePage").innerHTML="";
        var Text=document.createElement("p");
        Text.setAttribute("class","gameover")
        Text.innerText=Aksharkhadi.hardCodeddata.gameovertext;
        document.getElementById("gamePage").append(Text);
    }
    next++;
}
Aksharkhadi.rePlay= function(){
    document.getElementById("PlayAudio").pause();
    clearTimeout(x);
    next=0;
    Aksharkhadi.Play();
}