/* Remove filters from list.
 *
 * Removes CSS 'selected' and 'visited' as well.
 */
ResetFunction = function() {
	$('.vcard').show('fast');
	$(".vcard > div").hide();
	$('.vcard').removeClass('selected');
	$('.vcard').removeClass('visited');
	$('#filterbox input[type=text]').val('');
	$('#filterbox input[type=text]')[0].focus();
	return false;
}

/*
 * Filter out .vcard elements that don't match a search.
 *
 * <.vcard>  <---- whole card.
 * 	<h3?><.org>      <------------ title section
 * 	<div>*      <------------ content/details
 */
FilterFunction = function() {
	var $newValue = ($(this).val()).toLowerCase();
	$('.vcard').removeClass('selected');
	$('.vcard > div').hide(); // hide all 'details' before filtering.
	$('.vcard').map(function() { // filter on the title.
		var index = this.innerHTML.replace(/<[^>]+>/g,"").toLowerCase().indexOf($newValue)
		if (index >= 0) {
			// TODO for each match add a hilight attribute
			// and then remove the attribute on a new search.
			// this.innerHTML.replace("/$newValue/gi","<pre class=\"hilight\">$newValue</pre>")
			$(this).fadeIn();
		}
		else {
			$(this).hide();
		}
	});
	return false;
}

/* 
 * On click, make the current child <div>'s toggle - and close any other ones. 
 *
 * <.vcard>    <--- what we're bound to.
 * 	<.org/>     <-------- header
 * 	<div>*    <-------- content
 * </.vcard>
 * 
 * CSS:
 * When a .vcard is visited 'selected' class is added.
 * Previously visited cards have the 'visisted' clas added.
 */
AccordianFunction = function() {
	var $nextDiv = $(this);
	var $visibleSiblings = $nextDiv.siblings('.vcard').children('div:visible');
	if ($visibleSiblings.length > 0) {
		$visibleSiblings.parent('.vcard').removeClass('selected');
		$visibleSiblings.parent('.vcard').addClass('visited');
		$visibleSiblings.slideUp('fast', function() {
			$nextDiv.children('div').show('fast');
			$nextDiv.addClass('selected');
			$nextDiv.removeClass('visited');
		});
	} else {
		$nextDiv.toggleClass('selected');
		if ($nextDiv.hasClass('selected')) {
			$nextDiv.removeClass('visited');
		}
		else {
			$nextDiv.addClass('visited');
		}
		$nextDiv.children('div').toggle('fast');
	}
}
