search_attorney = function(){
	//if($('#first_name').val() || $('#last_name').val() || $('#practice').val()){
		//location.href = '#first_name='+$('#first_name').val()+'&last_name='+$('#last_name').val()+'&practice='+$('#practice').val();
		$.ajax({
			type: "POST",
			url: "/attorneys/search_attorney",
			data: $('#search_att').serialize(),
			dataType: "json",
			cache: false,
			success: function(data){
				//debug(data);
				if(data != 0){
					populate_search(data);
				}else{
					$('#search-results').empty();
				}
			}
		});
//	}else{
		
//	}

}

stopTyping = {

   interval : 500,

   lastKeypress : null,

   interceptKeypress : function() {
      this.lastKeypress = new Date().getTime();
      var that = this;
      setTime = setTimeout(function() {
         var currentTime = new Date().getTime();
         if(currentTime - that.lastKeypress > that.interval) {
            that.sendRequest();
			clearTimeout(setTime);
         }
      }, that.interval + 100);
   },

   sendRequest : function() {
	  $('#search-results').html('<img src="/images/search-loader.gif" style="margin:30px 73px" width="54" height="55" border="0" />');
	  $('#practice').val('');
      search_attorney();
   }

}

MM_preloadImages('/images/search-loader.gif');
populate_search = function(){

	a = arguments;

	var h = '';
	if(a[0]){
		jQuery.each(a[0], function(i, val) {
			h += '<a href="/attorneys/'+val.url_title+'">'+val.name+' '+val.photo+'</a>';
		});
	}

	if(!h){
		h = '<div style="text-align:left;margin:10px">There were no attorneys found.<br />Please try again.</div>';
	}

	$('#search-results').html(h);

}

search_practice = function(){
	
	$('#first_name').val('');
	$('#last_name').val('');
	search_attorney();
	
}

$(function(){

	$('#first_name, #last_name').bind('keyup', function(){
		//search_attorney();
		stopTyping.interceptKeypress();
	});

	$('#practice').bind('change', function(){
		search_practice();
	});

	$('#location').bind('change', function(){
		search_attorney();
	});
	
	// first_name=getURLpound_param('first_name');
	// 	last_name=getURLpound_param('last_name');
	// 	practice=getURLpound_param('practice');
	if(my_first_name || my_last_name || my_practice){
		$('#first_name').val(my_first_name);
		$('#last_name').val(my_last_name);
		$('#practice').val(my_practice);
		search_attorney();
	}
	

});