// Interactivity functions for the SR-Datasets page

// Trim function - kudos to Shailesh N. Humbad:
// The calls to trim() are needed in the functions below because IE6 for some
// reason returns h3 contents with trailing space.
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, "");
}

function SelectButton($class) {
	$("*.btn").removeClass("btn_selected");
	$("*."+$class).addClass("btn_selected");
}

function ClickBtn($class) {
	SelectButton($class + "_btn");
	// Hide all items
	$("*.item").hide();	
	// Hide all lists; Show the clicked list; 
	// find and save the header from the first item from the clicked list
	$firstItem = $("*.lst").hide().filter("."+$class).show().find("li:first").text();
	$firstItem = $firstItem.trim(); // IE6 specific workaround noted at the top
	// like calling ShowItem($firstItem)
	$("*.item").hide().find("h3").filter(function(index) { return ($firstItem == $(this).text()) }).parent().show();
	$("*.lst").find("li").removeClass("lst_selected").filter(function(index) { return ($firstItem == $(this).text().trim()) }).addClass("lst_selected");
}

function ShowItem() {
	var $thisName = $(this).text().trim();
	// Hide all displayed items, show the item whose H3 contents are same as the clicked one's.
	$("*.item").hide().find("h3").filter(function(index) { return ($thisName == $(this).text()) }).parent().show();	
	// Move the selection highlight
	$("*.lst").find("li").removeClass("lst_selected").filter(function(index) { return ($thisName == $(this).text().trim()) }).addClass("lst_selected");
	return false;
}

function SetEventHandlers() {
	$("*.lst").find("li").bind("click", ShowItem);
}

$(function(){
    // Create events for various buttons
	$("span.gray_btn").click(function() { ClickBtn('gray'); });
	$("span.color_btn").click(function() { ClickBtn('color'); });
	$("span.cfa_btn").click(function() { ClickBtn('cfa'); });
	// Set the event handlers
	SetEventHandlers();
	// On load, show grayscale
	ClickBtn('gray');
});