package classes.matuto.mov
{
	
	
	import classes.gustavo.events.EventStartDrag;
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.system.*;
	

	
	
	public class ObjectStartDrag extends Sprite
	{
		
		
		private var rootX							:Number;
		private var rootY							:Number;
		
		private var r_mask							:Rectangle;
		private var rect						 	:Rectangle;
		private var r_img							:Rectangle;	
		private var retangulo						:Rectangle;
		
		private var obj								:*;
		private var type							:String;
		private var timeDivision					:int;
		
		public var  iniciado						:Boolean = false;
		
		private var altura							:Number;
		private var largura							:Number;
		
		private var hit								:Sprite;
		private var mao								:MovieClip;
		
		private var possoHit						:Boolean	= true;
		
		private var limiteX							:Number;
		private var limiteY							:Number;
		
		private var offsetX							:Number;
		private var movimentoX						:Number;
		private var xMouseAtual						:Number;
		
		private var offsetY							:Number;
		private var movimentoY						:Number;
		private var yMouseAtual						:Number;
		
		private var dragM							:Drag;
		
		
		
		public function ObjectStartDrag(_obj:*=null,_mao:MovieClip=null,_type:String="xy",_timeDivision:int=8,_largura:Number=10,_altura:Number=10)
		{
			
			obj 				= _obj;
			type                = _type;
			timeDivision		= _timeDivision;
			
			largura				= _largura;
			altura				= _altura;
			mao					= _mao;
					
			hit					= getHit;
			
			limiteX				= obj.width;
			limiteY				= obj.height;
			
			
			addEventListener(Event.ADDED_TO_STAGE,onStage);
			
		}
		
		private function onStage(e:Event):void
		{
			inicialize();
		}
		
		
		
		//=======================
		//INIT
		//=======================
		
		
		public function inicialize():void
		{
			init(type,timeDivision);
			iniciado = true;
			initMao();
		}
		
		private function init(_type:String="xy",_timeDivision:int=8):void
		{
			
			type 			= _type;
			timeDivision 	= _timeDivision;
			
			addMascara();
			
			return void;
	
		}
		
		private function addMascara():void
		{
			rect 				= new flash.geom.Rectangle(0,0,largura,altura);
			r_img 				= new flash.geom.Rectangle(0,0, obj.width, obj.height);
			r_mask 				= new flash.geom.Rectangle(0,0, rect.width, rect.height);
			obj.scrollRect 		= rect;
		}
		
		
		private function get getHit():Sprite
		{
			var sprite:Sprite = new Sprite();
			sprite.graphics.beginFill(0x000000);
			sprite.graphics.drawRect(obj.x,obj.y,largura,altura);
			sprite.graphics.endFill();
			
			sprite.alpha = 0;
			addChild(sprite);
			
			setChildIndex(sprite,numChildren-1);
			
			return sprite;
		}
		
		
		//=======================
		//=======================
		
		
		//=======================
		//MAO
		//=======================
		
		private function initMao():void
		{
			dragM				= new Drag(mao,hit);
			addMao();
			dragM.inicialize();
		}
		
		private function segurando(e:EventStartDrag):void
		{
			xMouseAtual      = mouseX;
			offsetX			 = mouseX + rect.x; 
			
			yMouseAtual      = mouseY;
			offsetY			 = mouseY + rect.y; 
		}
		
		private function soltou(e:EventStartDrag):void
		{
			
		}
		
		private function over(e:EventStartDrag):void
		{
			addEvent();		
		}
		
		private function out(e:EventStartDrag):void
		{
			removeEvent();
		}
		
		private function removeMao():void
		{
			dragM.removeEventListener(EventStartDrag.SEGURA_MAO,segurando);
			dragM.removeEventListener(EventStartDrag.SOLTA_MAO,soltou);
			dragM.removeEventListener(EventStartDrag.OVER_HIT,over);
			dragM.removeEventListener(EventStartDrag.OUT_HIT,out);
		}
		
		private function addMao():void
		{
		
			dragM.addEventListener(EventStartDrag.SEGURA_MAO,segurando);
			dragM.addEventListener(EventStartDrag.SOLTA_MAO,soltou);
			dragM.addEventListener(EventStartDrag.OVER_HIT,over);
			dragM.addEventListener(EventStartDrag.OUT_HIT,out);
		
		}
		
		//=======================
		//=======================
		
		
	
		
		
		//=========================
		//EVENT
		//=========================
	
		private function addEvent():void
		{
			if(type=="xy")
			this.addEventListener(flash.events.Event.ENTER_FRAME, mouseMoveXY);
			if(type=="x")
			this.addEventListener(flash.events.Event.ENTER_FRAME, mouseMoveX);
			else
			this.addEventListener(flash.events.Event.ENTER_FRAME, mouseMoveY);	
		}
		
		private function removeEvent():void
		{
			if(type=="xy")
			this.removeEventListener(flash.events.Event.ENTER_FRAME, mouseMoveXY);
			if(type=="x")
			this.removeEventListener(flash.events.Event.ENTER_FRAME, mouseMoveX);
			else
			this.removeEventListener(flash.events.Event.ENTER_FRAME, mouseMoveY);	
		}
		
		//=========================
		//=========================

		
		public function close():void
		{
			dragM.dispose();
			removeMao();
			removeEvent();	
			return void;
		}
	
		public function resize(_largura:Number=10,_altura:Number=10):void
		{
			hit.width  			= _largura;
			hit.height 			= _altura;
			
			largura				= hit.width;
			altura				= hit.height;
			
			(iniciado)
			{			
				rect 				= new flash.geom.Rectangle(rect.x, rect.y,largura,altura);
				obj.scrollRect 		= rect;	
			}
			
			return void;
		}
			
		//=========================
		//moviments
		//=========================
		
		private function mouseMoveXY(e:Event):void
		{
			dragM.moveMao();
			
			movimentoX 			   = (mouseX - offsetX) * -1;	
			movimentoY 			   = (mouseY - offsetY) * -1;	
			
			if(dragM.getEstaSegurando)
			{
				rect.x 				= getMovimentoX(movimentoX);
				rect.y				= getMovimentoY(movimentoY);		
			}
			
			obj.scrollRect 		= rect;		
		}
		
		private function mouseMoveX(e:Event):void
		{
			dragM.moveMao();
			
			movimentoX 			   = (mouseX - offsetX) * -1;	
			
			if(dragM.getEstaSegurando)
			{
				rect.x 				= getMovimentoX(movimentoX);
			}
			
			obj.scrollRect 		= rect;		
		}
		
		private function mouseMoveY(e:Event):void
		{
			dragM.moveMao();
			
			movimentoY 			   = (mouseY - offsetY) * -1;	
			
			if(dragM.getEstaSegurando)
			{
				rect.y				= getMovimentoY(movimentoY);		
			}
			
			obj.scrollRect 		= rect;			
		}
		
		//=========================
		//=========================
		
		
		//=========================
		//condicoes paradas
		//=========================

		public function getMovimentoX(n:Number):Number
		{

			if (n+rect.width >= limiteX)
			{
				n = limiteX - rect.width;	
				
			}else
			if (n <= 0)
			{
				n = 0;	 	
			}
			
			return n;
		
		}
		
		public function getMovimentoY(n:Number):Number
		{
			
			
			if (n+rect.height >= limiteY)
			{
				n = limiteY - rect.height;	
				
			}else
			if (n <= 0)
			{
					n = 0;	 	
			}
			
			return n;
			
		}
		
		
		
		
	}//end class
}



/*

*/
