//3 sep
var MPOW={};
var Utils={};
Utils.Path='';
Utils.mobileDeviceFlag=false;
var resId=0,quesId=0,startTime=0,scoredMarks=0,totalMarks=10;

MPOW.skippedCnt=0;
MPOW.levelOfData=0;
MPOW.questionArr=[];
MPOW.attemptCnt=0;
MPOW.totalQue=0;
MPOW.skipFlag=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('MPOW');
		MPOW.levelOfData=Android.getLevel();
	}	
	else
	{
		MPOW.levelOfData=null;
		Utils.Path="";
	}		
	$(".se-pre-con").fadeOut("slow");
	setTimeout(function()
	{
		MPOW.setImages();
	},200);
});


MPOW.getAlldata=function()
{
	var randomLesson=0,randomIndex=0;
	while (MPOW.questionArr.length !== 9) 
	{
		randomLesson=Math.floor(Math.random()*MPOW.jsonData.length);
		randomIndex =Math.floor(Math.random()*MPOW.jsonData[randomLesson]['level'].length);
		if($.inArray(MPOW.jsonData[randomLesson]['level'][randomIndex],MPOW.questionArr) === -1)
			MPOW.questionArr.push(MPOW.jsonData[randomLesson]['level'][randomIndex]);
	}
};

MPOW.setImages=function()
{
	document.body.style.backgroundImage="url("+Utils.Path+MPOW.coverImage+")";
	document.getElementById("homeImg").src=Utils.Path+"images/Home.png";
	document.getElementById("logoImg").src=Utils.Path+"images/logo.png";
	document.getElementById("playImg").src=Utils.Path+"images/Play.png";
	document.getElementById("nextImage").src=Utils.Path+"images/skip.png";
	document.getElementById("sound").src=Utils.Path+"images/Sound.png";
}
MPOW.playGame=function()
{
	document.body.style.backgroundImage="url("+Utils.Path+'images/BG.png'+")";
	document.getElementById('coverpage').style.display="none";
	document.getElementById('gamepage').style.display="block";
	document.getElementById("homeImg").style.display = "block";
	MPOW.createDivs();
	MPOW.showObjects();
}
//creates No. of Divs
MPOW.createDivs=function()
{
	var showMeDiv,pictureDiv,img;
	
	for(var j=0;j<3;j++)
	{
		pictureDiv= document.createElement("div");
		pictureDiv.setAttribute("id","pictureDiv"+j); 
		pictureDiv.setAttribute("class","borderClass pointerClass imageDivs noPadding col-md-3 col-xs-3 col-lg-3 col-sm-3 col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1"); 
		pictureDiv.setAttribute("onclick","MPOW.checkAnswer(this)"); 
		showMePageDiv.appendChild(pictureDiv);

		img=document.createElement('img');
		img.setAttribute("id","img"+j); 
		img.setAttribute("class","noPadding img-responsive col-lg-12 col-md-12 col-xs-12 col-sm-12");
		pictureDiv.appendChild(img);
	}
	if(MPOW.levelOfData!=null)
		MPOW.questionArr=populate(MPOW.jsonData[MPOW.levelOfData].level,MPOW.questionArr,9);
	else
		MPOW.getAlldata();
	
}

