(function($){

function replaceListingTable(overrides) {
    var defaults = {};
    $.get('get_imio_folder_listing', $.extend(defaults, overrides), function(data) {
        $("#imio-helpers-folder-listing").replaceWith(data);
        // fix up links generated by batching
        $("div.listingBar a").each(function(){
            $(this).attr("href", $(this).attr("href").replace(/imio_folder_listing_get_table/, ""));
        });
        /* recover odd/even classes on table TR's */
        tbody = $("#imio-folder-listing-table").find('tbody:first');
        tbody.each(setoddeven);
    });
}

function setoddeven() {
var tbody = $(this);
// jquery :odd and :even are 0 based
tbody.find('tr').removeClass('odd').removeClass('even')
.filter(':odd').addClass('even').end()
.filter(':even').addClass('odd');
}

$(document).ready(function(){
    /* folder listing table loading actions */
    $("#content-core").delegate("div.listingBar a", "click", function(event) {
        event.preventDefault();
        var link = $(this).attr("href");
        var page = decodeURI((RegExp("b_start\:int" + '=' + '(.+?)(&|$)').exec(link)||[,null])[1]);
        replaceListingTable({ "b_start\:int": page });
    });
});

})(jQuery);
