Ext.namespace('Ext.ux.grid.ansGrid');

Ext.ux.grid.ansGrid = Ext.extend(Ext.grid.GridPanel, {

    initComponent: function() {

        //check for an existing tbar config
        if (!this.tbar) {

            Ext.apply(this, {

                tbar: [{
                    icon: '../ext/resources/images/csv.gif',
                    cls: 'x-btn-icon',
                    tooltip: 'Export to csv',
                    scope: this,
                    handler: function(button, event) {

                        var mystore = this.getStore();

                        var module = mystore.baseParams.extModule;
                        //check to see if a specific call back fn has been defined for exporting data
                        if (mystore.baseParams.exportMethod) {
                            var method = mystore.baseParams.exportMethod;
                        }
                        else {
                            var method = mystore.baseParams.extMethod;
                        }

                        var params = Ext.util.JSON.encode(mystore.baseParams);

                        postwith('../extAjax.php', {
                            extModule: module,
                            extMethod: method,
                            __format: 'csv',
                            params: params
                        });
                    }
                }]

            },
            {}

            ); //end apply
        }
        else {
            //wait for on render as we cant add to toolbar before this
            this.on('render',
            function() {

                var mytoolbar = this.getTopToolbar();

                mytoolbar.add('-', {
                    style: 'width:100px',
                    icon: '../ext/resources/images/csv.gif',
                    cls: 'x-btn-icon',
                    tooltip: 'Export to csv',
                    scope: this,
                    handler: function(button, event) {

                        var mystore = this.getStore();

                        var module = mystore.baseParams.extModule;
                        //check to see if a specific call back fn has been defined for exporting data
                        if (mystore.baseParams.exportMethod) {
                            var method = mystore.baseParams.exportMethod;
                        }
                        else {
                            var method = mystore.baseParams.extMethod;
                        }

                        var params = Ext.util.JSON.encode(mystore.baseParams);

                        postwith('../extAjax.php', {
                            extModule: module,
                            extMethod: method,
                            __format: 'csv',
                            params: params
                        });
                    }

                }); //end add

            }); //end on render

        } //end if

        Ext.ux.grid.ansGrid.superclass.initComponent.call(this);

    } //end init

});
