var Kahaniyaan={};
var Utils={};
Utils.Path='';
Utils.mobileDeviceFlag=false;
var questionId=0,startTime=0,scoredMarks=0,totalMarks=10,resId=0;
Kahaniyaan.nextPage=0;
Kahaniyaan.storyReadCnt=0;
Kahaniyaan.goBackFlag=null;
Kahaniyaan.playPauseFlag=false;
Kahaniyaan.soundFlag=true;
$(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("KhelbadiKahaniyan"); */
	
	setTimeout(function(){
	document.getElementById('coverpage').style.display="block";
	document.getElementById('showMenu').style.display="none";
	document.getElementById('storyPage').style.display="none";
	document.getElementById('endpage').style.display="none";
	document.getElementById("home").style.display = "none";
	Kahaniyaan.setImages();
	},300);
});
Kahaniyaan.setImages=function()
{
	document.body.style.backgroundImage="url("+Utils.Path+"images/skin/"+Kahaniyaan.coverImage+")";
	document.getElementById("imgDiv").style.backgroundImage="url("+Utils.Path+"images/skin/pageCover1.png)";
	document.getElementById("textPart").style.backgroundImage="url("+Utils.Path+"images/skin/pageCover2.png)";
	document.getElementById("acknowledgementDiv").style.backgroundImage="url("+Utils.Path+"images/skin/creditsBG.png)";
	document.getElementById('home').src=Utils.Path+"images/skin/Home.png";
	document.getElementById('logo').src=Utils.Path+"images/skin/logo.png";
	document.getElementById('play').src=Utils.Path+"images/skin/Play.png";
	document.getElementById('prev').src=Utils.Path+"images/skin/Pre.png";
	document.getElementById('next').src=Utils.Path+"images/skin/Next.png";
	document.getElementById('credits').src=Utils.Path+"images/skin/credits.png";
	document.getElementById('playAgain').src=Utils.Path+"images/skin/playAgain.png";
}
Kahaniyaan.showStoryMenu=function()
{
	var scrollBar;
	document.body.style.backgroundImage="url("+Utils.Path+"images/skin/BG3.png)";
	document.getElementById("acknowledgementDiv").innerHTML="";
	Kahaniyaan.goBackFlag="storyMenu";
	document.getElementById('Stories').style.visibility="visible";
	$("#playPauseDiv").hide();
	$("#storyBookDiv").hide();
	$("#storyBookDiv").text('');
	document.getElementById('Stories').innerHTML=Kahaniyaan.story;
	document.getElementById("playSound").pause();
	Kahaniyaan.nextPage=0;
	document.getElementById('storyPage').style.display="none";
	document.getElementById('endpage').style.display="none";
	document.getElementById('scrollBar').innerHTML="";
	document.getElementById('showMenu').style.display="block";
	document.getElementById('coverpage').style.display="none";
	document.getElementById("home").style.display = "block";
	scrollBar=document.getElementById("scrollBar");
	Kahaniyaan.populateMenu(scrollBar);
}
Kahaniyaan.populateMenu=function(parentDiv)
{
	var mainDiv,newDiv,newImage,para,imgCnt=0;
	for(var i=0;i<Math.ceil(Kahaniyaan.storyData.length/3);i++)
	{
		mainDiv= document.createElement("div");
		mainDiv.setAttribute("id","mainDiv"+i); 
		mainDiv.setAttribute("class","mainDivClass col-lg-12 col-md-12 col-xs-12 col-sm-12 noPadding"); 
		parentDiv.appendChild(mainDiv);
		
		for(var j=0;j<3;j++)
		{
			if(imgCnt<Kahaniyaan.storyData.length)
			{
				newDiv= document.createElement("div");
				newDiv.setAttribute("id","div"+imgCnt); 
				newDiv.setAttribute("class","bgClass divClass col-lg-3 col-md-3 col-xs-3 col-sm-3 col-sm-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-offset-1 noPadding"); 
				newDiv.setAttribute("onclick","Kahaniyaan.openStory(this)");
				var randomBookImage=Math.floor(Math.random()*4);
				newDiv.style.backgroundImage="url("+Utils.Path+"images/skin/bookCover_"+randomBookImage+".png)";
				mainDiv.appendChild(newDiv);
				
				newImage= document.createElement("img");
				newImage.setAttribute("id","img"+imgCnt);
				newImage.setAttribute("class","img-responsive borderClass col-lg-12 col-md-12 col-xs-12 col-sm-12");
				newImage.setAttribute("src",Utils.Path+"images/"+Kahaniyaan.storyData[imgCnt].storyCover);
				newDiv.appendChild(newImage);
				
				para= document.createElement("p");
				para.setAttribute("id","para"+imgCnt);
				para.setAttribute("class","menuNames text-center col-lg-12 col-md-12 col-xs-12 col-sm-12");
				para.innerHTML=Kahaniyaan.storyData[imgCnt].storyName;
				newDiv.appendChild(para);
				$("#div"+imgCnt).data("value",imgCnt);
				$("#div"+imgCnt).data("image",randomBookImage);
					imgCnt++;
			}
		}
	}
}

