//December 20 2018
var TowerOfHanoi={};
var Utils = {};
Utils.Path='';
Utils.mobileDeviceFlag=false;
TowerOfHanoi.loadmetaDataFlag=false;
TowerOfHanoi.discCount=4;
TowerOfHanoi.movesCount=0;
TowerOfHanoi.src=[3,2,1,0];
TowerOfHanoi.aux=[];
TowerOfHanoi.dest=[];
var questionId=0,startTime=0,scoredMarks=0,resId=0,totalMarks=50;
$(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("TowerOfHanoi");  
	setTimeout(function()
	{
		TowerOfHanoi.init();
		TowerOfHanoi.setImages();
	},100);
},200);
TowerOfHanoi.init=function()
{
	$('#gamePage,#endPage').hide();
};
// set Images//
TowerOfHanoi.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('helpBtn').src=Utils.Path+"images/help.png";
	document.body.style.backgroundImage="url("+Utils.Path+"images/Bg.png)";
	$("#tower1,#tower2,#tower3").css('backgroundImage',"url("+Utils.Path+"images/rod.png)");
};
//function which executes on play button click
TowerOfHanoi.playGame=function()
{
	d=new Date();				
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	$('#coverPage,#animalsCard,#Card').hide();
	$('#homeImage').css('visibility','visible');
	$('#logoImage,#gamePage').show();
	TowerOfHanoi.setDataToTowers();
	TowerOfHanoi.sequence=TowerOfHanoi.src.slice();
	$('#movesText').text(TowerOfHanoi.hardcodedData.moves);
	TowerOfHanoi.createDisc();
	$('.tower').droppable
	({
		 drop:TowerOfHanoi.dropDiscInTower 	
	});	
};
TowerOfHanoi.setDataToTowers=function()
{
	$("#tower1").data('arr',"TowerOfHanoi.src");
	$("#tower2").data('arr',"TowerOfHanoi.aux");
	$("#tower3").data('arr',"TowerOfHanoi.dest");
	$("#tower1").data('no',"0");
	$("#tower2").data('no',"1");
	$("#tower3").data('no',"2");
};
TowerOfHanoi.createDisc=function()
{
	var Disc,discOuter;
	for(var i=0;i<TowerOfHanoi.discCount-1;i++)
	{	
		for(j=1;j<=TowerOfHanoi.discCount;j++)
		{
			discOuter=document.createElement('div');
			discOuter.setAttribute('id',"discOuter"+i+j);
			discOuter.setAttribute('class',"nopadding outerDisc col-xs-12 col-md-12 col-lg-12 col-sm-12");
			if(i==0)
			{
				Disc=document.createElement('div');
				Disc.setAttribute('id',"Disc"+j);
				Disc.setAttribute('class',"nopadding text-center disc ");
				Disc.style.backgroundColor=TowerOfHanoi.colors[i];
				Disc.innerHTML=j;
				$(Disc).data('value',j-1);
				if(j==1)
					$(Disc).draggable({revert:'invalid',disabled:false});
				else
					$(Disc).draggable({revert:'invalid',disabled:true});
				discOuter.append(Disc);				
			}	
			document.getElementById('tower'+(i+1)).appendChild(discOuter);
		}	
	}
};
TowerOfHanoi.dropDiscInTower=function(event,ui)
{
	var currentTowerArr=[],previousTowerArr=[];
	var arrLength=0;
	var src=ui.draggable;	
	var dstDiv=$(this);
	var towerNo=$(this).data('no');
	currentTowerArr=eval($(this).data('arr'));
	arrLength=currentTowerArr.length;
	if(arrLength==0)
	{
		currentTowerArr.push($(src).data('value'));
		previousTowerArr=eval($((src).parent().parent()).data('arr'));
		$("#discOuter"+towerNo+""+(TowerOfHanoi.discCount-arrLength)).append(src);
		TowerOfHanoi.enableDrag();
		previousTowerArr.splice(previousTowerArr.length-1,1); 
		TowerOfHanoi.movesCount++;
	}
	else if($(src).data('value')<(currentTowerArr[currentTowerArr.length-1]))
	{
		currentTowerArr.push($(src).data('value'));
		previousTowerArr=eval($((src).parent().parent()).data('arr'));
		$("#discOuter"+towerNo+""+(TowerOfHanoi.discCount-arrLength)).append(src);
		TowerOfHanoi.enableDrag();
		previousTowerArr.splice(previousTowerArr.length-1,1); 
		TowerOfHanoi.movesCount++;
	}	
	else
		$(src).draggable("option", "revert", true);
	
	$('#moves').text(TowerOfHanoi.movesCount);
	$(src).css({'top':'0%','left':'0%'});
	
	 if($(this).prop('id')=="tower3")
			TowerOfHanoi.checkAnswer(); 
};
TowerOfHanoi.enableDrag=function()
{
	var disc;
	for(var i=0;i<TowerOfHanoi.discCount;i++)
	{	
		disc=$("#tower"+(i+1)).children().children('div')[0];
		if(disc!=undefined)
			$(disc).draggable({disabled:false});
		$("#tower"+(i+1)).children().children().not(disc).draggable({disabled:true});
	}
};
TowerOfHanoi.checkAnswer=function()
{
	if(TowerOfHanoi.dest.length==TowerOfHanoi.discCount)
	{
		if(arraysEqual(TowerOfHanoi.sequence,TowerOfHanoi.dest))
		{
			alert("Correct");
			if(TowerOfHanoi.movesCount==(Math.pow(2,TowerOfHanoi.discCount)-1))
			{
				console.log(resId,questionId,50,totalMarks,1,startTime);	
				if(Utils.mobileDeviceFlag)	
					Android.addScore(resId,questionId,50,totalMarks,1,startTime);	
			}	
			else if(TowerOfHanoi.movesCount<=((Math.pow(2,TowerOfHanoi.discCount)-1)+15)) 
			{
				console.log(resId,questionId,30,totalMarks,1,startTime);	
				if(Utils.mobileDeviceFlag)	
					Android.addScore(resId,questionId,30,totalMarks,1,startTime);	
			}
			else if(TowerOfHanoi.movesCount>((Math.pow(2,TowerOfHanoi.discCount)-1)+15)) 			
			{
				console.log(resId,questionId,20,totalMarks,1,startTime);	
				if(Utils.mobileDeviceFlag)	
					Android.addScore(resId,questionId,20,totalMarks,1,startTime);	
			}	
		}	
	}
};
//function which executes on home button click
TowerOfHanoi.goToHome=function()
{
	location.reload(true);	
};	
