function bb_list_init() {
	// on pages where only the filter_start (with hardlinks) is shown, don't register AJAX behaviors for it
	if (!bb_list_is_static) {
		bbBehaviors.add_behavior('.bb-admin-filter-reset a', 'click', function (context, evt) {
			evt.stop();
			new Ajax.Request(bb_list_controller_url + '/a_list_do_clear_criteria', {
									onComplete: function(resp) {
													bb_list_filter_refresh();
													bb_list_results_refresh();
													bb_ads_refresh();
												}
							 }); // request
		}); // add filter reset link behavior

		bbBehaviors.add_behavior('.bb-listing-pagination a', 'click', function(context, evt) {
				evt.stop();
				var offset = get_anchor_value(this).substr(1);
				if (!offset) {
					offset = 0;
				}
				new Ajax.Request(bb_list_controller_url + '/a_list_do_set_pagination', {
											method: 'post',
											parameters: {'offset': offset },
											onComplete: function() {
													bb_list_results_refresh();
													bb_ads_refresh();
											}
								}); // request
		}); // add pagination link behavior
	} // -- END DYNAMIC RESULTS ONLY BEHAVIORS
	
	// -- BEGIN STATIC and DYNAMIC RESULTS BEHAVIORS
	bbBehaviors.add_behavior('.bb-admin-checkbox-list a.choice-dyn', 'click', function(context, evt) {
		evt.stop();
		if (this.up().hasClassName('local-more-toggle')) {
			return;
		}

		this.up().hide();
		new Ajax.Request(bb_list_controller_url + '/a_list_do_add_criteria', {
									method: 'post',
									parameters: {'key': this.next('input').name,
												 'value': this.next('input').value},
									onComplete: function(resp) {
													if (bb_list_is_static) {
														bb_list_dyn_redirect();
													} else {
														bb_list_filter_refresh();
														bb_list_results_refresh();
														bb_ads_refresh();
													}
												} // onComplete
						}); // request
	}); // add condition click behavior
	
	bbBehaviors.add_behavior('.bb-admin-checkbox-list a.chosen', 'click', function(context, evt) {
		evt.stop();
		new Ajax.Request(bb_list_controller_url + '/a_list_do_remove_criteria', {
									method: 'post',
									parameters: {'key': this.next('input').name,
												 'value': this.next('input').value},
									onComplete: function(resp) {
													if (bb_list_is_static) {
														bb_list_dyn_redirect();
													} else {
														// this = removed criteria
														bb_list_filter_refresh();
														bb_list_results_refresh();
														bb_ads_refresh();
													}
												} // onComplete
						}); // request
	}); // add chosen-condition click behavior
	
	
	bbBehaviors.add_behavior('h2.bb-admin-local-group-name a', 'click', function(context, evt) {
		// this behavior submits an AJAX request to store the filter open/closed status
		// see below for the scriptaculous effect script (triggered on landing and results filters)
		new Ajax.Request(bb_list_controller_url + '/a_list_do_toggle_filter_group', {
										 parameters: {'group_name': get_anchor_value(this) },
										 method: 'post'
										 });
	});

	bbBehaviors.add_behavior('li.local-more-toggle a', 'click', function (context, evt) {
		// same as group headers: send request to save state of 'show more' areas
		var grp = get_anchor_value(this) + '-more';
		new Ajax.Request(bb_list_controller_url + '/a_list_do_toggle_filter_group', {
										 method: 'post',
										 parameters: {'group_name': grp}
										 });
	}); // add 'show more' click ajax behavior
	
	bbBehaviors.add_behavior('li.local-more-toggle a', 'click', function (context, evt) {
		evt.stop();
		var hider_ul = $('local-toggle-more-' + get_anchor_value(this));
		if (hider_ul.visible()) {
			this.innerHTML = 'Show more...';
		} else {
			this.innerHTML = 'Show fewer...';
		}
		Effect.toggle(hider_ul, 'blind');
	}); // add 'show more' click behavior

	bbBehaviors.add_behavior('h2.bb-admin-local-group-name a', 'click', function (context, evt) {
		evt.stop();
		var head = this.up();
		var items_ul = head.next('ul');

		head.toggleClassName('local-filter-closed');
		Effect.toggle(items_ul, 'blind');
	});

	bbBehaviors.add_behavior('form.local-list-keyword-choice', 'submit', function (context, evt) {
		evt.stop();
		new Ajax.Request(bb_list_controller_url + '/a_list_do_add_criteria', {
									method: 'post',
									parameters: {'key': this.down('input').name,
												 'value': this.down('input').value},
									onComplete: function (resp) {
														bb_list_filter_refresh();
														bb_list_results_refresh();
														bb_ads_refresh();
												} // onComplete
						}); // request
	}); // add keyword search submit behavior

	bbBehaviors.add_behavior('.bb-list-cbox input', 'click', function(context, evt) {
			new Ajax.Request(bb_list_controller_url + '/a_list_do_toggle_selected', {
										method: 'post',
										parameters: {'id': this.value}
							}); // request
	}); // add checked listing behavior
	
	bbBehaviors.add_behavior('.bb-list-hdr input', 'click', function (context, evt) {
		$$('.bb-list-cbox input').each(function (cbox) {
			new Ajax.Request(bb_list_controller_url + '/a_list_do_toggle_selected', {
							method: 'post',
							parameters: {'id': cbox.value}
				}); // request
			cbox.checked = !cbox.checked;
		}); // each checkbox input
	});

	bbBehaviors.add_behavior('a.sort', 'click', bb_list_handle_sort);
	bbBehaviors.add_behavior('a.sort_d', 'click', bb_list_handle_sort);
	bbBehaviors.add_behavior('a.sort_u', 'click', bb_list_handle_sort);

	bbBehaviors.add_behavior('.bb-list-limit select', 'change', function (context, evt) {
			evt.stop();
			new Ajax.Request(bb_list_controller_url + '/a_list_do_set_pagination', {
										method: 'post',
										parameters: {'limit': this.value },
										onComplete: function() { 
												if (bb_list_is_static) {
													bb_list_dyn_redirect();
												} else {
													bb_list_results_refresh(); 
												}
											} // onComplete
							});
	}); // add max-per-page change behavior
	
			
	bbBehaviors.add_behavior('#bb-result-count-links a', 'click', function(context, evt) {
											evt.stop();
											new Ajax.Request(bb_list_controller_url + '/a_list_do_set_pagination', {
														method: 'post',
														parameters: {
															'limit': get_anchor_value(this)
														},
														onComplete: function() { 
															if (bb_list_is_static) {
																bb_list_dyn_redirect();
															} else {
																bb_list_results_refresh(); 
															}
													}
											}); // request
	}); // add count a click behavior
										

	bbBehaviors.add_behavior('.bb-list-view-type a', 'click', function(context, evt) {
													evt.stop();
													new Ajax.Request(bb_list_controller_url + '/a_list_do_set_view_type', { 
																method: 'post',
																parameters: {
																	'view_type': get_anchor_value(this)
																},
																onComplete: function() { 
																	if (bb_list_is_static) {
																		bb_list_dyn_redirect();
																	} else {
																		bb_list_results_refresh(); 	
																		if($('map-multiple')!=null) $('map-multiple').remove();
																	}
																} // onComplete
													}); // request
												}); // add view-type a click behavior
} // bb_list_init

