//©2008 Scott Schwab/Foundeffect
//<Pagination Class>
function Pagination(total_pages, options)
{
	if (!options)
	{
		options = {};
	}
	
	this.nav_sets = options.nav_sets || ['top', 'bottom'];
	
	this.total_pages = total_pages;
	this.current_page = options.start_page || 1;
	this.page_base = options.page_base || 'page';
	this.nav_base = options.nav_base || 'link';
	this.previous_label = options.previous_label || 'previous';
	this.next_label = options.next_label || 'next';

	var hide_class = options.hidden_class || 'non';
	this.hide_regex = new RegExp(hide_class);
	
	this.nav_class = options.nav_class || 'pagination-nav';
	this.nav_selected_class = options.nav_selected_class || 'pagination-nav-current';
}

Pagination.prototype = 
{
	//Model
	set_page: function (new_page)
	{
		if (this.valid_new_page(new_page))
		{
			this.current_page = new_page;
		}
	},
	
	valid_new_page: function (new_page)
	{
		if (new_page > 0 && new_page <= this.total_pages && new_page != this.current_page)
		{
			return true;
		}
		else
		{
			return false;
		}
	},
	
	//View
	toggle: function (id, display_type)
	{
		var node = document.getElementById(id);
		if (node.style.display == 'none' || (node.style.display == '' && node.className.match(this.hide_regex)))
		{
			this.show(id, display_type);
		}
		else
		{
			this.hide(id);
		}
	},
	
	show: function (id, display_type)
	{
		var display_mode = display_type || 'block';
		document.getElementById(id).style.display = display_mode;
	},
	
	hide: function (id)
	{
		document.getElementById(id).style.display = 'none';
	},
	
	update_nav: function (nav_set, new_page, old_page)
	{
		document.getElementById(this.nav_base + nav_set + new_page).className = this.nav_selected_class;
		document.getElementById(this.nav_base + nav_set + old_page).className = this.nav_class;
	},

	//Controller
	change_page: function (new_page)
	{
		if (this.valid_new_page(new_page))
		{
			this.toggle(this.page_base + this.current_page);
			this.toggle(this.page_base + new_page);
			
			for (var i = 0; i < this.nav_sets.length; i++)
			{
				this.update_nav(this.nav_sets[i], new_page, this.current_page);
				if (new_page > 1)
				{
					this.show(this.nav_base + this.nav_sets[i] + this.previous_label, 'inline');
				}
				else
				{
					this.hide(this.nav_base + this.nav_sets[i] + this.previous_label);
				}
				if (new_page < this.total_pages)
				{
					this.show(this.nav_base + this.nav_sets[i] + this.next_label, 'inline');
				}
				else
				{
					this.hide(this.nav_base + this.nav_sets[i] + this.next_label);
				}
			}
			
			this.set_page(new_page);
		}
	},
	
	previous_page: function ()
	{
		this.change_page(this.current_page - 1);
	},
	
	next_page: function ()
	{
		this.change_page(this.current_page + 1);
	}
}
//</Pagination Class>