package  
{
	import flash.display.Bitmap;
	import flash.display.SimpleButton;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	import flash.sampler.NewObjectSample;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author beisik
	 */
	public class Start extends Sprite 
	{
		[Embed(source = 'textures/bg.png')] private const BACKGROUND: Class;
		[Embed(source = 'textures/b.png')] private const START: Class;
		[Embed(source = 'textures/end.png')] private const END: Class;
		[Embed(source = 'textures/ok.png')] private const OK: Class;
		
		[Embed(source = 'textures/music.mp3')] private const MUSIC: Class;
		
		[Embed(source = 'textures/p1.png')] private const PAUSE: Class;
		[Embed(source = 'textures/p2.png')] private const PLAY: Class;
		[Embed(source = 'textures/m1.png')] private const MUSICON: Class;
		[Embed(source = 'textures/m2.png')] private const MUSICOFF: Class;
		
		public var game: Game;
		public var goScreen: Sprite;
		public var start: Sprite;
		
		public var music: Sound;
		public var channel: SoundChannel;
		
		private var scoreTF: Vector.<TextField> = new Vector.<TextField>(2);
		
		public var volume: Boolean = true;
		public var volumeButton: SimpleButton;
		private var von: Bitmap, voff: Bitmap;
		
		public var pause: Boolean = false;
		public var pauseButton: SimpleButton;
		private var pon: Bitmap, poff: Bitmap;
		
		public function Start(s: Sprite) 
		{
			var bgImg: Bitmap = new BACKGROUND();
			addChild(bgImg);
			
			goScreen = new Sprite();
			var goImg: Bitmap = new END();
			goScreen.addChild(goImg);
			goScreen.visible = false;
			//goScreen.buttonMode = true;
			//goScreen.addEventListener(MouseEvent.CLICK, onEnd);
			addChild(goScreen);
			var ok: Sprite = new Sprite();
			var okImg: Bitmap = new OK();
			ok.addChild(okImg);
			ok.x = (Game.Width - okImg.width) / 2;
			ok.buttonMode = true;
			ok.addEventListener(MouseEvent.CLICK, onEnd);
			goScreen.addChild(ok);
			
			start = new Sprite();
			var stImg: Bitmap = new START();
			start.x = (Game.Width - stImg.width) / 2;
			start.buttonMode = true;
			start.addChild(stImg);
			start.addEventListener(MouseEvent.CLICK, onStart);
			addChild(start);
			
			var label: Vector.<TextField> = new Vector.<TextField>(2);
			for (var i: uint = 0; i < 2; i++)
			{
				label[i] = new TextField();
				label[i].autoSize = TextFieldAutoSize.LEFT;
				label[i].embedFonts = true;
				label[i].defaultTextFormat = new TextFormat("life-font", 32);
				if (i == 1)
					label[i].textColor = 0x656665;
				else
					label[i].textColor = 0xdbdbdb;
				label[i].selectable = false;
				label[i].mouseEnabled = false;
				label[i].y = 280 + (1 - i) * 5;
				label[i].text = "Очки:";
				label[i].x = Game.Width / 2 + (50 - label[i].width) / 2;
				goScreen.addChild(label[i]);
				
				scoreTF[i] = new TextField();
				scoreTF[i].autoSize = TextFieldAutoSize.LEFT;
				scoreTF[i].embedFonts = true;
				scoreTF[i].defaultTextFormat = new TextFormat("life-font", 32);
				if (i == 1)
					scoreTF[i].textColor = 0x656665;
				else
					scoreTF[i].textColor = 0xdbdbdb;
				scoreTF[i].selectable = false;
				scoreTF[i].mouseEnabled = false;
				scoreTF[i].y = 312 + 5 + (1 - i) * 5;
				goScreen.addChild(scoreTF[i]);
			}
			
			// кнопка звук
			von = new MUSICON();
			voff = new MUSICOFF();
			volumeButton = new SimpleButton();
			volumeButton.upState = von;
			volumeButton.downState = von;
			volumeButton.overState = von;
			volumeButton.hitTestState = von;
			volumeButton.x = Game.Width - von.width - 10;
			volumeButton.y = 10; 
			volumeButton.addEventListener(MouseEvent.CLICK, onVolume);
			s.addChild(volumeButton);
			
			// кнопка пауза
			pon = new PAUSE();
			poff = new PLAY();
			pauseButton = new SimpleButton();
			pauseButton.upState = pon;
			pauseButton.downState = pon;
			pauseButton.overState = pon;
			pauseButton.hitTestState = pon;
			pauseButton.x = Game.Width - von.width - pon.width - 10;
			pauseButton.y = 10; 
			pauseButton.addEventListener(MouseEvent.CLICK, onPause);
			pauseButton.visible = false;
			s.addChild(pauseButton);
			
			music = new MUSIC();
			channel = music.play(0, int.MAX_VALUE);
		}
		
		public function onStart(e: Event): void
		{
			start.visible = false;
			this.visible = false;
			
			pauseButton.visible = true;
			game = new Game();
			parent.addChildAt(game, 0);
		}
		
		public function onEnd(e: Event): void
		{
			start.visible = true;
			goScreen.visible = false;
		}
		
		public function gameOver(scores: Number): void
		{
			pauseButton.visible = false;
			this.visible = true;
			scoreTF[0].text = "" + Math.round(scores);
			scoreTF[0].x = Game.Width / 2 + (50 - scoreTF[0].width) / 2;
			scoreTF[1].text = "" + Math.round(scores);
			scoreTF[1].x = Game.Width / 2 + (50 - scoreTF[1].width) / 2;
			goScreen.visible = true;
			parent.removeChild(game);
			game = null;			
		}
		
		public function onVolume(e: Event): void
		{
			volume = !volume;
			if (volume)
			{
				channel = music.play(0, int.MAX_VALUE);
				volumeButton.downState = von;
				volumeButton.upState = von;
				volumeButton.overState = von;
			}
			else
			{
				channel.stop();
				volumeButton.downState = voff;
				volumeButton.upState = voff;
				volumeButton.overState = voff;
			}
		}
		
		public function onPause(e: Event): void
		{
			pause = !pause;
			game.pause(pause);
			if (pause)
			{
				pauseButton.downState = poff;
				pauseButton.upState = poff;
				pauseButton.overState = poff;
			}
			else
			{
				pauseButton.downState = pon;
				pauseButton.upState = pon;
				pauseButton.overState = pon;
			}
		}
		
	}

}