(function(){

Template = function(method) {
    return new function(wnd, data) {
        this.fill = method;
    };
}

// ---[ MACROS ]------------------------------------------------------------------------------------

function DEFAULT(obj, defaultValue) {
    return (_undefined(obj) ? defaultValue : obj);
}

function AUTOREF_WINDOW_BUTTONS(extwin) {
    if(_defined(extwin.content.buttons)) {
        var content_buttons = extwin.content.buttons;
        for(var i = 0; i < content_buttons.length; i++) {
            var button = content_buttons[i];
            if(_defined(button.ref)) {
                if(_undefined(extwin.buttons)) extwin.buttons = {};
                extwin.buttons[button.ref] = button;
            }
        }
    }
}

//////[ window ]////////////////////////////////////////////////////////////////////////////////////
/*

    <- - - - - -<(C)windowWidth>- - - - - ->
    ,--------------------------------------,  ^
    | [(C)windowTitle]                     |  '
    |--------------------------------------|  '
    | <(D)bodyLayout>                      |  '
    |                                      |  '
    |                                      |  '
    |            [(D)content]              |  <(C)windowHeight>
    |                                      |  '
    |                                      |  '
    |                                      |  '
    |--------------------------------------|  '
    |                   [(D)windowButtons] |  ' 
    '--------------------------------------'  ^
    
    references:

        extwin
         |-- content
         '-- buttons

*/

core.jstemplates.window = Template(function(wnd, data) {

    // ---[ DEFAULTS ]------------------------------------------------------------------------------
;
    this.bodyLayout     = DEFAULT( this.bodyLayout,     'anchor'        );
    this.windowButtons  = DEFAULT( this.windowButtons,  []              );
    this.content        = DEFAULT( this.content,        []              );
    this.windowPos      = DEFAULT( this.windowPos,      'center center' );
    
    // ---[ TEMPLATE ]------------------------------------------------------------------------------
    
    var tpl = {
        xtype: 'window',
        pos: this.windowPos,
        layout: 'container',
        items: [{
            xtype: 'panel',
            ref: 'content',
            anchor: '100% 100%',
            border: false,
            layout: this.bodyLayout,
            buttons: this.windowButtons,
            items:[
                this.content
            ]
        }]
    };

    // ---[ CONDITIONNALS ]-------------------------------------------------------------------------

	if(_defined(this.windowMaximized)) tpl.maximized = this.windowMaximized;
    if(_defined(this.windowTitle))  tpl.title   = this.windowTitle;
    if(_defined(this.windowWidth))  tpl.width   = this.windowWidth;
    if(_defined(this.windowHeight)) tpl.height  = this.windowHeight;
    if(_defined(this.windowModal))  tpl.modal   = this.windowModal;
    if(_defined(this.windowToolbar)) tpl.items[0].items.splice(0, 0, {
        xtype: 'toolbar',
        ref: '../toolbar',
        items: this.windowToolbar
    });
    
    // ---< handler >-------------------------------------------------------------------------------
    
    var handler = function(wnd, data) {
        AUTOREF_WINDOW_BUTTONS(wnd.extwin);
    };

    // ---[ STD RETURN ]----------------------------------------------------------------------------

    return {
        tpl: tpl,
        handler: [handler]
    };
});

//////[ login window ]////////////////////////////////////////////////////////////////////////////////////
/*

    <- - - - - -<(C)windowWidth>- - - - - ->
    ,--------------------------------------,  ^
    | [(C)windowTitle]                     |  '
    |--------------------------------------|  '
    | <(D)bodyLayout>                      |  '
    |                                      |  '
    |                                      |  '
    |            [(D)content]              |  <(C)windowHeight>
    |                                      |  '
    |                                      |  '
    |                                      |  '
    |--------------------------------------|  '
    |                   [(D)windowButtons] |  ' 
    '--------------------------------------'  ^
    
    references:

        extwin
         |-- content
         '-- buttons

*/



//////[ block ]/////////////////////////////////////////////////////////////////////////////////////
/*

    ,--------------------------------------,
    | [(C)windowTitle]                     |
    |--------------------------------------|
    | <(D)bodyLayout>                      |
    |                                      |
    |                                      |
    |           [(D)content]               |
    |                                      |
    |                                      |
    |                                      |
    |--------------------------------------|
    |                   [(D)windowButtons] |
    '--------------------------------------'
    
    references:

        extwin
         |-- content
         '-- buttons

*/

core.jstemplates.block = Template(function(wnd, data) {

    // ---[ DEFAULTS ]------------------------------------------------------------------------------

    this.bodyLayout     = DEFAULT( this.bodyLayout,     'anchor'    );
    this.windowButtons  = DEFAULT( this.windowButtons,  []          );
    this.content        = DEFAULT( this.content,        []          );

    // ---[ TEMPLATE ]------------------------------------------------------------------------------

    var tpl = {
        xtype: 'window',
        layout: 'anchor',
        items: [{
            xtype: 'panel',
            ref: 'content',
            anchor: '100% 100%',
            layout: this.bodyLayout,
            //buttons: this.windowButtons,
            items:[
                this.content
            ]
        }] 
    };
    
    // ---[ CONDITIONNALS ]-------------------------------------------------------------------------
    
    if(_defined(this.windowTitle)) tpl.title = this.windowTitle;
    
    // ---< handler >-------------------------------------------------------------------------------
    
    var handler = function(wnd, data) {
        AUTOREF_WINDOW_BUTTONS(wnd.extwin);
    };

    // ---[ STD RETURN ]----------------------------------------------------------------------------
    
    return {
        tpl: tpl,
        handler: [handler]
    };
});

///// window_two_columns ///////////////////////////////////////////////////////////////////////////
/*

    <- - - - - - - -<(C)windowWidth> - - - - - - - ->
    ,-----------------------------------------------,  ^
    | [(C)windowTitle]                              |  '
    |-----------------------------------------------|  '
    | {form} - - - - - - - - - - - - - - - - - - -> |  '
    |  ' <(D)hiddenFields)>  ,                      |  '
    |  '                     |                      |  '
    |  '                     |                      |  <(C)windowHeight>
    |  '   <(D)layoutLeft>   |   <(D)>layoutRight   |  '
    |  '  [(D)contentLeft]   |  [(D)contentRight]   |  '
    |  '                     |                      |  '
    |  ^                     |                      |  '
    |-----------------------------------------------|  '
    |                            [(D)windowButtons] |  ' 
    '-----------------------------------------------'  ^
    
    references:
    
        extwin
         |-- form
         |    '-- hiddenFields
         |-- content (inside form)
         |    |-- left
         |    '-- right
         '-- buttons

*/

core.jstemplates.window_two_columns = Template(function(wnd, data) {

    // ---[ PASSTHROUGH ]---------------------------------------------------------------------------

    // this.windowTitle
    // this.windowWidth
    // this.windowHeight
    // this.windowButtons

    // ---[ FIXED ]---------------------------------------------------------------------------------
    
    this.bodyLayout = 'anchor';

    // ---[ DEFAULTS ]------------------------------------------------------------------------------
    
    this.hiddenFields   = DEFAULT(this.hiddenFields,    []      );
    this.layoutLeft     = DEFAULT(this.layoutLeft,      'form'  );
    this.layoutRight    = DEFAULT(this.layoutRight,     'form'  );
    this.contentLeft    = DEFAULT(this.contentLeft,     []      );
    this.contentRight   = DEFAULT(this.contentRight,    []      );
    
    // ---< content >-------------------------------------------------------------------------------
    
    this.content = {
        xtype: 'form',
        ref: '../form',
        anchor: '100% 100%',
        border: false,
        items: [{
            xtype: 'container',
            ref: 'hiddenFields',
            forceLayout: true,
            hidden: true,
            items: this.hiddenFields
        },{
            xtype: 'container',
            layout: 'column',
            items: [{
                xtype: 'container',
                ref: '../../left',
                layout: this.layoutLeft,
                columnWidth: 0.5,
                style:{
                    padding:'10px'
                },
                items: this.contentLeft           
            },{
                xtype: 'container',
                ref: '../../right',
                layout: this.layoutRight,
                columnWidth: 0.5,
                style:{
                    padding:'10px'
                },
                items: this.contentRight
            }]
        }]
    };
    
    // ---[ CONDITIONNALS ]-------------------------------------------------------------------------
    
    // none
    
    // ---< handler >-------------------------------------------------------------------------------
    
    var handler = function(wnd, data) {
        //...
    }

    // ---[ STD RETURN ]----------------------------------------------------------------------------
    
    var parent = core.jstemplates.window.fill.call(this, wnd, data);
    parent.handler.push(handler);

    return {
        tpl: parent.tpl,
        handler: parent.handler
    };
});

core.jstemplates.window_tree_columns = Template(function(wnd, data) {

    // ---[ PASSTHROUGH ]---------------------------------------------------------------------------

    // this.windowTitle
    // this.windowWidth
    // this.windowHeight
    // this.windowButtons

    // ---[ FIXED ]---------------------------------------------------------------------------------
    
    this.bodyLayout = 'anchor';

    // ---[ DEFAULTS ]------------------------------------------------------------------------------
    
    this.hiddenFields   = DEFAULT(this.hiddenFields,    []      );
    this.layoutLeft     = DEFAULT(this.layoutLeft,      'form'  );
    this.layoutcenter   = DEFAULT(this.layoutCenter,    'form'  );
    this.layoutRight    = DEFAULT(this.layoutRight,     'form'  );
    this.contentLeft    = DEFAULT(this.contentLeft,     []      );
    this.contentCenter  = DEFAULT(this.contentCenter,   []      );
    this.contentRight   = DEFAULT(this.contentRight,    []      );
    this.widthRight     = DEFAULT(this.widthRight,      (1/3)   );
    this.widthCenter    = DEFAULT(this.widthCenter,     (1/3)   );
    this.widthLeft      = DEFAULT(this.widthLeft,       (1/3)   );
    // ---< content >-------------------------------------------------------------------------------
    
    this.content = {
        xtype: 'form',
        ref: '../form',
        anchor: '100% 100%',
        border: false,
        items: [{
            xtype: 'container',
            ref: 'hiddenFields',
            forceLayout: true,
            hidden: true,
            items: this.hiddenFields
        },{
            xtype: 'container',
            layout: 'column',
            items: [{
                xtype: 'container',
                ref: '../../left',
                layout: this.layoutLeft,
                columnWidth: this.widthLeft,
                style:{
                    padding:'10px'
                },
                items: this.contentLeft       
            },{
                xtype: 'container',
                ref: '../../center',
                layout: this.layoutCenter,
                columnWidth: this.widthCenter,
                style:{
                    padding:'10px'
                },
                items: this.contentCenter 
            },{
                xtype: 'container',
                ref: '../../right',
                layout: this.layoutRight,
                columnWidth: this.widthRight,
                style:{
                    padding:'10px'
                },
                items: this.contentRight
            }]
        }]
    };
    
    // ---[ CONDITIONNALS ]-------------------------------------------------------------------------
    
    // none
    
    // ---< handler >-------------------------------------------------------------------------------
    
    var handler = function(wnd, data) {
        //...
    }

    // ---[ STD RETURN ]----------------------------------------------------------------------------
    
    var parent = core.jstemplates.window.fill.call(this, wnd, data);
    parent.handler.push(handler);

    return {
        tpl: parent.tpl,
        handler: parent.handler
    };
});

///// window_order_layout //////////////////////////////////////////////////////////////////////////

core.jstemplates.window_order_layout = Template(function(wnd, data) {

    // ---[ PASSTHROUGH ]---------------------------------------------------------------------------

    // this.windowTitle
    // this.windowWidth
    // this.windowHeight
    // this.windowButtons

    // ---[ FIXED ]---------------------------------------------------------------------------------
    
    this.bodyLayout = 'anchor';
    
    // ---[ DEFAULTS ]------------------------------------------------------------------------------
    
    this.hiddenFields       = DEFAULT(this.hiddenFields,        []          );
    this.layoutTopLeft      = DEFAULT(this.layoutLeft,          'anchor'      );
    this.layoutTopRight     = DEFAULT(this.layoutRight,         'anchor'      );
    this.layoutCenter       = DEFAULT(this.layoutCenter,        'anchor'    );
    this.layoutBottomLeft   = DEFAULT(this.layoutBottomLeft,    'anchor'      );
    this.layoutBottomRight  = DEFAULT(this.layoutBottomRight,   'anchor'      );
    this.contentTopLeft     = DEFAULT(this.contentTopLeft,      []          );
    this.contentTopRight    = DEFAULT(this.contentTopRight,     []          );
    this.contentCenter      = DEFAULT(this.contentCenter,       []          );
    this.contentBottomLeft  = DEFAULT(this.contentBottomLeft,   []          );
    this.contentBottomRight = DEFAULT(this.contentBottomRight,  []          );
    
    // ---< content >-------------------------------------------------------------------------------
    
    this.content = {
        xtype: 'form',
        ref: '../form',
        anchor: '100% 100%',
        border: false,
        items: [{
            xtype: 'container',
            ref: 'hiddenFields',
            forceLayout: true,
            hidden: true,
            items: this.hiddenFields
        },{
            xtype: 'container',
            ref: '../top',
            layout: 'column',
            items: [{
                xtype: 'container',
                ref: 'left',
                layout: this.layoutTopLeft,
                columnWidth: 0.5,
                style:{
                    padding:'10px'
                },
                items: this.contentTopLeft           
            },{
                xtype: 'container',
                ref: 'right',
                layout: this.layoutTopRight,
                columnWidth: 0.5,
                style:{
                    padding:'10px'
                },
                items: this.contentTopRight
            }]
        },{
            xtype: 'container',
            ref: '../center',
            layout: this.layoutCenter,
            items: this.contentCenter
        },{
            xtype: 'container',
            ref: '../bottom',
            layout: 'column',
            items: [{
                xtype: 'container',
                ref: 'left',
                layout: this.layoutBottomLeft,
                columnWidth: 0.5,
                style:{
                    padding:'10px'
                },
                items: this.contentBottomLeft           
            },{
                xtype: 'container',
                ref: 'right',
                layout: this.layoutBottomRight,
                columnWidth: 0.5,
                style:{
                    padding:'10px'
                },
                items: this.contentBottomRight
            }]
        }]
    };
    
    // ---[ CONDITIONNALS ]-------------------------------------------------------------------------
    
    // none
    
    // ---< handler >-------------------------------------------------------------------------------
    
    var handler = function(wnd, data) {
        //console.log(wnd.extwin);
    }

    // ---[ STD RETURN ]----------------------------------------------------------------------------
    
    var parent = core.jstemplates.window.fill.call(this, wnd, data);
    parent.handler.push(handler);

    return {
        tpl: parent.tpl,
        handler: parent.handler
    };
    
});

////////////////////////////////////////////////////////////////////////////////////////////////////

})();
