[% MACRO modal_header BLOCK -%]

<div id="mod_edit" class="modal hide ngcp-modal">
    <div class="modal-header">
        <button id="mod_close" type="button" class="close mod_close">×</button>
        <h3>[% m.create_flag == 1 ? c.loc('Create [_1]',m.name) : c.loc('Edit [_1]',m.name) %]</h3>
    </div>

[% END -%]

[% MACRO modal_header_dynamic BLOCK -%]
[%-
    # Variables: m.header – Complete header
    #            m.id     – ID of the modal div, to access it via javascript
-%]

<div id="[% m.id %]" class="modal hide">
    <div class="modal-header">
        <button type="button" class="close mod_close fade" data-dismiss="modal">×</button>
        <h3>[% m.header %]</h3>
    </div>

[% END -%]

[% MACRO modal_footer BLOCK -%]
</div>
[% END -%]

[% MACRO modal_script BLOCK %]
<script>
    var modalFormScript = function (formitem,repeat) {
        $('#mod_edit').modal({keyboard: false, backdrop: 'static'});
        $('.mod_close').click(function(event) {
            [%IF m.close_target_direct%]
            [%m.close_target_direct%]
            [%ELSIF m.ajax_load%]
            $('#mod_edit').modal('hide');
            $('#mod_edit').parent('div').html('');
            [%ELSE%]
            console.log("redirecting to [% m.close_target ? m.close_target : c.uri_for() %]");
            window.location.href="[% m.close_target ? m.close_target : c.uri_for() %]";
            [%END%]
        });
        [%IF m.ajax_load%]
        $('#mod_edit script').each(function( key, value ) {
            eval(value.innerHTML);
        });
        [%END%]

        // on clicking a button within the form, add a hidden field "submitid"
        // determining the name of the button being clicked
        $('input[type=button]').click(function() {
            $(this).parents('form').find('#submitid').attr('value', $(this).attr('name'));
            [%IF !m.ajax_load%]
            $(this).parents('form').submit();
            [%ELSE%]
            $('#mod_edit').modal('hide');
            processModalFormAjax($(this).parents('form'),function(status,form){
                //alert('in callback: status='+status);
                if( status == 'error' ){
                    modalFormScript(formitem);
                }
                if( status != 'error' ){
                    //form object is necessary to compose uri for refresh
                [%IF m.ajax_list_refresh%]
                    refreshAjaxList( '[%m.ajax_list_refresh%]', form );
                [%END%]
                    $('#mod_edit').parent('div').html('');
                }
                [%m.ajax_callback%]
            });
            [%END%]   
        });
    };
    [%IF !m.ajax_load%]$(modalFormScript);[%ELSE%]
    //$('#'+$(this).parents('form').id()+' :input:enabled:visible:first').focus();
    [%END%]
</script>
[% END -%]

[% # vim: set tabstop=4 syntax=html expandtab: -%]

