<script type="text/javascript">

$(document).ready(function() {

    $(document).on( "click", '#[% table_id %] tr td input[type="checkbox"]', function() {

        if($(this).attr("checked") == "checked") {
            $(this).parents("tr").siblings().find("td input[type='checkbox']").attr("checked", false);
            $("#[% hidden_id %]").val( $(this).parents("tr")
                        .find("td:first").text() );
        } else {
            $("#[% hidden_id %]").val("");
        }

    });

    $('#[% table_id %]')
        .dataTable( {
        "sDom": "<'row-fluid'<'pull-left'r><'pull-right'f>>t<'row-fluid'<'pull-left'i><'pull-right'p>>",
        "bProcessing": true,
        "bServerSide": true,
        "bPaginate": true,
        "sPaginationType": "bootstrap",
        "bLengthChange": false,
        "bSort": true,
        "bInfo": true,
        "iDisplayLength": 4,
        "iShowPages": 5,
        "oLanguage": {
            "sUrl": "/js/i18n/[% language_file %]"
        },
        "sAjaxSource": "[% ajax_src %]",
        "bStateSave": false,

        "aoColumns": [
                        [% FOREACH f IN table_fields -%]
                            [%index = loop.count - 1%]
                            [%IF table_titles.${index} %]
                        {
                           "mData": "[% f.replace('\.','_') %]",
                           "mRender": function ( data, type, full, opt ) {
                                if(data == null)
                                    return '';
                                [% IF custom_renderers.${f} -%]
                                    var renderCustom = [% custom_renderers.${f} -%];
                                [% ELSE -%]
                                    var renderCustom = function(data, type, full, opt) { return data; };
                                [% END -%]
                                return String(renderCustom(data, type, full)).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
                            }
                        },
                            [% END -%]
                        [% END -%]
                        { "mRender": function ( data, type, full, opt ) {
                                return '<input type="checkbox" value="' + full.id + '"/>';
                            },
                          "mData": null,
                          "bSortable": false
                        }
        ],
        "fnRowCallback": function(nRow, aData, iDisplayIndex) {
            nRow.className = "sw_action_row";
            if($(nRow).find("td:first").text() == $("#[% hidden_id %]").val())
            {
                $(nRow).find("td input[type='checkbox']").attr("checked", "checked");
            }
            return nRow;
        },
        "fnServerParams": function ( aoData ) {
            aoData.push( {"name":"iIdOnTop","value":"[% value %]"} );
        },
        "initComplete": function(settings, json) {
            if ("[% c.loc(search_tooltip) %]".length > 0) {
                $( "#[% table_id %]_filter input" ).tooltip({
                    title: "[% c.loc(search_tooltip) %]"
                });
            }
        }
    } );
} );
</script>

<div class="control-group [% IF errors.size %]error[% END %]">
     <label class="control-label" for="[% table_id %]">[% label %]</label>
     <div class="controls">
         <input type="hidden" name="[% field_name %]" value="[% value %]" id="[% hidden_id %]"/>
         <table class="table table-bordered table-striped table-highlight table-hover" id="[% table_id %]">
            <thead>
                <tr>
                    [% FOREACH t IN table_titles %]
                        [%IF t%]
                        <th>[% t %]</th>
                        [% END %]
                    [% END %]
                    <th class="span"></th>
                </tr>
            </thead>
            <tbody>
                <tr class="sw_action_row">
                    <td>Loading</td>
                </tr>
            </tbody>
        </table>
        [% IF errors.size -%]
        <span class="help-inline">
            [% errors.join('<br/>') %]
        </span>
        [% END -%]
    </div>
</div>
[% # vim: set tabstop=4 syntax=html expandtab: -%]
