package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.media.SoundChannel;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.events.KeyboardEvent;
	import flash.display.Bitmap;
	import flash.display.BlendMode;
	import flash.media.Sound;

	[Frame(factoryClass="Preloader")]
	public class Main extends Sprite 
	{
		private var timer_update : Timer;		
		private var map_parts : Array = new Array();
		private var text_max : Label = new Label("", 0, 150, 13/8);
		private var text_parts : Label = new Label("", 0, -200, 13);
		private var text_max2 : Label = new Label("", 0, 150, 13 / 8, 0x888888);
		private var text_parts2 : Label = new Label("", 0, -200, 13, 0x888888);
		
		[Embed(source = '../lib/sky.jpg')]
		public var sky_embed : Class;
		private var sky : Bitmap = new sky_embed() as Bitmap;			
		private var sky2 : Bitmap = new sky_embed() as Bitmap;
		
		[Embed(source = '../lib/noise.png')]
		public var noise_embed : Class;
		private var noise : Bitmap = new noise_embed() as Bitmap;		
		
		private var layer_parts : Sprite = new Sprite();
		
		private var menu : Menu = new Menu();
		
		[Embed(source='../lib/music.mp3')]
        public var music_embed : Class;
		private var music : Sound = new music_embed() as Sound; 
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public function update(event:TimerEvent):void {
			const W : int = 820;
			sky.x = -Player.player.pos.x;
			sky2.x = sky.x - W;
			if (sky.x < 0) sky2.x = sky.x + W;			
			
			if (menu.visible){
				menu.Update();
				if (!Global.game_started){
					Player.player.pos.x += 0.5;		
					if (Player.player.pos.x > Global.WIDTH + Player.player.part_width / 2) Player.player.pos.x = -Player.player.part_width / 2;
					Player.player.angle = -Math.PI / 6;
					if (Player.player.pos.x > Global.WIDTH / 2) Player.player.angle = Math.PI + Math.PI / 6;
				}
				return;
			}			
			
			Global.time_360 += 0.05;
			if (Global.time_360 >= Math.PI * 2) Global.time_360 -= Math.PI * 2;
				
			var remove_map_parts : Boolean;
						
			Player.player.Update();
				
			text_max.textColor = 0xAAAAFF;
			text_max.text = (Player.player.parts_max - 10).toString();
			if (Player.player.parts_max != Player.player.parts.length){
				if (text_max.alpha < 0.15)
					text_max.alpha += 0.01;
			}else
				if (text_max.alpha > 0)
					text_max.alpha -= 0.01;
			
			if (Player.player.parts.length > 10)
				if (text_parts.alpha < 0.15)
					text_parts.alpha += 0.01;
			text_parts.textColor = 0xAAAAFF;
			if (Player.player.parts.length < Player.player.parts_max)
				text_parts.textColor = 0xFFAAAA;			
			var t : int = int(text_parts.text);
			if (t < Player.player.parts.length - 10) t = Player.player.parts.length - 10;
			if (t > Player.player.parts.length - 10) t--;
			text_parts.text = t.toString();	

			text_parts.x = Global.WIDTH / 2 - text_parts.width / 2;
			text_max.x = Global.WIDTH - text_max.width - 10;
			
			text_parts2.x = Global.WIDTH / 2 - text_parts2.width / 2;
			text_max2.x = Global.WIDTH - text_max2.width - 10;
			
			if ((text_max2.text != text_max.text) && (!text_max2.fade_out) && (Player.player.parts_max != Player.player.parts.length)){				
				text_max2.fade_out = true;			
				text_max2.alpha = 0.1;
			}
			if (text_max2.alpha > 0)
				text_max2.alpha -= 0.001;
			else{
				text_max2.fade_out = false;			
				text_max2.text = text_max.text;
			}					
			
			if ((text_parts.text != "1") && (text_parts2.text != text_parts.text) && (!text_parts2.fade_out)){				
				text_parts2.fade_out = true;
				text_parts2.alpha = 0.1;
			}
			if (text_parts2.alpha > 0)
				text_parts2.alpha -= 0.001;
			else{
				text_parts2.fade_out = false;			
				text_parts2.text = text_parts.text;
			}
			//if (text_max2.alpha < 0.2) text_max2.alpha = 0.2;
			
			remove_map_parts = Player.player.SelfCollide();
			
			var e : MapPart;
			var b : Boolean;
			
			for (var i : int = 0; i < map_parts.length; i++)
				if (map_parts[i]){			
					if (remove_map_parts && map_parts[i].dead)
						map_parts[i].fade_out = true;
						
					b = (map_parts[i].dead && map_parts[i].extra);
					map_parts[i].Collide() 
					if (b && (!map_parts[i].dead)){
						e = new MapPart();
						layer_parts.addChild(e);
						map_parts.push(e);
					}
				}
		}
		
		private function onKeyDown(event:KeyboardEvent):void	{
			if ((event.keyCode == 37) || (event.keyCode == 65)) Player.player.angle_control = -1;
			if ((event.keyCode == 39) || (event.keyCode == 68)) Player.player.angle_control = +1;
			if ((event.keyCode == 27) || (event.keyCode == 32)) menu.visible = !menu.visible;
		}
		
		private function onKeyUp(event:KeyboardEvent):void	{
			if ((event.keyCode == 37) || (event.keyCode == 39)) 
				Player.player.angle_control = 0;
		}		
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			music.play(0, int.MAX_VALUE);
			
			Global.seed = 71;			
			
			timer_update = new Timer(20);
			timer_update.addEventListener(TimerEvent.TIMER, update);
			timer_update.start();			
			
			addChild(sky);
			addChild(sky2);
			
			text_max2.alpha = text_parts2.alpha = 0;
			text_max2.fade_out = text_parts2.fade_out = true;
			addChild(text_max2);			
			addChild(text_parts2);			
			text_max.alpha = 0;
			text_parts.alpha = 0;
			addChild(text_max);
			addChild(text_parts);	
			
			Player.player = new Player();
			addChild(Player.player);
			
			addChild(layer_parts);
			for (var i : int = 0; i < 3; i++) {
				map_parts[i] = new MapPart();
				layer_parts.addChild(map_parts[i]);
			}		
			
			addChild(menu);
			
			addChild(noise);

			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);	
		}
		
	}
	
}