/**
 * @author fred
 */
// TODO: Tidy up this, ContentScroller and ScrollLoader so everything's neater and we can use indexOf rather than doing DOM lookups 
// TODO: Fix so there aren't the horrible checks against "latest"
var ButtonScrollLoader = Class.create( ScrollLoader,
{
	activeClassName:	'active',
	
	//ids:		[],
	buttons:	[],
	positions:	[],
	scrollIDs:	null,
	
	activeButton:	null,
	latestButton:	null,
	
	initialize: function( $super, handles, tracks, container, content, scrollIDs, options )
	{
		$super( handles, tracks, container, content, options );
		
		for( var i=0; i<scrollIDs.length; i++ )
			if( $(scrollIDs[i]) )
			{
				//this.ids.push( scrollIDs[i] );
				this.buttons.push( $$('.pageLink.'+scrollIDs[i])[0] );
				this.positions.push( $(scrollIDs[i]).cumulativeOffset().top - this.container.cumulativeOffset().top );
				
				if( scrollIDs[i-1] == "latest" )
					this.latestButton = this.buttons[i-1];
				else if( i>0 && this.positions[i]<this.positions[i-1] )
					console.log('ERROR: ButtonScrollLoader: IDs passed in incorrect order!');
			}
			else
				$$('.pageLink.'+scrollIDs[i])[0].up().addClassName( 'hidden' );
		
		this.scrollIDs = scrollIDs;
		this.activeButton = this.buttons[0];
		this.activeButton.addClassName( this.activeClassName );
	},
	
	onStop: function( $super )
	{
		var pos = this.sliders.getValue() + this.container.getHeight()*0.5;
		
		for( var i=this.positions.length-1; i>-1; i-- )
			if( this.scrollIDs[i] != "latest" && this.positions[i] <= pos )
			{
				this.activeButton.removeClassName( this.activeClassName );
				this.activeButton = this.buttons[i];
				this.activeButton.addClassName( this.activeClassName );
				this.activeButton.blur();
				break;
			}
		
		$super();
	},
	
	scrollTo: function( $super, element )
	{
		$super( element );
		
		if( element == "latest" )
			this.latestButton.blur();
	}
});
