﻿
	// check for scriptacolous libraries
	if(typeof Ajax.Autocompleter == 'undefined') {
		throw("td.library.js requires including script.aculo.us' controls.js library");
	}

	/**
	 * This class extends the original Ajax.Autocompleter provided by
	 * scriptacolous and used in the TechDivision.mapSearchForm by 
	 * adding a option called autoSelect that checks the number of 
	 * found results and if only one result was found it preselects it.
	 *
	 * @author Tim Wagner <t.wagnert@techdivision.com>
	 * @see Ajax.Autocompleter
	 */
	TechDivision.Autocompleter = Class.create(Ajax.Autocompleter, {
	     
	    /**
	     * Constructor to initialize the parent class and to
	     * set the autoSelect option.
	     *
	     * @param element The element this class is bound to
	     * @param update The name of destination element
	     * @param url The url to send the AJAX request to
	     * @param options Additional options
	     */
		initialize: function(element, update, url, options, dependency) {
			this.baseInitialize(element, update, options);
			this.options.asynchronous = true;
			this.options.onComplete = this.onComplete.bind(this);
			this.options.defaultParams = this.options.parameters || null;
			this.url = url;
			// set the autoSeletect option
			this.options.autoSelect = false;
			
			this.dependency = false;
			if(dependency)
			{
				this.dependency = dependency;
				Event.stopObserving(this.element, 'keydown');
				Event.observe(this.element, 'keydown', this.onKeyPressDepend.bindAsEventListener(this));
			}
			
		},
		  
		onKeyPressDepend : function (event)
		{
			if(this.dependency.value != '' && this.dependency.value != 'Was?')
			{
				this.onKeyPress(event);
			}
			else
			{
				Event.stop(event);
				alert('Bitte geben Sie zuerst einen Ort ein.');
			}
		}

	});
