$(function(){
	function updatePseudoCategories() {
		if ($('#f_categories :checked').size()) {
			$('#pseudo_f_categories').text(
				$('#f_categories label:has(:checked)').map(function(){ return $(this).text() }).get().join(',')
			);
		} else {
			$('#pseudo_f_categories').text(
				'Any'
			);
		}
		if ($('#f_tags :checked').size()) {
			$('#pseudo_f_tags').text(
				$('#f_tags label:has(:checked)').map(function(){ return $(this).text() }).get().join(',')
			);
		} else {
			$('#pseudo_f_tags').text(
				'Any'
			);
		}
	}
	$('a.pseudo-select').fancybox({
		scrolling: 'no',
		overlayOpacity: 0.5,
		overlayColor: '#000',
		transitionIn: 'none',
		transitionOut: 'none',
		onClosed: updatePseudoCategories
	});
	$('input:radio').live('click', function(){
		var id = $(this).parents('div').attr('id');
		$('#pseudo_' + id).text( $(this).parent().text() );
		$('#' + id + '2').val($(this).val());
		$.fancybox.close();
		/*
		if (!$.browser.msie) {
		}
		*/
	});
	$('#f_country_id input:radio').live('click', function(){
		var country_id = $(this).val();
		if (country_id) {
			var link_to = link_to_cities_collection.replace(':id', country_id);
			$('#f_city').load(link_to);
		} else {
			$('#f_city').html('<div class="table4">Select Country first.</div>');
		}
		$('#pseudo_f_city').text('I don\'t care');
		$('#f_city2').val('');
	});
	$('#f_country_id input:radio:checked').each(function(){
		if (this.value) {
			$('#pseudo_f_country_id').text($(this).parent().text());
			if (f_city) {
				$('#pseudo_f_city').text(f_city);
			}
		}
	});
	$('label.label-group input:checkbox').click(function(){
		var label = $(this).parent();
		var checked = $(this).attr('checked');
		label.nextUntil('label.label-group').find('input:checkbox').attr('checked', checked);
	});
	$('a.reset').click(function(){
		$(this).parents('form').find(':checkbox,:radio').attr('checked', false);
		$(this).parents('form').find(':radio[value=]').attr('checked', true);
		$('#pseudo_f_country_id').text('Any');
		$('#pseudo_f_city').text('I don\'t care');
		$('#pseudo_f_categories').text('Any');
		$('#pseudo_f_tags').text('Any');
		return false;
	});
	$('a.submit').click(function(){
		$(this).parents('form').submit();
		return false;
	});
	updatePseudoCategories();
});

