$.fn.dyntable = function(tablesourcescript, controllerformname, popuptargetname, callback) {
    this.each(function() {
        var tablecontainer = $(this);
        if (controllerformname != null) {
            // There is a form controlling this table
            var cf = $(controllerformname);
            cf.submit(function() {
                tablecontainer.html('<div style="text-align: center;">Søger...</div>');
                $.post(tablesourcescript, cf.serialize(), function(data) {
                    tablecontainer.html(data);
                    attachFunctions(tablecontainer);
                })
                return false;
            }, 'html');
        } else {
            // Load table data
            $.post(tablesourcescript, null, function(data) {
                tablecontainer.html(data);
                attachFunctions(tablecontainer);
            }, 'html');
        }

    })
    function attachFunctions(tablecontainer) {
        if (typeof callback == 'function') callback();
        $('th.sortcol',tablecontainer).css('cursor', 'pointer').click(function() {
            var currentsearch = (controllerformname != null) ? $(controllerformname).serialize() : 'dummy=1';
            $.post(tablesourcescript, currentsearch+'&sortcolumn='+$(this).getcv('columnname'), function(data) {
                tablecontainer.html(data);
                attachFunctions(tablecontainer);
            })
        });
        // Multicheck
        $('.multicheck').click(function() {
            if ($(this).is(':checked')) $(this).closest('table').find('.dyncheck').attr('checked', true);
            else $(this).closest('table').find('.dyncheck').attr('checked', false);
        })
        $('.dyncheck').click(function() {
            var multi = $(this).closest('table').find('.multicheck');
            var check = true;
            $(this).closest('table').find('.dyncheck').each(function() {
                if (! $(this).is(':checked')) {
                    check = false;
                    return false;
                }
                return true;
            })
            multi.attr('checked', check);
        })



        $('.sortabletablediv', tablecontainer).hover(function() {
            $(this).addClass('sortabletablerevealer');
        },function() {
            $(this).removeClass('sortabletablerevealer');
        });
        if (popuptargetname != null) {
            $('.sttpop',tablecontainer).mypop(popuptargetname);
        }
        $('.columnedit', tablecontainer).click(function() {
            var currentsearch = (controllerformname != null) ? $(controllerformname).serialize() : 'dummy=1';
            $.post(tablesourcescript, currentsearch+'&editmode=1', function(data) {
                tablecontainer.html(data);

                attachFunctions(tablecontainer);
                $('.fieldlist.shown', tablecontainer).sortable({
                    connectWith: '.fieldlist'
                })
                $('.fieldlist.hidden', tablecontainer).sortable({
                    connectWith: '.fieldlist',
                    receive: function(event, ui) {
                        if (ui.item.hasClass('requiredcol')) $(ui.sender).sortable('cancel');
                    }
                })
                
                $('.canceltablechanges', tablecontainer).click(function() {
                    $.post(tablesourcescript, currentsearch, function(data) {
                        tablecontainer.html(data);
                        attachFunctions(tablecontainer);
                    }, 'html')
                    return false;
                })
                $('.accepttablechanges', tablecontainer).click(function() {
                    // Gather columns into string
                    var selectedcolumns = '';
                    $('.fieldlist.shown li').each(function() {
                        selectedcolumns += $(this).getcv('fieldname') + ' ';
                    })
                    // Save
                    $.post(tablesourcescript, currentsearch+'&tableobjective=setcolumns&columns='+selectedcolumns, function(data) {
                        tablecontainer.html(data);
                        attachFunctions(tablecontainer);
                    }, 'html');
                    return false;
                })
                $('.backtostandard', tablecontainer).click(function() {
                    // Save
                    $.post(tablesourcescript, currentsearch+'&tableobjective=resetcolumns', function(data) {
                        tablecontainer.html(data);
                        attachFunctions(tablecontainer);
                    }, 'html');
                    return false;
                })
            }, 'html')
        }).css('cursor', 'pointer');
        applyDesign();

    }
    return this;
}

function dt_getids() {
    var ids = new String();
    $('.dyncheck:checked').each(function() {
        if (ids.length > 0) ids = ids + ',';
        ids = ids + $(this).val();
    });
    return ids;
}
