var PageList = {
	pages: [],
	wrap: document.getElementById('ministrylist'),
	init: function(pagelist) {
		this.pages = pagelist;
		this.table = document.createElement('table');
		this.wrap.appendChild(this.table);
		this.thead = document.createElement('thead');
		this.table.appendChild(this.thead);
		this.tbody = document.createElement('tbody');
		this.table.appendChild(this.tbody);
		this.thead.appendChild(this.makerow(['Name','','Location'], 1));
		this.show('title');
	},
	show: function(order) {
		this.table.removeChild(this.tbody);
		this.tbody.innerHTML = '';
		this.pages.sort(this['order_'+order]);
		for (var i=0; i<this.pages.length; i++) this.tbody.appendChild(this.makerow([
			'<a href="' + this.pages[i].url + '">' + this.pages[i].title + '</a>',
			'', this.pages[i].loc
		]));
		this.table.appendChild(this.tbody);
	},
	order_title: function(a, b) {
		if (a.title > b.title) return 1;
		if (a.title == b.title) return 0;
		return -1;
	},
	order_loc: function(a, b) {
		if (a.loc > b.loc) return 1;
		if (a.loc == b.loc) return 0;
		return -1;
	},
	makerow: function(cells, head) {
		var row = document.createElement('tr');
		for(var i=0; i<cells.length; i++) {
			var cell = document.createElement(head?'th':'td');
			if (i==0) cell.className = 'min_name';
			else if (i==1) cell.className = 'middle';
			row.appendChild(cell);
			cell.innerHTML = cells[i];
		}
		return row;
	}
};
