
function ajaxOverview(container)
{
    var self = this;
    
    
    this.container     = $('#'+container);
    this.tableWrapper  = this.container.find('div.tableWrap');
    this.rowsContainer = this.container.find('tbody');
    
    this.onTableLoad();
}

ajaxOverview.prototype.onTableLoad = function()
{
    var self = this;

    this.applyFilterBehaviour();
    this.applyPageBehaviour();
    
    BackendMain.attachBehaviour(this.tableWrapper); 
    
    setTimeout(function() {
        self.container.find('div.filter input[type=text]').focus();
    }, 50);
};

ajaxOverview.prototype.onFilterChange = function(input) 
{
    var self = this;
    
    clearTimeout(this.filterTimeout);
    
    this.filterTimeout = setTimeout(function() {
        
        var value = input.value;

        if (value == self.currentFilter) {
            return;
        }
        
        if (self.currentFilterRequest && self.currentFilterRequest.abort) {
        	self.currentFilterRequest.abort();
        }
        
        self.currentFilter = value;
        
        self.currentFilterRequest = $.get($(input).attr('ajaxlink').replace(/\/_/g, '/' + encodeURIComponent(value)), function(response) {
        	self.tableWrapper.html(response);
        });
        
    }, 250);
};

ajaxOverview.prototype.applyFilterBehaviour = function()
{
    var self = this;
    
    this.container.find('div.filter input[type=text]')
        .keyup(function() {
            self.onFilterChange(this);
        })
        .change(function() {
            self.onFilterChange(this);
        });
};

ajaxOverview.prototype.applyPageBehaviour = function()
{
    var self = this;

    this.container.find('div.paginator a').click(function() {
        
        self.tableWrapper.load($(this).attr('href')+'?format=html', function() {
            self.onTableLoad();
        });
        
        return false;
    });
};
