/* uncheckes the specific options if the 'all' checkbox is checked */
function uncheck_specific_options(checkboxname, toogle_parent, ajax_filter_url)
{
	// if the parent toogle (the 'all' option) is checked
	if(toogle_parent.checked)
	{
		// for each checkbox with the current name
		$("input." + checkboxname).each(function() {
			
			// unless it's the first, general item
			if(this.value != 'all')
			{
				// uncheck all specific options
				this.checked = false;
			}
	    });
	}
	
	// submit form
	submit(ajax_filter_url);
}

/* checks/unchecks the 'all' checkbox based on whether or not any
 * of the specific options are checked
 */
function toogle_general_option(checkboxname, current_item, ajax_filter_url)
{
	var check_all_checkbox = true;
	var check_all = false;
	
	$("input." + checkboxname).each(function() {
		
		// unless it's the first, general item
		if(this.value == 'all')
		{
			check_all = this;
		}
		else
		{
			// if the current item is checked
			if(this.checked)
			{
				// uncheck all checkboxe
				check_all_checkbox = false;
			}
		}
    });
	
	// perfom the actual unchecking
	check_all.checked = check_all_checkbox;
	
	// submit form
	submit(ajax_filter_url);
}

/* submit data to filter */
function submit(ajax_filter_url)
{
	// for each checkbox
	var formfields = new Object();
	
	$("#filtersearch input.path, #filtersearch input.month, #filtersearch input.place, ").each(function()
	{
		/*
		if(typeof formfields[this.name] == "undefined")
		{
			formfields[this.name] = new Array();
		}
		*/
		
		// if the current check box is checked
		if(this.checked)
		{
			formfields[this.name+'['+ this.value + ']'] = this.value;
		}
	});
	
	/* for each hidden form field */
	$("#filtersearch #search_query").each(function()
	{
		formfields[this.name] = this.value;
	});
	
	// display loading message
	//$.blockUI({ message: loadingMsg });
	
	
	
	jQuery('#ajax_filter_result .content-view-children').fadeOut('fast', function(){
		jQuery('.loading').css( 'display', 'block' );
		$.ajax({
		  type: 'post', 
		  url: ajax_filter_url,
		  data: formfields,
		  success: handleFilterResponse
		});
	});
}

/* handle filter response */
function handleFilterResponse(data)
{
	jQuery('.loading').fadeOut( 'slow', function () {
		
		$('#ajax_filter_result').html(data);
		jQuery('#ajax_filter_result .content-view-children').show('slow');
	});
	
	
	//$.unblockUI();
}
