function setTable(table_id)
{
	//alert('setTable ' + table_id);
	var t = document.getElementById(table_id);
	var divs = t.getElementsByTagName('div');
	for(var p = 0;p<divs.length;p++){
		}
		var rows = t.getElementsByTagName('tr');
		for(var i=1; i<rows.length; i++){
			var cells = rows[i].getElementsByTagName('td');

			var date = cells[0].innerHTML;
			var keywords = cells[2].innerHTML;

			//strip out the html
			var date = date.replace(/(<([^>]+)>)/ig,"");

			//var fotopat = /.*\{.*?photos.*?(\d+?)/i;
			var fotopat = /.*\{.*?photos.*?(\d+)/i;
			var fotoslurp = fotopat.exec(keywords);
			var photocount = (fotoslurp != null && fotoslurp.length > 1)? fotoslurp[1]:0;

			var audiopat = /.*\{.*?audio.*?(true|false|\d)/i;
			var audioslurp = audiopat.exec(keywords);
			var audioboolean = (audioslurp != null && audioslurp.length > 1)? ((audioslurp[1].toLowerCase().indexOf('true')+1 || audioslurp[1].indexOf('1')+1)? true:false):0;
			var audiostr = (audioboolean) ? '<strong>&bull;</strong>' : '&nbsp;';


			cells[0].innerHTML = daysAgo(date);
			cells[2].innerHTML = audiostr;
			cells[3].innerHTML = photocount;
			cells[0].className = cells[2].className = cells[3].className = '';
		}

		getSortData(table_id);
}

function daysAgo(dateString)
{
	var blog_date = new Date(dateString);
	var now = new Date();
	//determine the difference in dates, using milliseconds
	var delta_date = Date.parse(now.toGMTString()) - Date.parse(blog_date.toGMTString());
	//convert milliseconds to days
	var delta_days = Math.floor((delta_date / 1000) / 60 / 60 / 24);

	//var return_val = (delta_days <= 0) ? "NEW" : delta_days;
	var return_val = (delta_days <= 0) ? '<span style="background: url(images/new.gif) no-repeat center center;"><span style="visibility: hidden;">00</span></span>' : delta_days;

	return return_val;
}

function showSummaries()
{
	table_id = 'archives';
	var t = document.getElementById(table_id);
	var divs = t.getElementsByTagName('div');
	for(var p = 0;p<divs.length;p++){
		if(divs[p].className.indexOf('excerpt')+1){
				if(divs[p].getElementsByTagName('p')) {
				truncate(divs[p].getElementsByTagName('p')[0]);
				}
			}
		}
	var attr = t.className;
	if(attr == "off"){
		t.className = "on";
	} else{
		t.className = "off";
	}
}

/**
 * Create the data structure that will allow for table sorting
 * @param: table_id - id name of the table to be sorted
 */
function getSortData(table_id)
{
	//alert('getSortData: ' + table_id);

	//a globally accessible array specific to the table to hold the table data
	window[table_id + 'Data'] = new Array();
	//a globally accessible array specific to the table to hold the names of the columns
	window[table_id + 'ColNames'] = new Array();

	var t = document.getElementById(table_id);
	var rows = t.getElementsByTagName('tr');
	var headers = rows[0].getElementsByTagName('th');

	//populate the ColNames array
	for(var i=0; i<headers.length; i++){
		window[table_id + 'ColNames'].push(headers[i].id);
	}

	//populate the Data array
	for(var i=1; i<rows.length; i++){
		var tds = rows[i].getElementsByTagName('td');
		var o = new Object();

		//give each object a property named after the corresponding header and assign the property that cell data
		//e.g. o.row0 = cellData0
		for(var j=0; j<tds.length; j++){
			o[window[table_id + 'ColNames'][j]] = tds[j].innerHTML;
		}

		//add the object to the Data array
		window[table_id + 'Data'].push(o);

	}
	
	if(qs('media')){
		sortColumn(table_id, 'sort-photos', 'numeric_inverse');
	} else{
		sortColumn(table_id, 'sort-days', 'numeric');
	}
}

/**
 * Sort a column in a sortable table
 * @param: table_id - id name of the table to be sorted
 * @param: column_id - id name of the column to be sorted
 * @param: sort_type - how should the column be sorted?
 *			Should be 'numeric', 'alpha', or the name of a custom comparison function that you create
 *			custom function names should start with 'sort' and be an array order function
 *				e.g. 'function sortprice(a, b)'
 */
function sortColumn(table_id, column_id, sort_type)
{

	//global var used for sorting
	sortBy = column_id;

	//copy our Data array for sorting
	var sortedData = window[table_id + 'Data'];

	//copy our ColNames array for legibility
	//var colNames = window[table_id + 'ColNames'];

	//do the sort using an order function
	sortedData.sort(window['sort' + sort_type]);

	var t = document.getElementById(table_id);
	var rows = t.getElementsByTagName('tr');
	var ths = rows[0].getElementsByTagName('th');
	var selected_th = document.getElementById(column_id);
	//set the class name for all the headers to empty and the selected header to 'selected'
	for(var i=0; i<ths.length; i++){
		ths[i].className = '';
	}

	selected_th.className = 'selected';

	//write the sorted data back into the table
	for(var i=0; i<sortedData.length; i++){
		var row_num = i+1;
		var tds = rows[row_num].getElementsByTagName('td');

		for(var j=0; j<tds.length; j++){
			var col = window[table_id + 'ColNames'][j];
			tds[j].innerHTML = sortedData[i][col];
		}
	}


	window[table_id + 'Data'] = sortedData;
}

function sortnumeric(a,b)
{
	var x = a[sortBy].replace(/(<([^>]+)>)/ig,"");
	var y = b[sortBy].replace(/(<([^>]+)>)/ig,"");

	return parseInt(x) - parseInt(y);
}

function sortnumeric_inverse(a,b)
{
	var x = a[sortBy].replace(/(<([^>]+)>)/ig,"");
	var y = b[sortBy].replace(/(<([^>]+)>)/ig,"");

	return parseInt(y) - parseInt(x);
}

function sortalpha(a,b)
{
	//strip out any html tags before comparing
	var a = a[sortBy].replace(/(<([^>]+)>)/ig,"");
	var b = b[sortBy].replace(/(<([^>]+)>)/ig,"");

	return ((a < b)? -1:((a > b)? 1:0));
}

function sortempty(a,b)
{
	//have to get the character code because the browser renders the &bull; character
	var x = a[sortBy].replace(/(<([^>]+)>)/ig,"").charCodeAt(0);
	var y = b[sortBy].replace(/(<([^>]+)>)/ig,"").charCodeAt(0);

	return ((x > y)? -1:((x < y)? 1:0));
}

function qs(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  //alert('Query Variable ' + variable + ' not found');
}
