package classes.matuto.mov
{

	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.system.*;
	

	public class ObjectMove 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 possoHit						:Boolean	= true;
		
		public function ObjectMove(_obj:*=null,_type:String="xy",_timeDivision:int=8,_largura:Number=10,_altura:Number=10)
		{
			obj 				= _obj;
			type                = _type;
			timeDivision		= _timeDivision;
					
			largura				= _largura;
			altura				= _altura;
			
											
			inicialize();
		}
		
		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);
			
			return sprite;
		}
		
		private function inicialize():void
		{
			init(type,timeDivision);
			iniciado = true;
		}
		
		private function init(_type:String="xy",_timeDivision:int=8):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;
			
			addEvent();
			
			//hit.addEventListener(MouseEvent.ROLL_OVER,over);

							
			return void;
		}
		
		private function over(e:MouseEvent):void
		{
		
			addEvent();					
		}
		
		private function out(e:Event):void
		{
	
		}
		
		
		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
		{
			removeEvent();	
			return void;
		}
		
		
		public function resize(_largura:Number=10,_altura:Number=10):void
		{
			
		
			
			largura				= _largura;
			altura				= _altura;

			(iniciado)
			{			
				rect 				= new flash.geom.Rectangle(rect.x, rect.y,largura,altura);
				obj.scrollRect 		= rect;	
			}
			
			return void;
		}
		
		private function mouseMoveXY(e:Event):void
		{
		
			var loc1:*			= (Math.round(mouseX*100)/100);
			var loc2:*			= (Math.round(mouseY*100)/100);
			
			var loc3:*			= Math.abs(obj.y - loc2) / rect.height * (r_img.height - rect.height);
			var loc4:*			= Math.abs(obj.x - loc1) / rect.width  * (r_img.width - rect.width);
			
			var auxX:Number		= rect.x + (loc4 - rect.x) / timeDivision;
			var auxY:Number		= rect.y + (loc3 - rect.y) / timeDivision;
		
			rect.x 				= rect.x + (loc4 - rect.x) / timeDivision;
			rect.y 				= rect.y + (loc3 - rect.y) / timeDivision;
			
			obj.scrollRect 		= rect;
		}
		
		private function mouseMoveX(e:Event):void
		{
			
			var loc1:*			= this.mouseX;
			var loc2:*			= this.mouseY;
			
			var loc3:*			= Math.abs(obj.y - loc2) / rect.height * (r_img.height - rect.height);
			var loc4:*			= Math.abs(obj.x - loc1) / rect.width  * (r_img.width - rect.width);
			
			var auxX:Number		= rect.x + (loc4 - rect.x) / timeDivision;
			var auxY:Number		= rect.y + (loc3 - rect.y) / timeDivision;
			
			rect.x 				= rect.x + (loc4 - rect.x) / timeDivision;
			
			obj.scrollRect 		= rect;
			
		}
		
		private function mouseMoveY(e:Event):void
		{
			
			var loc1:*			= this.mouseX;
			var loc2:*			= this.mouseY;
			
			var loc3:*			= Math.abs(obj.y - loc2) / rect.height * (r_img.height - rect.height);
			var loc4:*			= Math.abs(obj.x - loc1) / rect.width  * (r_img.width - rect.width);
			
			var auxX:Number		= rect.x + (loc4 - rect.x) / timeDivision;
			var auxY:Number		= rect.y + (loc3 - rect.y) / timeDivision;
			
			rect.y 				= rect.y + (loc3 - rect.y) / timeDivision;
			
			obj.scrollRect 		= rect;
			
		}
		
		
		
		
	}//end class
}



/*

import classes.internas.home.elementos.ObjectFullMove;

private var moveMouse:ObjectFullMove;

moveMouse = new ObjectFullMove(imagem,"xy",8);
addChild(moveMouse);


moveMouse.close();


*/
