//November 28th 2018
var GadbadGhotala={};
var Utils = {};
Utils.Path='';
Utils.mobileDeviceFlag=false;
GadbadGhotala.loadmetaDataFlag=false;
var questionId=0,startTime=0,scoredMarks=0,resId=0,totalMarks=10;
$(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("GadbadGhotala");  */  
	setTimeout(function()
	{
		GadbadGhotala.init();
		GadbadGhotala.setImages();
		GadbadGhotala.setEventListeners();
	},100);
},200);
GadbadGhotala.init=function()
{
	GadbadGhotala.correctFlag=false,GadbadGhotala.counter=0;
	$('#gamePage,#endPage').hide();
	$("#checkDiv").css('pointer-events','none');
	$("#checkSoundDiv").css('pointer-events','none');
	document.getElementById("playSound").src=Utils.Path+"sounds/1_colours_small.mp3";
};
// set Images//
GadbadGhotala.setImages=function()
{
	document.getElementById('play').src=Utils.Path+"images/Play.png";
	document.getElementById('homeImage').src=Utils.Path+"images/Home.png";
	document.getElementById('logoImage').src=Utils.Path+"images/logo.png";
	document.getElementById('checkImg').src=Utils.Path+"images/Done.png";  
	document.getElementById('soundImg').src=Utils.Path+"images/sound.png";
	document.getElementById('checkSoundImg').src=Utils.Path+"images/sound.png";
	document.getElementById('nextImg').src=Utils.Path+"images/Next.png";
	document.getElementById('boyImg').src=Utils.Path+"images/boy.png";
	document.body.style.backgroundImage="url("+Utils.Path+"images/cover.jpg)";
};
GadbadGhotala.setEventListeners=function()
{
	$("#checkImg").on("click",function(){
		GadbadGhotala.checkAnswer(this.id);
	});
	$("#soundImg").on("click",function(){
		GadbadGhotala.counter=0;
		GadbadGhotala.hightlightTextWithAudio("Div");
	});
	$("#checkSoundImg").on("click",function(){
		GadbadGhotala.counter=0;
		GadbadGhotala.hightlightTextWithAudio("blank");
	});
};
//function which executes on play button click
GadbadGhotala.playGame=function()
{
	d=new Date();				
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	$('#coverPage').hide();
	$('#homeImage').css('visibility','visible');
	$('#logoImage,#gamePage').show();
	document.body.style.backgroundImage="url("+Utils.Path+"images/Bg.jpg)";
	shuffleArray(GadbadGhotala.data);
	GadbadGhotala.createSongParts();
	GadbadGhotala.createEmptyParts();
};
GadbadGhotala.createSongParts=function()
{
	var Div,img;
	for(var i=0;i<4;i++)
	{	
		Div=document.createElement('div');
		Div.setAttribute('id',"Div"+i);
		Div.setAttribute('class',"part nopadding col-sm-2 col-md-2 col-lg-2 col-xs-2 col-xs-offset-1 col-md-offset-1 col-lg-offset-1 col-sm-offset-1");
		Div.setAttribute('onclick','GadbadGhotala.playAudio(this);');
		Div.innerHTML=GadbadGhotala.data[i].name;
		$(Div).data('from',GadbadGhotala.data[i].From);
		$(Div).data('to',GadbadGhotala.data[i].To);
		document.getElementById('partsDiv').appendChild(Div);
	}
	$('.part').draggable({
		appendTo:'body', 
		helper : "clone", 
		start: function(e, ui)
		{
			$(ui.helper).addClass("ui-draggable-helper");
		} 
	});
};
GadbadGhotala.playAudio=function(id)
{
	if(GadbadGhotala.pauseAudioTimer)
	{
		clearTimeout(GadbadGhotala.pauseAudioTimer);
		GadbadGhotala.pauseAudioTimer=null;
	}	
	if(GadbadGhotala.highlightTimer){
		clearTimeout(GadbadGhotala.highlightTimer);
		GadbadGhotala.highlightTimer=null;
	}
	$('.opacityCls').removeClass('opacityCls');
	$(id).addClass('opacityCls');
	document.getElementById('playSound').pause();
	document.getElementById("playSound").src=Utils.Path+"sounds/1_colours_small.mp3";
	GadbadGhotala.playTime=($(id).data('to'))-($(id).data('from'));
	document.getElementById('playSound').currentTime=$(id).data('from');
	document.getElementById("playSound").play();
	GadbadGhotala.pauseAudioTimer=setTimeout(function()
	{
		$(id).removeClass('opacityCls');
		document.getElementById('playSound').pause();
	},GadbadGhotala.playTime*1000);
};
GadbadGhotala.hightlightTextWithAudio=function(divName)
{ 
	document.getElementById('playSound').pause();
	$('.opacityCls').removeClass('opacityCls');
	$("#"+divName+GadbadGhotala.counter).addClass('opacityCls');
	GadbadGhotala.playTime=$("#"+divName+GadbadGhotala.counter).data('to')-$("#"+divName+GadbadGhotala.counter).data('from');
	document.getElementById('playSound').currentTime=$("#"+divName+GadbadGhotala.counter).data('from');
	document.getElementById("playSound").play();
	if(GadbadGhotala.pauseAudioTimer)
	{
		clearTimeout(GadbadGhotala.pauseAudioTimer);
		GadbadGhotala.pauseAudioTimer=null;
	}	
	if(GadbadGhotala.highlightTimer){
		clearTimeout(GadbadGhotala.highlightTimer);
		GadbadGhotala.highlightTimer=null;
	}
	GadbadGhotala.highlightTimer=setTimeout(function()
	{
		if(GadbadGhotala.counter<3)
		{
			$('.opacityCls').removeClass('opacityCls');
			GadbadGhotala.counter++;
			GadbadGhotala.hightlightTextWithAudio(divName);
			return 0;
		}
		else
		{
			$(".part").draggable({ disabled:false});          
			$(".blank").draggable({ disabled:false });          
			if(GadbadGhotala.correctFlag)
			{
				$('.block').text("");
				$('.block').removeClass('block');
			}		
			GadbadGhotala.counter=0;
			document.getElementById('playSound').pause();
			$('.opacityCls').removeClass('opacityCls');
			GadbadGhotala.textHighlightIndex=0;
		}	
	},GadbadGhotala.playTime*1000);
}; 
GadbadGhotala.createEmptyParts=function()
{
	var Div,img;
	for(var i=0;i<4;i++)
	{	
		Div=document.createElement('div');
		Div.setAttribute('id',"blank"+i);
		$(Div).data('raga',GadbadGhotala.ids[i]);
		Div.setAttribute('class',"blank nopadding col-sm-2 col-md-2 col-lg-2 col-xs-2 col-xs-offset-1 col-md-offset-1 col-lg-offset-1 col-sm-offset-1");
		document.getElementById('blankDiv').appendChild(Div);
	}
	 $('.blank').draggable({
		appendTo:'body', 
		helper : "clone", 
		start: function(e, ui)
		{
			$(ui.helper).addClass("ui-draggable-helper");
		} 
	}); 
	$('.blank').droppable
	({
		 drop:GadbadGhotala.dropParts 	
	});	
};
GadbadGhotala.dropParts=function(event,ui)
{

	if($(ui.draggable).parent().prop('id')=="partsDiv")
	{
		if($(ui.draggable).text()!="")
		{
			$(this).data('attach',"on");	
			$(this).text($(ui.draggable).text());
			$(this).addClass('block');
			$(this).data('from',$(ui.draggable).data('from'));
			$(this).data('to',$(ui.draggable).data('to'));
		}
	}
	else	
	{
		if($(ui.draggable).text()!="")
		{
			$(ui.draggable).data('attach',"off");
			$(this).data('attach',"on");
			$(this).data('from',$(ui.draggable).data('from'));
			$(this).data('to',$(ui.draggable).data('to'));
			$(this).text($(ui.draggable).text());
			$(this).addClass('block');
			$(ui.draggable).removeClass('block');
			$(ui.draggable).text("");
		}	
	}
	if(GadbadGhotala.checkAllOn())
		{	
			//$("#checkSoundDiv").css('pointer-events','auto');
			$("#checkDiv").css('pointer-events','auto');	
		}
		else
			$("#checkDiv").css('pointer-events','none');	
};
GadbadGhotala.checkAnswer=function()
{
	$(".part").draggable({ disabled: true });          
	$(".blank").draggable({ disabled: true });   
	$("#boyDiv").css('visibility','visible');
	$("#checkDiv").css('pointer-events','none');	
	if(GadbadGhotala.checkEquals())
	{		
		GadbadGhotala.correctFlag=true;
		/* document.getElementById("audioPlay").src=Utils.Path+"sounds/Bahotacche.mp3";
		document.getElementById("audioPlay").play(); */
		$(".blank").data('attach',"");
		GadbadGhotala.counter=0;
		GadbadGhotala.hightlightTextWithAudio("blank");
	}	
	else	
	{	
		/* $('.block').text("");
		$('.block').removeClass('block'); */
		GadbadGhotala.correctFlag=false;
		GadbadGhotala.counter=0;
		GadbadGhotala.hightlightTextWithAudio("blank");
		/* document.getElementById("audioPlay").src=Utils.Path+"sounds/ohhno.mp3";
		document.getElementById("audioPlay").play(); */
	}	
};
GadbadGhotala.checkEquals=function()
{
	for(var i=0;i<4;i++)
	{
		var data=$("#blank"+i).data('raga');
		if(data!=($("#blank"+i).text()))
			return false;			
	}	
	return true;
};
GadbadGhotala.checkAllOn=function()
{
	for(var i=0;i<4;i++)
	{
		var data=$("#blank"+i).data('attach');
		if(data!="on")
			return false;			
	}	
	return true;
};
GadbadGhotala.goToHome=function()
{
	location.reload(true);	
};	
