Ext.namespace ('fcbm.component');

/*
 *   config object must have the following properties :
 *   - title: String 
 *   - documentType: String (training|atlas)
 *   - tabPanel: Ext.TabPanel
 *   - codeLangue: String
 */
fcbm.component.DocumentList = function (config) {
	
	this.initialConfig = Ext.apply(config);
	this.ready = false;

	// Redimensionne la grille 
	var checkResize = function(){
		Ext.util.Observable.capture(Ext.getCmp('documentList'), function(e){
			if (e == 'resize') {
				 this.resize();
			}
		}, this);
	};
	this.resize = function() {
		this.grid.setHeight(Ext.getCmp('documentList').getInnerHeight()/2);
	};
	checkResize.defer(200, this);
	this.resize.defer(200, this);
	
	
	if (this.initialConfig.documentType == 'training')
		var file = 'FileList.xml';
	else
		var file = 'AtlasList.xml';
	
	// load the document list
    var storeDocuments = new Ext.data.Store({
        url: 'data/documents/' + this.initialConfig.codeLangue + '/' + file,
		reader: new Ext.data.XmlReader(
			{
               record: 'Document',
               id: '@id',
               totalRecords: '@total'
			},[
				{name: 'Document', mapping: '@id'},
           		'Description', 
           		{name: 'Type', mapping: '@type'}
			]
		)
    });
	
	function renderDocumentName(data, cell, record, rowIndex, columnIndex, storeDocuments){
        return "<span class=\"colDocument\">"+data+"</span>";
	};
	
	var cm = [
            {id: 'doc', header: "Documents", menuDisabled: true, width: 250, dataIndex: 'Document', sortable: true, renderer: renderDocumentName}
    ];
	
    // create the grid
    this.grid = new Ext.grid.GridPanel({
		width: '100%',
		height: 350,
		title: this.initialConfig.title,
        store: storeDocuments,
        columns: cm,
		collapsible: false,
		autoExpandColumn: 'doc',
        border: false,
		stripeRows: true,		
		hideHeaders: true,
		viewConfig: {
			forceFit: true,
            enableRowBody:true,
            showPreview:true,
			getRowClass : function(record, rowIndex, p, store){
                if(this.showPreview){
                    p.body = '<p class="documentDescription">'+record.data.Description+'</p>';
                    return 'x-grid3-row-expanded';
                }
                return 'x-grid3-row-collapsed';
            }
        },
		autoScroll: true
    });

	storeDocuments.on ('load', function (store, records, options) {
		this.ready = true;
	}, this.grid);
	storeDocuments.load();
	
	this.grid.getSelectionModel().on('rowselect', function (sm, rowIndex, record ) {
		this.selModel.deselectRow(rowIndex);
		
		var loadFile = function () {
			if (document.currentFile != null) {
				document.currentFile.destroy();
				document.currentFile = null;
			}
			
			document.currentFile = new fcbm.component.DocumentRenderer ({
				codeLangue: this.codeLangue,
				name: record.get('Document'),
				type: this.documentType,
				circumstances: record.get('Description'),
				tabPanel: this.tabPanel
			});
			
			if (this.documentType == "atlas")
				new fcbm.model.Compteurs ("AtlasConsultes");
			
			document.currentFile.loadDocument();
			Ext.getCmp('documentList').collapse();
		};
		
		var handle_msgBoxAnswer = function (btn, text) {
			if (btn == 'ok') {
				loadFile.defer (1, {
					codeLangue: this.codeLangue, 
					tabPanel: this.tabPanel, 
					selModel: this.selModel, 
					documentType: this.documentType
				});	
			}
		};
		
		if (document.currentFile != null) {
			if (document.currentFile.document.type == 'training') {
				if (document.currentFile.document.name == record.get('Document')) {
					this.tabPanel.setActiveTab('tabClinicPresentation');
					Ext.getCmp('documentList').collapse();
				}
				else {
					Ext.MessageBox.show({
						title: 'Changement de dossier ?',
						msg: 'Vous êtes en train de travailler sur ce dossier, si vous chargez un autre document, les réponses que vous avez donné seront perdues.<br/><br/>' +
						'Voulez-vous tout de même charger ce document ?',
						buttons: {
							ok: 'Oui',
							no: 'Non'
						},
						fn: handle_msgBoxAnswer,
						icon: Ext.MessageBox.QUESTION,
						scope: this
					});
				}
			} else {
				loadFile.defer (1, {
						codeLangue: this.codeLangue, 
						tabPanel: this.tabPanel, 
						selModel: this.selModel, 
						documentType: this.documentType
				});	
			}
		} else {
			loadFile.defer (1, {
					codeLangue: this.codeLangue, 
					tabPanel: this.tabPanel, 
					selModel: this.selModel, 
					documentType: this.documentType
			});	
		}
		
	}, {
		codeLangue: this.initialConfig.codeLangue, 
		tabPanel: this.initialConfig.tabPanel, 
		selModel: this.grid.getSelectionModel(), 
		documentType: this.initialConfig.documentType
	});


	return this.grid;
};

Ext.extend(fcbm.component.DocumentList, Ext.util.Observable);

