(function($){$(function(){

	filter = {
		
		// filter form object
		
		//lets get the current module,  we will need to know where to post the search criteria
		distribuidor_pais		: $('input[name="distribuidor_pais"]').val(),
		distribuidor_estado		: $('input[name="distribuidor_estado"]').val(),
		distribuidor_cidade		: $('input[name="distribuidor_cidade"]').val(),

		/**
		 * Constructor
		 */
		init: function(){
			
			$('#distributorsForm').live('submit', function(){
				$('#distribuidores').html('Carregando...');
				$.post('', $(this).serialize(), function(data){
					$('#distribuidores').html(data);
				});
				return false;
			});

			//listener for pagination
			$('.pagination a').live('onclick', function(e){
				e.preventDefault();
				url = $(this).attr('href');
				filter(filter.distribuidor_pais, url);
			});
			
						
		},
	
		handler_response_json: function(json)
		{
			if ('update_filter_field' in json && typeof json.update_filter_field == 'object')
			{
				$.each(json.update_filter_field, filter.update_filter_field);
			}
		},

		update_filter_field: function(field, data)
		{
			var $field = filter.$filter_form.find('[name='+field+']');

			if ($field.is('select'))
			{
				if (typeof data == 'object')
				{
					if ('options' in data)
					{
						var selected, value;

						selected = $field.val();
						$field.children('option').remove();

						for (value in data.options)
						{
							$field.append('<option value="' + value + '"' + (value == selected ? ' selected="selected"': '') + '>' + data.options[value] + '</option>');
						}
					}
				}
			}
		}
	};

	filter.init();

});})(jQuery);
