Suagencia.Classes.ScrollBar = Class.create();

Suagencia.Classes.ScrollBar.prototype = {
	pointerY:0,
	initialize: function ( myDiv , vOptions )
	{
		var myID = "scrollarea-" + Suagencia.ScrollBars.items.size();
		if( vOptions )
			{this.Options = Suagencia.ScrollBars.defaultOptions.merge( vOptions );}
		else
			{this.Options = Suagencia.ScrollBars.defaultOptions.clone();}

		this.initialized = false
		this.identification = myID;
		Suagencia.ScrollBars.items.set( myID ,  this );

		MyWraper = new Element("div" , {"class":"scrollarea-ativo","suag:objeto":"scrollWrapper","id":myID} );
		myDiv.childElements().each( function( s ) { this.insert( s ) } , MyWraper );
		myDiv.update(MyWraper);
	
		myDiv.appendChild( MyWraper );
				
		vScrollbar 		= new Element("div" , {"class":"scrollbar-ativo"} );
		vButtonUp 		= new Element("button" , {"class":"botao up"} );
		vButtonDown 	= new Element("button" , {"class":"botao down"} );
		vScrollArea 	= new Element("div" , {"class":"scrollLine"} );
		vScrollButton 	= new Element("button" , {"class":"scrollButton"} )

		vScrollArea.appendChild( vScrollButton );

		vScrollbar.appendChild( vButtonUp );
		vScrollbar.appendChild( vScrollArea );
		vScrollbar.appendChild( vButtonDown );

		myDiv.insert( vScrollbar );		


		/*
		this.ScrollDiv 		= $(myID);
		this.ButtonDown 	= myDiv.select(".down").first();
		this.ButtonUp 		= myDiv.select(".up").first();
		this.Scrollbar 		= myDiv.select(".scrollbar-ativo").first();
		this.ScrollArea  	= this.Scrollbar.select(".scrollLine").first();
		this.ScrollButton 	= this.Scrollbar.select(".scrollButton").first();
		*/

		this.ScrollDiv 		= $(myID);
		this.ButtonDown 	= vButtonDown;
		this.ButtonUp 		= vButtonUp;
		this.Scrollbar 		= vScrollbar;
		this.ScrollArea  	= vScrollArea;
		this.ScrollButton 	= vScrollButton;

		this.ButtonDown.addClassName( "botao" );
		this.ButtonDown.addClassName( "down" );
		this.ButtonUp.addClassName( "botao" );
		this.ButtonUp.addClassName( "up" );
		this.Scrollbar.addClassName( "scrollbar-ativo" );
		this.ScrollArea.addClassName( "scrollLine" );
		this.ScrollButton.addClassName( "scrollButton" );

		this.AlturaScroll		= 0; 	// Altura da area do botão scroll
		this.AlturaScrollBall 	= 0; 	// Altura do Botão de scroll
		this.MaxBar 		= 0; 	// Quantos pixels o botão scroll percorrerá
		this.MaxScroll 		= 0;	// Quantos pixels a area pode ser rolada
		this.AlturaArea		= 0;  // Altura da Area de scroll

		

		this.calculaDimensoesOnce();
		
		myDiv.style.overflow = "visible"
		
		this.ScrollButton.style.top = "0px";

		this.ButtonDown.observe("mousedown" , this.buttonDownPressed.bindAsEventListener( this ) );
		this.ButtonDown.observe("mouseup" , this.buttonDownReleased.bindAsEventListener( this ) );
		this.ButtonDown.observe("mouseout" , function () {clearTimeout(Suagencia.ScrollBars.trackTime);}) ;

		this.ButtonUp.observe("mousedown" , this.buttonUpPressed.bindAsEventListener( this ) );
		this.ButtonUp.observe("mouseup" , this.buttonUpReleased.bindAsEventListener( this ) );
		this.ButtonUp.observe("mouseout" , function () {clearTimeout(Suagencia.ScrollBars.trackTime);}) ;

		myDiv.observe("DOMMouseScroll", this.onMouseWheel.bindAsEventListener( this ));
		myDiv.observe("mousewheel", this.onMouseWheel.bindAsEventListener( this ));
		myDiv.observe("keydown", this.keyDown.bindAsEventListener( this ) );


		myDiv.observe("suag:ajaxlink-AfterUpdateArea" , this.ajaxUpdate.bindAsEventListener( this ) );

		this.ScrollButton.observe("mousedown" , this.startDrag.bindAsEventListener( this ) );
		this.updateScroll();
		this.initialized = true
		vMyCount = 0
	},
	calculaDimensoesOnce: function()
	{
		this.AlturaArea = this.ScrollDiv.getHeight(); 
		this.AlturaScroll = this.AlturaArea - this.ButtonUp.getHeight() - this.ButtonDown.getHeight()
		this.ScrollArea.style.height = (this.AlturaScroll) + "px" ;
		this.DefaultScrollBall = this.ScrollButton.getHeight();
		this.update();
	},
	update: function()
	{
		if( this.ScrollDiv.getHeight() < this.ScrollDiv.scrollHeight )
		{
			this.ButtonDown.disabled = false;
			this.ButtonUp.disabled = false;
			this.ScrollButton.disabled = false;

			this.ScrollDiv.removeClassName("scrollarea-inativo");
			this.Scrollbar.removeClassName("scrollbar-inativo");

			this.ScrollDiv.addClassName("scrollarea-ativo");
			this.Scrollbar.addClassName("scrollbar-ativo");

			this.AlturaArea = this.ScrollDiv.getHeight(); 
			this.AlturaScroll = this.AlturaArea - this.ButtonUp.getHeight() - this.ButtonDown.getHeight()
			this.MaxBar = this.AlturaScroll - this.DefaultScrollBall;
			this.MaxScroll = this.ScrollDiv.scrollHeight - this.ScrollDiv.getHeight() 

			if( this.MaxBar >= this.MaxScroll )
			{
				if( this.AlturaScrollBall <= ( this.MaxBar - this.MaxScroll ))
				{
					if( this.Options.get("fixedScrollButton") == false )
						{this.AlturaScrollBall = parseInt( this.MaxBar - this.MaxScroll); }
					else
						{this.AlturaScrollBall = parseInt(this.DefaultScrollBall)}
				}
			} else
				{this.AlturaScrollBall = parseInt(this.DefaultScrollBall)}

		} else
		{
			this.ButtonDown.disabled = true;
			this.ButtonUp.disabled = true;
			this.ScrollButton.disabled = true;

			this.ScrollDiv.removeClassName("scrollarea-ativo");
			this.Scrollbar.removeClassName("scrollbar-ativo"); 

			this.ScrollDiv.addClassName("scrollarea-inativo");
			this.Scrollbar.addClassName("scrollbar-inativo");
		}

		if( this.AlturaScrollBall != 0 )
			{this.ScrollButton.setStyle( {"height": (this.AlturaScrollBall + "px") } );}
		else 
			{this.ScrollButton.setStyle( {"height": (this.DefaultScrollBall + "px") } );}
		this.MaxBar = this.AlturaScroll - this.AlturaScrollBall;

		if( this.ScrollDiv.scrollTop != 0 )
			{this.updateScroll();}
	},
	scrollUp: function() {
		this.ScrollDiv.scrollTop += -10;
		this.updateScroll();
	},

	scrollDown: function() {
		this.ScrollDiv.scrollTop += 10;
		this.updateScroll();
	},

	startDrag: function( event ) {
		var pointerY = event.pointerY();
		var initialScroll = parseInt( this.ScrollButton.style.top )

		this.listener1 = this.dragged.bindAsEventListener( this , pointerY ,  initialScroll );
		this.listener2 = this.stopDrag.bindAsEventListener( this );
		document.observe( 'mousemove' , this.listener1 );
		document.observe( 'mouseup' , this.listener2 );	
	},
	stopDrag: function ()
	{
		Event.stopObserving( document , 'mousemove' , this.listener1 )
		Event.stopObserving( document ,'mouseup' , this.listener2 );
	},
	dragged: function(event, initialY , initialScroll) {

		var scrolledPix = (event.pointerY() - initialY + initialScroll );

		if( isNaN( this.MaxScroll ) || this.MaxScroll == 0 ) 
			{this.update();}

		var vFinalPos = scrolledPix
		if( vFinalPos <= 0 )
		{
			vFinalPos = 0;
			this.ScrollDiv.scrollTop = 0;
			
		}else if( vFinalPos >= this.MaxBar )
		{
			vFinalPos = this.MaxBar;
			this.ScrollDiv.scrollTop = this.MaxScroll;	
		}else
		{
			vScroll = ( this.MaxScroll * vFinalPos )/this.MaxBar;
			this.ScrollDiv.scrollTop = Math.round( vScroll );
		}

		this.ScrollButton.style.top = vFinalPos + "px"

	},
	ajaxUpdate: function () {
		this.ScrollDiv.scrollTop = 0;
		this.update();
		this.updateScroll();
	},
	updateScroll: function() {
		var vAlturaAtual = this.ScrollDiv.scrollTop;
		if (this.ScrollDiv.scrollTop > this.MaxScroll && this.initialized)
		{
			this.update();
		}
		if( isNaN( this.MaxScroll ) || this.MaxScroll == 0 ) 
			{this.update();}

		if( vAlturaAtual == 0 )
			{this.ScrollButton.style.top = "0px";}
		else
		{

			var vPosition = parseInt((vAlturaAtual * this.MaxBar) / this.MaxScroll);
			if( vPosition < 0 )
				{vPosition = 0}
			else if( vPosition > this.MaxBar )
				{vPosition = this.MaxBar}
			this.ScrollButton.style.top = Math.round(vPosition) + "px";
		}

	},

	buttonDownPressed: function() {
		this.scrollDown();
		Suagencia.ScrollBars.trackTime = setTimeout("Suagencia.ScrollBars.items.get('" + this.identification + "' ).fastScrollDown()" , 500 );
	},

	buttonDownReleased: function() {
		clearTimeout(Suagencia.ScrollBars.trackTime);
	},

	fastScrollDown: function() {
		this.scrollDown();
		if( this.ScrollDiv.scrollTop < this.MaxScroll )
			{Suagencia.ScrollBars.trackTime = setTimeout("Suagencia.ScrollBars.items.get('" + this.identification + "' ).fastScrollDown()" , 100 );}
	},

	buttonUpPressed: function() {
		this.scrollUp();
		Suagencia.ScrollBars.trackTime = setTimeout("Suagencia.ScrollBars.items.get( '" + this.identification + "' ).fastScrollUp()" , 500 );
	},

	buttonUpReleased: function() {
		clearTimeout(Suagencia.ScrollBars.trackTime);
	},

	fastScrollUp: function() {
		this.scrollUp();
		if( this.ScrollDiv.scrollTop > 0 )
			{Suagencia.ScrollBars.trackTime = setTimeout("Suagencia.ScrollBars.items.get( '" + this.identification + "' ).fastScrollUp()" , 100 );}
	},
	onMouseWheel: function ( event )
	{
		this.moveWheel( Suagencia.ScrollBars.getDelta( event ) );
	},
	moveWheel: function( vDelta )
	{
		this.ScrollDiv.scrollTop += -vDelta * this.Options.get("deltaSpeed");
		this.updateScroll();
	},

	keyDown: function( e )
	{
		var Key = e.keyCode
		if( Key == Event.KEY_UP )
			{this.scrollUp();}
		
		else if( Key == Event.KEY_DOWN )
			{this.scrollDown();}
		
		else if( Key == Event.KEY_PAGEDOWN )
			{
				this.ScrollDiv.scrollTop += this.AlturaArea;
				this.updateScroll();				
			}
		
		else if( Key == Event.KEY_PAGEUP )
			{
				this.ScrollDiv.scrollTop += -this.AlturaArea;
				this.updateScroll();
			}
		else if( Key == Event.KEY_TAB )
			{this.updateScroll();}

		else if( Key == Event.KEY_END )
			{
				this.ScrollDiv.scrollTop = this.MaxScroll;
				this.updateScroll();
			}
		else if( Key == Event.KEY_HOME )
			{
				this.ScrollDiv.scrollTop = 0;
				this.updateScroll();
			}

			
	}	
}

