var MujhePehchano={};
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('MujhePehchano');
	} */
	$(".se-pre-con").fadeOut("slow");
	setTimeout(function()
	{
        MujhePehchano.init();
        MujhePehchano.initVariables();
        MujhePehchano.setImgSource();
	},100);
},200);

MujhePehchano.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/MujhePehchano.png')";
}
MujhePehchano.initVariables=function()
{
    MujhePehchano.flag=true;
    MujhePehchano.QueArray=[];
    MujhePehchano.random=0;
}
MujhePehchano.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";
}
MujhePehchano.gamePageFun=function()
{
    document.body.style.backgroundImage=Utils.path+"url('images/BG.png')";
    document.getElementById("home").style.display="block";
    document.getElementById("playbutton").style.display="none";
    document.getElementById("nextButton").style.display="block";
    document.getElementById("sound").style.display="block";
    MujhePehchano.showQue();
}
MujhePehchano.showQue=function()
{
    document.getElementById("outerDiv").innerHTML="";
    MujhePehchano.populateRandomQue();
    MujhePehchano.createDivs();
}
MujhePehchano.populateRandomQue=function()
{
    MujhePehchano.QueArray=[];
    if(MujhePehchano.flag==false)
        var n=9;
    else
        var n=4;
    for(var i=0;i<n;i++)
    {
        MujhePehchano.randomNo=Math.floor(Math.random()*MujhePehchano.wordsData.length); 
        while(MujhePehchano.QueArray.includes(MujhePehchano.wordsData[MujhePehchano.randomNo]))
        {
            MujhePehchano.randomNo=Math.floor(Math.random()*MujhePehchano.wordsData.length); 
        }
        MujhePehchano.QueArray.push(MujhePehchano.wordsData[MujhePehchano.randomNo]);
    }
}
MujhePehchano.createDivs=function()
{
    var outerDivision = document.createElement("div");
    outerDivision.setAttribute("id","queDiv");
    if(MujhePehchano.flag==false)
    {
    outerDivision.setAttribute("class","nopadding col-sm-7 col-lg-7 col-md-7 col-xs-7 col-sm-offset-5 col-lg-offset-5 col-md-offset-5 col-xs-offset-5");/* 3x3*/
    var n=9;
    }
    else
    {
        outerDivision.setAttribute("class","nopadding divHeight col-sm-6 col-lg-6 col-md-6 col-xs-6 col-sm-offset-6 col-lg-offset-6 col-md-offset-6 col-xs-offset-6");/*2x2 */
        var n=4;
    }
    document.getElementById('outerDiv').appendChild(outerDivision);
    for(var i=1;i<=n;i++)
		{
			var division = document.createElement("div");
            division.setAttribute("id","div"+i);
            if(MujhePehchano.flag==false)
                division.setAttribute("class","nopadding divStyle col-sm-4 col-lg-4 col-md-4 col-xs-4 ");
            else
                division.setAttribute("class","nopadding divStyle col-sm-6 col-lg-6 col-md-6 col-xs-6 ");
            document.getElementById('queDiv').appendChild(division);
            division.setAttribute("data",MujhePehchano.QueArray[i-1].sound);
            division.setAttribute("dataDivNumber",i);
            division.setAttribute("onclick","MujhePehchano.checkAns(this.id)");

            var imgDiv = document.createElement("img");
			imgDiv.setAttribute("id","img"+i);
            imgDiv.setAttribute("class","nopadding imgSize img-responsive col-lg-12 col-md-12 col-xs-12 col-sm-12 ");
            document.getElementById("div"+i).appendChild(imgDiv);
            document.getElementById("img"+i).src=Utils.path+'images/'+MujhePehchano.QueArray[i-1].img;
        }
    MujhePehchano.random=Math.floor(Math.random()*MujhePehchano.QueArray.length);
    document.getElementById("audio").src=Utils.path+'sounds/'+MujhePehchano.QueArray[ MujhePehchano.random].sound;
    document.getElementById("audio").play();
   
}
MujhePehchano.checkAns=function(obj)
{
    var ans=document.getElementById(obj).getAttribute("data");
    if(ans==MujhePehchano.QueArray[ MujhePehchano.random].sound)
    {
        var a=document.getElementById(obj).getAttribute("dataDivNumber");
      // var a=$('#'+obj).children().prop('id');
        document.getElementById("img"+a).src=Utils.path+"images/BG"+a+".jpeg";   
    }
    else
    {
        for(var i=0;i<$("#queDiv").children().length;i++)
        {
            var dataSound=document.getElementById("div"+(i+1)).getAttribute("data");
            if(MujhePehchano.QueArray[ MujhePehchano.random].sound==dataSound)
            document.getElementById("img"+(i+1)).src=Utils.path+"images/backWrong1.png";
        }
    }
    MujhePehchano.QueArray.splice( MujhePehchano.random,1);
    /*-------- Play Next Sound --------*/
    MujhePehchano.random=Math.floor(Math.random()*MujhePehchano.QueArray.length);
    document.getElementById("audio").src=Utils.path+'sounds/'+MujhePehchano.QueArray[ MujhePehchano.random].sound;
    document.getElementById("audio").play();
   // MujhePehchano.QueArray.splice( MujhePehchano.random,1);
}
MujhePehchano.nextQue=function()
{
    MujhePehchano.showQue();
}
MujhePehchano.playSound=function()
{
    document.getElementById("audio").play();
}