
$(document).ready(function() {

	//Count number of cells per row in table
	$("#resultsHeaderRow").data('numCells',0);
	$("#resultsHeaderRow th").each(function(){ $("#resultsHeaderRow")
		.data('numCells',
			$("#resultsHeaderRow").data('numCells')+1);});
	
	//Enable collapsible behaviour (why am I suddenly British?)
	var legends = document.getElementsByTagName('legend');
	for(idx in legends) {
		var legend = legends[idx];
		if(hasClass(legend.parentNode,'collapsible')) {
		  $(legend).click(function() {
			toggleCollapse(this.parentNode);
		  });
		} 
	}
	
	//Tooltips for soft-matching cells (softCell's)
	$("td.closeCell").tooltip({
		track: true,
		extraClass: "closeTooltip",
		left: -15
		});
	$("td.movedDataCell").tooltip({
		track: true,
		extraClass: "movedDataTooltip",
		left: -15
		});
	$("td.mergedValueCell").tooltip({
		track: true,
		extraClass: "mergedValueTooltip",
		left: -15
		});
	$("td.synonymCell").tooltip({
		track: true,
		extraClass: "synonymTooltip",
		left: -15
		});
	$("td.assumedCell").tooltip({
		track: true,
		extraClass: "assumedTooltip",
		left: -15
		});
	
	//Help icons for soft-matching cells (softCell's)
	$("td.softCell").append('<span class="helpIcon">&nbsp;</span>');
	
	
	//Tooltips for legends
	$("tr.exactRow .labelCell .label").tooltip({
		track: true,
		extraClass: "exactTooltip",
		bodyHandler: function(){return "Perfect match for your search";},
		left: -15
		});
	$("tr.assumedRow .labelCell .label").tooltip({
		track: true,
		extraClass: "assumedTooltip",
		bodyHandler: function(){return "Missing values would likely match, if present.";},
		left: -15
		});
	$("tr.movedDataRow .labelCell .label").tooltip({
		track: true,
		extraClass: "movedDataTooltip",
		bodyHandler: function(){return "Match found, but data labeled in a nonstandard manner";},
		left: -15
		});
	$("tr.mergedValueRow .labelCell .label").tooltip({
		track: true,
		extraClass: "mergedValueTooltip",
		bodyHandler: function(){return "Multiple values were found under the same attribute.";},
		left: -15
		});
	$("tr.synonymRow .labelCell .label").tooltip({
		track: true,
		extraClass: "synonymTooltip",
		bodyHandler: function(){return "Value is synonymous with that specified in your search query.";},
		left: -15
		});
	$("tr.closeRow .labelCell .label")
		.tooltip({
		track: true,
		extraClass: "closeTooltip",
		bodyHandler: function(){return "Inexact match, but result may be of interest to you";},
		left: -15
		}
		);
		
	//Show/hide functionality!
	$("tr.exactRow .labelCell .labelOptions")
		.append('Always displayed');
	/*$("tr.resultRow .labelCell .labelOptions")
		.not("tr.exactRow .labelCell .labelOptions")
		.filter("tr.closeRow .labelOptions, tr.synonymRow .labelOptions, tr.movedDataRow .labelOptions, "
			+"tr.mergedValueRow .labelOptions, tr.assumedRow .labelOptions")
		*/
	$("tr.resultRow")
		.each(function(i){
			var m = /\b([-a-zA-Z]+)(_-_[-_a-zA-Z0-9]+|Row)$/.exec(this.className);
			if(m != null && m[1] != 'exact') {
				var matchType = m[1];
				var specific = m[0];
				var needsSpecific = (matchType+'Row' != specific);
				$(".labelOptions", this).append('<span class="labelLink" onclick="hide_rows_with_class(\'' 
					+ matchType+'Row' 
					+ '\')">Hide All</span>'
					+ (needsSpecific? ' | <span class="labelLink" onclick ="hide_rows_with_class(\'' 
						+ specific 
						+ '\')">Hide Similar</span>' : '')
					);
			}
		});
		
		
	$(".hideClose")	
		.click(function(){
			hide_rows_with_class('closeRow');
		});

});

//className *MUST* start with match type and either end with "Row" (e.g. "closeRow") or end
//with "_-_*" where * as anything (while a valid class name obviously), e.g. synonym_-_02_-_2002
function hide_rows_with_class(className) {
	var matchType = className.replace(/^([-a-zA-Z]+)(_-_.*|Row)$/,"$1");
	var toggleExpression = "tr."+className;
	$(toggleExpression)/*.not(".closeRow ~ .closeRow").*/ .before('<tr class="collapsedRow ' + matchType +'Collapsed ' + className + 'CollapsedOnThis"><td colspan="'+$("#resultsHeaderRow").data('numCells')+'" onclick="show_rows_with_class(\''+className+'\')"><span class="collapsedLabel">Not showing '
	+ (className==matchType+'Row'?matchType+' matches (click to show)': 'certain '+matchType+' matches (click to show)')
	+ '</span></td></tr>') /*.end()*/
	.addClass("hidden");
}
function show_rows_with_class(className) {
	var toggleExpression = "tr."+className;
	$('.'+className).removeClass('hidden'); 
	$('.'+className+'CollapsedOnThis').remove();
}
