window.pager =
{
	additionalParameters: '',

	backstage: null,

	closedList: null,
	closedListId: '',

	currentPage: 0,

	errorMessageBox: null,

	loader: null,

	openList: null,
	openListId: '',

	order: null,
	orderDescending: false,

	requestSent: false,

	tableHolder: null,

	init: function( again )
	{
		again = again || false;

		if ( !again )  {
			// Elements that will always be on the page.
			this.backstage = $( "PagerBackstage" );
			this.errorMessageBox = $( "PagerErrorMessage" );
			this.loader = $( "PagerLoadingMessage" );
		} // if

		// Elements that will be reloaded on subsequent requests.
		this.openList = $( this.openListId );
		this.closedList = $( this.closedListId );
		this.tableHolder = $( "PagerTableHolder" );

		this.hide( this.closedList );

		this.backstage.removeChild( this.openList );
		this.backstage.removeChild( this.closedList );
		this.backstage.removeChild( this.tableHolder );

		var parent = this.loader.parentNode;

		parent.insertBefore( this.tableHolder, this.loader );
		parent.insertBefore( this.openList, this.loader );
		parent.insertBefore( this.closedList, this.loader );

		this.hide( this.loader );

		if ( /RowList/.test( this.openList.id ) && !this.iconsLoaded ) {
			this.renderImages();
		} // if
	}, // init()

	setView: function( id, button )
	{
		if ( id == this.openListId )
			return;

		var temp = this.openList;
		var tempId = this.openListId;

		this.openList = this.closedList;
		this.openListId = this.closedListId;

		this.closedList = temp;
		this.closedListId = tempId;

		this.hide( this.closedList );
		this.show( this.openList );

		button.src = button.src.replace( /Up/, "Down" );

		var otherButton = null;

		if ( /Thumbnail/.test( button.id ) ) {
			otherButton = $( button.id.replace( /Thumbnail/, "List" ) );
			setPreference( this.collection, this.action, "showthumbs", 1, true );

			if ( !this.iconsLoaded ) {
				this.renderImages();
			} // if
		} else {
			otherButton = $( button.id.replace( /List/, "Thumbnail" ) );
			setPreference( this.collection, this.action, "showthumbs", 0, true );
		} // if-else

		otherButton.src = otherButton.src.replace( /Down/, "Up" );
	}, // setView()

	orderBy: function( column, how )
	{
		if ( this.requestSent )
			return;

		if ( how == "desc" )
			this.orderDescending = true;
		else
			this.orderDescending = false;

		this.order = column;

		this.showPage( this.currentPage );
	},

	renderImages: function()
	{
		var images = $A( document.images );

		for ( var i = 0; i < images.length; i++ ) {
			if ( /row\d+img/i.test( images[i].id ) ) {
				images[i].src = $( images[i].id + "_src" ).value;

				if ( typeof fixPNG == "function" )
					fixPNG( images[i], true );
			}
		}

		this.iconsLoaded = true;
	},

	showPage: function( number )
	{
		if ( this.requestSent )
			return;

		this.requestSent = true;

		if ( number != this.currentPage ) {
			$A( $( "PaginatorLeftCell" ).childNodes ).each( function( child ) {
				if ( child.className && child.className == "PaginatorCurrentPage" )
					child.className = '';
				else if ( child.innerHTML && child.innerHTML == number )
					child.className = "PaginatorCurrentPage";
			});
		} // if

		this.hide( this.openList );
		this.show( this.loader );

		this.iconsLoaded = false;

		var url = URI_BASE + "/" + this.collection + "/" + this.action;

		var params = "only_render_lists=yes&page=" + number;

		if ( this.order )
			params += "&order=" + this.order;

		if ( this.orderDescending )
			params += "&o2=desc";

		if ( this.additionalParameters )
			params += "&" + this.additionalParameters;

		var self = this;

		new Ajax.Request( url, {
			parameters: params,

			onSuccess: function( response )
			{
				var parent = self.loader.parentNode;

				parent.removeChild( self.openList );
				parent.removeChild( self.closedList );
				parent.removeChild( self.tableHolder );

				self.openList = self.closedList = self.tableHolder = null;

				self.backstage.innerHTML = response.responseText;

				self.init( true );

				self.currentPage = number;

				self.show( self.openList );

				self.requestSent = false;

				if ( /RowList/.test( self.openList.id ) ) {
					this.renderImages();
				} // if
			}, // onSuccess()

			onFailure: function( response )
			{
				self.hide( self.loader );
				self.show( self.openList );

				self.errorMessageBox.innerHTML = "Could not retrieve page from server.  Please wait a few minutes and try again.";
				self.show( self.errorMessageBox );

				self.requestSent = false;
			} // onFailure()
		});
	}, // showPage()

	hide: function( el ) { el.style.display = "none" },
	show: function( el ) { el.style.display = "block" }
} // window.pager