Suagencia.ScrollBars = {
	items: new Hash(),
	listener: null,
	trackTime: null,
	defaultOptions: $H({
		fixedScrollButton:false,
		deltaSpeed:10
	}),
	getDelta: function ( event ) {
		var delta = 0;
		if (!event) event = window.event;
		if (event.wheelDelta) {
			delta = event.wheelDelta/120; 
			if (window.opera) delta = -delta;
		} else if (event.detail) {
			delta = -event.detail/3;
		}
		if (delta)
			{return delta;}
			if (event.preventDefault)
					event.preventDefault();
			event.returnValue = false;
	},
	config:  function (event , vDOM)
	{
		var vList;
		var vAnchors;

		if( vDOM )
			{vList = $(vDOM).select("[suag:objeto~=scrollbar]");}
		else
			{vList = $$("[suag:objeto~=scrollbar]");}
		
		vList.each( function (s , index) {
			if( s.readAttribute("suag:scrollParam") != null && s.readAttribute("suag:scrollParam") != "" )
			{
				var vOptions = $H(s.readAttribute("suag:scrollParam").evalJSON())
				new Suagencia.Classes.ScrollBar(s , vOptions);
			}
			else
				{new Suagencia.Classes.ScrollBar(s);}
		} );
		
	},
	changeDefaultOptions: SuagenciaFramework.changeDefaultOptions
}