﻿/// <reference path="http://extjs.cachefly.net/ext-3.3.1/adapter/ext/ext-base.js" />
/// <reference path="http://extjs.cachefly.net/ext-3.3.1/ext-all.js" />
/// <reference path="locale.js" />
Ext.ns('BolResults');

function ResourceLinkRenderer(value) {
	return '<a href="' + BaseResourceURL + value + '">' + value + '</a>';
}

Ext.onReady(function() {
	Ext.QuickTips.init();

	// DATA STUFF
	var RecordDef = Ext.data.Record.create([
		{ name: 'GroupResourceId', mapping: 'GroupResourceId', type: 'int' },
		{ name: 'Category', mapping: 'ResourceCategory' },
		{ name: 'Resource', mapping: 'ResourceFile' },
		{ name: 'Description', mapping: 'Description' }
	]);

	var DataReader = new Ext.data.JsonReader({
		totalProperty: 'results',    // The property which contains the total dataset size (optional)
		root: 'rows',                // The property which contains an Array of row objects
		id: 'GroupResourceId'             // The property within each row object that provides an ID for the record (optional)
	}, RecordDef);

	var DataStore = new Ext.data.GroupingStore({
		proxy: new Ext.data.HttpProxy({ url: DataProxyURL }),
		reader: DataReader,
		groupField: DefaultGroupField,
		sortInfo: { field: DefaultSortField, direction: 'ASC' },
		remoteSort: false
	});
	DataStore.load();

	// GRID STUFF
	var ColumnModel = new Ext.grid.ColumnModel([
		{ id: 'cat', header: locale.getLocaleString('cmheaderCategory'), dataIndex: 'Category', width: 100, sortable: true },
		{ id: 'res', header: locale.getLocaleString('cmheaderResource'), dataIndex: 'Resource', width: 50, sortable: true, renderer: ResourceLinkRenderer },
		{ id: 'desc', header: locale.getLocaleString('cmheaderResourceDescription'), dataIndex: 'Description', sortable: true }
	 ]);

	var grid = new Ext.grid.GridPanel({
		id: "ResourcesGrid"
		, title: GridTitle
		, layout: 'fit'
		, autoScroll: true
		, store: DataStore
		, colModel: ColumnModel
		// Set the grouping configuration
		, view: new Ext.grid.GroupingView({
			forceFit: true,
			emptyText: locale.getLocaleString('resourceGridEmptyTxt'),
			groupTextTpl: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? \'' + locale.getLocaleString('resourceGridGroupFiles') + '\' : \'' + locale.getLocaleString('resourceGridGroupFile') + '\']})',
			hideGroupedColumn: true,
			autoExpandColumn: "desc"
		})
		, title: GridTitle
		, frame: true
		, stripeRows: true
		, loadMask: true
		, iconCls: 'icon-grid'
		, renderTo: RenderTagId
	});
	loadViewPort(grid)
});    // eo function onReady



function loadViewPort(myGrid) {

	var viewport = new Ext.Viewport({
		id: 'viewport'
		, layout: 'border'
		, border: false
		, renderTo: 'results'
		, items: [{
			id: 'northRegion'
			, region: 'north'
			, border: false
			, collapsible: false
			, contentEl: 'northdiv'
		}, {
			region: 'south'
			, border: false
			, collapsible: false
			, contentEl: 'footerdiv'
		}, {
			region: 'center'
			, border: false
			, layout: 'border'
			, contentEl: 'contentdiv'
			, id: 'contentView'
			, items: [{
				region: 'north'
				, contentEl: 'IntroDiv'
			}, {
				region: 'center'
				, border: false
				, items: myGrid
				, layout: 'fit'
			}]
		}]
	});

}
