package {
	import adobe.utils.CustomActions;
	import com.netgrind.core.Scheduler;
	import com.netgrind.core.Utils;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	
	/**
	 * ...
	 * @author netgrind
	 */
	public class RaceManager extends Sprite {
		private var icon:Bitmap;
		private var wp:Waypoint;
		
		[Embed(source = "../lib/ui/ui_parkingicon.png")]public var parkingTex:Class;
		public var car:Car;
		public var level:Level;
		public var raceName:String = "RACE01";
		public var called:Boolean = false;
		
		public function RaceManager(car:Car, level:Level) {
			super();
			this.car = car;
			this.level = level;
			wp = Waypoint.getFromName("wGokartTrack");
			icon = new parkingTex();
			addChild(icon);
			icon.x = wp.x-icon.width*.5;
			icon.y = wp.y-icon.height*.5;
			Scheduler.invokeEvery(update);
		}
		
		private function update():void {
			if (Utils.getDistance(wp, car) < 50 && car.speed > 30) {
				if (!called) {
					popup();
					called = true;
				}
			}
		}
		
		private function popup():void {
			var t:Vector.<TextChunk>  = new Vector.<TextChunk>();
			t.push(new TextChunk("Welcome to Elmore Speedway! Are you ready to race?", false, 0, 0));
			t.push(new TextChunk("Let's do this!", true, 0, 1));
			level.displayDialog(t,startRace,Character.all[CharacterManager.LARRY]);
		}
		
		private function startRace():void {
			level.startRace(raceName,finished);
		}
		
		private function finished():void {
			called = false;
		}
		
		/*<waypoint name="wGokartTrack">
			<requireStop>true</requireStop>
			<dialog>
				<text>18</text>  <!-- Welcome to Elmore Speedway! Are you ready to race?! -->
				<characterState>0</characterState>
				<momState>0</momState>
				<momTalking>false</momTalking>
			</dialog>
			<dialog>
				<text>19</text>  <!-- Let's do this! -->
				<characterState>0</characterState>
				<momState>1</momState>
				<momTalking>true</momTalking>
			</dialog>
		</waypoint>*/		
	}	
}