package classes.gustavo.midia.player 
{
	import classes.gustavo.media.player.NomeMusic;
	import flash.display.*;
	import flash.events.Event;
	import flash.media.*
		


	public class Player extends Sprite
	{
		
		private var dados					:Array;
		
		private var paginaAtual				:int;
		
		private var prevBtn					:MovieClip;
		private var nextBtn					:MovieClip;
		private var playBtn					:MovieClip;
		private var pauseBtn				:MovieClip;
		private var nomeMusica				:NomeMusic;
		private var volume					:Volume;
		private var som						:Som;
		private var total					:int;
		
		
		public function Player() 
		{
			
			playBtn 				= _play_mc;
			pauseBtn				= _pause_mc;
			nextBtn 				= _next_mc;
			prevBtn					= _prev_mc;
			nomeMusica				= _nomeMusica_mc;
			volume					= _volume_mc;
			som						= new Som();
			
			addEventBotao();
			
			
		}
		
		private function addEventBotao():void
		{
			
			playBtn.removeEvent();
			
			nextBtn.setIdName = "next";
			prevBtn.setIdName = "prev";
			
			playBtn.addEventListener(Botao.SELECT,selectPlay);
			pauseBtn.addEventListener(Botao.SELECT, selectPause);
			
			nextBtn.addEventListener(Botao.SELECT,selectControl)
			prevBtn.addEventListener(Botao.SELECT,selectControl)
			
			
		}
		
		private function selectControl(e:Event):void 
		{
			
			var idBotao:String = e.currentTarget.getIdName;
			
			if (idBotao == "next")
			paginaAtual++; 
			else
			paginaAtual--; 
			
			paginaAtual = validaPagina(paginaAtual);
			
			update();
			
		}
		
		private function validaPagina(n:int):int
		{
			
			if (n == 0)
			{
				prevBtn.removeEvent();
				nextBtn.addEvent();
			}
			else
			if (n == total)
			{
				prevBtn.addEvent();
				nextBtn.removeEvent();
				
			}else
			{
				prevBtn.addEvent();
				nextBtn.addEvent();
			}
			
			return n;
		}
		
		private function selectPlay(e:Event):void 
		{
			playBtn.removeEvent();
			pauseBtn.addEvent();
			
			nomeMusica.playText();
			som.playSound();
			
		}
		
		private function selectPause(e:Event):void 
		{
			pauseBtn.removeEvent();
			playBtn.addEvent();
			
			nomeMusica.stopText();
			som.stopSound();

		}
	
		public function set setDados(_dados:Array):void
		{
			
			if (_dados != null)
			{
				dados 		= _dados;
				total 		= dados.length - 1;	
				paginaAtual	= validaPagina(0);
				
			}
			else
			throw new Error("DADOS DO PLAYER NULL");
			
			return void;
		}
		
		public function init():void
		{
			
			update();
			
			return void;
		}
		
		private function update():void
		{
			
			
			var tituloAtual		:String = dados[paginaAtual]['titulo']
			var urlMusicaAtual	:String = dados[paginaAtual]['urlMusica']
			
			playBtn.removeEvent();
			pauseBtn.addEvent();

			
			nomeMusica.reset();
			nomeMusica.setText 			= tituloAtual;
			nomeMusica.init();
			
			som.reset();
			som.setVolume  			    = volume;
			som.loadSound(urlMusicaAtual);

		}
		
		
		
		
			
	}//end class

}