package classes.gustavo.ui.scrollItem
{
	
	import classes.gustavo.events.EventStartDrag;
	import classes.gustavo.ui.scrollItem.menuList.MenuList;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	
	public class DragItem extends MovieClip
	{
		
		private var pode_movimentar		:Boolean;
		private var pode_bg				:Boolean = false;
		private var movie				:MovieClip;
		private var bg					:MovieClip;
		private var controle			:MovieClip;
		private var esta_segurando		:Boolean;
		
		private var pontoYAtual			:Number;
		private var sobra				:Number;
		private var maximo				:Number;
		private var minimo				:Number;
				
		public function DragItem()
		{
			
			super();
			
			
			pode_movimentar				= true;
			esta_segurando 				= false;
			
			bg							= this['_bg'];
			minimo						= bg.height;
			movie 						= this['_movie'];
			movie.mouseChildren 		= false;
			movie.mouseEnabled	  		= false;
			movie.alpha 				= 0;
			
			movie.stop();
			
			bg.alpha   					= .8
		
		}
		
		
		//===========================
		// INIT, CLOSE , INICIALIZE
		//===========================
		
		
		public function inicialize():void
		{
			addEvents();
			bg.addEventListener(MouseEvent.ROLL_OVER,over);
			bg.addEventListener(MouseEvent.ROLL_OUT,out);
		}
		
		public function init():void
		{
			Mouse.hide();
			movie.x 			= mouseX;
			movie.y 			= mouseY;
			movie.alpha 		= 1;
			addEvents();
		}
		
		
		public function close():void
		{
			Mouse.show();
			movie.alpha 		= 0;
			removeEvents();
		}
		
		
		
		//===========================
		//===========================

		
		
		//====================
		// EVENTS
		//====================
		
		
		private function addEvents():void
		{
			bg.addEventListener(MouseEvent.MOUSE_DOWN,segurando);
			stage.addEventListener(MouseEvent.MOUSE_UP,soltou);
		}
		
		private function removeEvents():void
		{
			stage.removeEventListener(MouseEvent.MOUSE_DOWN,segurando);
			stage.removeEventListener(MouseEvent.MOUSE_UP,soltou);
		}
		
		
		
		//====================
		//====================
		
		
		//===============================
		//OVER , OUT , SEGURANDO, SOLTOU
		//===============================
		
		private function over(e:MouseEvent):void
		{
			INIT_RENDER();
		}
		
		private function out(e:MouseEvent):void
		{
			
			if(esta_segurando)
			{
				
			}else
			{
				CLOSE_RENDER();	
			}
			
		}
		
		private function segurando(e:MouseEvent):void
		{
			
			pontoYAtual 	= mouseY;
			sobra			= (bg.height-pontoYAtual)
		
			pode_bg			= true;
			
			esta_segurando  = true;
			movie.gotoAndStop(2);
			dispatchEvent(new EventStartDrag(EventStartDrag.SEGURA_MAO));
		
		}
		
		private function soltou(e:MouseEvent):void
		{
			esta_segurando = false;
			pode_bg		   = false;

			movie.gotoAndStop(1);
			dispatchEvent(new EventStartDrag(EventStartDrag.SOLTA_MAO));

			
			if(testeColisionMao_e_colision)
			{
				
				
			}else{
				
				close();
				CLOSE_RENDER();
				
			}
			
		}
	
		
		//==========================
		//==========================
		
		
		
		//==========================
		//RENDER
		//==========================

		
		
		private function INIT_RENDER():void
		{
			init();
			addEventListener(Event.ENTER_FRAME,RENDER);	
		}
		
		
		private function CLOSE_RENDER():void
		{
			close();
			removeEventListener(Event.ENTER_FRAME,RENDER);	
		}
		

		
		private function RENDER(e:Event):void
		{
			moveMao();
			moveHeightBg();

		}
		
		private function moveMao():void
		{
			if(pode_movimentar)
			{		
				movie.x 	= mouseX;
				movie.y 	= mouseY;
			}	
		}
		
		private function moveHeightBg():void
		{
			if(pode_bg)
			{
				bg.height 	= testeLimites(mouseY+sobra);
				dispatchEvent(new EventStartDrag(EventStartDrag.RENDER_SEGURANDO));
			}
		}

		private function get testeColisionMao_e_colision():Boolean
		{
			return movie.hitTestObject(bg);
		}
		
		private function testeLimites(value:Number):Number
		{
		
			if(value>=maximo)
			{
			  value = maximo;
			}else 
			if(value<=minimo)
			{ 
			  value = minimo;	
			}
			
			return value;
		
		}
		
		//==========================
		//==========================
		
		//================
		//GET e SET
		//================
		
		public function get getPodeMovimentar():Boolean
		{
			return pode_movimentar;
		}
		
		public function get getCurrentScroll():Number
		{
		   return bg.height;
		}
		
		public function get getEstaSegurando():Boolean
		{
			return esta_segurando;
		}
		
		public function set setMaximo(value:Number):void
		{
			maximo = value;
		}
		

		
	}//end class
}