package
{
	import flash.display.*;
	import flash.events.*;
	
	
	/**
	 * @author - (t)ad Green
	 * @copy - Copyright 2010 A.D. Green
	 * @version - 2
	 *
	 *  Free for personal and educational use;
	 *  if you would like to use it commercially,
	 *  you must make significant modifications and use your own images.
	 *
	 * */
	[SWF(frameRate = '12', backgroundColor = '0xc8c8c8', width = '433', height = '400')]
	public class tadsBlackJack extends blackJackGraphics
	{
		
		/* The game engine, it only has the core functions of the game;
		   ini, deal, hit, calculateDealerHand, and calculateWinner.
		  */
		
		public var brain:blackJackBrain;
		public var sounds:blackJackSounds;
		public var cardShuteOrganizer:shuteOrganizer;

		
		public function tadsBlackJack() {
			
			
			brain = new blackJackBrain(this);
			sounds = new blackJackSounds();
			super(this);
			cardShuteOrganizer = new shuteOrganizer(this);

			if (stage) ini()
			else addEventListener(Event.ADDED_TO_STAGE, ini, false, 0, true);
			
		}
		
		
		private function ini(e:Event = null):void {
			
			removeEventListener(Event.ADDED_TO_STAGE, ini);
			dealButton.addEventListener(MouseEvent.CLICK, deal);
			addEventListener(MouseEvent.CLICK, hit, true);
			dealerGo.addEventListener(MouseEvent.CLICK, calculateDealerHand);
			cardConfigureButton.addEventListener(MouseEvent.CLICK, addOrganizer);
			
		}
		
		
		private function deal(event:MouseEvent):void
		{

			if (contains(winField)) {
				winField.text = ""; removeChild(winField);
			}
			
			brain.splitHandOne = 0;
			brain.splitHandTwo = 0;
			brain.splitHandThree = 0;

			var spotsToClear:Array = [cardSpotOne, cardSpotTwo, cardSpotThree, dealerSpot];
			var spot:DisplayObjectContainer;
			for (var i:int = 0; i < 4; i++) {
				spot = spotsToClear[i];
				while( spot.numChildren > 0) {
					spot.removeChildAt(spot.numChildren - 1);
				}
			}
			spotsToClear = null;
			
			brain.calculateDeal();
			
			initializePlayerHandGraphics(brain.playerCardOne, brain.playerCardTwo, brain.firstSuit, brain.secondSuit);
			initializeDealerHandGraphics(brain.dealerCardOne, brain.dealerCardTwo, brain.dealerSuitOne, brain.dealerSuitTwo);

		}
		
		
		public function hit(event:MouseEvent):void
		{
			brain.calculateHit(event);
		}
		
		
		private function calculateDealerHand(event:MouseEvent):void
		{
			/* now the aces used value is for the dealer so we reset it accordingly */

			if (brain.dealerCardOne == "A" && brain.dealerCardTwo == "A")
			{brain.acesUsed = 1;}else {brain.acesUsed = 0;}

			dealerSpot.removeChildAt(0); //remove the face down image
			
			var dealerCardIndex:int = getIndexFor(brain.dealerCardOne);
			
			dealerSpot.addChildAt(blackJackImages[cardChoices[dealerCardIndex] + brain.dealerSuitOne+"Image"](), 0); //show what that card was
			
			dealerSpot.getChildAt(0).x = dealerSpot.getChildAt(0).y = 0;
			dealerSpot.getChildAt(1).x = dealerSpot.getChildAt(0).x + 105;
			dealerSpot.getChildAt(1).y = dealerSpot.getChildAt(0).y;

			if (dealerSpot.scaleX == 1.0)
			{dealerSpot.scaleX /= 1.80;dealerSpot.scaleY /= 1.80;}

			var givedealer:String;
			var dealercards:Array = [brain.dealerCardOne, brain.dealerCardTwo];

			if (brain.dealerHand < 17)
			{
				while (brain.dealerHand < 17)
				{
					//place new cards as long as the hand is less than 17
					givedealer = blackJackFunctions.giveCard();
					var dSu:String = cardShute.suitOfLastCardPulled;
					brain.dealerHand=brain.hitThisHand(brain.dealerHand, dealercards, givedealer);

					dealercards = dealercards.concat(givedealer);
					showDealerHitGraphics(givedealer, dSu);
					
					if (brain.dealerHand >= 17)
					{calculateWinner();break;}

				}
			}
			else
			{calculateWinner();}

		}
		
		public function calculateWinner():void
		{

			if (!contains(winField))
			{
				winField.x = secondSplitButton.x + 30; winField.y = secondSplitButton.y + 42;
				
				brain.winEifel(winField); //win Eifel, an 'Eifle Tower' of if statements

				addChild(winField);
			}

		}
		
	}
}