$(document).ready(function() {
	var down_arrow = "/contact/images/minus.gif";
	var right_arrow = "/contact/images/plus.gif";
	var ListIds = $("#ListIds").val();
	var details_key = new Array();
	var details_html = new Array();
	var openDetails = new Array();

	// Click bindings for toggling between basic and advanced search
	$("a#search_type").click(function(e) {
		$("div#basic_search, div#advanced_search").toggleClass("hidden");
		if ($(this).html() == "Basic Search")
			$(this).html("Advanced Search");
		else
			$(this).html("Basic Search");
		return false;
	});

	// Click binding for the detailed view
	function ToggleDetails(obj) {
		if ($(obj).parent().hasClass("activeRow")) {
			$(obj).children("img").attr("src", right_arrow);
			$(obj).parent().toggleClass("activeRow").next().remove();
		} else {
			$(obj).children("img").attr("src", down_arrow);
			var ResID = $(obj).parent().attr("id").replace("_entry", "");
			var json_data = {
				"ListIds": ListIds,
				"ResID": ResID
			};
			var cache_index = jQuery.inArray(ResID, details_key);
			var parity = $(obj).parent().attr("class");
			$(obj).parent().toggleClass("activeRow").after("<tr id='"+ResID+"_details' class='detailsrow "+parity+"'></tr>");
			if (cache_index != -1) {
				// Has already been loaded, so just display it
				$(obj).parent().next().html(details_html[cache_index]);
			} else {
				// Load and display
				$(obj).parent().next().load("/contact/details.cfm", json_data, function(responseText, responseStatus, XMLHttpRequest) {
					details_key.push(ResID);
					details_html.push(responseText);
				});
			}
		}
	}
	$("#directory_results").click(function(e) {
		if ($(e.target).is('.expander')) {
			ToggleDetails(e.target);
		} else if ($(e.target).parent().is('.expander')) {
			ToggleDetails($(e.target).parent());
		}
	});
	
	// Control for showing all peronnel details
	$("a#show_all_details").click(function(e) {
		$(".expander").filter(function(index) {
			return $(this).children("img").attr("src") == right_arrow;
		}).each(function() {
			ToggleDetails(this);
		});
		return false;
	});
	
	// Control for hiding all peronnel details
	$("a#hide_all_details").click(function(e) {
		$(".expander").filter(function(index) {
			return $(this).children("img").attr("src") == down_arrow;
		}).each(function() {
			ToggleDetails(this);
		});
		return false;
	});
	
	// Function to clear out the undisplayed form (basic/advanced) just in case there 
	// is any lingering data still in it, then perform the ajax call to update the search results.
	function updateResults() {
		$("div.hidden input").val("");
		var json_data = {
			"ListIds": ListIds,
			"basic": $("#basic").val(),
			"adv_name": $("#adv_name").val(),
			"adv_phone": $("#adv_phone").val(),
			"adv_email": $("#adv_email").val(),
			"adv_title": $("#adv_title").val(),
			"subgroup": $("#subgroup").val()
		};
		$("div#directory_results").load("/contact/result_table.cfm", json_data, function() {
			BindResultsTable();
		});
	}
	
	// Submit bindings to update results
	$("#directory_search").submit(function () {
		updateResults();
		return false;
	});
	$("#reset_search").click(function () {
		$("div input:text").val("");
		$("#subgroup").val("");
		updateResults();
		return false;
	});
	$("#subgroup").change(function () {
		updateResults();
	});
	
	function BindResultsTable() {
		// Add the tablesorter and bindings to keep any open detail windows open
		$("#results").tablesorter({
			headers: {
				3: {
					sorter: false
				}
			},
			sortList: [[0,0]],
			widgets: ['zebra']
		});
		$("#results").bind("sortStart", function() {
			// remove all of the existing open windows
			$(".activeRow").each(function() {
				openDetails.push($(this).attr("id"))
			}).toggleClass("activeRow").next().remove();
		}).bind("sortEnd", function() {
			// open any of the previously open windows
			for (id in openDetails) {
				ToggleDetails($("#"+openDetails[id]).children(".expander"));
			}
			openDetails = new Array();
		});
	}
	BindResultsTable();
});