// JavaScript Document

var gameInfo = new objGame;

	
	function objGame()
	{
		this.playerId = 0;
		this.playerTarget = '';
		this.defaultScore = 25;
		this.stage = 0;
		this.played = true;
		this.useKiller = false;
		this.shootingWith = 0;
		this.killersRoled = 0;
	}

	function initialize()
	{	

		switch(gameInfo.stage)
		{
			case 0: //Add Players
				addPlayers();
				break;			
			case 1: //Play to Start
				playToStart();
				break;
			case 2: //Play for Kill
				playForKill();
				break;		
			case 3: //Who to Kill 	
				targetPlayer();
				break;
			case 4: //playToKill
				playToKill();
				break;
		}
		
		printPlayers();
		printDice();
	}

	function startGame_onClick()
	{				
		if(arrPlayers.length > 1)
		{
			gameInfo.stage = 1;
			initialize();
		}
		else
		{
			alert('You need more players!');
		}
	}
	
	function addPlayers()
	{
		arrPlayers[0] = new objPlayer('Andrew');arrPlayers[1] = new objPlayer('Fred');arrPlayers[2] = new objPlayer('Matt');	
		divMenuBoard.innerHTML = '';
		changeScore_MenuItem();
		addPlayer_MenuItem();
		startGame_MenuItem();		
	}
	
	function playToStart()
	{
		gameInfo.playerId = Math.floor(((Math.random()* arrPlayers.length)));
		alert(arrPlayers[gameInfo.playerId].name + ' Starts!');
		gameInfo.stage = 2;
		initialize();
	}
		
	function playForKill()
	{	
		divMenuBoard.innerHTML = '';
		RoleDice_MenuItem(); //Prints 'Role Dice' Button. Found in menu.js
		
		if(diceCount() >= 5)
		{			
		printDice();
			if (shootingWith() >= 1)	
			{
				alert('Nice One, You are Shooting With ' + shootingWith());
				gameInfo.shootingWith = shootingWith();
				gameInfo.stage = 3;				
			}
			else
			{			
				arrPlayers[gameInfo.playerId].score = arrPlayers[gameInfo.playerId].score + shootingWith();
				
				if (shootingWith() == 0)
				{
					alert('Could Be Worse. You Scored ' + shootingWith());
				}
				else
				{
					alert('Unlucky. You Scored ' + shootingWith());
				}

				nextPlayer();
				resetInfo();
			}
			
		clearDice();
		initialize();
		}	
	}
	
	function targetPlayer()
	{
		if(diceCount() == 0) // If no dice have been selected then it is the start of the round. dice.js
		{
			if(arrPlayers[gameInfo.playerId].killers >= 1)
			{
				gameInfo.useKiller = confirm('Would you like to use your killer?');
				if(gameInfo.useKiller)
				{
					arrPlayers[gameInfo.playerId].killers = arrPlayers[gameInfo.playerId].killers - 1;
				}
			}
			printPlayers();
		}
		gameInfo.stage = 4;
		initialize();
		targetSelect_MenuItem();
		clearDice();
	}
	
	function playToKill()
	{
		RoleKiller_MenuItem();
	}

	function changeScore_onClick()
	{
		var cs = prompt('Change Score',25 );
		if (isNaN(cs)==false)
		{
			for(i in arrPlayers)
			{
				arrPlayers[i].score = parseInt(cs);	
			}
			printPlayers();
		}
	}	
	function targetPlayer_onClick(id)
	{
		gameInfo.playerTarget = id;
		RoleKiller_MenuItem()
	}
	
	function dice_onClick(id)
	{

		if(arrDice[id].isSelected != true)
		{
			gameInfo.played = true;
			arrDice[id].isSelected = true;		
		}
		//else
		//{
		//	var f = false;
		//	for(i in arrDice)
		//	{
		//		if(arrDice[i].isSelected){ gameInfo.played=false;}
		//	}
			
	//		arrDice[id].isSelected = false;		
		//}
		
		initialize();
	}
	
	function RoleDice_onClick()
	{
		if(gameInfo.played)// equals true
		{
			populateDice();
			gameInfo.played = false;
		}		
		printDice();
		initialize();dashBoard();
	}	

	function RoleKiller_onClick()
	{
		populateDice();
		
		var killers = checkKiller();
		
		printKillers();

		if(killers > gameInfo.killersRoled && killers <= 5)
		{
			gameInfo.killersRoled = killers;
			alert('Well Done you have rolled a killer');			
		}
		else if(killers != 0 && killers == gameInfo.killersRoled)
		{
			var Score = (killers*gameInfo.shootingWith);
			if(gameInfo.useKiller){Score = Score * 2}
			arrPlayers[gameInfo.playerTarget].score = arrPlayers[gameInfo.playerTarget].score - Score;
			
			
			if(arrPlayers[gameInfo.playerTarget].killers > 0 && arrPlayers[gameInfo.playerTarget].score <= 0)
			{
				arrPlayers[gameInfo.playerId].killers = arrPlayers[gameInfo.playerId].killers + arrPlayers[gameInfo.playerTarget].killers;
				arrPlayers[gameInfo.playerTarget].killers = 0;
			}
			
			nextPlayer();
			gameInfo.stage = 2;
			resetInfo();
			clearDice();
			initialize();
		
		}
		else
		{
			alert('Sorry you didnt Role a Killer');
			nextPlayer();
			gameInfo.stage = 2;
			resetInfo();
			clearDice();
			initialize();
		}		
	}

	function resetInfo()
	{
		gameInfo.playerTarget = '';
		gameInfo.played = true;
		gameInfo.shootingWith = 0;
		gameInfo.killersRoled = 0;		
	}
	function resetGame()
	{
		gameInfo.playerId = 0;
		gameInfo.playerTarget = '';
		gameInfo.played = true;
		gameInfo.shootingWith = 0;
		gameInfo.killersRoled = 0;
		gameInfo.stage = 0;
		gameInfo.useKiller = false;	

		clearDice();
//		arrDice = [];
			
			resetPlayers();

			initialize();
		
	}

	function resetPlayers()
	{
		for(i in arrPlayers)	
		{
			arrPlayers[i].score = gameInfo.defaultScore;
			arrPlayers[i].killers = 1;
		}
	}
	

	function dashBoard()
	{
		var divdashBoard = document.getElementById('divDashBoard');		divdashBoard.innerHTML="";

		var textNode;
					
		textNode = document.createTextNode('Combined Value: ' + diceTotal());
		var pCombinedValue = document.createElement('p').appendChild(textNode);
		var liCombinedValue = document.createElement('li')
		liCombinedValue.appendChild(pCombinedValue);
		
		textNode = document.createTextNode('Shooting With: ' +  shootingWith());
		var pShootWith = document.createElement('p').appendChild(textNode);
		var liShootWith = document.createElement('li')
		liShootWith.appendChild(pShootWith);
		
		var ulThrowInfo = document.createElement('ul');
		ulThrowInfo.appendChild(liShootWith);
		ulThrowInfo.appendChild(liCombinedValue);
		
		divdashBoard.appendChild(ulThrowInfo);		
	}


	initialize();