//14th January  2019
var Monopoly={};
var Utils = {};
Utils.Path='';
Utils.mobileDeviceFlag=false;
Monopoly.loadmetaDataFlag=false;
Monopoly.inFlag=false;
Monopoly.imagesArr=[];
Monopoly.currentPlayer=1;
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("Monopoly");   */
	setTimeout(function()
	{
		Monopoly.init();
		Monopoly.setImages();
	},100);
},200);
Monopoly.init=function()
{
	$('#gamePage,#endPage').hide();
};
// set Images//
Monopoly.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('foregroundImg').src=Utils.Path+"images/cage.png";
	document.body.style.backgroundImage="url("+Utils.Path+"images/cover.jpg)";
};
//function which executes on play button click
Monopoly.playGame=function()
{
	d=new Date();				
	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
	$('#coverPage,#animalsCard,#Card').hide();
	$('#homeImage,#scoreBoard,#questionDiv').css('visibility','visible');
	$('#logoImage,#gamePage').show();
	Monopoly.createDivs();
	Monopoly.createSongti();
	Monopoly.getRandomImages();
	Monopoly.setImagesToCards();
	Monopoly.setHardcodedData();
	Monopoly.setdDataToDices();
	Monopoly.setColors();
	Monopoly.moveToNextPlayer();
	Monopoly.disableClick();
	document.getElementById('backgroundMusic').src=Utils.Path+"sounds/background.mp3";
	document.getElementById('backgroundMusic').loop=true;
	//document.getElementById('backgroundMusic').play();
};
//function which create board structure
Monopoly.createDivs=function()
{
	var div,idCnt=1;
	questionDiv.className=Monopoly.classname+" "+"nopadding col-lg-"+Monopoly.questionDivCol+" "+"col-md-"+Monopoly.questionDivCol+" "+"col-xs-"+Monopoly.questionDivCol+" "+"col-sm-"+Monopoly.questionDivCol;			
	document.getElementById("center").className=Monopoly.classname+" "+"col-lg-"+Monopoly.centerDivCol+" "+"col-sm-"+Monopoly.centerDivCol+" "+"col-lg-offset-"+Monopoly.centerDivOffset+" "+"col-md-offset-"+Monopoly.centerDivOffset+" "+"col-xs-offset-"+Monopoly.centerDivOffset+" "+"col-sm-offset-"+Monopoly.centerDivOffset;		
	for(var i=0;i<4;i++)
	{
		document.getElementById("div"+(i+1)).className=Monopoly.classname+" "+"col-lg-"+Monopoly.colsize[i]+" "+"col-md-"+Monopoly.colsize[i]+" "+"col-xs-"+Monopoly.colsize[i]+" "+"col-sm-"+Monopoly.colsize[i]+" "+"col-lg-offset-"+Monopoly.coloffset[i]+" "+"col-md-offset-"+Monopoly.coloffset[i]+" "+"col-xs-offset-"+Monopoly.coloffset[i]+" "+"col-sm-offset-"+Monopoly.coloffset[i];		
		for(j=0;j<Monopoly.letters[i];j++)
		{
			songtiPath=document.createElement('div');
			songtiPath.id="innerPath"+idCnt;
			songtiPath.className="nopadding path col-lg-12 col-md-12 col-xs-12 col-sm-12";
			div=document.createElement('div');
			if(i==0 || i==2) 
				div.className="nopadding letter col-lg-1 col-md-1 col-xs-1 col-sm-1";
			else
				div.className="nopadding letter col-lg-12 col-md-12 col-xs-12 col-sm-12";
			
			div.id="innerDiv"+idCnt;
			div.style.backgroundColor="white";
			div.setAttribute('cage','in');
			img=document.createElement('img');
			img.id="img"+idCnt;
			img.className="nopadding img-responsive col-lg-12 col-md-12 col-xs-12 col-sm-12";
			div.appendChild(img);
			div.appendChild(songtiPath);
			idCnt++;
			document.getElementById("div"+(i+1)).appendChild(div);
			 if(idCnt>1 && idCnt<=8)
				$(div).insertBefore("#innerDiv"+(idCnt-2));
			 else if(idCnt==10)
				$(div).insertBefore("#innerDiv"+(idCnt-2));    
		}		
	}
};
//function which gets 28 random images
Monopoly.getRandomImages=function()
{
	var randomIndex=0; 
	for(var i=1;i<25;i++)
	{	
		randomIndex=Math.floor(Math.random()*Monopoly.data.length);
			while($.inArray(randomIndex,Monopoly.imagesArr)!=-1)
				randomIndex=Math.floor(Math.random()*Monopoly.data.length);
		Monopoly.imagesArr.push(randomIndex);	
	}	
	shuffleArray(Monopoly.imagesArr);
};
//Set images to blocks 
Monopoly.setImagesToCards=function()
{
	for(var i=0;i<18;i++)
	{
		if(i!=0)
		{
		  document.getElementById("innerDiv"+(i+1)).setAttribute('no',Monopoly.imagesArr[i]);	
		  $("#img"+(i+1)).prop('src',Utils.Path+"images/"+Monopoly.data[Monopoly.imagesArr[i]].img);
		}
		else
			$("#img"+(i+1)).prop('src',Utils.Path+"images/start.png");	
	}		 
};
//function which sets hardcoded text used in the game 
Monopoly.setHardcodedData=function()
{	
	$("#player"+1+"Text").text(Monopoly.hardcodedData.player1);
	$("#player"+2+"Text").text(Monopoly.hardcodedData.player2);
	$("#player"+3+"Text").text(Monopoly.hardcodedData.player3);
	$("#player"+4+"Text").text(Monopoly.hardcodedData.player4);
	$("#player1").text(Monopoly.hardcodedData.player1);
	$("#player2").text(Monopoly.hardcodedData.player2);
	$("#player3").text(Monopoly.hardcodedData.player3);
	$("#player4").text(Monopoly.hardcodedData.player4);
	$("#yesButton").text(Monopoly.hardcodedData.yes);
	$("#noButton").text(Monopoly.hardcodedData.no);	
	$("#gameOverText").text(Monopoly.hardcodedData.gameover);
	$("#winnerText").text(Monopoly.hardcodedData.winner);
};
Monopoly.setdDataToDices=function()
{
	for(var i=1;i<=4;i++)
	{
		$("#score"+i+"Text").text("0");
		document.getElementById('songti'+i).setAttribute('playerId','songti'+i);
		document.getElementById('songti'+i).setAttribute('currentPos',1);
		document.getElementById("songti"+i).setAttribute('score',0);
	}
};
//function which sets colors to songti
Monopoly.setColors=function()
{
	for(var i=1;i<=4;i++)
		$("#player"+i).css('backgroundColor',Monopoly.colors[i-1]);
};
//function which executes on home button click
Monopoly.goToHome=function()
{
	location.reload(true);	
};
//function which moves highlighter to next player
Monopoly.moveToNextPlayer=function()
{
	$("#player"+Monopoly.currentPlayer).addClass('blink_me');
};
//function which moves highlighter to next player
Monopoly.putDice=function(obj)
{
	$("#"+obj).css('pointer-events','none');
	randomDice=Math.floor(Math.random()*6)+1;
	$('#diceImg').prop('src',Utils.Path+'images/'+randomDice+".png");
	$('#diceDiv').addClass('animated rotateIn');
	setTimeout(function(){$('#diceDiv').removeClass('animated rotateIn');},600);
	$("#player"+Monopoly.currentPlayer).removeClass('blink_me');
	Monopoly.moveSongti(obj,randomDice);
};
//function which disables the songti(players) apart from current player 
Monopoly.disableClick=function()
{
	$(".glyphicon").css('pointer-events','auto');
	$(".glyphicon").not('#songti'+Monopoly.currentPlayer).css('pointer-events','none');
};
//function which creates the songti
Monopoly.createSongti=function()
{
	var Songti;
	for(var i=1;i<=4;i++)
	{
		div=document.createElement('div');
		div.className="nopadding col-lg-4 col-md-4 col-xs-4 col-sm-4";
		Songti=document.createElement('span');
		Songti.id="songti"+i;
		Songti.className="nopadding glyphicon glyphicon-user "+Monopoly.classes[i-1];
		Songti.setAttribute('onclick','Monopoly.putDice(this.id)');
		div.appendChild(Songti);
		document.getElementById("innerPath1").appendChild(div);	
	}	
};
//function which moves songti from one block to another  
Monopoly.moveSongti=function(obj,noOfMoves)
{
	var nodeNo,player,position;
	player=document.getElementById(obj).getAttribute('playerId');
	position=parseInt(document.getElementById(obj).getAttribute('currentPos'));
	document.getElementById(obj).setAttribute('currentPos',noOfMoves+position);
	position=document.getElementById(obj).getAttribute('currentPos');
	$span=$("#"+player).clone();
	$("#"+player).remove();
	if(position > 18)
		position=(position-18);	
	console.log(player+"  "+position);
	$("#innerPath"+position).append($span);
	document.getElementById(obj).setAttribute('currentPos',position);
	if(position > 1)	
	{
		nodeNo=document.getElementById("innerDiv"+position).getAttribute('no');
		Monopoly.showQuestion(position,nodeNo);
	}	
	else
	{
		if(Monopoly.currentPlayer==4)
			Monopoly.currentPlayer=1;
		else
			Monopoly.currentPlayer++;
		Monopoly.disableClick();
		Monopoly.moveToNextPlayer();
	}	
}; 
//function which shows question in the model
Monopoly.showQuestion=function(position,nodeNo)
{
	console.log(nodeNo);
	Monopoly.inFlag=false;
	$('.buttonCls').css('pointer-events','auto');
	document.getElementById('imgDiv').style.backgroundImage="url("+Utils.Path+"images/"+Monopoly.data[nodeNo].img+")";
	if(document.getElementById("innerDiv"+position).getAttribute('cage')!="in")
	{
		$('#foregroundImg').css('visibility','hidden');
		Monopoly.randomquestion=Math.floor(Math.random()*2);	
		$("#questionText").text(Monopoly.data[nodeNo].arr[Monopoly.randomquestion]);
		$("#gifImg").prop('src',Utils.Path+'images/'+Monopoly.data[nodeNo].gifArr[Monopoly.randomquestion]);
	}	
	else
	{
		Monopoly.inFlag=true;
		document.getElementById("innerDiv"+position).setAttribute('cage',"out");
		$("#questionText").text(Monopoly.data[nodeNo].question1);
		$("#gifImg").prop('src',Utils.Path+'images/'+Monopoly.data[nodeNo].img);
	}	
	Monopoly.showQueTimer=setTimeout(function(){
		$('#responseBoxDiv').show();
		/* clearInterval(Monopoly.timer); */
		},1000);
};
Monopoly.calculateScore=function(obj)
{
	$('.buttonCls').css('pointer-events','none');
	var score=0;
	score=parseInt(document.getElementById("songti"+(Monopoly.currentPlayer)).getAttribute('score'));
	if($(obj).text()=="YES")
	{	
		document.getElementById("songti"+Monopoly.currentPlayer).setAttribute('score',(score+10));
		$("#score"+Monopoly.currentPlayer+"Text").text(document.getElementById("songti"+Monopoly.currentPlayer).getAttribute('score'));
		if(Monopoly.inFlag)
			Monopoly.shutterUp();
		 setTimeout(function(){$("#questionPart").hide();$("#animationDiv").show();},1000); 
		 setTimeout(function(){
			 $('#foregroundImg').css('top','0px');
			 $('#foregroundImg').css('visibility','visible');
			 $('#responseBoxDiv').hide();
			 $("#questionPart").show();
			 $("#animationDiv").hide();
		 },3000); 
	}	
	else
		$('#responseBoxDiv').hide();

	if(Monopoly.currentPlayer==4)
		Monopoly.currentPlayer=1;
	else
		Monopoly.currentPlayer++;
	console.log(Monopoly.currentPlayer);
	Monopoly.disableClick();
	Monopoly.moveToNextPlayer();
};
Monopoly.countDown=function()
{
	Monopoly.timer=setInterval(function()
	{
        var myTime = $("#timer").html();
        var ss = myTime.split(":");
        var dt = new Date();
        dt.setHours(0);
        dt.setMinutes(ss[0]);
        dt.setSeconds(ss[1]);
        
        var dt2 = new Date(dt.valueOf() + 1000);
        var temp = dt2.toTimeString().split(" ");
        var ts = temp[0].split(":");
			
       $("#timer").html(ts[1]+":"+ts[2]);
	     if(ts[1]==1)
		 {
			 clearInterval(Monopoly.timer);
			 setTimeout(Monopoly.showQueTimer);
			 Monopoly.getWinner();  
			 $('#gamePage').hide();
			 $('#endPage').show();
			 $('#scoreCard').css('visibility','visible');
		 }	 
	},1000);
};
//function which gives the winners
Monopoly.getWinner=function()
{
	var arr=[],winnersArr=[]; 
	for(i=1;i<=4;i++)
		arr.push(document.getElementById("songti"+i).getAttribute('score'));
	arr.sort();
	arr.reverse();
	for(j=0;j<arr.length;j++)
	{
		if(arr[0]==arr[j])
			winnersArr.push(j);
	}	
	console.log(winnersArr);
	Monopoly.showWinnersList(winnersArr);
};
Monopoly.showWinnersList=function(arr)
{ 
	var player;
	for(var i=0;i<arr.length;i++)
	{
		player=document.createElement('div');
		player.id="winner"+i;
		player.className="nopadding text-center winner col-sm-6 col-lg-6 col-md-6 col-xs-6 col-lg-offset-3 col-md-offset-3 col-xs-offset-3 col-sm-offset-3";
		player.innerHTML="player"+(arr[i]+1);
		document.getElementById('winnersDiv').appendChild(player);
	}	
};
Monopoly.shutterUp=function()
{	
	var value=0;
	$('#foregroundImg').css('top','-10px');
	Monopoly.shutterInterval=setInterval(function()
	{
		value=value-200;
		if(value =='-600')
		{
			clearInterval(Monopoly.shutterInterval);
			Monopoly.shutterInterval=null;
		}	
		else
			$('#foregroundImg').css('top',value+'px');
	},50);
}
setTimeout(Monopoly.countDown,1000);