
var Toolbar = Class.create(
{
	scrolling: false,
	
	faded: false,
	
	timeout: null,
	
	initialize: function( element )
	{
		this.element = $( element );
		
		this.initEvents();
	},
	
	initEvents: function()
	{
		//if( scrollTarget !== null )
			Event.observe( window, "scroll", this.handleScroll.bind( this ) );
	},
	
	handleScroll: function(e)
	{
		if( this.scrolling === false )
			this.scrolling = true;
		
		if( this.faded === false )
			this.fadeOut();
		
		this.setFadeInTimer();
	},
	
	setFadeInTimer: function()
	{
		clearTimeout( this.timeout );
		this.timeout = setTimeout( this.fadeIn.bind( this ), 660 );
	},
	
	fadeOut: function()
	{
		this.element.setOpacity( 0.25 );
		this.faded = true;
	},
	
	fadeIn: function()
	{
		new Effect.Opacity( this.element,
		{
			from: 0.25,
			to: 1,
			duration: 0.5,
			afterFinish: function()
			{
				setTimeout( function(){ this.faded = false; this.scrolling = false }.bind( this ), 300 );
			}.bind( this )
		});
	}
});

document.observe( "dom:loaded", function(){ $$( "div.toolbar" ).each( function( el ){ new Toolbar( el ) } ) } );		
