package  classes.gustavo.ui.scrollItem
{
	
	import classes.gustavo.events.EventStartDrag;
	import classes.gustavo.ui.scrollItem.menuList.Item;
	import classes.gustavo.ui.scrollItem.menuList.MenuList;
	
	import flash.display.Sprite;
	
	public class ScrollItem extends Sprite
	{
		
		
		private var alturaItens			:Number;
		private var total				:int;
		private var quatidade			:int;
		
		private var menuList			:MenuList;
		private var drag				:DragItem;
		private var itemAtual			:Item;
		private var itemAnterior		:Item;
		
		private var idAtual				:int = 0;
		private var idAnterior			:int;
		
		public function ScrollItem()
		{
			super();
			
			
			menuList 		= 		_menuList;
			drag			=       _drag;
			
		}
		
		
		//==========================
		//INIT e CLOSE e INICIALIZE
		//==========================

		public function inicialize(_total:int,_alturaItens:Number):void
		{
			total 		= _total;
			alturaItens	= _alturaItens;
			
			INICIALIZE_MENU_LIST();
			INICIALIZE_DRAG();
		}
		
		public function init():void
		{
			
		}
		
		public function close():void
		{
			
		}
		
		public function resize(_alturaItens:Number):void
		{
			alturaItens    = _alturaItens;
	       	menuList.resize(getAlturaItens);
			drag.setMaximo = menuList.getContainer.height;
		}
		
		//===============
		//===============
		
		//===============
		// MENU LIST
		//===============
		
		protected function INICIALIZE_MENU_LIST():void
		{
			menuList.init(total,getAlturaItens);
		}
		
		private function get getAlturaItens():Number
		{
			return alturaItens/total;
		}
		
		
		
		//===============
		//===============
		
		
		//===============
		// DRAG
		//===============
		
		protected function INICIALIZE_DRAG():void
		{
			drag.inicialize();
			drag.setMaximo = menuList.getContainer.height;
			drag.addEventListener(EventStartDrag.RENDER_SEGURANDO,RENDER_SEGURANDO);
		}
		
		private function RENDER_SEGURANDO(e:EventStartDrag):void
		{ 
			
			testeValidationItens();
			
			if(testeValidationId())
			{
				dispatchEvent(new EventStartDrag(EventStartDrag.SELECT_POSICAO));
	
			};
			
		}
		
		private function testeValidationItens()
		{		
			
			for each(var item:Item in menuList.getItens)
			{
				if(drag.getCurrentScroll > item.y && drag.getCurrentScroll < (item.y+item.height) )
				{
					idAnterior = idAtual;
				    idAtual    = item.getId;
				}
			}
			
		}
		
		private function testeValidationId():Boolean
		{
			if(idAtual != idAnterior)
			{
			   return true;
			}else
			{
			   return false;	
			}
		}
		
		public function get getId():int
		{
			return idAtual;
		}
		
		//===============
		//===============
		
		
		//===============
		// RENDER
		//===============
		
		
		//===============
		//===============
		
		
		//===============
		// CALCULOS
		//===============
		
		
		//===============
		//===============
		
		
		
		
		
		
	}//end class
}