package classes.matuto.mov
{
	
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	
	final public class MouseScroll
	{
		
		private static var _scrolls_obj :Object = new Object();
		
		private static var render_sprite	:Sprite = new Sprite();
		
		public static function addMouseScroll(obj :DisplayObject, params :Object) :void
		{
			
			var dados :Object = new Object();
			
			dados.obj 			= obj;
			dados.targetMouse	= params.targetMouse;
			dados.targetScroll	= params.targetScroll;
			dados.ease			= params.ease;
			dados.x				= params.x;
			dados.y				= params.y;
			dados.moveX			= params.moveX;
			dados.moveY			= params.moveY;
			
			if(dados.moveX==undefined) dados.moveX = true;
			if(dados.moveY==undefined) dados.moveY = true;
			
			if(!dados.targetMouse) dados.targetMouse = obj;
			
			if(!dados.targetScroll){
				
				dados.width 	= params.width;
				dados.height 	= params.height;
				
				
			}
			
			_scrolls_obj[obj.name] = dados;
			
			if(!render_sprite.hasEventListener(Event.ENTER_FRAME)) render_sprite.addEventListener( Event.ENTER_FRAME, render );
			
		}
		
		
		public static function removeMouseScroll(obj :DisplayObject) :void
		{
			
			delete _scrolls_obj[obj.name];
			
		}
		
		
		private static function render(e :Event) :void
		{
			
			for(var i :String in _scrolls_obj){
				
				var dados :Object = _scrolls_obj[i];
				
				var obj 			:DisplayObject 	= dados.obj;
				var alvo_mouse 		:DisplayObject 	= dados.targetMouse;
				var alvo_scroll 	:* 				= dados.targetScroll;
				var _posx			:Number			= dados.x;
				var _posy			:Number			= dados.y;
				var ease 			:Number 		= dados.ease;
				var _width_scroll	:Number			= dados.width;
				var _height_scroll	:Number			= dados.height;
				var moveX			:Boolean		= dados.moveX;
				var moveY			:Boolean		= dados.moveY;
				
				//----
				
				var _width			:Number;
				var _height			:Number;
				var _xmouse 		:Number = alvo_mouse.mouseX;
				var _ymouse			:Number = alvo_mouse.mouseY;
				
				var _posx_bool :Boolean = isNaN(_posx);
				var _posy_bool :Boolean = isNaN(_posy);
				
				if(alvo_scroll){
					
					if(alvo_scroll is Stage){
						_width 	= alvo_scroll.stageWidth;
						_height = alvo_scroll.stageHeight;
						
						if(!_posx_bool) _posx = alvo_scroll.root.x;
						if(!_posy_bool) _posx = alvo_scroll.root.y;
						
					}else if(alvo_scroll is DisplayObject){
						_width 	= alvo_scroll.width;
						_height = alvo_scroll.height;
						
						if(!_posx_bool) _posx = alvo_scroll.x;
						if(!_posy_bool) _posx = alvo_scroll.y;
					}
					
				}else{
				
					_width 	= _width_scroll;
					_height	= _height_scroll;
					
				}
				
				var pct_x :Number = _xmouse/_width;
				var pct_y :Number = _ymouse/_height;
				
				var _x :Number = -obj.width*pct_x + _width*.5;
				var _y :Number = -obj.height*pct_y + _height*.5;
				
				if(_x>_posx) _x = _posx;
				if(_x< (-obj.width+_width) + _posx ) _x = (-obj.width+_width) + _posx;
				if(_y>_posy) _y = _posy;
				if(_y< (-obj.height+_height) + _posy ) _y = (-obj.height+_height) + _posy;
				
				_x = obj.width>_width ? _x : _posx;
				_y = obj.height>_height ? _y : _posy;
				
				if(!ease) ease = .1;
				
				if(moveX) obj.x += (_x -obj.x)*ease;
				if(moveY) obj.y += (_y -obj.y)*ease;
				
				//dispatchEvent( new MouseScrollEvent(MouseScrollEven.ON_RENDER, {obj:obj, pctx:pct_x, pcty:pct_y } ) );
						
			}
			
		}
		
		
		
		//##############################################
		// EVENT CONTROLS
		//##############################################
		
		
		public static function addEventListener(type :String, listener :Function, useCapture :Boolean = false, priority :int = 0, useWeakReference :Boolean = false) :void
		{
			render_sprite.addEventListener(type, listener, useCapture, priority);
		}
		
		public static function dispatchEvent(evt :Event) :Boolean
		{
			return render_sprite.dispatchEvent(evt);
		}
		
		public static function hasEventListener(type :String) :Boolean
		{
			return render_sprite.hasEventListener(type);
		}
		
		public static function removeEventListener(type :String, listener :Function, useCapture :Boolean = false) :void
		{
			render_sprite.removeEventListener(type, listener, useCapture);
		}
		
		public static function willTrigger(type :String) :Boolean
		{
			return render_sprite.willTrigger(type);
		}
		
		
	}
}