document.observe( "dom:loaded" , ConfigScroll );



function ConfigScroll() {
	vList = $$(".scrolls")

	vList.each( function (s , index)
			{ 
				if( s.scrollTop == 0 )
				{ 
					s.scrollTop = 10
					if( s.scrollTop != 0 )
					{
						s.scrollTop = 0;
						x = new ScrollBar(s , index);
					}
				}
				else
					{x = new ScrollBar(s , index);}				
			} 
		);
	
}

var ScrollBar = Class.create();

ScrollBar.prototype = {
	initialize: function ( myDiv , myID)
	{
		this.identification = "scrollarea-" + myID;
		ScrollBars.items[ "scrollarea-" + myID ] =  this;

		vWaperID = ("scrollarea-" + myID)
		MyWraper = new Element("div" , {"class":"scrolldiv","id":vWaperID} );
		vChildNodes = myDiv.childNodes
		for( i = 0 ; i < vChildNodes.length ; i ++ )
		{
			MyWraper.appendChild( vChildNodes[i].cloneNode(true) );
		}
		myDiv.update();
	
		myDiv.appendChild( MyWraper );
		
		
		vScrollbar 		= new Element("div" , {"class":"scrollbar"} );
		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 		= $(("scrollarea-" + myID));
		this.ButtonDown 		= myDiv.select(".down").first();
		this.ButtonUp 		= myDiv.select(".up").first();
		this.Scrollbar 		= myDiv.select(".scrollbar").first();
		this.ScrollArea  		= this.Scrollbar.select(".scrollLine").first();
		this.ScrollButton 	= this.Scrollbar.select(".scrollButton").first();
*/

		this.ScrollDiv 		= $(this.identification);
		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.ScrollButton.update("HTML CODE"); */

		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.AlturaArea = myDiv.getHeight(); 

		this.calculaDimensoes();
		
		myDiv.style.overflow = "visible"
		
		this.ScrollButton.style.top = "0px";

		this.ButtonDown.observe("mousedown" , ScrollBars.eventTrigger_ButtonDownPressed.bindAsEventListener( this.ButtonDown , this.identification ) );
		this.ButtonDown.observe("mouseup" , ScrollBars.eventTrigger_ButtonDownReleased.bindAsEventListener( this.ButtonDown , this.identification ) );
		this.ButtonDown.observe("mouseout" , function () {clearTimeout(ScrollBars.trackTime);}) ;

		this.ButtonUp.observe("mousedown" , ScrollBars.eventTrigger_ButtonUpPressed.bindAsEventListener( this.ButtonUp , this.identification ) );
		this.ButtonUp.observe("mouseup" , ScrollBars.eventTrigger_ButtonUpReleased.bindAsEventListener( this.ButtonUp , this.identification ) );
		this.ButtonUp.observe("mouseout" , function () {clearTimeout(ScrollBars.trackTime);}) ;


		myDiv.observe("DOMMouseScroll", ScrollBars.eventTrigger_MoveWheel.bindAsEventListener( this.ScrollDiv, this.identification ) );
		myDiv.observe("mousewheel", ScrollBars.eventTrigger_MoveWheel.bindAsEventListener( this.ScrollDiv, this.identification ) );
		myDiv.observe("keydown", ScrollBars.eventTrigger_KeyDown.bindAsEventListener( this.ScrollDiv, this.identification ) );


		this.ScrollButton.observe("mousedown" , ScrollBars.eventTrigger_StartDrag.bindAsEventListener( this.ScrollButton , this.identification ) );
		this.updateScroll();
	},

	calculaDimensoes: function()
	{
		this.AlturaScroll = this.AlturaArea - this.ButtonUp.getHeight() - this.ButtonDown.getHeight()
		this.ScrollArea.style.height = (this.AlturaScroll) + "px" ;
		this.AlturaScrollBall = this.ScrollButton.getHeight();

		this.MaxBar = this.AlturaScroll - this.AlturaScrollBall;
		

		vtempScroll = this.ScrollDiv.scrollTop;
		vMaxScroll = -1;
		while( vMaxScroll <= this.ScrollDiv.scrollTop )
		{
			vMaxScroll += 1000;
			this.ScrollDiv.scrollTop = vMaxScroll;
		}
		this.MaxScroll = this.ScrollDiv.scrollTop;
		this.ScrollDiv.scrollTop = vtempScroll;

		if( this.MaxBar >= this.MaxScroll )
		{
			if( this.AlturaScrollBall < ( this.MaxBar - this.MaxScroll ))
			{
				/* Remova o comentário da linha abaixo para ter um botão de barra de rolagem com tamanho variável */
				/* this.AlturaScrollBall = this.MaxBar - this.MaxScroll; */
				this.MaxBar = this.AlturaScroll - this.AlturaScrollBall;
			}
		}

		this.ScrollButton.style.height = this.AlturaScrollBall + "px";
	},

	scrollUp: function() {
		this.ScrollDiv.scrollTop += -10;
		this.updateScroll();
	},

	scrollDown: function() {
		this.ScrollDiv.scrollTop += 10;
		this.updateScroll();
	},

	startDrag: function( pointerY ) {
		ScrollBars.listener = ScrollBars.eventTrigger_Dragged.bindAsEventListener( this.ScrollButton , this.identification , pointerY , parseInt( this.ScrollButton.style.top )  );
		Event.observe( document , 'mousemove' , ScrollBars.listener );
		document.observe( 'mouseup' , ScrollBars.eventTrigger_StopDrag );
	
	},
	
	dragged: function(scrolledPix) {

		if( isNaN( this.MaxScroll ) || this.MaxScroll == 0 ) 
			{this.calculaDimensoes();}

		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"

	},
	
	updateScroll: function() {
		vAlturaAtual = this.ScrollDiv.scrollTop;

		if( isNaN( this.MaxScroll ) || this.MaxScroll == 0 ) 
			{this.calculaDimensoes();}

		if( vAlturaAtual == 0 )
			{this.ScrollButton.style.top = "0px";}
		else
		{

			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();
		ScrollBars.trackTime = setTimeout("ScrollBars.items[ '" + this.identification + "' ].fastScrollDown()" , 500 );
	},

	buttonDownReleased: function() {
		clearTimeout(ScrollBars.trackTime);
	},

	fastScrollDown: function() {
		this.scrollDown();
		if( this.ScrollDiv.scrollTop < this.MaxScroll )
			{ScrollBars.trackTime = setTimeout("ScrollBars.items[ '" + this.identification + "' ].fastScrollDown()" , 100 );}
	},

	buttonUpPressed: function() {
		this.scrollUp();
		ScrollBars.trackTime = setTimeout("ScrollBars.items[ '" + this.identification + "' ].fastScrollUp()" , 500 );
	},

	buttonUpReleased: function() {
		clearTimeout(ScrollBars.trackTime);
	},

	fastScrollUp: function() {
		this.scrollUp();
		if( this.ScrollDiv.scrollTop > 0 )
			{ScrollBars.trackTime = setTimeout("ScrollBars.items[ '" + this.identification + "' ].fastScrollUp()" , 100 );}
	},

	moveWheel: function( vDelta )
	{
		this.ScrollDiv.scrollTop += -vDelta;
		this.updateScroll();
	},

	keyDown: function( Key )
	{
		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();
			}

			
	}	
}

