package com.terry.engine.util {
	import flash.display.*;
	import flash.geom.*;
  import flash.events.*;
  import flash.net.*;
	import com.terry.engine.*;
	
	public class textboxclass extends Sprite {
		public function textboxclass():void {
			firstcreate();
		}
		
		public function firstcreate():void {
			//Like clear, only it creates the actual arrays, etc
			for (iter = 0; iter < 10; iter++) {
				var t:String = new String;	t = "";
				line.push(t);
			}
			x = 0; y = 0; w = 0; h = 0; numlines = 0; lw = 0; tl = 0; active = false;
			showname = true;
			tbcursor = 0; tbline = 1;
			textboxstate = 0; textboxdelay = 0;
			col = 0;
		}
		
		public function clear():void {
			//Set all values to a default, required for creating a new entity
			for (iter = 0; iter < 12; iter++) {
				line[iter]="";
			}
			textrect = new Rectangle();
			xp = 0; yp = 0; w = 0; h = 0; numlines = 1; lw = 0; tl = 0; active = true;
			showname = true;
			tbcursor = 0; tbline = 1; //Start at 1 until no-named textboxes are introduced
			textboxstate = 0; textboxdelay = 0;
		}
		
		public function centerx():void { resize();	xp = (def.SCREENWIDTH/2) - (w / 2);	resize(); }
		public function centery():void { resize();	yp = (def.SCREENHEIGHT / 2) - (h / 2); resize(); }
		
		public function adjust():void {
			resize();
			if (xp < 3) xp = 3;
			if (yp < 3) yp = 3;
			if (xp + w > def.SCREENWIDTH - 3) xp = (def.SCREENWIDTH - 3) - w;
			if (yp + h > def.SCREENHEIGHT - 3) yp = (def.SCREENHEIGHT - 3) - h;	
			resize();
		}
		
		public function update():void {
			if (textboxstate == 1) {
				tl += .1;	if (tl >= 1) { tl = 1; }
				
				textboxdelay += 4;
			  if (textboxdelay >= 40) {
				  textboxdelay = 0;
				  textboxstate++;
				  tbcursor = 0; tbline = 0;
			  }
			}else if (textboxstate == 2) {
				textboxdelay--;
				if (textboxdelay <= 0) {
					textboxdelay = 0;
					tbcursor += 3;
					
					if (tbcursor > line[tbline].length) {
						tbline++; tbcursor = 0;
						if (tbline >= numlines) {
							textboxstate++;
						}
					}
				}
			}else if (textboxstate == 4) {
				tl -= .1; if (tl <= 0.5) { tl = 0.5; }
				
				textboxdelay+=4;
				if (textboxdelay >= 40 || tl <= 0.5) {
					tl = 0.5;
					
					textboxdelay = 0;
					textboxstate = 5;
					tbcursor = 0; tbline = 0;
				}
			}
		}
		
		public function remove():void {
			textboxstate = 4; tl = 1; //Remove mode
		}
		
		public function removefast():void {
			textboxstate = 4; tl = 0.4; //Remove mode
		}
		
		public function resize():void {
			//Set the width and height to the correct sizes
			//old resize function
			max = 0;
			for (iter = 0; iter < numlines; iter++) if (font.len(line[iter]) > max) max = font.len(line[iter]);
			
			lw = max;
			if (showname) {
				w = max + 44;
			}else {
				w = max + 32;
			}
			w = w + (16 - (w % 16));
			h = (numlines * 12) + 16;
			h = h + (16 - (h % 16));
		}
		
		public function addline(t:String):void {
			line[numlines] = t;
			numlines++;
			resize();
			if (numlines >= 12) numlines = 0;
		}
		
    //Fundamentals
		public var line:Vector.<String> = new Vector.<String>;
		public var xp:int, yp:int, lw:int, w:int, h:int, numlines:int;
		public var textrect:Rectangle;
		public var active:Boolean;
		public var showname:Boolean;
		public var tbcursor:int, tbline:int;
		public var textboxstate:int, textboxdelay:int;
		public var col:int;
		
		public var tl:Number; //Alpha values
		
		public var iter:int, max:int;
	}
}