function bb_list_filter_refresh() {
	// refresh the search filter
	new Ajax.Updater('bb-admin-cat-edit', bb_list_controller_url + '/a_list_get_filter',
							{onComplete: function(resp) { bbBehaviors.walk_and_register($('bb-admin-cat-edit')); }}
					);
} // bb_list_filter_refresh

function bb_list_results_refresh() {	
	new Ajax.Updater('bb-listing-list-rows', bb_list_controller_url + '/a_list_get_rows',
							{/* insertion: 'bottom', */
								onComplete: function(resp) {
									bbBehaviors.walk_and_register($('bb-listing-list-rows'));
									mopMap();
								},
								evalScripts: true
							}
					);
} // bb_list_refresh

function bb_ads_refresh() {
	if ($$('.ad-iframe').size() > 0) {
		$$('.ad-iframe').each(function(ad) {
		ad.contentWindow.location.reload(true);
		});
	}
}

function bb_list_dyn_redirect() {
	// user clicked something that should result in a non-static search page, redirect
	window.location = '/guides/search';
}


var bb_list_handle_sort = function bb_list_handle_sort(context, evt) {
	evt.stop();
	var key = get_anchor_value(this);

	var dir = 1;
	if (this.hasClassName('sort')) {
		// ascending sort
		dir = 1;
		this.removeClassName('sort');
		this.addClassName('sort_d');
	} else if (this.hasClassName('sort_d')) {
		// descending
		dir = 0;
		this.removeClassName('sort_d');
		this.addClassName('sort_u');
	} else if (this.hasClassName('sort_u')) {
		// ascending
		dir = 1;
		this.removeClassName('sort_u');
		this.addClassName('sort_d');
	}

	new Ajax.Request(bb_list_controller_url + '/a_list_do_set_sort', {
							method: 'post',
							parameters:  { 'key': key,
										   'direction': dir },
							onComplete: function(resp) {
								if (bb_list_is_static) {
									bb_list_dyn_redirect();
								} else {
									bb_list_results_refresh();
									bb_ads_refresh();
								}
							} // onComplete
					});  // request
}; // bb_list_handle_sort