Kahaniyaan.openStory=function(element)
{
	$('#acknowledgementDiv').hide();
	var d=new Date();
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	Kahaniyaan.currentElement=element;
	scoredMarks=10;
	Kahaniyaan.soundFlag=true;
	Kahaniyaan.storyReadCnt=0;
	$(".marginClass").css("pointer-events","none");
	$("#storyBookDiv").show();
	document.getElementById('Stories').style.visibility="hidden";
	Kahaniyaan.goBackFlag="storyOpened";
	//document.getElementById('storyPage').style.display="block";
	document.getElementById('showMenu').style.display="none";
	Kahaniyaan.currentStoryNumber=$(element).data("value");
	questionId=Kahaniyaan.storyData[Kahaniyaan.currentStoryNumber].questionId;
	getDuration(Utils.Path+"sounds/"+Kahaniyaan.storyData[Kahaniyaan.currentStoryNumber].titleAudio,element);
	document.getElementById("playSound").play();
	//document.getElementById("textPart").style.display="none";
	Kahaniyaan.showStoryBook($(element).data("image"));
	document.getElementById("storyImg").src=Utils.Path+"images/"+Kahaniyaan.storyData[Kahaniyaan.currentStoryNumber].pages[Kahaniyaan.nextPage].img;
}
Kahaniyaan.showStoryBook=function(randomBookImage)
{
	newDiv= document.createElement("div");
	newDiv.setAttribute("id","bookDiv"); 
	newDiv.setAttribute("class","bgClass  col-lg-10 col-md-10 col-xs-10 col-sm-10 col-sm-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-offset-1 noPadding"); 
	newDiv.style.backgroundImage="url("+Utils.Path+"images/skin/bookCover_"+randomBookImage+".png)";
	document.getElementById("storyBookDiv").appendChild(newDiv);
	
	newImage= document.createElement("img");
	newImage.setAttribute("id","bookImg");
	newImage.setAttribute("class","img-responsive col-lg-10 col-md-10 col-xs-10 col-sm-10 col-sm-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-offset-1");
	newImage.setAttribute("src",Utils.Path+"images/"+Kahaniyaan.storyData[Kahaniyaan.currentStoryNumber].storyCover);
	newDiv.appendChild(newImage);
	
	para= document.createElement("p");
	para.setAttribute("id","bookPara");
	para.setAttribute("class","menuNames text-center col-lg-12 col-md-12 col-xs-12 col-sm-12");
	para.innerHTML=Kahaniyaan.storyData[Kahaniyaan.currentStoryNumber].storyName;
	newDiv.appendChild(para);
}
function getDuration(src,element)
{
	var duration=0;
	if(Kahaniyaan.soundFlag)
	{
		$('#playSound').on("loadedmetadata", function()
		{
			Kahaniyaan.soundFlag=false;
			duration=$('#playSound').prop('duration');
			$('#playSound').unbind("loadedmetadata");
			if(Kahaniyaan.storyStartTimer)
			{
				clearTimeout(Kahaniyaan.storyStartTimer);
				Kahaniyaan.storyStartTimer=null;
			}
			Kahaniyaan.storyStartTimer=setTimeout(function(){
				//document.getElementById("textPart").innerHTML='';
				$("#storyBookDiv").hide();
				document.getElementById('storyPage').style.display="block";
				document.getElementById("textPart").style.display="block";	
				$(".marginClass").css("pointer-events","auto");
				Kahaniyaan.nextStoryPage(element);
			},duration*1000);
		});
		$('#playSound').prop('src',src);
	}
}
Kahaniyaan.nextStoryPage=function(element)
{
	var spanEle;
	Kahaniyaan.txtArr=[];
	Kahaniyaan.wordNo=0;
	Kahaniyaan.playPauseFlag=false;
	$("#storyImg").removeClass("zoomInCls");
	$("#next").css("visibility","visible");
	clearTimeout(Kahaniyaan.stopHighlighting);
	clearTimeout(Kahaniyaan.playStoryPage);
	$("#playPauseDiv").show();
	if(element.id=="prev" && Kahaniyaan.nextPage>0)
		Kahaniyaan.nextPage--;
	else if(element.id=="next" && Kahaniyaan.nextPage<Kahaniyaan.storyData[Kahaniyaan.currentStoryNumber].pages.length)
	{
		Kahaniyaan.nextPage++;
		Kahaniyaan.storyReadCnt++;
	}
	if(Kahaniyaan.nextPage<Kahaniyaan.storyData[Kahaniyaan.currentStoryNumber].pages.length)
	{
		document.getElementById("txtDiv").innerHTML="";
		Kahaniyaan.txtArr=Kahaniyaan.storyData[Kahaniyaan.currentStoryNumber].pages[Kahaniyaan.nextPage].text.split(" ");
		for(var i=0;i<Kahaniyaan.txtArr.length;i++)
		{
			spanEle= document.createElement("span");
			spanEle.setAttribute("id","span"+i);
			spanEle.setAttribute("class","spans");
			spanEle.innerHTML=" "+Kahaniyaan.txtArr[i];
			document.getElementById("txtDiv").appendChild(spanEle);
		}
		document.getElementById("storyImg").src=Utils.Path+"images/"+Kahaniyaan.storyData[Kahaniyaan.currentStoryNumber].pages[Kahaniyaan.nextPage].img;
		document.getElementById("playSound").src=Utils.Path+"sounds/"+Kahaniyaan.storyData[Kahaniyaan.currentStoryNumber].soundFile;
		$("#storyImg").addClass("zoomInCls");
		Kahaniyaan.playStory();
	}
	if(Kahaniyaan.nextPage==Kahaniyaan.storyData[Kahaniyaan.currentStoryNumber].pages.length)
	{
		document.getElementById("playSound").pause();
		document.getElementById("storyPage").style.display="none";
		$('#endpage').show();
		document.getElementById('Stories').style.visibility="visible";
		$("#Stories").text(Kahaniyaan.endMsg);
		$("#playPauseDiv").hide();
		Kahaniyaan.createHardcodeddivs();
	}
	if(Kahaniyaan.storyReadCnt==1)
	{
		console.log(resId,questionId,scoredMarks,totalMarks,1,startTime);
		if(Utils.mobileDeviceFlag)
			Android.addScore(resId,questionId,scoredMarks,totalMarks,1,startTime);
	}
}
Kahaniyaan.createHardcodeddivs=function()
{
	var ackDiv;
	for(var i=0;i<Object.keys(Kahaniyaan.hardcoded[Kahaniyaan.currentStoryNumber]).length;i++)
	{
		if(Object.values(Kahaniyaan.hardcoded[Kahaniyaan.currentStoryNumber])[i]!="")
		{
			ackDiv= document.createElement("div");
			ackDiv.setAttribute("id","ack"+i); 
			ackDiv.setAttribute("class","noPadding firstLetter text-center fontClass col-sm-12 col-lg-12 col-md-12 col-xs-12"); 
			document.getElementById("acknowledgementDiv").appendChild(ackDiv);
			//ackDiv.innerHTML=Object.values(Kahaniyaan.hardcoded[Kahaniyaan.currentStoryNumber])[i];
			ackDiv.innerHTML=Object.keys(Kahaniyaan.hardcoded[Kahaniyaan.currentStoryNumber])[i]+" : "+Object.values(Kahaniyaan.hardcoded[Kahaniyaan.currentStoryNumber])[i];
		}
	}
	
}
Kahaniyaan.showCredits=function(){
	$('#acknowledgementDiv').show();
};
Kahaniyaan.playAgain=function()
{
	$('#endpage').hide();
	$("#storyBookDiv").text('');
	$("#acknowledgementDiv").empty();
	Kahaniyaan.nextPage=0;
	Kahaniyaan.openStory(Kahaniyaan.currentElement);
};
Kahaniyaan.playStory=function()
{
	var totalPages;
	document.getElementById("playPause").src=Utils.Path+"images/skin/pause.png";
	totalPages=Kahaniyaan.storyData[Kahaniyaan.currentStoryNumber].pages[Kahaniyaan.nextPage].read.length-1;
	Kahaniyaan.currentTime=Kahaniyaan.storyData[Kahaniyaan.currentStoryNumber].pages[Kahaniyaan.nextPage].read[Kahaniyaan.wordNo].From;
	
	Kahaniyaan.playTime=Kahaniyaan.storyData[Kahaniyaan.currentStoryNumber].pages[Kahaniyaan.nextPage].read[totalPages].To-Kahaniyaan.storyData[Kahaniyaan.currentStoryNumber].pages[Kahaniyaan.nextPage].read[Kahaniyaan.wordNo].From;
	
	document.getElementById("playSound").currentTime=Kahaniyaan.currentTime;
	document.getElementById("playSound").play();
	if(Kahaniyaan.playStoryPage)
	{
		clearTimeout(Kahaniyaan.playStoryPage);
		Kahaniyaan.playStoryPage=null;
	}
	setTimeout(function(){$("#storyImg").removeClass("zoomInCls");},800);
	Kahaniyaan.playStoryPage=setTimeout(function()
	{
		document.getElementById("playSound").pause();
	},Kahaniyaan.playTime*1000);
	Kahaniyaan.highLight();
}
Kahaniyaan.highLight=function()
{
	var getDuration=Kahaniyaan.storyData[Kahaniyaan.currentStoryNumber].pages[Kahaniyaan.nextPage].read[Kahaniyaan.wordNo].Duration;
	$("#span"+Kahaniyaan.wordNo).css({'color':'yellow'});
	if(Kahaniyaan.stopHighlighting)
	{
		clearTimeout(Kahaniyaan.stopHighlighting);
		Kahaniyaan.stopHighlighting=null;
	}
	Kahaniyaan.stopHighlighting=setTimeout(function()
	{
		if(Kahaniyaan.wordNo<Kahaniyaan.storyData[Kahaniyaan.currentStoryNumber].pages[Kahaniyaan.nextPage].read.length-1)
		{
			Kahaniyaan.wordNo++;
			$('.spans').css({'color':'rgba(0, 0, 0, 0.6)'});
			Kahaniyaan.highLight();
		}
		else
		{
			Kahaniyaan.playPauseFlag=true;
			document.getElementById("playPause").src=Utils.Path+"images/skin/playStory.png";
			Kahaniyaan.wordNo=0;
			$('.spans').css({'color':'rgba(0, 0, 0, 0.6)'});			
		}
	},getDuration*1000);
}

Kahaniyaan.playPause=function()
{
	if(Kahaniyaan.playPauseFlag==false)
	{
		Kahaniyaan.playPauseFlag=true;
		clearTimeout(Kahaniyaan.stopHighlighting);
		document.getElementById("playPause").src=Utils.Path+"images/skin/playStory.png";
		document.getElementById("playSound").pause();
		Kahaniyaan.currentTime=Kahaniyaan.storyData[Kahaniyaan.currentStoryNumber].pages[Kahaniyaan.nextPage].read[Kahaniyaan.wordNo].Duration;
	}
	else
	{
		Kahaniyaan.playPauseFlag=false;
		clearTimeout(Kahaniyaan.stopHighlighting);
		Kahaniyaan.playStory();
	}
}
Kahaniyaan.callHome=function()
{
	clearTimeout(Kahaniyaan.stopHighlighting);
	clearTimeout(Kahaniyaan.playStoryPage);
	clearTimeout(Kahaniyaan.storyStartTimer);
	document.getElementById("playSound").pause();
	if(Kahaniyaan.goBackFlag=="storyOpened")
		Kahaniyaan.showStoryMenu();
	else if(Kahaniyaan.goBackFlag=="storyMenu")
		location.reload();
}