
/**
 * This function will initialize the dynamic pager object
 * when called with the `new` keyword.
 * 
 * @param blockId	Block Identifier to attach pager to
 * @return
 */
function dynamic_pager(blockId, itemClass, pagerRenderer)
{
	// copy functions
	for (var idx in DynamicPagerObject)
	{
		this[idx] = DynamicPagerObject[idx];
	}
	
	this.__construct(blockId, itemClass, pagerRenderer);
}

//--------------------------------------------------------

/**
 * This object will assist you to create paging functionality
 * in static html without having to refresh the page. It is
 * assumed that all pages are already available on the page. 
 *  
 * The object contains a number of functions that will assist
 * you in navigating through the pages. 
 * 
 * Instanciate using: 
 * 	var pager = 
 * 			new dynamic_pager(
 * 				"myId", "myelementclass", 
 * 				function(dynamic_pager_instance) {}
 *			);
 */
var DynamicPagerObject = 
{
	DEFAULT_MAX_PER_PAGE : 3,
		
	// data-members
	base : null,
	itemClass : null,
	
	numResults : 0,
	maxPerPage : 0,
	nPages : 0,
	currentPage : 0,
	pagerRenderer : null,
	
		
	/**
	 * Constructor for this object
	 */
	__construct : function(blockIdentifier, itemClass, pagerRenderer)
	{
		// initialize data-members
		this.blockId = blockIdentifier;
		this.itemClass = itemClass;
		this.pagerRenderer = pagerRenderer;
		
		this.base = $("#" + blockIdentifier);
		this.numResults = $("." + itemClass, this.base).length - 1;
		
		this.setMaxPerPage(this.DEFAULT_MAX_PER_PAGE);
		
		// initialize display
		this.numberElements();
		this.setPage(0);
	},
	
	
	/**
	 * Show the page specified in parameter `pageNumber`
	 * in the base element of this pager object using the
	 * number of results as specified in maxPerPage
	 */
	redraw : function()
	{
		var 
			firstElement = this.currentPage * this.maxPerPage,
			lastElement = 1 + (this.currentPage + 1) * this.maxPerPage;
		
		// fill the block with the appropriate
		$("." + this.itemClass, this.base).hide();

		// select elements in interval <firstElement, lastElement>
		// using itemClass:gt(x):lt(y) syntax.
		var elements = $("." + this.itemClass + ":lt(" + lastElement + "):gt(" + firstElement + ")", this.base);
		$(".reaction", elements).removeClass("noBorder");
		$(".reaction", elements[elements.length - 1]).addClass("noBorder");
		elements.show();
		
		// redraw the pager
		this.pagerRenderer(this);
		
	},
	
	
	/**
	 * Setup a unique sequenced number for each element in
	 * the paged items.
	 */
	numberElements : function()
	{
		var idx = -1;
		$("." + this.itemClass, this.base).each(
				function()
				{
					$(this).attr("elementNumber", idx);
					++idx;
				}
			);
	},
	
	
	/**
	 * This method allows you to jump to the page on which
	 * the element specified in the `element` parameter is located.
	 * This is done using the `elementNumber` attribute that has
	 * been assigned to the DOM node.
	 */
	jumpTo : function(element)
	{
		if (element.attr("elementNumber") !== undefined)
		{
			this.setPage(
					Math.floor(
							parseInt(element.attr("elementNumber")) / this.maxPerPage
						)
				);
			
		}
	},
	
	
	/**
	 * Skip to the next page in the pager and redraw
	 */
	nextPage : function()
	{
		if (this.currentPage < this.nPages - 1)
			++this.currentPage;
		
		window.scrollTo(0, $(this.base).position().top);
		
		this.redraw();
	},
	
	
	/**
	 * Go a page back (if possible) and redraw
	 */
	previousPage : function()
	{
		if (this.currentPage > 0)
			--this.currentPage;
		
		window.scrollTo(0, $(this.base).position().top);

		this.redraw();
	},
	
	
	/**
	 * Set the page within limits without redrawing.
	 */
	setPage : function(pageNumber)
	{
		if (pageNumber >= 0 && pageNumber < this.nPages)
			this.currentPage = pageNumber;
		
		this.redraw();
	},
	
	/**
	 * Make the last entry of this pager visible by setting
	 * the page number to its maximum.
	 */
	showFirstEntry : function()
	{
		window.scrollTo(0, $(this.base).position().top);
		this.setPage(0);
	},
		
	
	/**
	 * Make the last entry of this pager visible by setting
	 * the page number to its maximum.
	 */
	showLastEntry : function()
	{
		window.scrollTo(0, $(this.base).position().top);
		this.setPage(this.nPages - 1);
	},
	
	
	/**
	 * Set a new number of maximum per page
	 */
	setMaxPerPage : function(maxPerPage)
	{
		this.maxPerPage = maxPerPage;
		this.nPages = Math.ceil(this.numResults / this.maxPerPage);		
		this.setPage(0);
	}
	
};

