package classes.matuto.mov
{
	
	
	import classes.gustavo.events.EventStartDrag;
	import classes.gustavo.ui.scrollItem.menuList.MenuList;
	import classes.matuto.core.Matuto;
	import classes.matuto.utils.model.MathG;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	

	
	public class Drag extends MovieClip
	{
		
		private var pode_movimentar		:Boolean;
		private var pode_bg				:Boolean = false;
		public  var movie				:*;
		public  var bg					:*
		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  var onSegura			:Function;
		
		
		private var rootStage:Stage;
	
		public function Drag(_movie:*,_bg:*)
		{
			
			super();
			
			
			
			
			pode_movimentar				= true;
			esta_segurando 				= false;
			
			bg							= _bg;
			minimo						= bg.height;
			movie 						= _movie;
			movie.mouseChildren 		= false;
			movie.mouseEnabled	  		= false;
			
			movie.stop();
			
			addEventListener(Event.ADDED_TO_STAGE,addStage);
		
		}
		
		
		private function addStage(e:Event):void
		{
		     rootStage = stage;
			 rootStage.align = "TL";
			 rootStage.scaleMode  = "noScale";
			 inicialize();
		}
		
		
		//===========================
		// INIT, CLOSE , INICIALIZE
		//===========================
		
		public function inicialize():void
		{
			addEvents();
			bg.addEventListener(MouseEvent.ROLL_OVER,over);
			bg.addEventListener(MouseEvent.ROLL_OUT,out);
		}
		
		private function init():void
		{
			Mouse.hide();
			bg.addEventListener(MouseEvent.ROLL_OVER,over);
			bg.addEventListener(MouseEvent.ROLL_OUT,out);
			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
		{
			Matuto.getStage.addEventListener(MouseEvent.MOUSE_DOWN,segurando);
			Matuto.getStage.addEventListener(MouseEvent.MOUSE_UP,soltou);
		}
		
		private function removeEvents():void
		{
			Matuto.getStage.removeEventListener(MouseEvent.MOUSE_DOWN,segurando);
			Matuto.getStage.removeEventListener(MouseEvent.MOUSE_UP,soltou);
		}
		
		//====================
		//====================
		
		//===============================
		//OVER , OUT , SEGURANDO, SOLTOU
		//===============================
		
		private function over(e:MouseEvent):void
		{
			init();
			dispatchEvent(new EventStartDrag(EventStartDrag.OVER_HIT,this));

		}
		
		private function out(e:MouseEvent):void
		{
			
			if(esta_segurando)
			{
				
			}else
			{
				close();
				dispatchEvent(new EventStartDrag(EventStartDrag.OUT_HIT,this));
			}
			
		}
		
		private function segurando(e:MouseEvent):void
		{
			pontoYAtual 	= mouseY;
					
			esta_segurando  = true;
			movie.gotoAndStop(2);
			dispatchEvent(new EventStartDrag(EventStartDrag.SEGURA_MAO,this));
		}
		
		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();
			}
			
		}
	
		public function moveMao():void
		{
			movie.x 	= movie.parent.mouseX;
			movie.y 	= movie.parent.mouseY;
		}
		
		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;
		
		}
		
		//==========================
		//==========================
		
		public function dispose():void
		{
			bg.removeEventListener(MouseEvent.ROLL_OVER,over);
			bg.removeEventListener(MouseEvent.ROLL_OUT,out);
			Mouse.show();
			movie.alpha 		= 0;
			removeEvents();
		}
		
		//================
		//GET e SET
		//================
		
		public function get getPodeMovimentar():Boolean
		{
			return pode_movimentar;
		}
			
		public function get getEstaSegurando():Boolean
		{
			return esta_segurando;
		}
		
		public function set setMaximo(value:Number):void
		{
			maximo = value;
		}
		

		
	}//end class
}