window.onload = function(ev) {
	try {
		//quick references
		window.filter = $('filter');
		window.results = $('results');	
		window.data = $('data');
		window.thead = window.data.getElementsByTagName('THEAD')[0];
		
		//make data table sortable
		try {
			ts_makeSortable(window.data)
		} catch (ex) {}		
		
		//display filter
		document.getElementById('dynamic').style.display = 'block';
						
		//start filtering
		searchUpdate();

	} catch (ex) {}
	
	return true;
}

//element reference by id
function $(id) {
	return document.getElementById(id);
}

//Regularly check if searching is required
function searchUpdate() {
	if (searchUpdate.lastSearch != window.filter.value) {
		searchUpdate.lastSearch = window.filter.value;
		searchBibliography();
	}
	searchUpdate.timeout = setTimeout("searchUpdate()", 300);
	return true;
}

//Search bibliography
function searchBibliography() {
	if (window.filter.value.search(/\S/) >= 0) {
		var words = window.filter.value.split(' ');

		//Cycle through words, reducing rows to matches (logical AND)
		var rows = window.data.getElementsByTagName('TBODY')[0].getElementsByTagName('TR');
		for (var w=0; w<words.length; w++) {
			rows = searchRows(rows, words[w]);
		}

		//Only bother if rows have been filtered
		//if (rows.length < window.rows.length) {
		
			//Display results
			searchResults(rows);
			return true;
		//}
	}
	
	//Display empty result
	searchResults();
	return false;
}

//Search a nodelist of rows for a word, return a nodelist of matches
function searchRows(rows, word) {
	var regex = new RegExp(word.replace(/^ *| *$/, ''), 'i');

	var result = [];
	result.item = function(index){return result[index]}

	for (var r = 0; r < rows.length; r++) {
		if (rows[r].innerHTML.search(regex) >= 0) {
			result.push(rows[r]);
		}
	}

	return result;
}

//Display result of search. Rows = node list, or empty.
function searchResults(rows) {
	
	var result = '';
	searchResults.last = rows;

	if (rows) {
		if (rows.length) {	
			for (var r=0; r<rows.length; r++) {
				result += '<tr>' + rows[r].innerHTML + '</tr>';
			}
			//setTimeout("highlightFound()", 100);
		} else {
			result = '<tr><td colspan="100">No results found</td></tr>';
		}

		//highlight searched words
		var regex = createRegEx(3);
		if (regex) {
			result = result.replace(regex, "<span class='found'>$1</span>");
		}

		//assign results
		window.results.innerHTML = '<table cellspacing="0"><thead>' + window.data.getElementsByTagName('THEAD')[0].innerHTML + '</thead>' + result + '</table>';
				
		window.results.style.display = 'inline';
		window.data.style.display = 'none';

	} else {

		window.results.style.display = 'none';
		window.data.style.display = 'block';

	}

	window.filter.focus();
	return true;
}

//Create a regex that will search for the supplied words (logical OR)
function createRegEx(minWidth) {
	minWidth = (minWidth) ? parseInt(minWidth) : 1;

	if (window.filter.value.search(/\S/) >= 0) {
		var words = window.filter.value.split(' ');
		var pattern = '';

		for (var w=0; w<words.length; w++) {
			if (words[w].length >= minWidth) {
				if (pattern) {pattern += '|'}
				pattern += words[w].replace(/^ *| *$/, '');
			}
		}

		if (pattern) {
			return new RegExp('(' + pattern + ')', 'ig');
		}
	}
	
	return null;
}
