////////////////////////
// Tiina Vuorenmaa
// MM3322: Multi User Authoring
// MUABackgammon
// Started: 1/14/2010
// Updated: 3/25/2010

//unnamed package
package 
{
	//to use global items from flash, need to import
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	import flash.events.KeyboardEvent;
	import flash.display.MovieClip;
	import flash.net.NetConnection;
	import flash.net.SharedObject;
	import flash.events.SyncEvent;
	import flash.events.Event;
	import flash.events.NetStatusEvent;
	import flash.text.StyleSheet;

	//constructor for class *needs the same name as filename
	public class BGMain extends MovieClip
	{
		////////////////////////////////// VARIABLES //////////////////////////////
		
		//declare variables at the top (unless its a loop variable)
		//try to use private variables
		//member variables should have "m_" in front
		
		///////DEBUGGING/////////////////
		//to help bypass stuff and check end of game stuff
		private var DEBUG:Boolean = true;
		
		//variables for connecting
		private var m_netConn:NetConnection = new NetConnection();
		//at home
		private var m_netConnServerURL:String = "rtmp://localhost/tvuorenmaa";
		//at school
		//private var m_netConnServerURL:String = "rtmp://10.229.23.23/tvuorenmaa";
		private var m_sharedEnterGameObj:SharedObject;
		private var m_sharedGamePlayerObj:SharedObject;
		private var m_sharedPassedTurnObj:SharedObject;
		private var m_sharedDiceObj:SharedObject;
		private var m_sharedChipsObj:SharedObject;
		private var m_sharedOnBoardObj:SharedObject;
		private var m_sharedTurnObj:SharedObject;
		private var m_sharedBlotObj:SharedObject;
		private var m_playAgainObj:SharedObject;
		private var m_playerNamesObj:SharedObject;
		
		
		//the player number (1 or 2)
		private var m_thisPlayer:String; 
		private var m_thisPlayerName:String; 
		private var m_playerOnesName:String; 
		private var m_playerTwosName:String;
		private var m_playerColor:String;
		private var m_thisPlayerColor:String; 
		private var m_thisUserNum:Number; 
		

		//the dice
		private var m_dieOneNumber:Number = new Number();
		private var m_dieTwoNumber:Number = new Number();
		
		//temporary variables for chips moving
		//chips starting/ending spot for when moving the chips around (dragging)
		private var m_chipStartX:Number = new Number();
		private var m_chipStartY:Number = new Number();
		private var m_chipEndX:Number = new Number();
		private var m_chipEndY:Number = new Number();
		private var m_currentChip:Array = new Array();
		private var m_blotChip:Array = new Array();
		
		//the number of the space that the chip was picked up/landed on
		private var m_landedOnSpace:Number = new Number();
		private var m_pickedUpSpace:Number = new Number();
		private var m_distanceMoved:Number = new Number();

		//the number of the location on the space that the chip was picked up/landed on
		private var m_landedOnLocation:Number = new Number();
		private var m_pickedUpLocation:Number = new Number();

		//chip location array
		//gives only the location of the y coordinate since
		//m_chipLocationArray[0] = not there
		//m_chipLocationArray[1-5] = level one 0, -45, -90, -135, -180,
		//m_chipLocationArray[6-9] = level two -22.5, -67.5, -112.5, -157.5,
		//m_chipLocationArray[10-12] = level three -45, -90, -135,
		//m_chipLocationArray[13-14] = level four -67.5, -112.5,
		//m_chipLocationArray[15] = level five -90
		//be sure to add the chip to the stage each time it is moved
		private var m_chipWidth:Number = 36.95;
		private var m_chipsLocationArray:Array;
		private var m_chipsHomeYLocationArray:Array;
		private var m_chipsHomeXLocationArray:Array;

		//gives a position to be used in the m_chipsLocationArray
		private var m_playerOneChipsFirstPositionArray:Array = [1,2,1,2,3,4,5,1,2,3,1,2,3,4,5];
		private var m_playerTwoChipsFirstPositionArray:Array = [1,2,1,2,3,4,5,1,2,3,1,2,3,4,5];

		//shows the spaces where the chips are
		//the numbers are the first positions
		//0 is blotspace2
		//25 is blotspace1
		//26 is bear off the board for player one
		//27 is bear off the board for player  two
		//1-24 are spaces
		//this relates to the
		private var m_playerOneChipsFirstSpaceArray:Array = [24,24,13,13,13,13,13,8,8,8,6,6,6,6,6];
		private var m_playerTwoChipsFirstSpaceArray:Array = [1,1,12,12,12,12,12,17,17,17,19,19,19,19,19];
		
		//this array keeps track of whats on the board
		//0 is blotspace2 for player two
		//25 is blotspace1 for player one
		//26 is bear off the board for player one
		//27 is bear off the board for player  two
		//1-24 are spaces
		//will be multi-dimensional
		//m_onBoardArray[ player1 spacearray number] [ player1 position array number] = player1 chip movie clip
		//this way I can push and pop with the temporary variables above
		private var m_onBoardArray:Array = new Array();

		//keeping track of things
		
		//let flash know when to drop the chip
		private var m_pickedUpChip:Boolean = false;
		private var m_blotIsHit:Boolean = false;
		private var m_chipCanBeMoved:Boolean = false;
		private var a_playerBlotSpace:Number;
		private var m_correctMove:Boolean = false;
		private var m_chipIsHome:Boolean = false;
		private var m_gameOver:Boolean = false;
		
		//boolean on which die has been used
		private var m_dieOneUsed:Boolean;
		private var m_dieTwoUsed:Boolean;
		
		
		//kinda like a boolean, but used as a string to also help with the arrays
		//strings are "playerOne" or "playerTwo"
		private var m_currentPlayer:String;
		private var m_currentPlayerString:String;
		private var m_playersAreSet:Boolean;
		
		//for doubles roll
		private var m_doubles:Boolean;
		
		//if the player wants to play again, let the other player know
		private var m_playAgainHit:Boolean = false;
		
		//if the player passes their turn, let the other player know
		private var m_turnPassed:Boolean = false;
		
		//not a boolean, but keeps track of how many moves were made
		private var m_numberOfMoves:Number = 2;
		
		//variables used connecting with server and sending info only
		//in setCurrentPlayer
		private var a_turnPassedString:String;
		
		//in sendOnBoardInfo
		private var p2blot_landedOnSpace:Number;
		private var p2blot_pickedUpSpace:Number;
		private var p1blot_landedOnSpace:Number;
		private var p1blot_pickedUpSpace:Number;
		private var chip_pickedUpSpace:Number;
		private var chip_landedOnSpace:Number;
		
		//moving blot info
		private var a_currentBlotName:String;
		//moving chip info
		private var a_currentChipName:String;
		
		//fun stuff
		private var m_playerNameStyle:StyleSheet = new StyleSheet();
		


		////////////////////////////////// FUNCTIONS //////////////////////////////

		//constructor function *also same name as filename
		public function BGMain():void
		{		
			connectToServer();
			makeItPretty();
		}
		////MAKE IT PRETTY FUNCTIONS
		
		function makeItPretty():void
		{
			m_playerNameStyle.parseCSS(".playerOne {color: #415262;}");
			m_playerNameStyle.parseCSS(".playerTwo {color: #BA4B30;}");
			playersName.styleSheet = m_playerNameStyle;
			messageBox.styleSheet = m_playerNameStyle;

		}
		
		function showRollDiceButton():void
		{
			coverRollDice.x = 904;
		}
		
		function hideRollDiceButton():void
		{
			coverRollDice.x = 704;
		}
		
		function showDieOne():void
		{
			coverDieOne.x = 963;
		}
		
		function hideDieOne():void
		{
			coverDieOne.x = 663;
		}
		
		function showDieTwo():void
		{
			coverDieTwo.x = 1044;
		}
		
		function hideDieTwo():void
		{
			coverDieTwo.x = 744;
		}
		
		function showPassButton():void
		{
			coverPass.x = 1003;
		}
		
		function hidePassButton():void
		{
			coverPass.x = 703;
		}
		
		function showArrow():void
		{
			if(m_thisPlayer == "playerOne")
			{
				playerOneArrow.alpha = 0.8;
			}
			
			else
			{
				playerTwoArrow.alpha = 0.8;
			}
			
		}
		
		function hideArrow():void
		{
			playerOneArrow.alpha = 0;
			playerTwoArrow.alpha = 0;
		}
		
		function showIndicatorChip():void
		{
			if(m_currentPlayer == "playerOne")
			{
				playerOneIndicatorChip.alpha = 1;
				playerTwoIndicatorChip.alpha = 0;
			}
			
			else
			{
				playerOneIndicatorChip.alpha = 0;
				playerTwoIndicatorChip.alpha = 1;
			}
			
		}
		
		
		function connectToServer():void
		{
			//connecting
			m_netConn.connect(m_netConnServerURL);
			//checking if the connection is good
			m_netConn.addEventListener(NetStatusEvent.NET_STATUS, checkStatus);	
		}

		function moveRulesUp(event:MouseEvent):void
		{
			stage.addChild(gameRules);
			gameRules.arrowButton.rotation = 0;
			gameRules.y -=215;
			gameRules.removeEventListener(MouseEvent.CLICK,moveRulesUp);
			gameRules.addEventListener(MouseEvent.CLICK,moveRulesDown);
			
		}
		
		function moveRulesDown(event:MouseEvent):void
		{
			stage.addChild(ruleCover);
			gameRules.y +=215;
			gameRules.arrowButton.rotation = 180;
			gameRules.addEventListener(MouseEvent.CLICK,moveRulesUp);
			gameRules.removeEventListener(MouseEvent.CLICK,moveRulesDown);
		}
		
		function checkStatus(event:NetStatusEvent):void
		{
			trace(event.info.code);
			switch(event.info.code)
			{
				case "NetConnection.Connect.Success":
					trace("success");
					//creating the shared object
					m_sharedEnterGameObj = SharedObject.getRemote("enterGameSherie",m_netConn.uri,false);
					//connecting the shared object to the net connections
					m_sharedEnterGameObj.connect(m_netConn);
					m_sharedEnterGameObj.addEventListener(SyncEvent.SYNC, openGame);
					
					break;
				case "NetConnection.Connect.Rejected":
				case "NetConnection.Connect.Failed":
					trace("oops, try again!");
					break;
				
			}//end switch

		}
		
		function openGame(event:SyncEvent):void
		{
		
			startScreen.x = 395;
			startScreen.y = 165;
			//rules button
			gameRules.buttonMode = true;
			gameRules.arrowButton.alpha = 1;
			gameRules.addEventListener(MouseEvent.CLICK,moveRulesUp);
			
			switch(event.changeList[0].code)
			{
				case "clear":
					trace("code clear");
					//startScreen.startScreenText.appendText("Connected to SO (clear). " + "\n");
					
					if(!m_sharedEnterGameObj.data.numOfPlayersWaiting)
					{
						m_sharedEnterGameObj.setProperty("numOfPlayersWaiting", 1);				
					}
					else
					{
						m_thisUserNum =  m_sharedEnterGameObj.data.numOfPlayersWaiting+1;
						m_sharedEnterGameObj.setProperty("numOfPlayersWaiting", m_thisUserNum);
						
					}
					//startScreen.startScreenText.appendText(" User Waiting: " + String(m_sharedEnterGameObj.data.numOfPlayersWaiting) + "\n");
					showWelcomeMessage();
					break;
					
				case "success":
					//startScreen.startScreenText.appendText(" You changed the SO (success). " + "\n");
					startScreen.startScreenWaitText.text= "Number of Users Waiting: " + String(m_sharedEnterGameObj.data.numOfPlayersWaiting) + "\n";
					showWaitingMessage();
					break;
					
				case "change":
					//startScreen.startScreenText.appendText(" Someone else changed the SO (change). " + "\n");
					startScreen.startScreenWaitText.text= "Number of Users Waiting: " + String(m_sharedEnterGameObj.data.numOfPlayersWaiting) + "\n";
					showWaitingMessage();
					break;			
			}		
		}//end function open game
		
		function showWelcomeMessage():void
		{	
			startScreen.startScreenText.text= " Looking for 2 players " + "\n";
			startScreen.enterGameButton.addEventListener(MouseEvent.CLICK, mouseEnterGame);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyboardEnterGame);

		}

		function mouseEnterGame(event:MouseEvent):void
		{
			startScreen.enterGameButton.removeEventListener(MouseEvent.CLICK, mouseEnterGame);
			enterTheGame();
		}
		
		function keyboardEnterGame(event:KeyboardEvent):void
		{
			if (event.keyCode == Keyboard.ENTER)
   			{
       			 enterTheGame();
   			}
		}
		
		function enterTheGame():void
		{		
			if(!m_sharedEnterGameObj.data.numOfPlayersPlaying)
			{
				m_sharedEnterGameObj.setProperty("numOfPlayersPlaying", 1);
				m_thisPlayer = "playerOne";
				//m_thisPlayerNum =  m_sharedEnterGameObj.data.numOfPlayersPlaying;
				startScreen.startScreenText.text= " You are " + m_thisPlayer + "\n";
			}
			else
			{
				m_thisPlayer = "playerTwo";
				//m_thisPlayerNum =  m_sharedEnterGameObj.data.numOfPlayersPlaying+1;
				m_sharedEnterGameObj.setProperty("numOfPlayersPlaying", 2);
				startScreen.startScreenText.text= " You are " + m_thisPlayer + "\n";
			}
		}


		function showWaitingMessage():void
		{
			if(!m_sharedEnterGameObj.data.numOfPlayersPlaying)
			{
				//do nothing still waiting			
			}
			else if(m_sharedEnterGameObj.data.numOfPlayersPlaying == 1 && m_thisPlayer == "playerOne" )
			{
				//waiting for another player
				startScreen.startScreenPlayText.text= "Number of Users Playing: " + String(m_sharedEnterGameObj.data.numOfPlayersPlaying) + "\n";
				startScreen.startScreenText.text= "Waiting for another player to play. " ;
						
			}
			else if(m_sharedEnterGameObj.data.numOfPlayersPlaying == 2 && m_thisPlayer == "playerTwo")
			{
				startScreen.startScreenPlayText.text= "Number of Users Playing: " + String(m_sharedEnterGameObj.data.numOfPlayersPlaying) + "\n";
				startScreen.startScreenText.text= "Joining In! Let's Play! " ;
				//m_currentPlayer = "playerTwo";
				startGame();
			}
			else if(m_sharedEnterGameObj.data.numOfPlayersPlaying == 2 && m_thisPlayer == "playerOne" )
			{
				startScreen.startScreenPlayText.text= "Number of Users Playing: " + String(m_sharedEnterGameObj.data.numOfPlayersPlaying) + "\n";
				startScreen.startScreenText.text= "Found another player! Let's Play!" ;
				//m_currentPlayer = "playerOne";
				startGame();
			}

			else if(m_sharedEnterGameObj.data.numOfPlayersPlaying == 1)
			{
				//looking for another player
						
				startScreen.startScreenPlayText.text= "Number of Users Playing: " + String(m_sharedEnterGameObj.data.numOfPlayersPlaying) + "\n";
				startScreen.startScreenText.text= "Looking for another player to play." ;
						
			}
			else 
			{
				startScreen.startScreenPlayText.text= "Number of Users Playing: " + String(m_sharedEnterGameObj.data.numOfPlayersPlaying) + "\n";
				startScreen.startScreenText.text= "Game in Progress. Try Again Later! " ;
				//startScreen.enterGameButton.removeEventListener(MouseEvent.CLICK, enterGame);
				startScreen.enterGameButton.addEventListener(MouseEvent.CLICK, mouseEnterGame);
				stage.addEventListener(KeyboardEvent.KEY_DOWN, keyboardEnterGame);

			}
		}


		function setUpBoard():void
		{
			//setup the location arrays
			m_chipsLocationArray = new Array();
			m_chipsHomeYLocationArray = new Array();
			m_chipsHomeXLocationArray = new Array();
			//put in the first 5 spaces
			for (var m:int = 0; m < 6; m++)
			{
				m_chipsLocationArray[m] = 0 - (m_chipWidth*(m-1));
			}
			//setup the rest of the spaces
			m_chipsLocationArray[6] = -1*(m_chipWidth * .5);
			m_chipsLocationArray[7] = m_chipsLocationArray[13] = -1*(m_chipWidth * 1.5);
			m_chipsLocationArray[8] = m_chipsLocationArray[14] = -1*(m_chipWidth * 2.5);
			m_chipsLocationArray[9] = -1*(m_chipWidth * 3.5);
			m_chipsLocationArray[10] = m_chipsLocationArray[2];
			m_chipsLocationArray[11] = m_chipsLocationArray[15] = m_chipsLocationArray[3];
			m_chipsLocationArray[12] = m_chipsLocationArray[4];
			
			//set up the locations for the home arrays
			for (var n:int = 0; n < 16; n++)
			{
				m_chipsHomeYLocationArray[n] = 0 - (m_chipWidth*(0.5*(n-1)));
				m_chipsHomeXLocationArray[n] = 0 + ((-1)^n)*(m_chipWidth*0.3);
			}
			
			for (var t:int = 0; t < 28; t++)
			{
				//there is nothing on the triangles at the moment
				//create a multi-dimensional array
				m_onBoardArray[t] = new Array();
				//first spot on each "space" is 0, so if length is 1, then nothing is there.
				//rest of the spots will be movie clips
				m_onBoardArray[t] [0] = 0;
			}

			// add chips to board
			for (var c:int = 0; c < 15; c++)
			{
				//add Player ONE chips to the board
				var a_playerOneChip:PlayerOneChip = new PlayerOneChip  ;
				//assign name to chip for reference?
				a_playerOneChip.name = "playerOneChip_" + c.toString();
				
				//location of the chip
				//trace("here");
				//x position
				a_playerOneChip.x = getChildByName("space" + m_playerOneChipsFirstSpaceArray[c]).x;
				//trace(a_playerOneChip.x);
				
				//y position
				if (m_playerOneChipsFirstSpaceArray[c] < 13)
				{
					a_playerOneChip.y = getChildByName("space" + m_playerOneChipsFirstSpaceArray[c]).y + m_chipsLocationArray[m_playerOneChipsFirstPositionArray[c]];
				}
				else
				{
					a_playerOneChip.y = getChildByName("space" + m_playerOneChipsFirstSpaceArray[c]).y - m_chipsLocationArray[m_playerOneChipsFirstPositionArray[c]] + a_playerOneChip.height;
				}
				//trace(a_playerOneChip.height);
				
				//assign chip movieclips to the onBoard array
				m_onBoardArray [m_playerOneChipsFirstSpaceArray[c]] [m_playerOneChipsFirstPositionArray[c]] = a_playerOneChip;
				//m_playerOneChipsMCArray[c] = a_playerOneChip;
				
				//add the chip to the stage
				stage.addChild(a_playerOneChip);
				


				//add Player TWO chips to the board
				var a_playerTwoChip:PlayerTwoChip = new PlayerTwoChip  ;
				//assign name to chip for reference?
				a_playerTwoChip.name = "playerTwoChip_" + c.toString();
				
				//location of the chip
				//trace("here");
				//x position
				a_playerTwoChip.x = getChildByName("space" + m_playerTwoChipsFirstSpaceArray[c]).x;
				//trace(a_playerTwoChip.x);
				
				//y position
				if (m_playerTwoChipsFirstSpaceArray[c] < 13)
				{
					a_playerTwoChip.y = getChildByName("space" + m_playerTwoChipsFirstSpaceArray[c]).y + m_chipsLocationArray[m_playerTwoChipsFirstPositionArray[c]];
				}
				else
				{
					a_playerTwoChip.y = getChildByName("space" + m_playerTwoChipsFirstSpaceArray[c]).y - m_chipsLocationArray[m_playerTwoChipsFirstPositionArray[c]] + a_playerTwoChip.height;
				}
				//trace(a_playerTwoChip.y);
				
				//assign chip movieclips to the onboard array
				m_onBoardArray [m_playerTwoChipsFirstSpaceArray[c]] [m_playerTwoChipsFirstPositionArray[c]] = a_playerTwoChip;
				//m_playerTwoChipsMCArray[c] = a_playerTwoChip;
				//add the chip to the stage
				stage.addChild(a_playerTwoChip);

			}

			
		}//end function set up board
		
		function getPlayerNames():void
		{
			//startScreen.inputPlayersName.text.replace("\n", "");
			//startScreen.inputPlayersName.text.replace(" ", "");
			if(startScreen.inputPlayersName.text == "")
			{
				m_thisPlayerName = "BG Noob";
			}
			else
			{
				m_thisPlayerName = startScreen.inputPlayersName.text;
				
			}

			//show the players name
			switch(m_thisPlayer)
			{
				case "playerOne":
					//var a_playerOneIndicatorChip:PlayerOneChip = new PlayerOneChip;
					//a_playerOneIndicatorChip.x = 622;
					//a_playerOneIndicatorChip.y = 85;
					
					//addChild(a_playerOneIndicatorChip);			
					m_playerOnesName = "" + m_thisPlayerName + "";
					m_playerColor = "blue";
					m_thisPlayerColor =  "" + m_playerColor + "";
					playersName.htmlText = "Hi " + m_playerOnesName + "," + "\n" + "You are Player One.";
					//send the info to the server
					
					m_playerNamesObj.setProperty("playerOneName", m_playerOnesName);
					deBug.text = " |sent name info";
					
				break;
				case "playerTwo":
					//var a_playerTwoIndicatorChip:PlayerTwoChip = new PlayerTwoChip;
					//a_playerTwoIndicatorChip.x = 622;
					//a_playerTwoIndicatorChip.y = 85;
					
					//addChild(a_playerTwoIndicatorChip);
					m_playerTwosName = "" + m_thisPlayerName + "";
					m_playerColor = "red";
					m_thisPlayerColor =  "" + m_playerColor + "";
					playersName.htmlText = "Hi " + m_playerTwosName + "," + "\n" + "You are Player Two.";
					//send the info to the server
					
					m_playerNamesObj.setProperty("playerTwoName", m_playerTwosName);
					deBug.text = " |sent nameinfo";
					break;
				default:
			}//end switch
		}

		function setUpConnections():void
		{
			//clear old sharedobj for this swf
			m_sharedEnterGameObj.removeEventListener(SyncEvent.SYNC, openGame);
			
			//creating the shared object for the current player
			m_sharedGamePlayerObj = SharedObject.getRemote("GamePlayerSherie",m_netConn.uri,false);
			//connecting the shared object to the net connections
			m_sharedGamePlayerObj.connect(m_netConn);		
			m_sharedGamePlayerObj.addEventListener(SyncEvent.SYNC, setCurrentPlayer);
			
			//creating the shared object for the pass turn button
			m_sharedPassedTurnObj = SharedObject.getRemote("PassedTurnSherie",m_netConn.uri,false);
			//connecting the shared object to the net connections
			m_sharedPassedTurnObj.connect(m_netConn);		
			m_sharedPassedTurnObj.addEventListener(SyncEvent.SYNC, sendPassedTurnInfo);
			
			
			//creating the shared object for the dice
			m_sharedDiceObj = SharedObject.getRemote("DiceSherie",m_netConn.uri,false);
			//connecting the shared object to the net connections
			m_sharedDiceObj.connect(m_netConn);
			m_sharedDiceObj.addEventListener(SyncEvent.SYNC, sendDiceInfo);
			
			//creating the shared object for the turns
			m_sharedTurnObj = SharedObject.getRemote("TurnSherie",m_netConn.uri,false);
			//connecting the shared object to the net connections
			m_sharedTurnObj.connect(m_netConn);
			m_sharedTurnObj.addEventListener(SyncEvent.SYNC, sendTurnInfo);
			
			
			//creating the shared object for the chips
			m_sharedChipsObj = SharedObject.getRemote("ChipsSherie",m_netConn.uri,false);
			//connecting the shared object to the net connections
			m_sharedChipsObj.connect(m_netConn);
			m_sharedChipsObj.addEventListener(SyncEvent.SYNC, showChipMoving);
			

			//creating the shared object for the onBoardArray
			m_sharedOnBoardObj = SharedObject.getRemote("OnBoardSherie",m_netConn.uri,false);
			//connecting the shared object to the net connections
			m_sharedOnBoardObj.connect(m_netConn);
			m_sharedOnBoardObj.addEventListener(SyncEvent.SYNC, sendOnBoardInfo);
			
			//creating the shared object for the onBoardArray
			m_sharedBlotObj = SharedObject.getRemote("BlotSherie",m_netConn.uri,false);
			//connecting the shared object to the net connections
			m_sharedBlotObj.connect(m_netConn);
			m_sharedBlotObj.addEventListener(SyncEvent.SYNC, showBlotMoving);
			
			//creating the shared object for the onBoardArray
			m_playAgainObj = SharedObject.getRemote("playAgainSherie",m_netConn.uri,false);
			//connecting the shared object to the net connections
			m_playAgainObj.connect(m_netConn);
			m_playAgainObj.addEventListener(SyncEvent.SYNC, sendPlayAgainInfo);
			
			//creating the shared object for the onBoardArray
			m_playerNamesObj = SharedObject.getRemote("playerNamesSherie",m_netConn.uri,false);
			//connecting the shared object to the net connections
			m_playerNamesObj.connect(m_netConn);
			m_playerNamesObj.addEventListener(SyncEvent.SYNC, sendPlayerNames);

		}

		function startGame():void
		{
			setUpConnections();
			
			deBug.text = "start";
			setUpBoard();
			
			//remove the start boxes
			startScreen.x = -1000;
			
			//dont move chips until the dice are rolled
			dontMoveChips();
			makeItPretty();
			hidePassButton();
			hideArrow();

		}//end start game
		
		function sendPlayerNames(event:SyncEvent):void
		{
			switch(event.changeList[0].code)
			{
				case "clear":
					//do nothing
					deBug.appendText(" |clear name");
					getPlayerNames();
					break;
				
				case "success":
					deBug.appendText(" |success name");
					//great. do nothing

					break;
					
				case "change":
					deBug.appendText(" |change name");
					if(m_playerNamesObj.data.playerOneName && m_thisPlayer == "playerTwo")
					{
						m_playerOnesName = m_playerNamesObj.data.playerOneName;
						playersName.htmlText = "Hi " + m_playerTwosName + ", " + "\n" + "You are playing against " + m_playerOnesName;
						m_currentPlayer = "playerOne";
						m_currentPlayerString = m_playerOnesName;
						showMessage("rollDice");

					}
					if(m_playerNamesObj.data.playerTwoName && m_thisPlayer == "playerOne")
					{
						m_playerTwosName = m_playerNamesObj.data.playerTwoName;
						playersName.htmlText = "Hi " + m_playerOnesName + ", " + "\n" + "You are playing against " + m_playerTwosName;
						m_currentPlayer = "playerOne";
						m_currentPlayerString = m_playerOnesName;
						showMessage("rollDice");

					}
					
					
					break;
					
				default:
			}//end switch
			
		}//end function send name info
		
		function setCurrentPlayer(event:SyncEvent):void
		{
			switch(event.changeList[0].code)
			{
				case "clear":
					//do nothing
					//deBug.appendText(" clear plyr");
					if(!m_sharedGamePlayerObj.data.currentPlayer)
					{
						//deBug.appendText(" |setting plyr");
						//m_sharedGamePlayerObj.setProperty("currentPlayer", "playerOne");
					}
					else
					{
						//deBug.appendText(" |plyr already set");
						//m_sharedGamePlayerObj.setDirty("currentPlayer");
						
					}
					
					
				break;
				
				case "success":
					//deBug.appendText(" |success plyr");
					//shows the message on this swf 
					showMessage("rollDice");
					
					break;
				case "change":
					deBug.appendText(" |change plyr");
					
					//changes the turn passed boolean, but no change in message
					a_turnPassedString = m_sharedGamePlayerObj.data.turnPassed;
					switch(a_turnPassedString)
					{
						case "true":
							m_turnPassed = true;
							break;
						case "false":
							m_turnPassed = false;
							break;
						default:
							//m_turnPassed = false;
					}
					
					m_currentPlayer = m_sharedGamePlayerObj.data.currentPlayer;						
					switch(m_currentPlayer)
					{
						case "playerOne":
						m_currentPlayerString = m_playerOnesName;
						
						
						//sends the message to the other player only when the player changes
						showMessage("rollDice");
						break;
				
						case "playerTwo":
						m_currentPlayerString = m_playerTwosName;
						
						
						//sends the message to the other player only when the player changes
						showMessage("rollDice");
						break;
					
						default:
					}//end switch
					
					
					break;
				default:
			}//end switch
			
		}
		
		
		function sendPlayAgainInfo(event:SyncEvent):void
		{
			switch(event.changeList[0].code)
			{
				case "clear":
					//do nothing
					
				break;
				
				case "success":
					deBug.appendText(" |success playAgain");
					//deBug.appendText(" |"+ m_playAgainObj.data.playAgain);
					//check who asked to play again
					if(m_playAgainObj.data.playAgain && m_playAgainObj.data.playAgain == "meToo")
					{
						playAgainScreen.x = 4000;
						playAgainScreen.y = 3050;
						//m_playAgainHit = false;
						setUpBoard();
						//switchTurns();
						m_playAgainObj.setProperty("playAgain", "playingAgain");
					}
					
					break;
				case "change":
					deBug.appendText(" |change playAgain");
					
					if(m_playAgainObj.data.playAgain && m_playAgainObj.data.playAgain == "yes")
					{
						playAgainScreen.playAgainText.text = "The other player wants to play again. You?";

					}
					else if(m_playAgainObj.data.playAgain && m_playAgainObj.data.playAgain == "meToo")
					{
						playAgainScreen.x = 4000;
						playAgainScreen.y = 3050;
						//m_playAgainHit = false;
						setUpBoard();
						switchTurns();
						m_playAgainObj.setProperty("playAgain", "playingAgain");
					}
					break;
				default:
			}//end switch
			
		}
		
		function sendPassedTurnInfo(event:SyncEvent):void
		{
			switch(event.changeList[0].code)
			{
				case "clear":
					//do nothing
					break;
				
				case "success":
					//deBug.appendText(" |success passed");
					break;
					
				case "change":
					//deBug.appendText(" |change passed");
					
					//changes the turn passed boolean, but no change in message
					var a_turnPassedString = m_sharedPassedTurnObj.data.turnPassed;
					switch(a_turnPassedString)
					{
						case "true":
							m_turnPassed = true;
							passButton.removeEventListener(MouseEvent.CLICK,passTheTurn);
							hidePassButton();
							showMessage("RollDice");
							break;
						case "false":
							m_turnPassed = false;
							
							break;
						default:
							//m_turnPassed = false;
					}
					
					break;
				default:
			}//end switch
			
		}
		
		function sendTurnInfo(event:SyncEvent):void
		{
			switch(event.changeList[0].code)
			{
				case "clear":
					//do nothing
					//deBug.appendText(" clear turn");
					break;
				
				case "success":
					//deBug.appendText(" |success turn");
					if(!m_sharedTurnObj.data.distanceMoved)
					{
						//deBug.appendText(" |no distance info");
						//m_sharedTurnObj.setProperty("distanceMoved", m_distanceMoved);
					}
					
					if(!m_sharedTurnObj.data.numberOfMoves)
					{
						//deBug.appendText(" |no moves info");
						//m_sharedTurnObj.setProperty("numberOfMoves", m_numberOfMoves);
					}
					
					if(!m_sharedTurnObj.data.gameOver)
					{
						//deBug.appendText(" |no gameover info");
						//m_sharedTurnObj.setProperty("numberOfMoves", m_numberOfMoves);
					}
					break;
					
				case "change":
					//deBug.appendText(" |change turn");
					if(m_sharedTurnObj.data.numberOfMoves && m_sharedTurnObj.data.distanceMoved)
					{
						m_numberOfMoves = m_sharedTurnObj.data.numberOfMoves;
						
						if(m_numberOfMoves > 0)
						{
							//deBug.appendText(" |" + m_numberOfMoves.toString());
							//deBug.appendText(" |turn info in");
							m_distanceMoved = m_sharedTurnObj.data.distanceMoved;
							showMessage("nextMove");
						}
					}
					else
					{
						//deBug.appendText(" |no nextturn info");
					}
					
					if(m_sharedTurnObj.data.gameOver && m_sharedTurnObj.data.gameOver == "gameOver")
					{
						showMessage("gameOver");
					}
					
					break;
					
				default:
			}//end switch
			
		}//end function send turn info
		
		
		function sendOnBoardInfo(event:SyncEvent):void
		{
			switch(event.changeList[0].code)
			{
				case "clear":
					//do nothing
					//deBug.appendText(" clear onbrd");
					break;
				
				case "success":
					deBug.appendText(" |success onbrd");
					break;
					
				case "change":
					deBug.appendText(" |change onbrd");

					if(m_sharedOnBoardObj.data.blot_pickedUpSpace && m_sharedOnBoardObj.data.blot_landedOnSpace)
					{
						//hitting a blot
						
						if(m_sharedOnBoardObj.data.blot_pickedUpSpace == 666)
						//if(m_sharedOnBoardObj.data.blot_pickedUpSpace == 666 || m_sharedOnBoardObj.data.blot_landedOnSpace == 666)
						{
							//do nothing
							deBug.appendText(" |nothing done to blot ");
						}
						else if (m_sharedOnBoardObj.data.blot_landedOnSpace == 5306)
						{
							deBug.appendText(" |blothitinfo ");
							deBug.appendText(" |blot_landedOnSpace = "+ m_sharedOnBoardObj.data.blot_landedOnSpace);
							deBug.appendText(" |blot_pickedUpSpace = "+ m_sharedOnBoardObj.data.blot_pickedUpSpace);
							//if the data is 5306, then its playertwo's blotspace2 = 0
							p2blot_landedOnSpace = 0;
							p2blot_pickedUpSpace = m_sharedOnBoardObj.data.blot_pickedUpSpace;
							//pop the blot and push it on the the right spot
							m_onBoardArray[0].push(m_onBoardArray[p2blot_pickedUpSpace].pop());
							
							//after it has been push/popped , then reset the sharedobj
							m_sharedOnBoardObj.setProperty("blot_pickedUpSpace", 666);
							m_sharedOnBoardObj.setProperty("blot_landedOnSpace", 666);
							
							//checking the m_onBoardArray
							deBug.appendText(" |blotat " + p2blot_pickedUpSpace);
							deBug.appendText(" |atblot 0= " + String((m_onBoardArray[0].length)-1));
							deBug.appendText(" |now at space"  + p2blot_pickedUpSpace + "is chip" + m_onBoardArray[p2blot_pickedUpSpace][m_onBoardArray[p2blot_pickedUpSpace].length-1]);
						}
						else
						{
							p1blot_landedOnSpace = m_sharedOnBoardObj.data.blot_landedOnSpace;
							p1blot_pickedUpSpace = m_sharedOnBoardObj.data.blot_pickedUpSpace;
							//pop the blot and push it on the the right spot
							m_onBoardArray[25].push(m_onBoardArray[p1blot_pickedUpSpace].pop());
							
							//after it has been push/popped , then reset the sharedobj
							m_sharedOnBoardObj.setProperty("blot_pickedUpSpace", 666);
							m_sharedOnBoardObj.setProperty("blot_landedOnSpace", 666);
							
							//checking the m_onBoardArray
							deBug.appendText(" |blotat " + p1blot_pickedUpSpace);
							deBug.appendText(" |atblot 25= " +String((m_onBoardArray[25].length)-1));
							deBug.appendText(" |now at space"  + p1blot_pickedUpSpace + " is chip" + m_onBoardArray[p1blot_pickedUpSpace][m_onBoardArray[p1blot_pickedUpSpace].length-1]);
							
						}
					}// end if blot is true
					

					if(m_sharedOnBoardObj.data.pickedUpSpace && m_sharedOnBoardObj.data.landedOnSpace)
					{
						if(m_sharedOnBoardObj.data.pickedUpSpace == 1000 || m_sharedOnBoardObj.data.landedOnSpace == 1000)
						{
							//do nothing
							deBug.appendText(" |nothing done to onboardArray");
						}
						//else if (m_sharedOnBoardObj.data.blot_pickedUpSpace == 666 && m_sharedOnBoardObj.data.blot_landedOnSpace == 666)
						else
						{
						
							deBug.appendText(" |changing onboardArray");
							//deBug.appendText(" |onbrd info ");
							chip_landedOnSpace = m_sharedOnBoardObj.data.landedOnSpace;
							chip_pickedUpSpace = m_sharedOnBoardObj.data.pickedUpSpace
							
							//deBug.appendText(" |move chip info ");
							m_onBoardArray[chip_landedOnSpace].push(m_onBoardArray[chip_pickedUpSpace].pop()) -1;
							//after it has been push/popped , then reset the sharedobj
							m_sharedOnBoardObj.setProperty("pickedUpSpace", 1000);
							m_sharedOnBoardObj.setProperty("landedOnSpace", 1000);
						}
					}
					break;
					
				default:
			}//end switch
			
		}//end function send on board info
		
		//this function changes info on this swf if info is sent from the server
		function showBlotMoving(event:SyncEvent):void
		{
			switch(event.changeList[0].code)
			{
				case "clear":
					//do nothing
					//deBug.appendText(" clear blot");
					
					break;
				case "success":
					//deBug.appendText(" |success blot");
					break;
				case "change":
					//deBug.appendText(" |change blot");
					if(m_sharedBlotObj.data.blotX && m_sharedBlotObj.data.blotY && m_sharedBlotObj.data.blotChipName)
					{
					
					// when a sync event is fired
					// update the position of the chip in clients
					a_currentBlotName = m_sharedBlotObj.data.blotChipName;
					m_blotChip[0] = stage.getChildByName(a_currentBlotName);
					m_blotChip[0].x = m_sharedBlotObj.data.blotX;
					m_blotChip[0].y = m_sharedBlotObj.data.blotY;
					}
					
					
					
					break;
				default:
			}//end switch
			
			
		}//end function show blot moving
		
		
		//this function changes info on this swf if info is sent from the server
		function showChipMoving(event:SyncEvent):void
		{
			switch(event.changeList[0].code)
			{
				case "clear":
					//do nothing
					//deBug.appendText(" |clear chip");
					
					break;
				case "success":
					//deBug.appendText(" |success chip");
					break;
				case "change":
					//deBug.appendText(" |change chip");
					if(m_sharedChipsObj.data.chipX && m_sharedChipsObj.data.chipY && m_sharedChipsObj.data.chipName)
					{
					//messageBox.appendText("currentchip[0] moved");
					// when a sync event is fired
					// update the position of the chip in clients
					a_currentChipName = m_sharedChipsObj.data.chipName;
					m_currentChip[0] = stage.getChildByName(a_currentChipName);
					m_currentChip[0].x = m_sharedChipsObj.data.chipX;
					m_currentChip[0].y = m_sharedChipsObj.data.chipY;
					}
					
					
					
					break;
				default:
			}//end switch
			
			
		}//end function sho Chip moving
		
		//this function is at the end of a turn
		//a turn is two correct moves of 4  correct double moves
		// or a player can pass
		function switchTurns():void
		{
			//switches whose turn it is
			//switch the players
			switch(m_currentPlayer)
			{
				case "playerOne":
					//sends the message to the other player
					m_sharedGamePlayerObj.setProperty("currentPlayer", "playerTwo");
					//changes the info on this swf
					m_currentPlayer = "playerTwo";
					m_currentPlayerString = m_playerTwosName;
					break;
					
				case "playerTwo":
					//sends the message to the other player
					m_sharedGamePlayerObj.setProperty("currentPlayer", "playerOne");
					//changes the info on this swf
					m_currentPlayer = "playerOne";
					m_currentPlayerString = m_playerOnesName;
					break;
				default:
			}
			

		}//end functions switch turns
		
		
		function showMessage(a_msg:String):void
		{
			switch(a_msg)
			{
				case "rollDice":
					
					if(m_currentPlayer == m_thisPlayer)
					{
						if(m_turnPassed)
						{
							messageBox.htmlText = "The other player passed. It's now your turn. Roll the Dice!";
						}
						else
						{
							messageBox.htmlText = m_currentPlayerString + ", it's your turn. Roll the Dice!";
						}
						rollButton.addEventListener(MouseEvent.CLICK,rollTheDice);
						showRollDiceButton();
						
						//hideDieOne();
						//hideDieTwo();
						textFlash.gotoAndPlay(1);
						
					}
			
					else
					{
						if(m_turnPassed)
						{
							messageBox.htmlText = "You passed." + "\n" + "It's " + m_currentPlayerString + "'s turn to roll the Dice.";
						}
						else
						{
							messageBox.htmlText = "Hold up! It's " + m_currentPlayerString + "'s turn to roll the Dice.";
						}
						hideRollDiceButton();
						passButton.removeEventListener(MouseEvent.CLICK,passTheTurn);
						hidePassButton();

					}
					
					hideDieOne();
					hideDieTwo();
					hideArrow();
					showIndicatorChip();

				break;
				
				case "firstMove":
					if(m_currentPlayer == m_thisPlayer)
					{
						if (m_doubles)
						{
							messageBox.htmlText = m_currentPlayerString + ", you got Doubles! You can make four moves of " + m_dieOneNumber + " spaces. Move one of your " + m_thisPlayerColor + " chips " + m_dieOneNumber + " spaces." ;
						}
						else
						{
							messageBox.htmlText = m_currentPlayerString + ", you can move one of your " +  m_thisPlayerColor + " chips " + m_dieOneNumber + " or " + m_dieTwoNumber + " spaces.";
						}
						showDieOne();
						showDieTwo();
						showArrow();
						
					}
			
					else
					{
						if (m_doubles)
						{
							messageBox.htmlText = "Oh no! " + m_currentPlayerString + " got double " + m_dieOneNumber + "s.";
						}
						else
						{
							messageBox.htmlText = m_currentPlayerString + " can move a chip " + m_dieOneNumber + " or " + m_dieTwoNumber + " spaces.";
						}

				
					}
				break;
				
				case "nextMove":
				
					if(m_currentPlayer == m_thisPlayer)
					{
						if(m_doubles)
						{
							messageBox.htmlText = m_currentPlayerString + ", you've got " + m_numberOfMoves + " moves left to move one of your " + m_thisPlayerColor + " chips " + m_dieOneNumber + " spaces.";
						}
					
						else if(m_distanceMoved == m_dieOneNumber)
						{
							messageBox.htmlText = m_currentPlayerString + ", for your next move, you can move one of your " + m_thisPlayerColor + " chips " + m_dieTwoNumber + " spaces.";
							hideDieTwo();
						}
						else
						{
							messageBox.htmlText = m_currentPlayerString + ", for your next move, you can move one of your " + m_thisPlayerColor + " chips " + m_dieOneNumber + " spaces.";
							hideDieOne();
							
						}
						showArrow()
					}
					//the other player sees this
					else
					{
						if(m_doubles)
						{
							messageBox.htmlText = m_currentPlayerString + " has " + m_numberOfMoves + " moves left to move a chip " + m_dieOneNumber + "spaces.";
						}
					
						else if(m_distanceMoved == m_dieOneNumber)
						{
							messageBox.htmlText = m_currentPlayerString + "'s next move can be a " + m_dieTwoNumber;
						}
						else
						{
							messageBox.htmlText = m_currentPlayerString + "'s next move can be a " + m_dieOneNumber;
						}				
					}
					

					break;
					
				case "gameOver":
					trace("in showmessage gameOver");
					if(m_currentPlayer == m_thisPlayer)
					{
						playAgainScreen.playAgainText.text = "Woohoo! You won! Game Over!";
						//bring up play again button
						playAgainScreen.x = 400;
						playAgainScreen.y = 300;
						stage.addChild(playAgainScreen);
						playAgainScreen.playAgainButton.addEventListener(MouseEvent.CLICK, playAgain);
						
					}
			
					else
					{
						playAgainScreen.playAgainText.text = "Awwww...." + m_currentPlayerString + " won. Game Over!";
						//bring up play again button
						playAgainScreen.x = 400;
						playAgainScreen.y = 300;
						stage.addChild(playAgainScreen);
						playAgainScreen.playAgainButton.addEventListener(MouseEvent.CLICK, playAgain);
				
					}
					break;

				default:

			}//end switch
			
		}//end function showMessage


		function sendDiceInfo(event:SyncEvent):void
		{
			
			switch(event.changeList[0].code)
			{
				case "clear":
					//do nothing
					//deBug.appendText(" |clear dice");
				
					break;
				case "success":
					//deBug.appendText(" |success dice");
					//check to make sure all the information got in
					if(!m_sharedDiceObj.data.dieOne)
					{
						deBug.appendText(" |no die1 info");
						//m_sharedDiceObj.setProperty("dieOne", m_dieOneNumber);
					}
					
					if(!m_sharedDiceObj.data.dieTwo)
					{
						deBug.appendText(" |no die2 info");
						//m_sharedDiceObj.setProperty("dieTwo", m_dieTwoNumber);
					}
					
					if(!m_sharedDiceObj.data.doubles)
					{
						deBug.appendText(" |no dbls info");

					}

					break;
				case "change":
					deBug.appendText(" |change dice");
					//check if all the properties are there before showing them and sending message
					if(m_sharedDiceObj.data.dieOne && m_sharedDiceObj.data.dieTwo && m_sharedDiceObj.data.doubles)
					{
						deBug.appendText(" |dice info all in");
						//show the die number
						m_dieOneNumber = m_sharedDiceObj.data.dieOne;
						dieOne.gotoAndStop(m_dieOneNumber);
						m_dieTwoNumber = m_sharedDiceObj.data.dieTwo;
						dieTwo.gotoAndStop(m_dieTwoNumber);
				
						//convert the text into a true/false
						if (m_sharedDiceObj.data.doubles == "yes")
						{
							m_doubles = true;
						}
						else
						{
							m_doubles = false;
						}
						//after the dice is rolled, show the mesage of the first move
						showMessage("firstMove");
			
					}
					else
					{
						deBug.appendText(" |dice info not in");
			
					}
					
					break;
				default:
			}//end switch

		}
		
		function passTheTurn(event:MouseEvent):void
		{
			passButton.removeEventListener(MouseEvent.CLICK,passTheTurn);
			hidePassButton();
			m_turnPassed = true;
			//send info to server
			m_sharedPassedTurnObj.setProperty("turnPassed", "true");
			switchTurns();
			
			dontMoveChips();
			
			if(m_currentPlayer == m_thisPlayer)
			{
			allowChipsToBeMoved();
			}
			
		}
		
		function rollTheDice(event:MouseEvent):void
		{
			//debug used to skip to the end of the game
			/*if(DEBUG)
			{
				rollButton.removeEventListener(MouseEvent.CLICK,rollTheDice);
				endGame();
			}
			else
			{*/
			//remove event listener
			rollButton.removeEventListener(MouseEvent.CLICK,rollTheDice);
			hideRollDiceButton();
			m_turnPassed = false;
			m_sharedPassedTurnObj.setProperty("turnPassed", "false");
			showPassButton();
			passButton.addEventListener(MouseEvent.CLICK,passTheTurn);

			//get the die number
			m_dieOneNumber = Math.floor(Math.random() * 6 + 1);
			m_dieTwoNumber = Math.floor(Math.random() * 6 + 1);
			
			//changes the so for dice
			m_sharedDiceObj.setProperty("dieOne", m_dieOneNumber);
			dieOne.gotoAndStop(m_dieOneNumber);
			m_sharedDiceObj.setProperty("dieTwo", m_dieTwoNumber);
			dieTwo.gotoAndStop(m_dieTwoNumber);
			
			addChild(dieOne);
			addChild(dieTwo);
			
			//reset boolean variables
			m_dieOneUsed = false;
			m_dieTwoUsed = false;
			m_doubles = false;
			//check for doubles
			if (m_dieOneNumber == m_dieTwoNumber)
			{
				m_numberOfMoves = 4;
				m_doubles = true;
				//needs to be a string so that we can check if it is there at all
				//change so
				m_sharedDiceObj.setProperty("doubles", "yes");
			}
			else
			{
				m_numberOfMoves = 2;
				m_doubles = false;
				//needs to be a string so that we can check if it is there at all
				//change so
				m_sharedDiceObj.setProperty("doubles", "no");
			}
			showMessage("firstMove");
			//deBug.appendText(" |now 1st mov");
			//allows certain chips to be moved (just for this player!)
			if(m_currentPlayer == m_thisPlayer)
			{
			allowChipsToBeMoved();
			}
			
			// }//end debug if/else statement (see the top of this function)
			

		}//end function roll dice
		
		function dontMoveChips():void
		{
			//remove the event listeners for all chips
			for (var d:int = 0; d < m_onBoardArray.length; d++)
			{
				for (var e:int = 1; e < m_onBoardArray[d].length; e++)
				{
				m_onBoardArray[d] [e].removeEventListener(MouseEvent.MOUSE_DOWN,dragChip);
				m_onBoardArray[d] [e].removeEventListener(MouseEvent.MOUSE_UP,dropChip);
				}
			}
			
			
		}//end function don't move chips

		function allowChipsToBeMoved():void
		{
			deBug.appendText("\n" + "CHECKING BOARD ARRAY INFO");
			deBug.appendText("\n" + "BLOT 1: " + m_onBoardArray[25].length);
			deBug.appendText("\n" + "BLOT 2: " + m_onBoardArray[0].length);
			//check the currentplayer
			
			if(m_currentPlayer == "playerOne")
			{
				a_playerBlotSpace = 25;
			}
			else
			{
				a_playerBlotSpace = 0;
			}
				
			//if there is a blot, then don't move any other chip except the blot
			//plus send a message that there is a blot?
			if(m_onBoardArray[a_playerBlotSpace].length >1)
			{
				//allow the blot to be moved
				m_onBoardArray[a_playerBlotSpace] [m_onBoardArray[a_playerBlotSpace].length-1].addEventListener(MouseEvent.MOUSE_DOWN,dragChip);
				m_onBoardArray[a_playerBlotSpace] [m_onBoardArray[a_playerBlotSpace].length-1].addEventListener(MouseEvent.MOUSE_UP,dropChip);
			}
			else
			{

				//goes thru on board array to listen for moves
				for (var m:int = 0; m < m_onBoardArray.length; m++)
				{
					
					//checks to see if the next dimensional array is more than one
					//if it is 1 or 0, then there is nothing there
					if (m_onBoardArray[m].length > 1)
					{		 
						//checks to see if the top chip is the currentplayer
						//if the length of the next dimension is 2, then the top chip is in space 1, hence length-1
						//checks the first 9 characters of the movieclip name in the onboard array
						//trace(m_onBoardArray[m] [m_onBoardArray[m].length-1].name.substr(0,9));
						deBug.appendText("\n"+ "space" + m + " length = " + String((m_onBoardArray[m].length)-1));
						if (m_onBoardArray[m] [m_onBoardArray[m].length-1].name.substr(0,9) == m_currentPlayer)
						{
							//if so, then take the top chip movie clip
							
							deBug.appendText(" " + m_currentPlayer +" topchip is " + m_onBoardArray[m] [m_onBoardArray[m].length-1].name + " at space" + m);
							trace(m_currentPlayer +"'s top chip on space" + m + " that can move is " + m_onBoardArray[m] [m_onBoardArray[m].length-1].name);
							m_onBoardArray[m] [m_onBoardArray[m].length-1].addEventListener(MouseEvent.MOUSE_DOWN,dragChip);
							m_onBoardArray[m] [m_onBoardArray[m].length-1].addEventListener(MouseEvent.MOUSE_UP,dropChip);
							//m_onBoardArray[m] [m_onBoardArray[m].length-1].addEventListener(MouseEvent.MOUSE_MOVE,moveChip);
							
						}
					}
				}//end for loop
			}//end if/else
			
		}//end function allow chips to be moved


		
		function moveChip(event:Event):void
		{		
			//messageBox.appendText("currentchip[0] moved");
			//set the property of the shared object
			m_sharedChipsObj.setProperty("chipX", event.target.x); 
			m_sharedChipsObj.setProperty("chipY", event.target.y);
			
		}
		

		function dragChip(event:MouseEvent):void
		{
			//get original coordinates of the chip
			m_chipStartX = event.target.x;
			m_chipStartY = event.target.y;

			//get the target name to find the location/space of the chip on the onboard arrays
			trace(event.target.name);
			//loop thru the onboard array
			for (var p:int = 0; p < m_onBoardArray.length; p++)
			{
				//checks to see if the next dimensional array is more than one
				//if it is 1 or 0, then there is nothing there, so do nothing
				if (m_onBoardArray[p].length > 1)
				{	
					//check if the target's name matches the name of the place in the onboard array
					if(m_onBoardArray[p] [m_onBoardArray[p].length-1].name == event.target.name)
					{
						m_pickedUpSpace = p;
						m_pickedUpLocation = m_onBoardArray[p].length-1;
						//the current chip is at this location
						//m_onBoardArray[m_pickedUpSpace][m_pickedUpLocation]
						//m_sharedChipsObj.setProperty("pickedUpSpace", m_pickedUpSpace);
						//m_sharedChipsObj.setProperty("pickedUpLocation", m_pickedUpLocation);
						m_sharedChipsObj.setProperty("chipName", event.target.name);
					}

				}
				
			}
			

			//start the drag
			event.target.startDrag();
			event.target.addEventListener(MouseEvent.MOUSE_MOVE,moveChip);
			//set the boolean so that drop chip does not start until this one is done
			m_pickedUpChip = true;

		}

		function dropChip(event:MouseEvent):void
		{
			while (!m_pickedUpChip)
			{ 
				//wait until m_pickedUpChip is true
			}
			
			
			//stop the dragging
			event.target.stopDrag();
			event.target.removeEventListener(MouseEvent.MOUSE_DOWN,dragChip);
			event.target.removeEventListener(MouseEvent.MOUSE_UP,dropChip);
			event.target.removeEventListener(MouseEvent.MOUSE_MOVE,moveChip);
			
			//get chips ending location
			//m_chipEndX = event.target.x;
			//m_chipEndY = event.target.y;

			//set that a move wasn't made
			m_chipCanBeMoved = false;
			m_correctMove = false;
			m_chipIsHome = false;
			
			
			trace(m_currentPlayer);
			trace("move? " + m_chipCanBeMoved);
			//if there is a Movie Clip (not null) that the chip was dropped on, go ahead
			if (event.target.dropTarget != null)
			{
				trace("droptarget is not null");
				trace(event.target.dropTarget.parent.name);
				m_sharedChipsObj.setProperty("dropTargetName", event.target.dropTarget.parent.name);
				//check if the move is okay to do
				m_chipCanBeMoved = checkMoveLocation(event.target.name, event.target.dropTarget.parent.name);
				
				
			}
			else
			{
				trace("not a movie clip");
				//the player can not move the chip
				m_chipCanBeMoved = false;
			}
			//snap the chip into place and check if the game is over
			m_gameOver = snapChipIntoPlace(event.target.name, event.target.dropTarget.parent.name);
			
			
			
			
			if(m_gameOver)
			{
				//game over
				endGame();
				
			}
			else if (m_numberOfMoves ==0)
			{
				//switch turns
				switchTurns();
				
			}
			else if (m_correctMove)
			{
				//next move
				//send info that the chip was moved a certain distance
				m_sharedTurnObj.setProperty("distanceMoved", m_distanceMoved);
				//send info on the number of moves too
				m_sharedTurnObj.setProperty("numberOfMoves", m_numberOfMoves);
				showMessage("nextMove");
				

				if(m_distanceMoved == m_dieOneNumber && !m_doubles)
				{
					m_dieOneUsed = true;
				}
				else if(m_distanceMoved == m_dieTwoNumber && !m_doubles)
				{
					m_dieTwoUsed = true;
				}
				else
				{
					m_dieOneUsed = false;
					m_dieTwoUsed = false;
				}
							
			}
			dontMoveChips();
			
			if(m_currentPlayer == m_thisPlayer)
			{
			allowChipsToBeMoved();
			}

		}//end  drop chip function
		
		function checkMoveLocation(chipName:String, locationName:String):Boolean
		{

			//check if all the chips are on the homeboard
			var a_allHome:Boolean = checkIfAllChipsInHomeboard();
			var a_chipCanBeMoved:Boolean = false;
			trace("is the space =" + locationName.substr(0,9));
			m_chipIsHome = false;
			
			//all the chips are home and the location is playerOneOff or playerTwoOff
			if (a_allHome && locationName.substr(0,9) == m_currentPlayer)
			{
					//the chip is going home
					trace("the chip is going home");
					m_chipIsHome = true;

			}
			
			//if the chip is not going home, then it's going to a space
			//if the drop target is a space (not blotspace or offboard)
			 else if (locationName.substr(0,5) == "space")
			{
				trace("droptarget is a space");
				//get the number of the space landed on
				m_landedOnSpace = Number(locationName.substr(5));
					
				//if the place isn't the same spot 
				if (m_landedOnSpace != m_pickedUpSpace)
				{
					trace("droptarget is not the same space");
					
					//check for distance moved
					switch(m_thisPlayer)
					{
						case "playerOne":
							m_distanceMoved = m_pickedUpSpace-m_landedOnSpace;
							break;
				
						case "playerTwo":
							m_distanceMoved = m_landedOnSpace-m_pickedUpSpace;
							break;
						default:
						m_distanceMoved = 0;
				
					}//end switch
						
						
					trace("the distance of the move is " + m_distanceMoved);
					trace("m_dieOneNumber is " + m_dieOneNumber);
					trace("m_dieTwoNumber is " + m_dieTwoNumber);
					//need to pick which die
					switch(m_distanceMoved)
					{
						case m_dieOneNumber:
							if(m_dieOneUsed)
							{
								m_correctMove = false;
							}
							else
							{
								m_correctMove = true;
							}
							break;
						case m_dieTwoNumber:
							if(m_dieTwoUsed)
							{
								m_correctMove = false;
							}
							else
							{
								m_correctMove = true;
							}
							break;
						default:
							m_correctMove = false;
					}
					trace("was it a correct move? " + m_correctMove);
						
					//if the space is empty
					if(m_onBoardArray[m_landedOnSpace].length <=1)
					{
						trace("droptarget space is empty and has no chips");
						a_chipCanBeMoved = true;
					}
						
					//if its not an opponents space, but the current players space
					else if(m_onBoardArray[m_landedOnSpace] [m_onBoardArray[m_landedOnSpace].length-1].name.substr(0,9) == m_currentPlayer )
					{
						trace("droptarget is on a current players space");
						//the player can move the chip
						trace("move? " + m_chipCanBeMoved);
						a_chipCanBeMoved = true;

					}
					
					////////BLOT BLOT BLOT BLOT/////////	
					//or if only ONE (length = 2) opponent chip is there which is a blot
					else if((m_correctMove) && (m_onBoardArray[m_landedOnSpace].length == 2) && ( m_onBoardArray[m_landedOnSpace] [m_onBoardArray[m_landedOnSpace].length-1].name.substr(0,9) != m_currentPlayer ))
					{ 
							
						//hit the blot
						trace("droptarget is on a opponents blot");
						trace("hit the blot");
						//m_blotIsHit = true;
						//send that it is hit, so that the other chip doesn't move
						//m_sharedOnBoardObj.setProperty("blotIsHit", "true");
						//moves the blot
						deBug.appendText(" |m_thisPlayer = " + m_thisPlayer);
						switch(m_thisPlayer)
						{
							case "playerOne":
								
								//current player is player one so move the blot to player two's space (0)
								m_onBoardArray[m_landedOnSpace] [1].x = blotSpace2.x;
								m_onBoardArray[m_landedOnSpace] [1].y = blotSpace2.y - m_chipsLocationArray [m_onBoardArray[0].length] + m_chipWidth;
								
								//sends info the server to show that blot has moved
								m_sharedBlotObj.setProperty("blotX", m_onBoardArray[m_landedOnSpace] [1].x);
								m_sharedBlotObj.setProperty("blotY", m_onBoardArray[m_landedOnSpace] [1].y);
								m_sharedBlotObj.setProperty("blotChipName", m_onBoardArray[m_landedOnSpace] [1].name);
								
								deBug.appendText(" |P1 hit P2blot: blot_landedOnSpace = 0");
								deBug.appendText(" |P1 hit P2blot: blot_pickedUpSpace = " + m_landedOnSpace);
								//change the onboard array
								m_onBoardArray[0].push(m_onBoardArray[m_landedOnSpace].pop());
								
								
								//send the new location (onboard) info back to the server too
								m_sharedOnBoardObj.setProperty("blot_landedOnSpace", 5306);
								m_sharedOnBoardObj.setProperty("blot_pickedUpSpace", m_landedOnSpace);
								break;
								
							case "playerTwo":
								//current player is player two so move the blot to player one's space (25)
								m_onBoardArray[m_landedOnSpace] [1].x = blotSpace1.x;
								m_onBoardArray[m_landedOnSpace] [1].y = blotSpace1.y + m_chipsLocationArray [m_onBoardArray[25].length];
								
								//sends info the server to show that blot has moved
								m_sharedBlotObj.setProperty("blotX", m_onBoardArray[m_landedOnSpace] [1].x);
								m_sharedBlotObj.setProperty("blotY", m_onBoardArray[m_landedOnSpace] [1].y);
								m_sharedBlotObj.setProperty("blotChipName", m_onBoardArray[m_landedOnSpace] [1].name);
								
								//change the onboard array
								m_onBoardArray[25].push(m_onBoardArray[m_landedOnSpace].pop());
								
								//send the new location (onboard) info back to the server too
								m_sharedOnBoardObj.setProperty("blot_landedOnSpace", 25);
								m_sharedOnBoardObj.setProperty("blot_pickedUpSpace", m_landedOnSpace);
								break;
							default:
						}//end switch
							
						//the player can move the chip
						a_chipCanBeMoved = true;
					}
					
					else
					{
						//its not empty, not the current players space and not a blot, so..
						trace("opponents space");
						//the player can not move the chip
						a_chipCanBeMoved = false;
						//reset if the blot was hit, so that the chip can move into place
						//m_blotIsHit = false;
						//send the reset
						//m_sharedOnBoardObj.setProperty("blotIsHit", "false");
					}

				}
				else
				{
					//the location is the same space
					trace("same space");
					//the player can not move the chip
					a_chipCanBeMoved = false;
				}
			
			}
			else
			{
				//the location is not a triangle
				trace("no space# space");
				//the player can not move the chip
				a_chipCanBeMoved = false;

			}
				
			return a_chipCanBeMoved;
			
		}//end check move location function


		function showpossibleMovesWhenHovering():void
		{

		}

		function snapChipIntoPlace(a_targetName:String, a_dropTargetName:String):Boolean
		{
			var a_gameOver:Boolean = false;
			var a_theTarget = stage.getChildByName(a_targetName);
			
			trace(a_targetName + a_theTarget);
			var a_theDropTarget = getChildByName(a_dropTargetName);
			
			trace(a_dropTargetName + a_theDropTarget);
			//now move it or not
			//if all chips are home then, start bearing off
			//messageBox.htmlText = "snapping into place!";
			
			if(m_chipIsHome)
			{
				m_numberOfMoves --;
				//bearing chip off the board
				if (m_currentPlayer == "playerOne")
				{
					
					//pop off the chip
					m_landedOnLocation = m_onBoardArray[26].push(m_onBoardArray[m_pickedUpSpace].pop()) -1;
					//send the new location info back to the server too
					m_sharedOnBoardObj.setProperty("landedOnSpace", 26);
					m_sharedOnBoardObj.setProperty("pickedUpSpace", m_pickedUpSpace);
					
					a_theTarget.x = a_theDropTarget.x  + m_chipsHomeXLocationArray[m_landedOnLocation];
					a_theTarget.y = a_theDropTarget.y + m_chipsHomeYLocationArray[m_landedOnLocation];
					//check if game is over
					if(m_onBoardArray[26].length>=16)
					{
						a_gameOver = true;
					}
				}
				else
				{
					//pop off the chip
					m_landedOnLocation = m_onBoardArray[27].push(m_onBoardArray[m_pickedUpSpace].pop()) -1;
					//send the new location info back to the server too
					m_sharedOnBoardObj.setProperty("landedOnSpace", 27);
					m_sharedOnBoardObj.setProperty("pickedUpSpace", m_pickedUpSpace);
					
					a_theTarget.x = a_theDropTarget.x + m_chipsHomeXLocationArray[m_landedOnLocation];
					a_theTarget.y = a_theDropTarget.y - m_chipsHomeYLocationArray[m_landedOnLocation] + a_theTarget.width;
					if(m_onBoardArray[27].length>=16)
					{
						a_gameOver = true;
					}
				}
				
				
				
			}
			else if(m_chipCanBeMoved && m_correctMove)
			//if(m_chipCanBeMoved)
			{
				
				//less the number of moves
				m_numberOfMoves --;
				trace("Number of Moves Left is " + m_numberOfMoves);
				
				//m_sharedMessageObj.setProperty("theMessage", "nextMove");
				
				//then pop off the chip
				m_landedOnLocation = m_onBoardArray[m_landedOnSpace].push(m_onBoardArray[m_pickedUpSpace].pop()) -1;
				//send the new location info back to the server too
				m_sharedOnBoardObj.setProperty("landedOnSpace", m_landedOnSpace);
				m_sharedOnBoardObj.setProperty("pickedUpSpace", m_pickedUpSpace);
				
				trace("NOW the picked up space length is " +m_onBoardArray[m_pickedUpSpace].length);
				//snap the chip into place
				trace("landed on space" + m_landedOnSpace + " and location" + m_landedOnLocation);
				
				//now move it
				if (m_landedOnSpace < 13)
				{
					a_theTarget.x = a_theDropTarget.x;
					a_theTarget.y = a_theDropTarget.y + m_chipsLocationArray[m_landedOnLocation];
				}
				else
				{
					a_theTarget.x = a_theDropTarget.x;
					a_theTarget.y = a_theDropTarget.y - m_chipsLocationArray[m_landedOnLocation] + a_theTarget.width;
				}
				
				//put chip on top
				stage.addChild(a_theTarget);
				
			}
			else
			{
				//the chip is moved back
				a_theTarget.x = m_chipStartX;
				a_theTarget.y = m_chipStartY;
			}
			
			//send the chips SNAPPED location to the server
			m_sharedChipsObj.setProperty("chipX", a_theTarget.x); 
			m_sharedChipsObj.setProperty("chipY", a_theTarget.y);
			
			m_pickedUpChip = false;
			
			return a_gameOver;

		}//end snapping function

		
		function checkIfAllChipsInHomeboard():Boolean
		{
			var a_okayToBearOff:Boolean = true;
			trace("testing Check Homeboard");
			//checkt to see if there are any chips on the blot
				
			switch(m_currentPlayer)
			{
				case "playerOne":
					//for player one, the homeboard is 1-6, so
					//go thru spaces 7-25 if there is anything of their chips
					//(25 is player one's blot)
					for (var h:int = 7; h < 26; h++)
					{

						if((m_onBoardArray[h].length > 1) && (m_onBoardArray[h][m_onBoardArray[h].length-1].name.substr(0,9) == m_currentPlayer))
						{
							trace("player one's chips are not all home");
							//there are chips still on home board
							//check to see if they are player one's chips
							//the player's chips are not on homeboard
							a_okayToBearOff = false;
						}

					}

					break;
				
				case "playerTwo":
					//for player two, the homeboard is 19-24, so
					//go thru spaces 0-18 if there is anything of their chips 
					//(0 is player two's blot)
					for (var j:int = 0; j < 18; j++)
					{

						if((m_onBoardArray[j].length > 1) && (m_onBoardArray[j][m_onBoardArray[j].length-1].name.substr(0,9) == m_currentPlayer))
						{
							trace("player two's chips are not all home");
							//there are chips still on home board
							//check to see if they are player one's chips
							//the player's chips are not on homeboard
							a_okayToBearOff = false;
						}
					}

					break;
					
				default:
				trace("currentplayer???");
			}//end switch
			trace("a_okayToBearOff = " + a_okayToBearOff);
			return a_okayToBearOff;
			
		}//end function check if homeboard


		function endGame():void
		{
			//game is over
			trace("Game is Over!");
			trace(m_currentPlayer + "is the winner!");
			//show the message to the winner
			showMessage("gameOver");
			
			//send the message to the server
			m_sharedTurnObj.setProperty("gameOver", "gameOver");

		}


		function playAgain(event:MouseEvent):void
		{
			
			//playAgainScreen.x = 4000;
			//playAgainScreen.y = 3050;
			playAgainScreen.playAgainButton.removeEventListener(MouseEvent.CLICK, playAgain);

			
			//clear the board of chips
			for (var c:int = 0; c < 28; c++)
			{
				if(m_onBoardArray[c].length >1)
				{
					for (var d:int = 1; d < m_onBoardArray[c].length; d++)
					{
					trace("removing chip at space " + c + " and location " + d);
					stage.removeChild(m_onBoardArray[c][d]);
					trace("removed");
					}
				}

			}
			//switchTurns();
			//startGame();
			//m_playAgainHit = true;
			playAgainScreen.playAgainText.text = "Waiting for the other player.";
			
			if(m_playAgainObj.data.playAgain && m_playAgainObj.data.playAgain == "yes")
			{
				m_playAgainObj.setProperty("playAgain", "meToo");
			}
			
			else
			{
				m_playAgainObj.setProperty("playAgain", "yes");
			}
			
			
			

		}


	}
}