package
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.*;

	public class shuteOrganizer extends Sprite
	{
		private var cards:Array = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "K", "Q", "J"];
		private var suits:Array = ["c", "d", "h", "s"];


		public var theGame:tadsBlackJack;


		public function shuteOrganizer(game:tadsBlackJack = null) {

			this.graphics.beginFill(0x00000C);
			this.graphics.drawRect(0, 0, 433, 400);
			theGame = game;
			var cardType:String;
			var offset:int = 0;
			var cardAmount:int = 0;
			
			for (var a:int = 0; a < 5; a ++) {
					cardAmount = 0;
					offset = 0;

				for (var b:int = 0; b < 3; b ++) {

					if (cards.length >= 1) {
						cardType = cards.pop();
					
						for (var c:int = 0; c < 4; c++) {
							cardAmount ++;
							if (c == 0 && b != 0) offset += 2;
						
							var child:DisplayObject = new PlayingCard(cardType, suits[c], true);
							child.scaleX = child.scaleY /= 3.3;
							addChild(child);
							child.x = 433 - (  27 * (cardAmount+offset)   );
							child.y = 400 - ( 75 * (a + 1) );
							var t:TextField = cardField(cardType, suits[c]);
							t.x = child.x;
							t.y = child.y + child.height;
							addChild(t);
						
						}
					}
				}
			}

			var buttonBack:Sprite;
			var bbText:TextField = theGame.makeTXTField(new TextField(), "Go Back");
		
			buttonBack = theGame.spriteConfigure(
				{ x:10, y:10,
					commands:["lineStyle", "beginFill", "drawRect"],
					args:[[1, 0x000000], [0xC0C0C0], [0, 0, bbText.width, bbText.height]] } );
			buttonBack.addChild(bbText);
			addChild(buttonBack);


			buttonBack.addEventListener(MouseEvent.CLICK, goBackToGame);
			

			addEventListener(Event.ADDED, reSeeCardsInShute);

			cardShute.shuffle();
		}


		public function goBackToGame(e:MouseEvent):void {


			theGame.removeChild(this);
			cardShute.shuffle();

		}

		public function reSeeCardsInShute(e:Event):void {

			for (var i:int = 0; i < this.numChildren; i++) {

				var child:TextField = this.getChildAt(i) as TextField;
				if (child) {

					child.text = cardShute.cardsInShoot[child.name][1].toString();
		
				}
			}

		}


		public function cardField(num:String, suit:String):TextField {

			var t:TextField = new TextField();
			t.name = suit + "" + num;
			t.type = TextFieldType.INPUT;
			t.textColor = 0x000000;
			t.background = true;
			t.restrict = "0-9";
			t.backgroundColor = 0xFFFFFF;
			t.border = true;
			t.addEventListener(Event.CHANGE, updateShute);
			t.width = 20;
			t.height = 20;
			t.text = cardShute.cardsInShoot[t.name][1];
			return t;

		}

		public function updateShute(e:Event):void {

			cardShute.cardsInShoot[e.target.name][1] = Number(TextField(e.target).text);
		}


	}
}