/**
 * List Table Functions
**/

var ListClass = Class.create (
{
	initialize: function ()
	{
		this.sortedBy = [];
		this.sortedDir = [];
		this.page = [];
		this.total = [];
		this.pages = [];
		this.limit = [];
	},
	sortBy: function (id, column)
	{
		var name = id;
		var id = $(id);
		
		// get the header row
		var sorted = [id.down('tr')];
		id.down('tr').remove();
		var rows = $A(id.select('tr')).sortBy(function (x)
		{
			if (x.down(column).readAttribute('sortValue') == null)
			{
				if (x.down(column).innerHTML.match(/^[0-9\.]+$/))
					return parseFloat(x.down(column).innerHTML);
				else
					return x.down(column).innerHTML;
			} else
			{
				if (x.down(column).readAttribute('sortValue').match(/^[0-9\.]+$/))
					return parseFloat(x.down(column).readAttribute('sortValue'));
				else
					return x.down(column).readAttribute('sortValue');
			}
		});
		
		if (this.sortedBy[name] == column && this.sortedDir[name] == 'asc')
		{
			rows.reverse();
			this.sortedDir[name] = 'desc';
		} else
			this.sortedDir[name] = 'asc';
		rows.each(function (x)
		{
			x.removeClassName('line1'); x.removeClassName('line2');
			if (sorted.length % 2)
				x.addClassName('line1');
			else
				x.addClassName('line2');
			sorted.push(x);
		});
		id.down().update('');
		sorted.each(function (e) { id.down().insert(e); });
		this.sortedBy[name] = column;
	},
	
	makeSortable: function (id, columns, sortedby)
	{
		columns.each (function (i)
		{
			Event.observe($(id).down('th', i), 'click', function (e) { Lists.sortBy(id, i); });
			$(id).down('th', i).addClassName('pointer');
		});
		this.sortedBy[id] = sortedby;
		this.sortedDir[id] = 'asc';
	},
	
	makePaged: function (id, limit)
	{
		if (!$(id))
			return;

		// if we're less than the limit don't enable pagination
		if ($(id).select('tr').length - 1 <= limit)
			return;

		// count the total rows
		this.total[id] = $(id).select('tr').length - 1;
		this.pages[id] = (this.total[id] / limit).ceil();
		this.page[id] = 1;
		this.limit[id] = limit;

		// add the page identifiers to the bottom row of the table
		$(id).down('tbody').insert(
			new Element('tr').insert(
				new Element('td', { id: 'paginationcontainer' + id, colspan: $(id).down('tr').select('th').length }).setStyle(
				{
					'text-align': 'right',
					'border-bottom': '0px',
					'padding-right': '2px'
				}).addClassName('paginationcontrols')
			)
		);

		// build our controls
		var controls = [];
		controls.push(new Element('a', { href: 'javascript:;' }).update('&laquo; First Page'));
		controls.push(new Element('a', { href: 'javascript:;' }).addClassName('prev').update('&lt; Prev'));
		$R(1, this.pages[id]).each (function (i)
		{
			controls.push(new Element('a', { href: 'javascript:;' }).addClassName('page' + i).update(i));
		});
		controls.push(new Element('a', { href: 'javascript:;' }).addClassName('next').update('Next &gt;'));
		controls.push(new Element('a', { href: 'javascript:;' }).update('Last Page &raquo;'));

		controls.invoke('observe', 'click', function (ele) { Lists.showPage(id, ele.element().innerHTML); });

		controls.each(function (ele)
		{
			$('paginationcontainer' + id).insert(ele);
		});

		Lists.showPage(id, 1); 
	},
	
	showPage: function (id, page)
	{
		if (Object.isString(page) && page.match('First Page'))
			page = 1;
		else if (Object.isString(page) && page.match('Last Page'))
			page = this.pages[id];
		else if (Object.isString(page) && page.match('Prev'))
			page = this.page[id];
		else if (Object.isString(page) && page.match('Next'))
			page = this.page[id] + 2;

		page = page - 1;

		// hide all of the rows!
		$(id).select('tr').invoke('hide');
		
		// show the header
		$(id).down('th').up().show();

		// show the controls
		$('paginationcontainer' + id).up().show();

		// show the page data
		var rows = $(id).select('tr').findAll(function (x) { return !x.visible(); }).eachSlice(this.limit[id]);
		rows[page].invoke('show');
		this.page[id] = page;

		// Highlight the page number
		$('paginationcontainer' + id).select('a').invoke('removeClassName', 'selected');
		$$('.page' + (page + 1)).invoke('addClassName', 'selected');

		// show/hide the prev/next controls
		if (page == 0)
			$('paginationcontainer' + id).select('.prev').invoke('hide');
		else
			$('paginationcontainer' + id).select('.prev').invoke('show');

		if (page == (this.pages[id] - 1))
			$('paginationcontainer' + id).select('.next').invoke('hide');
		else
			$('paginationcontainer' + id).select('.next').invoke('show');
	}
});
var Lists = new ListClass();

