var AwazPehchano={};
var Utils={};
Utils.path="";

var questionId=0,startTime=0,scoredMarks=0,totalMarks=0,resId=0,mobileDeviceFlag=false,label,scoreFlag=false;

$(document).ready(function()
{
	if(navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i))
	Utils.mobileDeviceFlag=true;
	/* if(Utils.mobileDeviceFlag)
	{
		Utils.Path=Android.getMediaPath('AwazPehchano');
	} */
	$(".se-pre-con").fadeOut("slow");
	setTimeout(function()
	{
        AwazPehchano.init();
        AwazPehchano.initVariables();
        AwazPehchano.setImgSource();
	},100);
},200);

AwazPehchano.init=function()
{
    document.getElementById("home").style.display="none";
    document.getElementById("nextButton").style.display="none";
    document.getElementById("sound").style.display="none";
    document.body.style.backgroundImage=Utils.path+"url('images/AwazPehchano.jpg')";
}
AwazPehchano.initVariables=function()
{
    AwazPehchano.flag=true;
    AwazPehchano.QueArray=[];
    AwazPehchano.random=0;
    var checkCount=0;
}
AwazPehchano.setImgSource=function()
{
    document.getElementById("home").src=Utils.path+"images/home.png";
    document.getElementById("logo").src=Utils.path+"images/logo.png";
    document.getElementById("playbutton").src=Utils.path+"images/Play.png";
    document.getElementById("nextButton").src=Utils.path+"images/Next.png";
	document.getElementById("sound").src=Utils.path+"images/Sound.png";
}
AwazPehchano.gamePageFun=function()
{
    document.body.style.backgroundImage=Utils.path+"url('images/BG.jpg')";
    document.getElementById("home").style.display="block";
    document.getElementById("playbutton").style.display="none";
    document.getElementById("nextButton").style.display="block";
    document.getElementById("sound").style.display="block";
    AwazPehchano.showQue();
}
AwazPehchano.showQue=function()
{   
    scoreFlag=false;
    scoredMarks=0,totalMarks=0;
    AwazPehchano.checkCount=0;
    d=new Date();
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();

    AwazPehchano.ansCount=0;
    document.getElementById("imageDiv").innerHTML="";
    AwazPehchano.populateRandomQue();
    img=new Image();
    img.onload=AwazPehchano.createDivs;
    img.src=Utils.path+"images/LastImg.jpg";
}
AwazPehchano.populateRandomQue=function()
{
    AwazPehchano.QueArray=[];
    if(AwazPehchano.flag==false)
        var n=9;
    else
        var n=4;
    for(var i=0;i<n;i++)
    {
        AwazPehchano.randomNo=Math.floor(Math.random()*AwazPehchano.wordsData.length); 
        while(AwazPehchano.QueArray.includes(AwazPehchano.wordsData[AwazPehchano.randomNo]))
        {
            AwazPehchano.randomNo=Math.floor(Math.random()*AwazPehchano.wordsData.length); 
        }
        AwazPehchano.QueArray.push(AwazPehchano.wordsData[AwazPehchano.randomNo]);
    }
}
AwazPehchano.createDivs=function()
{  
    if(AwazPehchano.flag==false)
    {
        var divideBy=3;
        totalMarks=3*3*10
    }
    else
    {
        var divideBy=2;
        totalMarks=2*2*10;
    }
    var imgX=0,imgY=0,imgHeight=img.naturalHeight/divideBy,imgWidth=img.naturalWidth/divideBy;
    k=0;
    for(var i=0;i<divideBy;i++)
    {
        var outerDivision = document.createElement("div");
        outerDivision.setAttribute("id","outerDiv"+(i));
        outerDivision.setAttribute("class"," nopadding  col-sm-12 col-lg-12 col-md-12 col-xs-12 nopadding");
        document.getElementById('imageDiv').appendChild(outerDivision);
        
        for(var j=0;j<divideBy;j++)
        {
            Div = document.createElement("div");
            Div.setAttribute("id","Div"+i+""+j);
            if(AwazPehchano.flag==false)
               {
                    Div.setAttribute("class","nopadding pointerClass Canvas col-sm-4 col-lg-4 col-md-4 col-xs-4");
                    $(".divAlign").css("margin-left","26vw");
                    $(".soundNext").css("margin-left","15.6vw");
                }
            else
                Div.setAttribute("class","nopadding pointerClass Canvas col-sm-6 col-lg-6 col-md-6 col-xs-6");
            Div.setAttribute("onclick",'AwazPehchano.checkAns(this.id)');
            Div.setAttribute("data",AwazPehchano.QueArray[k].sound);
            outerDivision.appendChild(Div);

            var imgDiv = document.createElement("img");
			imgDiv.setAttribute("id","img"+i+""+j);
            imgDiv.setAttribute("class","nopadding imgSize img-responsive col-lg-11 col-md-11 col-xs-11 col-sm-11 ");
            document.getElementById("Div"+i+""+j).appendChild(imgDiv);
            document.getElementById("img"+i+""+j).src=Utils.path+'images/'+AwazPehchano.QueArray[k].img;
            k++;

            this.dynCanvas = document.createElement("canvas");
            this.dynCanvas.setAttribute("id","canvas"+i+""+j);
            this.dynCanvas.setAttribute("class","positionClass nopadding Canvas");
            Div.appendChild(this.dynCanvas);

            var destwidth=this.dynCanvas.width;
            var destheight=this.dynCanvas.height;
            dynContext=this.dynCanvas.getContext("2d");
            dynContext.drawImage(img,imgX,imgY,imgWidth,imgHeight,0,0,destwidth,destheight);
           // console.log(imgX,imgY,imgHeight,imgWidth,0,0,destheight,destwidth);
            this.dynCanvas.style.visibility="none";
            imgX=imgX+imgWidth;          
        }
        imgX=0;
        imgY=imgY+imgHeight;
    }
    AwazPehchano.queSound();
}
AwazPehchano.queSound=function()				//score Function
{
    document.getElementById("audio").pause();
    AwazPehchano.random=Math.floor(Math.random()*AwazPehchano.QueArray.length);
    questionId=AwazPehchano.QueArray[AwazPehchano.random]['questionId'];
    document.getElementById("audio").src=Utils.path+'sounds/'+AwazPehchano.QueArray[ AwazPehchano.random].sound;
    document.getElementById("audio").play();

    if(AwazPehchano.QueSound)
	{
		clearTimeout(AwazPehchano.QueSound);
		AwazPehchano.QueSound=null;
	}
    AwazPehchano.QueSound=setTimeout(function()
	{
    $(".pointerClass").css("pointer-events","auto");
    },1200);
}
AwazPehchano.checkAns=function(obj)
{
    clearTimeout(AwazPehchano.QueSound);
    $(".pointerClass").css("pointer-events","none");
    AwazPehchano.checkCount++;
    scoreFlag=true;
    var ans=document.getElementById(obj).getAttribute("data");
    if(ans==AwazPehchano.QueArray[ AwazPehchano.random].sound)
    {
        var a=$('#'+obj).children().not('img').prop('id');
        document.getElementById(a).style.visibility = "visible";
        AwazPehchano.ansCount++;
        document.getElementById("audio").src=Utils.path+'sounds/CorrectSound.mp3';
        document.getElementById("audio").play();
        scoredMarks+=10;
        $('#'+obj).css({'pointer-events':'none'});
        $('#'+obj).removeClass('pointerClass');
    }
    else
    {
        k=0;
        for(var i=0;i<$("#imageDiv").children().length;i++)
        {
            for(var j=0;j<$("#imageDiv").children().length;j++)
            {
                var dataSound=document.getElementById("Div"+i+""+j).getAttribute("data");
                if(AwazPehchano.QueArray[ AwazPehchano.random].sound==dataSound)
                {
                    document.getElementById("img"+i+""+j).src=Utils.path+"images/backWrong1.png";
                    document.getElementById("audio").src=Utils.path+'sounds/WrongSound.mp3';
                    document.getElementById("audio").play();
                    $('#Div'+i+""+j).css({'pointer-events':'none'});
                    $('#Div'+i+""+j).removeClass('pointerClass');
                }
            }
        }
    }

    if(AwazPehchano.ListenNextSound)
	{
		clearTimeout(AwazPehchano.ListenNextSound);
		AwazPehchano.ListenNextSound=null;
	}
    AwazPehchano.ListenNextSound=setTimeout(function()
    {
        if(($("#imageDiv").children().length*$("#imageDiv").children().length )== AwazPehchano.checkCount)
        {
            if(($("#imageDiv").children().length*$("#imageDiv").children().length )== AwazPehchano.ansCount)
            {
                document.body.style.backgroundImage=Utils.path+"url('images/LastImg.jpg')";
                document.getElementById("audio").src=Utils.path+'sounds/SoundForCorrect.mp3';
                document.getElementById("audio").play();
                document.getElementById("imageDiv").style.visibility = "hidden";
                $(".Canvas").css("visibility", "hidden");
                document.getElementById("soundDiv").style.visibility = "hidden";
            }
            else
            {
                document.body.style.backgroundImage=Utils.path+"url('images/joker.png')";
                document.getElementById("audio").src=Utils.path+'sounds/soundForWrong.mp3';
                document.getElementById("audio").play();
                document.getElementById("imageDiv").style.visibility = "hidden";
                $(".Canvas").css("visibility", "hidden");
                document.getElementById("soundDiv").style.visibility = "hidden";
            }
        }   
        else
        {
        AwazPehchano.QueArray.splice( AwazPehchano.random,1);
        AwazPehchano.queSound()
        }
    },1500);
}
AwazPehchano.nextQue=function()
{
    clearTimeout(AwazPehchano.ListenNextSound);
    clearTimeout(AwazPehchano.showLastImg);
    document.getElementById("soundDiv").style.visibility = "visible";
    if(scoreFlag==true)
         AwazPehchano.scoreFunction();
    else 
    {
        scoredMarks=-2;
        AwazPehchano.scoreFunction();
    }
    document.getElementById("imageDiv").style.visibility = "visible";
    $(".Canvas").css("visibility", "visible");
    document.body.style.backgroundImage=Utils.path+"url('images/BG.jpg')";
    if(($("#imageDiv").children().length*$("#imageDiv").children().length)== AwazPehchano.ansCount)
    {
        AwazPehchano.flag=false;
    }
    AwazPehchano.showQue();
}
AwazPehchano.playSound=function()
{
    document.getElementById("audio").src=Utils.path+'sounds/'+AwazPehchano.QueArray[ AwazPehchano.random].sound;
    document.getElementById("audio").play();
}
AwazPehchano.returnback=function()			//return to previous page (reload)
{
    if(scoreFlag==true)
        AwazPehchano.scoreFunction();
    location.reload(true);
}
AwazPehchano.scoreFunction=function()				//score Function
{
	console.log(resId,questionId,scoredMarks,totalMarks,1,startTime);
	if(Utils.mobileDeviceFlag)
	{
		Android.addScore(resId,questionId,scoredMarks,totalMarks,1,startTime);
	}
}
