/**
 * @author fred
 */
// TODO: add retry on request failure?
var ScrollLoader = Class.create( ContentScroller, {
	
	loadDistance:	100,
	loadPoint:		null,
	nextPage:		0,
	
	updateElement:	null,
	
	initialize: function( $super, handles, tracks, container, content, options )
	{
		options = options || {};
		options.onStop = this.onStop.bind(this);
		
		$super( handles, tracks, container, content, options );
		
		this.loadDistance	= options.loadDistance		|| this.loadDistance;
		this.updateElement	= $(options.updateElement)	|| this.content;
		
		this.loadPoint		= this.sliders.getRange().end - this.loadDistance;
		
		this.sliders.forceValue(0, true);
		this.scroller.jumpTo(0);
	},
	
	onStop: function()
	{
		if( this.nextPage != null && this.container.scrollTop > this.loadPoint )
			new Ajax.Request( '?page='+this.nextPage, { onComplete:	this.recieveContent.bind(this) });
	},
	
	recieveContent: function( transport )
	{
		switch( transport.status )
		{
			case 200:
				this.updateElement.insert({ bottom: transport.responseText });
				this.updateSliders();
				this.nextPage++;
				break;
			
			case 204:
				this.nextPage = null;
				break;
		}
	}
});
