
////////////////////
/*Output Object - modeled as a collection of the following inputs
  whose values will be set upon selection from the autosuggest:
  		VenueID
	  	venueName
	  	address1
	  	city
	  	state
	  	ZIP
  	
  	Objects creation is keyed on the one input parameter, venue picker ID.
*/
////////////////////



Autocompleter.Ajax.Grid = Autocompleter.Ajax.Base.extend({

	options: {
		resultDelim: '^',
		fieldDelim : '|',
		zIndex: 90000
	},

	initialize: function(el, url, options) {
		this.parent(el, url, $merge(this.options, options));
	},
	

	queryResponse: function(resp) {
		this.parent(resp);
		resultDelim = this.options.resultDelim;
		fieldDelim = this.options.fieldDelim;
		
		this.resultSet = resp.split(resultDelim);
		var choices = [];
		var cIndex = 0
		this.resultSet.each(function(theResult, index) {
			if(theResult.length > 0) {
				var thisResultSet = theResult.split(fieldDelim);
				var choiceText = thisResultSet[1].escapeRegExp();
				var regex = new RegExp('\\b' + choiceText + '\\b', 'ig');
				if(regex.test(choiceText))
					choices[cIndex++] = thisResultSet; // made the cut, ultimately!
			}
		});
		testThis = this.resultSet;
		dbug.log('testThis:');
		dbug.log(testThis);
		if (!choices || !choices.length) return;
		this.updateChoices(choices);
	},
	
	// This gets called when an item in the auto suggest list is
	// newly highlighted, but not yet chosen.
	setSelection: function() {
		this.parent();
	},
	
	markQueryValue: function(txt) {
		dbug.log(txt);
		var val = (this.options.mult)?this.lastQueryElementValue:this.queryValue;
		return (this.options.markQuery && val) ? 
			txt.replace(new RegExp('\\b(' + val.escapeRegExp() + ')', 'i'), 
				'<span class="autocompleter-queried">$1</span>') : txt
	},
	
	
	
	choiceSelect: function(el) {
		
		if(this.options.multi) {
			var del = this.options.delimeter;
			var value = (this.element.value.trimLastElement(del) + el.inputValue).split(del);
			var fin = [];
			if (!this.options.allowDupes) {
				value.each(function(item){
					if(fin.contains(item))fin.remove(item); //move it to the end
					fin.include(item);
				})
			} else fin = value;
			this.observer.value = this.element.value = fin.join(del)+del;
		} else this.observer.value = this.element.value = el.inputValue;
		
		this.hideChoices();
		this.fireEvent('onSelect', [this.element, el.inputValue], 20);
	}

});

// Created 04/25/2008 by Greg Spry
Autocompleter.Ajax.Record = Autocompleter.Ajax.Grid.extend({

	options: {
		injectChoice: function(choice) {
			choiceID = choice[0];
			choiceText = choice[1];
			var el = new Element('li').setHTML(this.markQueryValue(choiceText));
			el.RecordID = choiceID;
			el.RecordValue = choiceText;
			this.addChoiceEvents(el).injectInside(this.choices);
		}
	},
	
	initialize: function(el, url, options, RecordName, RecordID) {
		this.parent(el, url, $merge(this.options, options));
		this.RecordIDOutput	= $(RecordName + RecordID);	
	},
	choiceSelect: function(el) {
		
		this.observer.value = this.element.value = el.RecordValue;
		
		dbug.log('RecordID=' + el.RecordID);
		
		this.RecordIDOutput.value = el.RecordID;
		
		this.hideChoices();
		this.fireEvent('onSelect', [this.element, el.RecordValue], 20);
	}
});




Autocompleter.Ajax.Venue = Autocompleter.Ajax.Grid.extend({

	options: {
		injectChoice: function(choice) {
			choiceText = choice[1];
			var el = new Element('li').setHTML(this.markQueryValue(choiceText));
			el.inputValue = choiceText;
			el.VenueID = choice[0];
			el.address1 = choice[2];
			el.city = choice[3];
			el.state = choice[4];
			el.ZIP = choice[5];
			el.NeighborhoodID = choice[6];
			this.addChoiceEvents(el).injectInside(this.choices);
		}
	},
	
	
	initialize: function(el, url, options, venuePickerID) {
		this.parent(el, url, $merge(this.options, options));
		
		this.VenueIDOutput		= $('suggestOutput_VenueID_'		+ venuePickerID);
		this.venueNameOutput	= $('suggestOutput_venueName_'		+ venuePickerID);
		this.address1Output		= $('suggestOutput_address1_'		+ venuePickerID);
		this.cityOutput			= $('suggestOutput_city_'			+ venuePickerID);
		this.stateOutput		= $('suggestOutput_state_'			+ venuePickerID);
		this.ZIPOutput			= $('suggestOutput_ZIP_'			+ venuePickerID);
		this.NeighborhoodOutput	= $('suggestOutput_NeighborhoodID_'	+ venuePickerID);
		
		if(this.options.lockKnownAddresses) {
			el.addEvent('keydown', this.unlockAddressInputs.bind(this));
			dbug.log(properInt(this.VenueIDOutput.value, 0) + ' / ' + this.VenueIDOutput.value);
			if(properInt(this.VenueIDOutput.value, 0) > 0)
				this.lockAddressInputs();
				
		}
		
	},
	choiceSelect: function(el) {
		
		this.observer.value = this.element.value = el.inputValue;
		
		dbug.log('VenueID=' + el.VenueID);
		dbug.log('address1=' + el.address1);
		dbug.log('city=' + el.city);
		
		this.VenueIDOutput.value				= el.VenueID;
		this.address1Output.value				= el.address1;
		this.cityOutput.value					= el.city;
		this.stateOutput.value					= el.state;
		this.ZIPOutput.value					= el.ZIP;
		this.NeighborhoodOutput.value			= el.NeighborhoodID;
		
		this.hideChoices();
		this.fireEvent('onSelect', [this.element, el.inputValue], 20);
		
		// we've just picked a known Venue, lock the address inputs if called for:
		if(this.options.lockKnownAddresses)
			this.lockAddressInputs();
	},
	
	lockAddressInputs: function() {
		
		this.address1Output.disabled		= true;
		this.cityOutput.disabled			= true;
		this.stateOutput.disabled			= true;
		this.ZIPOutput.disabled				= true;
		this.NeighborhoodOutput.disabled	= true;
	},
	
	unlockAddressInputs: function() {
		this.VenueIDOutput.value			= 0
		this.address1Output.disabled		= false;
		this.cityOutput.disabled			= false;
		this.stateOutput.disabled			= false;
		this.ZIPOutput.disabled				= false;
		this.NeighborhoodOutput.disabled	= false;
	}
});
