//<script>



// GridPanelRenderer with support for RowExpanders
Ext.override(Ext.ux.Printer.GridPanelRenderer, {

	oRowExpander: null,
	oGridSummary: null,
	stylesheetPath: '/css/modules/recycling/grid-print.css',
	
	/**
	 * Generates the body HTML for the grid
	 * @param {Ext.grid.GridPanel} grid The grid to print
	 */
	generateBody: function(grid) {
		var columns = this.getColumns(grid);
		this.getGridSummary(grid);
		//use the headerTpl and bodyTpl XTemplates to create the main XTemplate below
		var headings = this.headerTpl.apply(columns);
		var summary = '';
		var body     = this.bodyTpl.apply(columns);
		//var bbar     = this.bbarTpl.apply(columns);
		if (this.oRowExpander) {
			body += String.format('<tr class="summary"><td colspan="{0}">{expander_summary}</td></tr>', columns.length);
		}
		if(this.oGridSummary) {
			summary = String.format('<tr><td colspan="{0}" class="gridSummaryTd">{1}</td></tr>', columns.length, this.oGridSummary);
		}	
		return String.format('<table>{0}<tpl for=".">{1}</tpl>{2}</table>', headings, body, summary);
	},

	/**
	 * Prepares data from the grid for use in the XTemplate
	 * @param {Ext.grid.GridPanel} grid The grid panel
	 * @return {Array} Data suitable for use in the XTemplate
	 */
	prepareData: function(grid) {
		//We generate an XTemplate here by using 2 intermediary XTemplates - one to create the header,
		//the other to create the body (see the escaped {} below)
		var columns = this.getColumns(grid);
		var oRowExpander = this.oRowExpander;
		
		//build a useable array of store data for the XTemplate
		var data = [];
		grid.store.data.each(function(item) {
			var convertedData = {};

			//apply renderers from column model
			Ext.iterate(item.data, function(key, value) {
				Ext.each(columns, function(column) {
					if (column.dataIndex == key) {
						convertedData[key] = column.renderer ? column.renderer(value, null, item) : value;
						return false;
					}
				}, this);
			});
			if (oRowExpander) {
				convertedData.expander_summary = oRowExpander.getBodyContent(item);
			}

			data.push(convertedData);
		});
		
		return data;
	},

	/**
	 * Returns the array of columns from a grid
	 * @param {Ext.grid.GridPanel} grid The grid to get columns from
	 * @return {Array} The array of grid columns
	 */
	getColumns: function(grid) {
		var columns = [];

		Ext.each(grid.getColumnModel().config, function(col) {
			if (col.id == 'expander') {
				this.oRowExpander = col;
			}
			if (col.hidden != true && col.dataIndex != '') columns.push(col);
		}, this);

		return columns;
	},
	/**
	* getGridSummary
	* returns the content of the grid summary
	* @param  {Ext.grid.GridPanel} grid The grid to get summar from
	* @return {html} The html source of the summary
	*/
	getGridSummary:function (grid) {
		if(summary = Ext.DomQuery.selectNode('.x-grid3-summary-table')) {
			this.oGridSummary =  summary.innerHTML;
			return true;
		}
		return false;

	},	
	
	/**
	* Returns the title to give to the print window
	* @param {Ext.Component} component The component to be printed
	* @return {String} The window title
	*/
	getTitle: function(component) {
		return typeof component.getTitle == 'function' ? component.getTitle() : (component.title || "Afdrukken");
	}
});

// Now add this printer to all grids as a print() function.
Ext.grid.GridPanel.prototype.print = function() {
	Ext.ux.Printer.print(this);
}

// Link it the window.print() function. Once a grid is created, it will
// automagically override the window.print() function with the one in here.
Ext.grid.GridPanel.prototype.registerWindowPrint = true;
Ext.grid.GridPanel.prototype.initComponent = Ext.grid.GridPanel.prototype.initComponent.createSequence(function() {
	if (this.registerWindowPrint) {
		var grid = this;
		window.print = function() {
			grid.print();
		}
	}
});

//</script>
