package classes.gustavo.ui.scrollCidadeEstados
{
	
	//MATUTO
	import classes.fl.motion.easing.*;
	import classes.greensock.*;
	import classes.matuto.core.*;
	import classes.matuto.core.module.*;
	import classes.matuto.interfaces.*;
	import classes.matuto.model.*;
	import classes.matuto.mtween.textinho.*;
	import classes.matuto.ui.grid.*;
	import classes.matuto.ui.scroll.Scroll;
	import classes.matuto.ui.simpleControl.*;
	import classes.matuto.utils.display.*;
	import classes.matuto.utils.model.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.text.TextField;
	import flash.utils.*;
	import classes.gustavo.ui.scrollCidadeEstados.ItemText;
	
	
	public class ScrollItemText extends MovieClip
	{
		
		private var grid				:Grid;
		private var alvo				:MovieClip;
		private var dados				:Array;
		
		private var mascara				:MovieClip;
		
		private var resposta			:TextField;
		
		private var estaMontando		:Boolean = false;
		
		public  var	id					:int 	 = 0;
		
		private var scroll				:Scroll;

		
		public function ScrollItemText()
		{
			super();
			
			alvo 				= _alvo;
			mascara				= _mascara;	
			mascara.scaleY 		= 0;
			_barraMenor.scaleY 	= 0;
			_barraMaior.scaleY 	= 0;
		
			resposta	    	= _resposta;
		
		}
		
		public function init(value:Array):void
		{
			dados = value;
			initGrid();	
			_tapia.addEventListener(MouseEvent.CLICK,onClick);
			_tapia.buttonMode = true;
			addEventListener(MouseEvent.MOUSE_OVER,onOverAlvo);
			addEventListener(MouseEvent.MOUSE_OUT, onOutAlvo);	
			
			scroll = new Scroll(alvo,_barraMenor,_barraMaior,_mascara);
			scroll.setBorder = 1;
			addChild(scroll);
		}
		
		//#################################################
		//NAVEGACAO
		//#################################################
		
		private function onClick(e:MouseEvent):void
		{
			if(!estaMontando)
			{
				
				TweenLite.to(mascara,.8,{scaleY:1,ease:Quintic.easeInOut});	
				TweenLite.to(_barraMaior,.8,{scaleY:1,ease:Quintic.easeInOut});	
				TweenLite.to(_barraMenor,.8,{scaleY:1,ease:Quintic.easeInOut});	
				TweenMax.to(_seta,.8,{frame:5,ease:Quintic.easeInOut,onComplete:function():void{
						
					//stage.addEventListener(MouseEvent.CLICK,onClick);
					estaMontando = true;
					
					scroll = new Scroll(alvo,_barraMenor,_barraMaior,_mascara);
					scroll.setBorder = 1;
					addChild(scroll);

				}});	
			
			}else
			{
				
				TweenLite.to(mascara,.8,{scaleY:0,ease:Quintic.easeInOut});	
				TweenLite.to(_barraMaior,.8,{scaleY:0,ease:Quintic.easeInOut});	
				TweenLite.to(_barraMenor,.8,{scaleY:0,ease:Quintic.easeInOut});	
				TweenMax.to(_seta,.8,{frame:0,ease:Quintic.easeInOut});	
				estaMontando  = false;
				//stage.removeEventListener(MouseEvent.CLICK,onClick);

			}
		}
		
		private function onOverAlvo(e:MouseEvent):void
		{
			//stage.removeEventListener(MouseEvent.CLICK,onClick);
		}
		
		private function onOutAlvo(e:MouseEvent):void
		{
			//stage.addEventListener(MouseEvent.CLICK,onClick);	
		}
		
		//#################################################
		//#################################################

		
		//#################################################
		//SET
		//#################################################

		public function set setTexto(texto:String):void
		{
			resposta.text = texto;
		}
		
		//#################################################
		//#################################################

		
		
		//#################################################
		//GRID
		//#################################################
		
		private function initGrid():void
		{
			grid  	  					 = new Grid();
			grid.addEventListener(GridEvent.SELECT, selectGrid);
			grid.inicialize(dados.length,GridType.SIMPLE,alvo,ItemText,false, setDados,0,0,8,4,{ horizontal:0,vertical:19} );	
		}
		
		private function selectGrid(e:GridEvent):void 
		{		
			id = e.getParams.posicao;
			dispatchEvent(new Event(Event.SELECT));
		}
		
		public function update(value:Array):void
		{
		  	dados 		 = value;
			
			var aux:int = 0;
			while(alvo.numChildren)
			{
				alvo.removeChildAt(0);
				aux++;
			}	
			
			grid.removeEventListener(GridEvent.SELECT, selectGrid);
			grid = null;
			
			initGrid();
		
			_barraMenor.y = 0;	
			_alvo.y		  = 0;
			scroll = new Scroll(alvo,_barraMenor,_barraMaior,_mascara);
			scroll.setBorder = 1;
			addChild(scroll);
			
		}
		
		private function setDados(item:Button,id:int,posicao:int):void
		{
			var objeto:Object 			  		   = new Object;
			
			objeto.id 					  		   = id;
			objeto.posicao 		   		  		   = posicao;
			objeto.texto						   = dados[posicao];
			item.setParams 				   		   = objeto;
		}
		
		//#################################################
		//#################################################
		
		public function close():void
		{
		}
		
		public function resize():void
		{
		}
		
		public function dispose():void
		{
		}
		
	}//end class
}