function notifyUpdating() { notification.show(updatingText, 'updating', -1); }

$(function() {
    function filterProducts()
    {
        var isFiltered  = false;
        var data        = { filter: true };

        //Populate the post data from each field
        $(filterFields).each(function() {
            var name = this.name, value = $(this).val();
            
            if (value) data[name] = value;
            if (this.selectedIndex > 0) isFiltered = true;
        });
        
        //Don't allow product reordering or pagination if filtering is active
        if (isFiltered)
        {
            $(adminControls).attr("disabled", "disabled");
			$(pagination).hide();
			$(filterReset).fadeIn("fast");
        }
        else
        {
            $(adminControls).removeAttr("disabled");
			$(pagination).show();
			$(filterReset).fadeOut("fast");
        }
        
        notifyUpdating();
        $.get(this.action, data, populateResults);
        
        return false;
    }
    
    //Note: this is accomplishing exactly the same thing as a standard form.reset() event would.
    //However, form.reset() kicks in too late in some (all?) browsers, resulting in the fields resetting after the form has already been submitted. So we do it by hand instead.
    function resetFilters()
    {
        $(filterFields).each(function() { this.selectedIndex = 0 });
        $(filterForm).submit();
        return false;
    }
    
    function populateResults(data)
    {
        $(productsWrapper).html(data);
        notification.hide();
    }
    
    
    $(filterFields).change(function()  { $(filterForm).submit() });
    $(filterReset).click(function()    { $(filterForm).trigger("reset") });
    
    $(filterForm).submit(filterProducts);
    $(filterForm).bind("reset", resetFilters);
    
    $(filterReset).hide();
    
    //This ensures that filter selection is not retained after refreshing the page (which would otherwise happen in Firefox.)
    //TODO: should we instead apply whatever the form's state is on page load?
    $(window).unload(function() { $(filterForm).trigger("reset") });
});