Ext.ux.BioportalReader = function(meta, recordType){
	meta = meta || {};
	meta.fields = ['label','concept_id','concept_type','version_id'];
	Ext.ux.BioportalReader.superclass.constructor.call(this, meta, recordType || meta.fields);
};

Ext.extend(Ext.ux.BioportalReader, Ext.data.ArrayReader, {
	readRecords : function(o){
		var recs = o.data.split('~!~');
		for(var i=0; i<recs.length; i++){
			if(recs[i].length == 0){
				recs.splice(i,1);	
			}else{
				recs[i] = recs[i].split('|');
			}
		}
		if(recs.length > 0){
			recs.push(['','BIOPORTAL_ATTRIBUTION']);
		} 

		return Ext.ux.BioportalReader.superclass.readRecords.call(this, recs);
	}
});


/****************************************************************************************************
	This should take care of everything you need to make the type-ahead searching from Bioportal.
	Accepts the same parameters as a normal Ext.data.Store with a few key default values:
		ontology_id: Which ontology you would like to search - defaults to that of Radlex.
		timeout: Shortened to a default of 10 seconds rather than 30.
		reader: You may create your own reader, but the default is to create a BioportalReader
****************************************************************************************************/
Ext.ux.BioportalJsonStore = Ext.extend(Ext.data.Store, {
	constructor : function(config){
		config.ontology_id = config.ontology_id || 1057;
		if(typeof config.proxy != 'object'){
			config.proxy = new Ext.data.ScriptTagProxy({
				url: 'http://bioportal.bioontology.org/search/json_search/'+(config.ontology_id || 1057)
				,timeout: config.timeout || 10000
			});
		}
		if(typeof config.reader != 'object'){
			config.reader = new Ext.ux.BioportalReader();
		}
		
		config.baseParams = Ext.apply(config.baseParams || {}, {response:'json'});
		Ext.ux.BioportalJsonStore.superclass.constructor.call(this,config);
	}
});




Ext.ux.BioportalComboBox = Ext.extend(Ext.form.ComboBox, {
	queryParam:'q',
	
	// Quick intercept to see if the selected record (if it is a record)
	// is actually the link back to Bioportal so we can keep that 
	// link code in one place for all of the extensions.
    onViewClick : function(doFocus){
        var index = this.view.getSelectedIndexes()[0],
            s = this.store,
            r = s.getAt(index);
        if(r){
			if(r.get('concept_id') == 'BIOPORTAL_ATTRIBUTION'){
				window.open('http://bioportal.bioontology.org');
			}else{
            	this.onSelect(r, index);
			}
        }else {
            this.collapse();
        }
        if(doFocus !== false){
            this.el.focus();
        }
    },
	
    onSelect : function(record, index){
        if(this.fireEvent('beforeselect', this, record, index) !== false){
            	this.setValue(record.data[this.valueField || this.displayField]);
				this.concept_id = record.get('concept_id');
				this.collapse();
				this.fireEvent('select', this, record, index);
        }
    },
	
	onKeyUp : function(e){
		this.concept_id = '';
		Ext.ux.BioportalComboBox.superclass.onKeyUp.call(this, e);
	}
	
	,concept_id:''
	,enableKeyEvents:true
    ,validationEvent:false
    ,validateOnBlur:false
	,hideTrigger:false
	,queryParam:'q'
	,tpl:new Ext.XTemplate(
		'<tpl for=".">',
			'<div class="x-combo-list-item">',
				'<tpl if="concept_id != \'BIOPORTAL_ATTRIBUTION\'">',
					'{label}&nbsp;<span style="font-size:7pt; color:blue">({concept_type})</span>',
				'</tpl>',
				'<tpl if="concept_id == \'BIOPORTAL_ATTRIBUTION\'">',
					'<span style="color: grey; font-size: 8pt; font-family: Verdana; padding: .8em .5em .3em;">Results provided by <a style="color: grey;" href="http://bioportal.bioontology.org" target="_blank">NCBO BioPortal</a></span>',
				'</tpl>',
			'</div>',
		'</tpl>'
	)
});



/* All we're really doing here is adding the functionality of a TwinTriggerField. */
Ext.ux.BioportalTwinTriggerField = Ext.extend(Ext.ux.BioportalComboBox, {
    initComponent : function(){
        Ext.ux.BioportalTwinTriggerField.superclass.initComponent.call(this);
		Ext.form.TwinTriggerField.prototype.initComponent.call(this);
        this.on('specialkey', function(f, e){
            if(e.getKey() == e.ENTER){
                this.onTrigger2Click();
            }
        }, this);
    },
	
    getTrigger : function(index){
		return Ext.form.TwinTriggerField.getTrigger.call(this,index);
//        return this.triggers[index];
    },
	
	initTrigger : function(){
		Ext.form.TwinTriggerField.prototype.initTrigger.call(this);
	},
	
	getTriggerWidth: function(){
		return Ext.form.TwinTriggerField.prototype.getTriggerWidth.call(this);
	},

    onDestroy : function() {
        Ext.destroy(this.triggers);
		Ext.form.ComboBox.prototype.onDestroy.call(this);
    },
	
    onTrigger1Click : Ext.emptyFn,
    
    onTrigger2Click : Ext.emptyFn
	
    ,trigger1Class:'x-form-clear-trigger'
//    ,trigger2Class:'x-form-search-trigger'
	,hideTrigger1:true
	,hideTrigger:false
});
