//9 oct

var FindWord={};
var Utils={};
Utils.Path='';
Utils.mobileDeviceFlag=false;

FindWord.totalscore=0;
FindWord.indexArray=[];
FindWord.nextTimeout=0;
FindWord.questionArr=[];
FindWord.questionCnt=0;

$(document).ready(function()
{
	$(".se-pre-con").fadeOut("slow");
	if(navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i))
		Utils.mobileDeviceFlag=true;
	if(Utils.mobileDeviceFlag)
		Utils.Path=Android.getMediaPath("FindWord"); 
	setTimeout(function()
	{
		FindWord.setImages();
		if(FindWord.hardCodedData['languageFont']!=null)
			setFontFamilyForLang("fonts/"+FindWord.hardCodedData['languageFont']);		
	},200);
});

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");            
}

FindWord.setImages=function()
{
	document.body.style.backgroundImage = "url("+Utils.Path+FindWord.hardCodedData.cover_page+")";
	document.getElementById('home').src=Utils.Path+"images/Home.png";
	document.getElementById('logo').src=Utils.Path+"images/logo.png";
	document.getElementById('playImage').src=Utils.Path+"images/Play.png";
	document.getElementById('playSoundBtn').src=Utils.Path+"images/Sound.png";
	document.getElementById('nextQueBtn').src=Utils.Path+"images/Next.png";
}

// set suffix and sound
FindWord.playgame=function()
{
	FindWord.correct=0;
	FindWord.wrong=0;
	FindWord.round++;
	document.getElementById("home").style.visibility="visible";
	document.getElementById("coverPage").style.display="none";
	document.getElementById("gamePage").style.display="block";
	document.getElementById('endpage').style.display="none";
	getUniqueValArr(FindWord.wordsData,FindWord.questionArr,3);
	console.log(FindWord.questionArr);
	FindWord.createDiv();
	FindWord.setQuestionAndOption();
}	
//create 16 div
FindWord.createDiv=function()			
{
	var parentDiv,wordDiv,outerDiv,wordSpan;
	parentDiv=document.getElementById("grid");
	
	for(var j=1;j<=16;j++)
	{
		wordSpan=document.createElement('span');
		wordSpan.setAttribute("id","word"+j);
		wordSpan.setAttribute("class","col-xs-3 col-sm-3 col-md-3 col-lg-3 nopadding");
		wordSpan.setAttribute("onclick","FindWord.checkSuffix(this.id)");
		parentDiv.appendChild(wordSpan);
	}	
}
//set Question and other option
FindWord.setQuestionAndOption=function()
{
	var randomGridDiv=0,randomQuestion=0,num=0,indexOfSuffix,tempArr2=[],tempArr=[];
	if(FindWord.questionCnt==3)
		FindWord.questionCnt=0;
	console.log(FindWord.totalscore);
	if(FindWord.totalscore>=30)
	{
		document.getElementById("suffixDiv").style.display="none";
		shuffle(FindWord.questionArr);
		console.log(FindWord.questionArr);
	}
	if(FindWord.totalscore>=60 )
	{
		document.getElementById("gamePage").style.display="none";
		document.getElementById("endpage").style.display="block";
		document.getElementById('conceptDiv').innerHTML=FindWord.hardCodedData.ConceptBy;
		document.getElementById('ArtWorkDiv').innerHTML=FindWord.hardCodedData.ArtWork;
		document.getElementById('AudioRecordingDiv').innerHTML=FindWord.hardCodedData.AudioRecording;
		document.getElementById('developerName').innerHTML=FindWord.hardCodedData.DevelopedBy;
		document.getElementById('music').innerHTML=FindWord.hardCodedData.Music;
	}
	else
	{
		document.getElementById("suffixValue").innerHTML=FindWord.questionArr[FindWord.questionCnt];
		FindWord.suffixCount=Math.floor(Math.random() * 4) + 2;
		for(var i=0;i<FindWord.suffixCount;i++)
		{
			randomGridDiv=(Math.floor(Math.random()*16)+1);
			if(FindWord.indexArray.includes(randomGridDiv))
			{
				i--;
				continue;
			}
			else
			{
				FindWord.indexArray[i]=randomGridDiv;
				document.getElementById("word"+randomGridDiv).setAttribute('class',"nopadding col-sm-offset-1 col-xs-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-2 col-sm-2 col-md-2 col-lg-2  spanclass1 spanclass2 ");
				document.getElementById("word"+randomGridDiv).innerHTML=FindWord.questionArr[FindWord.questionCnt];
				document.getElementById("word"+randomGridDiv).setAttribute('index',randomGridDiv);
			}
		}
		for(var i=0;i<16-(FindWord.indexArray.length);i++)
		{
			num=(Math.floor(Math.random()*16)+1);
			indexOfSuffix=(Math.floor(Math.random() *(FindWord.wordsData.length-1)) + 0);
			if(FindWord.indexArray.includes(num) || tempArr2.includes(indexOfSuffix) || tempArr.includes(num) || FindWord.wordsData[indexOfSuffix]==FindWord.questionArr[FindWord.questionCnt])
			{
				i--;
				continue;
			}
			else
			{
				tempArr[i]=num;
				tempArr2[i]= indexOfSuffix;
				document.getElementById("word"+num).setAttribute('class',"nopadding col-sm-offset-1 col-xs-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-2 col-sm-2 col-md-2 col-lg-2 spanclass1 spanclass2");
				document.getElementById("word"+num).innerHTML=FindWord.wordsData[indexOfSuffix];
				document.getElementById("word"+num).setAttribute('index',num);		
			}			
		}
		$('.spanclass1').css('pointer-events','auto');
		FindWord.playSound();
	}
}

