/*
 *
 * Pandora Pager - jQuery plugin for displaying paged content on Backstage pages.  It works
 * by selectively hiding and showing rows of a table.
 *
 * The pandora pager is able to incrementally download subsets of the table's contents.
 * If the user requests a page of data that has not yet been download, the pager will attempt
 * to fetch the required data.  To minimize data fetches. the pager will also attempt to fetch
 * a number of pages of data a head of what the user requested.  The number of pages fetched
 * at a time is specified by the batchSize setting.
 *
 * 	To use, simply attach it to a table:   $('#tableId').pandoraPager();
 *
 * Configurable Settings:
 * ---------------------
 * table_id: 		DOM id of the table
 * friendlyName: 	User readable name of the data contents.  Will be appended to the page's title up on page navigation
 * url_rowprovider: The URL of the page that serves up the table's row's HTML.
 *					see /favorites/profile_tablerows_artist.vm for an example
 * allowSkipToEnd: 	if true, a "skip to end" control and a "show all" control will be available in the navigation controls
 * navContainers: 	a jQuery list of elements to use as containers for navigation controls - if omitted
 *                	then navigation controls will be inserted in a DIV immediately following the table
 * showall:     	the initial view of the table is to show all records
 * currentPage: 	the initial page to display, once the control is initialized
 * show_index:      if specified, currentpage is ignored and the pager will make sure that the specified index is displayed
 * pageSize: 		number of rows to display per page
 * maxitemstoallowskiptoend: The max number of items before hiding the 'showall' and 'pagelast' button.
 * batchSize: 		the size (in pages) of each batch of data retrieved from the server
 * totalItemCount: 	Total number of items in the table
 * onDataDownload: 	Javascript method called after a batch of table rows have been downloaded
 * onPage: 			Javascript method called upon page navigation
 *
 *
 * These settings may be specified either when the pager is initialized
 *
 *		jqSearchResultsTable.pandoraPager({
 *			currentPage: 1,
 *			totalItemCount: searchResults.searchResultItems.length
 *		});
 *
 * or in the table's html attributes
 *
 *		<table id="tbl_artists_table" cellpadding="2" cellspacing="0" border="0" width="100%"
 *			   friendlyname="Artists"
 *			   totalitemcount="$all_artist_bookmarks.size()"
 *			   urlrowprovider="/favorites/profile_tablerows_artist.vm?webname=$profile.webName">
 *
 *
 */
(function($) {
$.fn.pandoraPager = function(options) {
	if (this.length == 0) {
		return false;
	}

	var settings = {
		table_id: this.attr("id"),
		friendlyName: this.attr("friendlyname"),

		url_rowprovider: this.attr("urlrowprovider"),

		allowSkipToEnd: this.attr("allowskiptoend") != "false",

		maxItemsToAllowSkipToEnd: parseInt(this.attr("maxitemstoallowskiptoend")),

		navContainers: null,

		show_index: parseInt(this.attr("show_index")),

		pageSize: parseInt(this.attr("pagesize")),

		batchSize: parseInt(this.attr("batchsize")),

		onDataDownload: this.attr("ondatadownload"),

		onPage: this.attr("onpage")
	}

	if (options) $.extend(settings, options);

	return this.each(function () {
		var jqTable = $(this);

		var pandoraPagerDomProxy = new PandoraPagerDomProxy(jqTable, settings);

		PandoraPagerManager.init(pandoraPagerDomProxy, settings, options,
				function (stateEvent) {
					PandoraPagerManager.eventHandler_onStateChange(pandoraPagerDomProxy, settings, stateEvent);
				});
	});
}
})(jQuery);