//september 2019
var Demo = {};
var Utils = {};
Utils.path = "";
var questionId = 0, startTime = 0, scoredMarks = 0, totalMarks = 0, resId = 0, label, scoreFlag = false;
var word="",totalNos=0,type="sentence";
 Utils.mobileDeviceFlag = false;
$(document).ready(function () {
	if ((navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) && !(window.location.href.includes('http')))
        Utils.mobileDeviceFlag = true;
	/* if(Utils.mobileDeviceFlag)
	{
		Utils.Path=Android.getMediaPath('makeRhymeWord');
	} */
	$(".se-pre-con").fadeOut("slow");
	setTimeout(function () {
		Demo.setImageSource();
		Demo.Init();
	}, 100);
});

Demo.setImageSource = function () {
	document.body.style.backgroundImage = Utils.path + "url('images/skin/cover_page.png')";
	document.getElementById("home").src = Utils.path + "images/skin/Home.png";
	document.getElementById("logo").src = Utils.path + "images/skin/logo.png";
	document.getElementById("playbutton").src = Utils.path + "images/skin/Play.png";
	document.getElementById("next").src = Utils.path + "images/skin/next.png";
	document.getElementById("birdsNameDiv").style.backgroundImage = Utils.path + "url('images/skin/text_box.png')";
}
//
Demo.Init = function () {
	$("#birdsNameDiv").css({"background-size":"100% 100%" });
	document.getElementById("home").style.display = "none";
	document.getElementById("gamePage").style.display = "none";
	document.getElementById("finalPage").style.display="none";
	
}
//Initialize variables 
Demo.initVariables = function ()
{
	Demo.queCount=0;
}
Demo.playGame = function () {
	Demo.initVariables();
	document.body.style.backgroundImage = Utils.path + "url('images/skin/background.png')";
	document.getElementById("gamePage").style.display = "block";
	document.getElementById("coverPage").style.display = "none";
	document.getElementById("home").style.display = "block"; 
	document.getElementById("finalPage").style.display="none";
	Demo.populateQue();
	Demo.createOptions();
	Demo.createOptionDiv();
}
// Populate 10 random questions from json
Demo.populateQue = function ()
{
	var noOfQue=5;
	Demo.queArray=[];
	for(var i=0;i<noOfQue;i++)
	{
		Demo.randomQue=Math.floor(Math.random()*Demo.data.length);
		while(Demo.queArray.includes(Demo.data[Demo.randomQue]))
			Demo.randomQue=Math.floor(Math.random()*Demo.data.length);
		Demo.queArray.push(Demo.data[Demo.randomQue]);
	}
	console.log(Demo.queArray);
}
//create three options as past present and future
Demo.createOptions = function ()
{
	var noOfDemo=4;
	for(var i=0;i<noOfDemo;i++)
	{
	var optionDiv = document.createElement("div");
	optionDiv.setAttribute("id","outerDiv"+i);
	optionDiv.setAttribute("class","nopadding col-sm-2 col-lg-2 col-md-2 col-xs-2");
	document.getElementById('frameDiv').appendChild(optionDiv);
	
	var soundDiv = document.createElement("div");
	soundDiv.setAttribute("id","soundDiv"+i);
	soundDiv.setAttribute("class","nopadding col-sm-12 col-lg-12 col-md-12 col-xs-12");
	document.getElementById("outerDiv"+i).appendChild(soundDiv);
	var soundImg = document.createElement("img");
	soundImg.setAttribute("id","soundImg"+i);
	soundImg.setAttribute("class","img-responsive nopadding col-sm-8 col-lg-8 col-md-8 col-xs-8 col-sm-offset-2 col-lg-offset-2 col-md-offset-2 col-xs-offset-2");
	$(soundImg).data("soundData",Demo.queArray[i].sound);
	soundImg.setAttribute("onclick","Demo.playSound(this.id)");
	document.getElementById("soundDiv"+i).appendChild(soundImg);
	document.getElementById("soundImg"+i).src = Utils.path + "images/skin/sound.png";

	var imgDiv = document.createElement("div");
	imgDiv.setAttribute("id","imgDiv"+i);
	imgDiv.setAttribute("class","imgDiv dropImg img-responsive nopadding col-sm-12 col-lg-12 col-md-12 col-xs-12");
	$(imgDiv).data("imgNameData",Demo.queArray[i].name);
	document.getElementById("outerDiv"+i).appendChild(imgDiv);
	var birdImg = document.createElement("img");
	birdImg.setAttribute("id","birdImg"+i);
	birdImg.setAttribute("class","img-responsive nopadding col-sm-12 col-lg-12 col-md-12 col-xs-12");
	document.getElementById("imgDiv"+i).appendChild(birdImg);

	var nameDiv = document.createElement("div");
	nameDiv.setAttribute("id","nameDiv"+i);
	nameDiv.setAttribute("class","birdName dropName nopadding col-sm-12 col-lg-12 col-md-12 col-xs-12");
	$(nameDiv).data("imgNameData",Demo.queArray[i].name);
	document.getElementById("outerDiv"+i).appendChild(nameDiv);

	}
}
Demo.createOptionDiv = function (){
	Demo.shuffle(Demo.queArray);
	for(var i=0;i<5;i++)
	{
		var imgDiv = document.createElement("div");
		imgDiv.setAttribute("id","optionDiv"+i);
		imgDiv.setAttribute("class","optionImgDiv img-responsive nopadding col-sm-2 col-lg-2 col-md-2 col-xs-2");
		document.getElementById("optionDiv").appendChild(imgDiv);

		var birdImg = document.createElement("img");
		birdImg.setAttribute("id","optionImg"+i);
		birdImg.setAttribute("class","img-responsive dragImg nopadding col-sm-10 col-lg-10 col-md-10 col-xs-10 col-sm-offset-1 col-lg-offset-1 col-md-offset-1 col-xs-offset-1");
		$(birdImg).data("birdNameData",Demo.queArray[i].name);
		document.getElementById("optionDiv"+i).appendChild(birdImg);
		document.getElementById("optionImg"+i).src = Utils.path + "images/"+Demo.queArray[i].imgName;
	}
	Demo.shuffle(Demo.queArray);
	for(var i=0;i<5;i++)
	{
		var nameDiv = document.createElement("div");
		nameDiv.setAttribute("id","nameDiv"+i);
		nameDiv.setAttribute("class","birdNameClass dragName img-responsive nopadding col-sm-2 col-lg-2 col-md-2 col-xs-2");
		$(nameDiv).data("birdNameData",Demo.queArray[i].name);
		document.getElementById("birdsNameDiv").appendChild(nameDiv);
		$(nameDiv).text(Demo.queArray[i].name);
	}
	Demo.dragdropImg();
	Demo.dragdropName();
}
Demo.playSound=function(object)
{
	var sound=$("#"+object).data("soundData");
	document.getElementById("audio").src=Utils.path+'sounds/'+sound;
	document.getElementById("audio").play();
}
Demo.shuffle = function (alphaArray)	//suffle the letterArray
{
	for (var i = 0; i < alphaArray.length; i++) {
		var j = Math.floor(Math.random() * (alphaArray.length - 1));
		var temp = alphaArray[i];
		alphaArray[i] = alphaArray[j];
		alphaArray[j] = temp;
	}
	return alphaArray;
}
/*================================================================ */
Demo.dragdropImg=function()
{
	$( ".dragImg" ).draggable(
		{
			create: function( event, ui ) {},
			cursor: 'move',
			revert: 'invalid',
			helper: 'clone',
			containment : "#gamePage",
			drag: function (event, ui){
				
			}
		});
	$(".dropImg").droppable(
	{
		accept: ".dragImg",
		drop:function(event,ui)
		{
			$(this).empty();
			$(this).append(ui.draggable.clone());
			var name=$(ui.draggable).data("birdNameData");
				console.log(name);
			var imgName=$(this).data("imgNameData");
			console.log(imgName);
			if(name==imgName)
			{
				document.getElementById("audio").src=Utils.path+'sounds/Instructions/Right_answer__Aditi.mp3';
			}
			else
			{
				document.getElementById("audio").src=Utils.path+'sounds/Instructions/Not_this_one__Aditi.mp3';
			}
			document.getElementById("audio").play();
		}
	});
}
//drag and drop bird name
Demo.dragdropName=function()
{
	$( ".dragName" ).draggable(
		{
			create: function( event, ui ) {},
			cursor: 'move',
			revert: 'invalid',
			helper: 'clone',
			containment : "#gamePage",
			drag: function (event, ui){
				
			}
		});
	$(".dropName").droppable(
	{
		accept: ".dragName",
		drop:function(event,ui)
		{
			$(this).empty();
			$(this).append(ui.draggable.text());
			var name=$(ui.draggable).data("birdNameData");
				console.log(name);
			var imgName=$(this).data("imgNameData");
			console.log(imgName);
			if(name==imgName)
			{
				document.getElementById("audio").src=Utils.path+'sounds/Instructions/Right_answer__Aditi.mp3';
			}
			else
			{
				document.getElementById("audio").src=Utils.path+'sounds/Instructions/Not_this_one__Aditi.mp3';
			}
			document.getElementById("audio").play();
		}
	});
}
Demo.nextQue = function () {
	$(".nextQue").empty();
	Demo.populateQue();
	Demo.createOptions();
	Demo.createOptionDiv();
}
Demo.returnback = function () {
	location.reload();
}