package classes.matuto.ui.scroll
{
	
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import classes.greensock.*;
	import classes.fl.motion.easing.*;
	import classes.fl.transitions.easing.*
	import classes.caurina.transitions.Tweener;
	
	public class Scroll extends Sprite 
	{
		
		private 	var yOffset 		 :Number;
		private 	var yMin    		 :Number;
		private     var yMax    		 :Number;
		
		private    	var  barraMaior      :*;
		private     var  barraMenor      :*;
		private		var  mascara 		 :*;
		private     var  conteudo		 :*;
		
		private     var  border			 :Number;
		private 	var  stagio  		 :Stage;
		private  	var  altura			 :Number;
		
	
		public function Scroll(
		
							  _conteudo:*,
							  _barraMenor:*,
							  _barraMaior:*,
							  _mascara:*
							  
							  ) 
		{
			
			barraMenor    			= _barraMenor
			barraMaior    			= _barraMaior
			mascara       			= _mascara
			conteudo      			= _conteudo;
		
			altura		  			= conteudo.height;
						
			barraMenor.buttonMode   = true;
			addEventListener(Event.ADDED_TO_STAGE, rootStage);
		}
		
		private function rootStage(e:Event):void
		{
			stagio    		 = stage;
			stagio.scaleMode = "noScale";
			stagio.align     = "TL"
			
			barraMenor.addEventListener(MouseEvent.MOUSE_DOWN, segurarBarra);
			stagio.addEventListener(MouseEvent.MOUSE_UP, soltarBarra);	
			stagio.addEventListener(MouseEvent.MOUSE_WHEEL, rola);
			
			yMin 	  = 0;
			yMax 	  = barraMaior.height - barraMenor.height;
			
			barraMaior.addEventListener(MouseEvent.CLICK, clickBarraMaior);
			barraMaior.buttonMode = true;
		}
		
		public function update()
	    {
			yMin 	  = 0;
			yMax 	  = altura - barraMenor.height;
			yOffset   = mouseY - barraMenor.y;
		}
		
		public function upDatePosition(n:Number)
	    {
			Tweener.addTween(barraMenor, { y:n - barraMenor.height} );
			var sp:Number = (barraMenor.y / yMax);
			TweenLite.to(conteudo,1.2, { y:( -sp * (conteudo.height - mascara.height)-border),ease:Exponential.easeInOut } );	
		}
		
		public function set setConteudo(obj:DisplayObject):void
	    {
			conteudo.addChild(obj);
		}
		
		public function set setBorder(_border:Number):void
	    {
		   border = _border;
		}
		
		public function set setAltura(_altura:Number):void
	    {
		   altura = _altura;
		}
		
		private function clickBarraMaior(e:MouseEvent):void
		{			
			Tweener.addTween(barraMenor, { y:testeParadaBarraMenor(mouseY - barraMenor.height)} );
			var sp:Number = (barraMenor.y / yMax);
			TweenLite.to(conteudo,1.2, { y:( -sp * (altura - mascara.height)-border) } );
		}
		
		private function testeParadaBarraMenor(n:Number):Number
	    {
			if (n < 0)
		    {
			   	n = 0;
			}
			
			return n;
		}

		private function segurarBarra(e:MouseEvent):void
		{
			stagio.addEventListener(MouseEvent.MOUSE_MOVE, moverBarra);
			yOffset = mouseY - barraMenor.y;	
		}
		
		private function soltarBarra(e:MouseEvent):void
		{
			stagio.removeEventListener(MouseEvent.MOUSE_MOVE, moverBarra);	
		}
		
		private function moverBarra(e:MouseEvent):void
		{	
			barraMenor.y = mouseY - yOffset;
			
			if (barraMenor.y <= yMin ) {
				barraMenor.y = yMin;
			}
			if (barraMenor.y >= yMax) {
				barraMenor.y = yMax;
			}
			
			var sp:Number = (barraMenor.y / yMax);
			Tweener.addTween(conteudo, { y: -sp * (altura - mascara.height - border), time:1 } );
			
			e.updateAfterEvent();
		}
		
		private function rola(e:MouseEvent):void
		{
		  barraMenor.y += e.delta * - 2;  
		 	  
		  if(barraMenor.y <= yMin){
				barraMenor.y = yMin;
			}
		  if(barraMenor.y >= yMax){
				barraMenor.y = yMax;
			}
			
			var sp:Number = barraMenor.y / yMax;
			Tweener.addTween(conteudo, { y: -sp * (altura - mascara.height-border), time:1 } );
			
			e.updateAfterEvent();
		}
		
		
	}//END CLASS
	
}