//for tts
function saySomething(whatToSay)
{
	var synth,speech;
	synth = window.speechSynthesis;
	if('speechSynthesis' in window)
	{
		speech = new SpeechSynthesisUtterance(whatToSay);
		speech.lang = 'en-US'; //en-US
		window.speechSynthesis.speak(speech);
	}
} 

//on sound click
FindWord.playSound=function()
{
	if(Utils.mobileDeviceFlag)
		Android.playTts(FindWord.questionArr[FindWord.questionCnt],'eng');
	else
		saySomething(FindWord.questionArr[FindWord.questionCnt]);	
}

//check suffix right or wrong and move jerry
FindWord.checkSuffix=function(myID)
{
	var getClickedAttribute;
	getClickedAttribute=document.getElementById(myID).getAttribute('index');
	$('#'+myID).css('pointer-events','none');
	//for correct suffix
	if(FindWord.indexArray.includes(Number(getClickedAttribute)))	
	{
		FindWord.correct++;
		document.getElementById(myID).style.backgroundColor="green";
	}
	//for wrong suffix
	else 
	{
		FindWord.wrong++;
		document.getElementById(myID).style.backgroundColor="red";
	}
	if( FindWord.correct==FindWord.suffixCount)
	{
		FindWord.answerwiseMessage('rightAns.gif','CorrectSound.mp3');
		FindWord.loadNextQue();
	}
	else if((FindWord.wrong+FindWord.correct)>=9)
	{
		FindWord.answerwiseMessage('wrong.png','wrong.mp3');
		FindWord.loadNextQue();
	}	
	FindWord.calculateResult();
}

FindWord.loadNextQue=function()
{
	if(FindWord.nextTimeout)
	{
		clearTimeout(FindWord.nextTimeout);
		FindWord.nextTimeout=null;
	}
	FindWord.nextTimeout=setTimeout(function()
	{
		FindWord.next();
	},3000);
}


FindWord.answerwiseMessage=function(imageName,soundName)
{
	$(".spanclass1").css({pointerEvents:'none'});
	document.getElementById('ansImgDiv').style.visibility="visible";
	document.getElementById('ansImg').src=Utils.Path+"images/"+imageName;
	document.getElementById('playSound').src=Utils.Path+"sounds/"+soundName;
	document.getElementById('playSound').play();
};
//results
FindWord.calculateResult=function()
{
	if(FindWord.correct==FindWord.suffixCount && FindWord.wrong==0)
		FindWord.totalscore+=10;
	
	else if(FindWord.correct==FindWord.suffixCount && FindWord.wrong!=0)
	{
		FindWord.totalscore+=5;
	}
}

FindWord.next=function()
{
	FindWord.questionCnt++;
	FindWord.correct=0;
	FindWord.wrong=0;
	FindWord.indexArray=[],FindWord.suffixCount=0;
	clearTimeout(FindWord.nextTimeout);
	document.getElementById("playSound").pause();
	document.getElementById('ansImgDiv').style.visibility="hidden";
	$(".spanclass2").css({backgroundColor:'#0e6573'});
	$('.spanclass1').css('pointer-events','auto');
	FindWord.setQuestionAndOption();
}
FindWord.callHome=function()
{
	document.getElementById("playSound").pause();
	location.reload();
}