ScrollBars = {
	items: [],
	listener: null,
	trackTime: null,
	eventTrigger_ScrollDown: function( event ,  callerID ) {ScrollBars.items[callerID].scrollDown();},
	eventTrigger_ButtonDownPressed: function( event ,  callerID ) {ScrollBars.items[callerID].buttonDownPressed();},
	eventTrigger_ButtonDownReleased: function( event ,  callerID ) {ScrollBars.items[callerID].buttonDownReleased();},
	eventTrigger_ButtonUpPressed: function( event ,  callerID ) {ScrollBars.items[callerID].buttonUpPressed();},
	eventTrigger_ButtonUpReleased: function( event ,  callerID ) {ScrollBars.items[callerID].buttonUpReleased();},
	eventTrigger_ScrollUp: function( event ,  callerID ) {ScrollBars.items[callerID].scrollUp();},
	eventTrigger_StartDrag: function( event ,  callerID ) {ScrollBars.items[callerID].startDrag(Event.pointerY(event));},
	eventTrigger_Dragged: function( event ,  callerID , initialY , initialScroll) { ScrollBars.items[callerID].dragged( (Event.pointerY(event) - initialY + initialScroll ) );},
	eventTrigger_MoveWheel: function( event ,  callerID ) {ScrollBars.items[callerID].moveWheel( getDelta( event ));},
	eventTrigger_KeyDown: function( event ,  callerID ) { ScrollBars.items[callerID].keyDown( event.keyCode );},
	eventTrigger_KeyUp: function( event ,  callerID ) { ScrollBars.items[callerID].keyUp();},
	eventTrigger_StopDrag: function ( event ) {
		Event.stopObserving( document , 'mousemove' , ScrollBars.listener )
		Event.stopObserving( document ,'mouseup' , ScrollBars.eventTrigger_StopDrag );
	}
}







function getDelta( 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;
}



