
$(document).ready(function() {

	$('#search_button').live('click', function() {
		document.forms["search_form"].submit();
	});

	$('.do_toggle').live('click', function() {
		var t = $(this).attr('toggle'),
			$t = $('#' + t);
		if (!$t.length) return false;
		if ($t.is(':visible')) {
			$t.slideUp('fast', function() {
				$t.addClass('hide');
			});
		} else {
			$t.slideDown('fast', function() {
				$t.removeClass('hide');
				$t.css('display: block');
			});
		}
		return false;
	});
	$('#toggle_all_cities').live('click', function() {
		var $this = $(this),
			t = $this.html(),
			$t = $('.toggled_city');
		if (t == 'More') {
			$this.html('Less');
			$t.slideDown('fast');
			// $t.slideDown('fast', function() {
			// 	$(this).removeClass('hide').css({ 'display': 'block' });
			// });
		}  else {
			$this.html('More');
			$t.slideUp('fast', function() {
				$(this).addClass('hide');
			});
		}
		return false;
	});
	$('.expand_listing, .contract_listing').live('click', function() {
		listing_fns.toggle_listing($(this));
		return false;
	});
	$('#collapse_all, #expand_all').live('click', function() {
		listing_fns.toggle_all($(this));
		return false;
	});

});

var listing_fns = {
	'expand' : function($link, $par, ide) {
		if (!$par) {
			$par = $link.closest('.event_listing');
			ide = $par.attr('ide');
		}
		var $to = $('.event_listing.expanded[ide=' + ide + ']');
		this._crossfade($par, $to);
	},
	'collapse' : function($link, $par, ide) {
		if (!$par) {
			$par = $link.closest('.event_listing');
			ide = $par.attr('ide');
		}
		$to = $('.event_listing.collapsed[ide=' + ide + ']');
		this._crossfade($par, $to);
	},
	'toggle_listing' : function($link) {
		var $par = $link.closest('.event_listing'),
			ide = $par.attr('ide');
		if ($link.hasClass('expand_listing')) {
			this.expand($link, $par, ide);
		} else {
			this.collapse($link, $par, ide);
		}
	},
	'toggle_all' : function($link) {
		$('#expand_all, #collapse_all').removeClass('active');
		if ($link.attr('id') == 'expand_all') {
			$('.expand_listing').click();
		} else {
			$('.contract_listing').click();
		}
		$link.addClass('active');
	},
	'_crossfade' : function($from, $to) {
		$from.slideUp('fast', function() {
			$from.addClass('hide');
			$to.slideDown('fast', function() {
				$to.removeClass('hide');
			});
		});
	}
}; 
