GridPrinter = {
    print: function(grid, title)
    {
        var colModel = grid.getColumnModel();
        var columns = [];
        var data = [];

        for (i = 0; i < colModel.config.length; i++)
        {
            if (!colModel.isHidden(i))
            {
                columns[columns.length] = colModel.config[i];
                columns[columns.length - 1].dataIndex = columns[columns.length - 1].dataIndex.replace(/ /g, "");
            }
        }

        grid.store.data.each(function(item)
        {
            var convertedData = [];

            //apply renderers from column model
            for (var key in item.data)
            {
                var value = item.data[key];
                var i = 0;

                key = key.replace(/ /g, "");

                Ext.each(columns, function(column)
                {
                    if (column.dataIndex == key && !colModel.isHidden(i))
                    {
                        convertedData[key] = column.renderer ? column.renderer(value) : value;
                    }
                    i++;
                }, this);
            }
            
            data.push(convertedData);
        });

        //use the headerTpl and bodyTpl XTemplates to create the main XTemplate below
        var headings = GridPrinter.headerTpl.apply(columns);
        var body = GridPrinter.bodyTpl.apply(columns);

        var html = new Ext.XTemplate(
      '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
      '<html>',
        '<head>',
          '<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />',
          '<link href="' + GridPrinter.stylesheetPath + '" rel="stylesheet" type="text/css" media="screen,print" />',
          '<title>' + grid.title + '</title>',
        '</head>',
        '<body style="padding: 10px;">',
          '<div align="center" style="font-weight: bold; font-family: arial; padding-bottom: 10px">',
            title,
          '</div>',
          '<table>',
            headings,
            '<tpl for=".">',
              body,
            '</tpl>',
          '</table>',
        '</body>',
      '</html>'
    ).apply(data);

        //open up a new printing window, write to it, print it and close
        var win = window.open('', 'printgrid', 'width=910px, height=650px"');

        win.document.write(html);
        win.document.close();

        var printWin = function()
        {
            win.print();
            //win.close();
        }

        printWin.defer(500);
    },

    stylesheetPath: getAppRoot() + '/Scripts/ext/plugins/GridPrinter.css',

    headerTpl: new Ext.XTemplate(
    '<tr>',
      '<tpl for=".">',
        '<th>{header}</th>',
      '</tpl>',
    '</tr>'
  ),

    bodyTpl: new Ext.XTemplate(
    '<tr>',
      '<tpl for=".">',
        '<td>\{{dataIndex}\}</td>',
      '</tpl>',
    '</tr>'
  )
};