//Show Objects on the screen
MPOW.showObjects=function()
{
	var d,imgCnt=0,splitImgArr=[],randomImg;
	tempQuestionArr=[];
	d=new Date();
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	splitImgArr=MPOW.questionArr[MPOW.totalQue]['image'].split(",");
	randomImg=Math.floor(Math.random()*(splitImgArr.length));
	MPOW.currentAnswImage=splitImgArr[randomImg];
	tempQuestionArr=MPOW.questionArr[MPOW.totalQue]['option'].split(",");
	tempQuestionArr.push(splitImgArr[randomImg]);
	shuffleArray(tempQuestionArr);
	console.log(tempQuestionArr);
	for(var j=0;j<tempQuestionArr.length;j++)
	{
		$("#img"+j).prop("src",Utils.Path+"images/"+tempQuestionArr[j].trim(''));
		$("#pictureDiv"+j).data("ans",tempQuestionArr[j].trim(''));
	}
	MPOW.playInstruction();
}
MPOW.playInstruction=function()
{
	document.getElementById('playSound').src=Utils.Path+'sounds/'+MPOW.questionArr[MPOW.totalQue].sound;
	document.getElementById('playSound').play();
	quesId=parseInt(MPOW.questionArr[MPOW.totalQue]['questionId']);
}
//Check Answer
MPOW.checkAnswer=function(element)
{	
	var randomSound;
	MPOW.attemptCnt++;
	if(($(element).data('ans')==MPOW.currentAnswImage))
	{
		scoredMarks=10;
		randomSound=Math.floor(Math.random()*(MPOW.correctAns.length));
		MPOW.answerwiseMessage('correct.png',MPOW.correctAns[randomSound]);
	}
	else
	{
		scoredMarks=0;
		randomSound=Math.floor(Math.random()*(MPOW.wrongAns.length));
		MPOW.answerwiseMessage('wrong.png',MPOW.wrongAns[randomSound]);
	}
	
	MPOW.questionArr.splice(MPOW.totalQue,1);
	console.log(resId,quesId,scoredMarks,totalMarks,1,startTime);
	if(Utils.mobileDeviceFlag)
		Android.addScore(resId,quesId,scoredMarks,totalMarks,1,startTime);
	$(".pointerClass").css("pointer-events","none");
	if(MPOW.nextQ)
	{
		clearTimeout(MPOW.nextQ);
		MPOW.nextQ=null;
	}
	MPOW.nextQ=setTimeout(function()
	{
		MPOW.nextQuestion();
	},1500);
	
}

MPOW.answerwiseMessage=function(imageName,soundName)
{
	document.getElementById("correctWrongDiv").style.visibility="visible";
	document.getElementById('correctWrongImg').src=Utils.Path+"images/"+imageName;
	document.getElementById('playSound').src=Utils.Path+"sounds/"+soundName;
	document.getElementById('playSound').play();
}

MPOW.nextQuestion=function()
{
	clearTimeout(MPOW.nextQ);
	if(MPOW.attemptCnt<5 && MPOW.totalQue<MPOW.questionArr.length)
	{
		document.getElementById("correctWrongDiv").style.visibility = "hidden";
		$(".pointerClass").css("pointer-events","auto");
		MPOW.showObjects();
	}
	else
	{
		MPOW.showLastPage();
	}
}

MPOW.skippedQuestion=function()
{	
	MPOW.skippedCnt++;
	MPOW.totalQue++;	
	if(MPOW.totalQue==MPOW.questionArr.length)
		MPOW.showPopup();
	else	
		MPOW.nextQuestion();
}

MPOW.showPopup=function()
{
	$('#responseBoxDiv').show();
	$('#questionMsg').text(MPOW.hardcodeddata.responseMsg);
	$('#Yes').text(MPOW.hardcodeddata.yes);
	$('#No').text(MPOW.hardcodeddata.no);
}

MPOW.showSkippedQuestions=function()
{
	$('#responseBoxDiv').hide();
	MPOW.skipFlag=true;
	MPOW.skippedCnt=0
	MPOW.totalQue=0;
	MPOW.showObjects();
}
MPOW.showLastPage=function()
{
	var length=0;
	length=MPOW.attemptCnt+MPOW.skippedCnt;
	$('#coverpage,#gamepage,#responseBoxDiv,#scoreBoard').hide();
	$('#lastPage,#msgDiv').show();
	$('#gamePage').empty();
	if(MPOW.skipFlag==true)
	{
		length=9;
	}	
	
	grade(MPOW.attemptCnt,length,'msgImg','msgText');
}
