/*
An object that takes care of pagination.
*/

AARP.Paginator = {
	__version__: '1.0b2'
	, Offset: {
		cur: 0
		, prev: -1
		, next: -1
	}
	, Page: {
		cur: 0
		, prev: -1
		, next: -1
	}
	, results: 0
	, results_per_page: 10
	, page_links_displayed: 5
	, Template: {
		link: ''
		, link_wrapper: "<a href='#{link}'>#{offset_page}</a>"
		, link_wrapper_off: '<span>#{offset_page}</span>'
		, body: "<div class='pagerSorting'>Records #{results_lo} to #{results_hi} of about #{results}</div> "
			+ "<div class='pagerPagination'>#{buffer}</div>"
		, buttonP_wrapper: "<a href='#{link}' class=\"previous btnSmallGray\"><span class=\"left\"></span><span class=\"middle\">#{button}</span><span class=\"right\"></span></a>"
		, buttonP_wrapper_off: "<span class=\"previous btnSmallGray inactive\"><span class=\"left\"></span><span class=\"middle\">#{button}</span><span class=\"right\"></span></span>"
		, buttonN_wrapper: "<a href='#{link}' class=\"next btnSmallGray\"><span class=\"left\"></span><span class=\"middle\">#{button}</span><span class=\"right\"></span></a>"
		, buttonN_wrapper_off: "<span class=\"next btnSmallGray inactive\"><span class=\"left\"></span><span class=\"middle\">#{button}</span><span class=\"right\"></span></span>"
	}
	, Buttons: {
		prev: "previous"
		, prev_off: "previous"
		, next: "next"
		, next_off: "next"
	}
	, separator: '<span>|</span>'
	
	, populate: function( container )
	{
		container = $( container );
		if ( !container ) return;
		
		var max_pages = Math.ceil( this.results / this.results_per_page );
		var page = Math.floor( this.Offset.cur / this.results_per_page ) + 1;
		
		// we want to 'center' the current link on anything greater than the midpoint
		// NEW (2007-12-11): do not center link displays around current offset
		//var mid = this.page_links_displayed / 2;
		var lo = page;
		var hi = this.page_links_displayed;
		
		if ( this.results > 0 )
		{
			hi = lo + this.page_links_displayed - 1;
			
			/*
			if ( page > mid )
			{
				lo = page - Math.floor( mid );
				hi = page + Math.floor( mid );
			}
			*/
			
			if ( hi > max_pages )
				hi = max_pages;
		
			// adjust so that page_links_displayed links will always be shown
			if ( hi - lo - 1 < this.page_links_displayed )
			{
				lo = hi - this.page_links_displayed + 1;
				if ( lo < 1 ) lo = 1;
			}
		}
		else
			this.Offset.cur = lo = hi = 0;
		
		var tpl_link = new Template( this.Template.link );
		var tpl_lwrap = new Template( this.Template.link_wrapper );
		var tpl_lwrap_off = new Template( this.Template.link_wrapper_off );
		var ph = { 'offset': 0, 'offset_page': 0, 'link': '' };
		var sep = '';
		
		var tpl_bpwrap = new Template( this.Template.buttonP_wrapper );
		var tpl_bpwrap_off = new Template( this.Template.buttonP_wrapper_off );
		var tpl_bnwrap = new Template( this.Template.buttonN_wrapper );
		var tpl_bnwrap_off = new Template( this.Template.buttonN_wrapper_off );
		
		// we're on at least page 2, so the previous button needs to show
		var buffer = '';
		if ( page > 1 )
		{
			ph.offset = ( page - 2 ) * this.results_per_page;
			ph.offset_page = page - 1;
			ph.button = this.Buttons.prev;
			ph.link = tpl_link.evaluate( ph ).replace( '/articles/article', '/article' );
			buffer += tpl_bpwrap.evaluate( ph ).replace( '/articles/article', '/article' );
		}
		else
		{
			ph.button = this.Buttons.prev_off;
			buffer += tpl_bpwrap_off.evaluate( ph ).replace( '/articles/article', '/article' );
		}
		buffer += ' ';
		
		for ( var i = lo; i <= hi; i++ )
		{
			ph.offset = ( i - 1 ) * this.results_per_page;
			ph.offset_page = i;
			ph.link = tpl_link.evaluate( ph );
			
			if ( i != page )
				buffer += sep + tpl_lwrap.evaluate( ph ).replace( '/articles/article', '/article' );
			else
				buffer += sep + tpl_lwrap_off.evaluate( ph ).replace( '/articles/article', '/article' );
			
			sep = this.separator;
		}
		
		// we're not at the last page, so show the next button
		buffer += ' ';
		if ( page < max_pages )
		{
			ph.offset = page * this.results_per_page;
			ph.offset_page = page + 1;
			ph.button = this.Buttons.next;
			ph.link = tpl_link.evaluate( ph );
			buffer += tpl_bnwrap.evaluate( ph ).replace( '/articles/article', '/article' );
		}
		else
		{
			ph.button = this.Buttons.next_off;
			buffer += tpl_bnwrap_off.evaluate( ph ).replace( '/articles/article', '/article' );
		}
		// for "item x to y of z"
		// TODO: put in page offsets
		ph.results_lo = ( page - 1 ) * this.results_per_page + 1;
		ph.results_hi = ph.results_lo + this.results_per_page - 1;
		if ( ph.results_hi > this.results )
			ph.results_hi = this.results;
		ph.results = this.results;
		ph.buffer = buffer;
		
		var body = new Template( this.Template.body );
		container.innerHTML = body.evaluate( ph ).replace( '/articles/article', '/article' );
	}
}