$('.attorney').each(function () {
	classesToAdd = $(this).children('.popup').children('.practice-areas').text();
	classesToAdd = classesToAdd.toLowerCase().replace(/\s+/g,'').replace(/\,/g,' ').replace(/\x26/g,'');
	$(this).addClass(classesToAdd);
});

$(function () {
	$('.attorney').each(function () {
		var distance = 10;
		var time = 250;
		var hideDelay = 50;

		var hideDelayTimer = null;
		var beingShown = false;
		var shown = false;

		var trigger = $('.trigger', this);
		var popup = $('.popup', this).css('opacity', 0);

		$([trigger.get(0), popup.get(0)]).mouseover(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);

			if (beingShown || shown) {
				return;
			} else {
				beingShown = true;
				
				$(trigger.get(0)).children('img').attr('src',$(trigger.get(0)).children('img').attr('src').replace('bw','clr'));
				$(this).addClass('active');

				popup.css({
					top: -137,
					left: 20,
					display: 'block'
				}).animate({
					top: '-=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					beingShown = false;
					shown = true;
				});
			}
		}).mouseout(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);

			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				$(trigger.get(0)).children('img').attr('src',$(trigger.get(0)).children('img').attr('src').replace('clr','bw'));
				$(trigger.get(0)).removeClass('active');
				
				popup.animate({
					top: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					shown = false;
					popup.css('display', 'none');
				});
			}, hideDelay);
		});
	});
});


$('ul#find-by li').click(function () {
	$(this).toggleClass('checked');
	$('#attorneys .attorney').hide();
	query = "";

	$("ul#find-by li").each(function() {
		if ($(this).hasClass('checked')) {
			checkedID = $(this).attr('id');
			query = query + "." + checkedID;
		}
	});
	
	if (query == "") {
		$('#main #attorneys .attorney').show();
	} else {
		$('#main #attorneys '+ query).show();	
	}
	
	return false;
});

$('#show-all').click(function () {
	$('#attorneys .attorney').show();
	
	$("ul#find-by li").each(function() {
		$(this).removeClass('checked');
	});

	return false;
});

$('.find-button').toggle(
  function () {
		$(this).addClass('activated');
		$('#find-by').slideDown();
  }, 
  function () {
		$(this).removeClass('activated');
		$('#find-by').slideUp();
  }
);
