NXC.Paginator.AJAX = new Class( {

	Extends: NXC.Paginator.Base,
	Implements: [Options, Events],

	options: {
		'onStartShowPage': $empty(),
		'onCompleteShowPage': $empty()
	},

	ajaxURL: '',
	itemsContainer: false,
	itemsCount: 0,

	initialize: function( ajaxURL, itemsContainer, itemsCount, options ) {
		this.ajaxURL        = ajaxURL;
		this.itemsContainer = document.id( itemsContainer );
		this.itemsCount     = itemsCount;

		this.setOptions( options );
		this.parent( options );
	},

	getPagesCount: function() {
		return Math.ceil( this.itemsCount / this.itemsPerPage );
	},

	showItems: function() {
		this.fireEvent( 'startShowPage' );

		this.itemsContainer.empty();
		new Request.HTML( {
			'url': this.ajaxURL.replace( '%offset%', ( this.currentPage -1 ) * this.itemsPerPage ).replace( '%limit%', this.itemsPerPage ),
			'method': 'get',
			'update': this.itemsContainer,
			'onSuccess': function() {
				new Fx.Scroll( window ).toElement( this.itemsContainer );

				this.fireEvent( 'onCompleteShowPage' );
			}.bind( this )
		} ).send( 'offset=' + ( this.currentPage -1 ) * this.itemsPerPage  + '&limit=' + this.itemsPerPage );
	}
} );

