/***********************************************************

 Ext.ux.form.NABPicker Release 1

 NAB Picker/Manipulation Scripts for Notes Domino with ExtJS v1.1.1 by M D McNamara
 
 Usage: Used to create a live address picker in a standard Ext form -- config with normal 
 		Ext.form.Combobox config parameters, plus optional NABPicker 
		parameters (DBPath, view, etc.). A common datastore will be used for all instances of 
		the NABPicker in your forms, unless you specifiy a different "DPPath", 
		"view", or "count".

 Constructor: Ext.ux.form.NABPicker(<config>)

 Inherits From: Ext.form.Combobox

 Optional Config Parameters: 
 	DBPath: 	the path to the particular NAB you want to use for this form item 
				(default: /names.nsf)
 	view: 		the view in the NAB that you want to access 
				(default: ($VIMPeopleByLastName))
 	count: 		how many entries you want returned with each query 
				(default: 60)
 	tpl: 		a specific Ext template to use with this form item
 				(default: Ext.ux.form.NABPicker.config.tpl)
 	queryDelay: a specific delay (in milliseconds) to use with this form item
 				(default: 150)
************************************************************/

Ext.namespace("Ext.ux.form", "Ext.ux.form.NABPicker");

Ext.ux.form.NABPicker = function(config) {
	// Grab shortened form of namespace
	var uxNABPicker = Ext.ux.form.NABPicker;

	// Default constructor call
	uxNABPicker.superclass.constructor.call(this, config);
	
	// must be "local" so that an automatic load is not performed
	this.mode = "local"; 

	this.displayField = "Abbrev";
	this.typeAhead = false;
	this.triggerAction = "all";
	this.resizable = true;
	
	this.tpl = (this.tpl || uxNABPicker.config.tpl);
	this.queryDelay = (this.queryDelay || 150);
	
	// Paging not yet enabled in any way, no no when, not no how.
	this.pageSize = 0;

	// If common store has not already been created
	if (!uxNABPicker.data.store)
	{
		// Passing "true" to createNABDataStore() will create the default datastore
		uxNABPicker.data.store = this.createNABDataStore(true);
	}

	// If any custom values have been passed, create custom datastore for this instance
	this.store = (this.DBPath || this.view || this.count) ? 
		this.createNABDataStore() :
		uxNABPicker.data.store;

	// beforequery handler creation -- load is handled explicitly here
	this.on("beforequery", function (e) {
		this.store.load({
			params: (
				e.query == "" ? 
				{} : 
				{
					"StartKey": e.query,
					"UntilKey": e.query + "~"
				}
			)
		});
	});
	
	// Configure second button
	this.triggerConfig = {
		// padding needed to prevent IE from clipping 2nd trigger button
		tag: 'span', 
		cls: 'x-form-twin-triggers', 
		style: 'padding-right:2px',  
		cn: [
		{
				tag: "img", 
				src: Ext.BLANK_IMAGE_URL, 
				cls: "x-form-trigger"
			},
			{
				tag: "img", 
				src: Ext.BLANK_IMAGE_URL, 
				cls: "x-form-trigger x-form-clear-trigger"
			}
		]
	};
};

Ext.ux.form.NABPicker.config = {
	"tpl": new Ext.Template(
		'<div class="x-combo-list-item">',
		'{Last}, {First} {Middle}',
		'</div>'
	),
	"reader": new Ext.data.XmlReader(
		{
			record: "viewentry",
			id: "entrydata[@columnnumber=1] > text"
		}, 
		[
			{name: "Last", mapping: "entrydata[@columnnumber=0] > text"},
			{name: "Abbrev", mapping: "entrydata[@columnnumber=1] > text"},
			{name: "First", mapping: "entrydata[@columnnumber=2] > text"},
			{name: "Middle", mapping: "entrydata[@columnnumber=3] > text"}
		]
	),
	"DBPath": "/names.nsf",
	"view": "($VIMPeopleByLastName)",
	"count": 60
};

Ext.ux.form.NABPicker.data = {
	"store": null
};

Ext.onReady(function(){
	// Speed up template usage
	Ext.ux.form.NABPicker.config.tpl.compile();
});

Ext.extend(Ext.ux.form.NABPicker, Ext.form.ComboBox, {
	createNABDataStore: function (initBool) {
		// Internal function to grab correct variable namespace
		function getValue(id) {
			return (initBool ?
				Ext.ux.form.NABPicker.config[id] :
				(this[id] || Ext.ux.form.NABPicker.config[id]));
		};
		
		// Create new datastore
		var newDataStore = new Ext.data.Store({
			baseParams: {
				"Count": getValue("count")
			},
			proxy: new Ext.data.HttpProxy({
				method: "GET",
				url: getValue("DBPath") + "/" + getValue("view") + "?readviewentries"
			}),
			reader: Ext.ux.form.NABPicker.config.reader
		});
		
		newDataStore.on("beforeload", function (store, options) {
			if (options.params.limit)
			{
				// If a paging operation is being performed
				return false;
			}
		});
		
		return newDataStore;
	},
	
	getTrigger: function (index) {
		return this.triggers[index];
	},
	
	initTrigger: function () {
		var ts = this.trigger.select('.x-form-trigger', true);
		this.wrap.setStyle('overflow', 'hidden');
		var triggerField = this;
		
		ts.each(function(t, all, index) {
			t.hide = function() {
				var w = triggerField.wrap.getWidth();
				this.dom.style.display = 'none';
				triggerField.el.setWidth(w-triggerField.trigger.getWidth());
			};
			
			t.show = function() {
				var w = triggerField.wrap.getWidth();
				this.dom.style.display = '';
				triggerField.el.setWidth(w-triggerField.trigger.getWidth());
			};
			
			var triggerIndex = 'Trigger'+(index+1);

			if (this['hide' + triggerIndex]) {
				t.dom.style.display = 'none';
			}
			
			t.on("click", this['on' + triggerIndex + 'Click'], this, 
				{preventDefault: true}
			);
			
			t.addClassOnOver('x-form-trigger-over');
			t.addClassOnClick('x-form-trigger-click');
		}, this);
		
		this.triggers = ts.elements;
	},
	
	// pass to original combobox trigger handler
	onTrigger1Click: function () {
		this.onTriggerClick()
	}, 
  
	// clear contents of combobox
	onTrigger2Click: function () {
		this.reset()
	}   
});
