$(document).ready(function(){
	
	var siteaccess;
	siteaccess = $('#siteaccess').text();
	
	$('#see_all').live("click", function(){
		uncheck_specific_options( 'customArea', '/' + siteaccess + '/tellus/filteractivity' );
	});
	$('input.customArea').live("click", function(){
		toogle_general_option( 'customArea', '/' + siteaccess + '/tellus/filteractivity' );
	});
	$('input.filterCategory').live("click", function(){
		toogle_general_option_category( 'filterCategory', '/' + siteaccess + '/tellus/filteractivity' );
	});
	$('#see_all_activity_categories').live("click", function(){
		uncheck_specific_options_category( 'filterCategory', '/' + siteaccess + '/tellus/filteractivity' );
	});
	function uncheck_specific_options_category(checkboxname, ajax_filter_url)
	{
		// if the parent toogle (the 'all' option) is checked

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

		if ($('#see_all').attr('checked'))
		{
			// for each checkbox with the current name
			$("input." + checkboxname).each(function() {
				// unless it's the first, general item
				if(this.value != 'see_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_category(checkboxname, ajax_filter_url)
	{
		var selectAll = true;
		$('input[name=see_all_activity_categories]').attr('checked', false);
		$("input." + checkboxname).each(function() {
			if ( this.checked == true )
			{
				selectAll = false;
			}
		});
		if ( selectAll )
		{
			$('input[name=see_all_activity_categories]').attr('checked', true);
		}
		// submit form
		submit(ajax_filter_url);
	}
	function toogle_general_option(checkboxname, ajax_filter_url)
	{
		var selectAll = true;
		$('input[name=see_all]').attr('checked', false);
		$("input." + checkboxname).each(function() {
			if ( this.checked == true )
			{
				selectAll = false;
			}
		});
		if ( selectAll )
		{
			$('input[name=see_all]').attr('checked', true);
		}
		// submit form
		submit(ajax_filter_url);
	}
	
	
	/* submit data to filter */
	function submit(ajax_filter_url)
	{
		// for each checkbox
		var formfields = new Object();
		
		$("#filter input.customArea").each(function()
		{
			// if the current check box is checked
			if(this.checked)
			{
				formfields[this.name+'['+ this.value + ']'] = this.value;
			}
		});
		$("#filter input.filterCategory").each(function()
		{
			// if the current check box is checked
			if(this.checked)
			{
				formfields[this.name+'['+ this.value + ']'] = this.value;
			}
		});
		$("#filter input.category_fallback").each(function()
		{
			// keeps the fallback category
			formfields[this.name] = this.value;
		});
		
		$("#filter input.topicFlag").each(function()
		{
			// keeps the fallback category
			formfields[this.name] = this.value;
		});
		$("#filter input.ParentNodeID").each(function()
		{
			// keeps the fallback category
			formfields[this.name] = this.value;
		});
		
		$.ajax({
		  type: 'post', 
		  url: ajax_filter_url,
		  data: formfields,
		  success: handleFilterResponse
		});
	}
	
	/* handle filter response */
	function handleFilterResponse(data)
	{
		$('#activity_list_results').html(data);
	}
});