
	// check for scriptacolous libraries
	if(typeof TechDivision.Autocompleter == 'undefined') {
		throw("td.library.mapsearchform.js requires including TechDivisions' td.library.autocompleter.js library");
	}
	
	/**
	 * Extended search form for searching maps.
	 * 
	 * @author Tim Wagner <t.wagnert@techdivision.com>
	 */
	TechDivision.mapSearchForm = Class.create();
	TechDivision.mapSearchForm.prototype = {
	        
	    /**
	     * Constructor to initialize the class.
	     *
	     * @param form The form the search field is bound to
	     * @param field The name of the search field to bound
	     * @param emptyText The text to set as initial value 
	     * @return void
	     */
	    initialize : function(form, field, emptyText, dependOnField){
	    	// initialize the member variables
	        this.form   = $(form);
	        this.field  = $(field);
	        this.emptyText = emptyText;
			
			this.dependOnField = false;
			if(dependOnField != undefined)
			{	this.dependOnField = $(dependOnField);
			}
			
			// register the events to observe
	        Event.observe(this.form, 'submit', this.submit.bind(this));
	        Event.observe(this.field, 'focus', this.focus.bind(this));
	        Event.observe(this.field, 'blur', this.blur.bind(this));
			// preset the value for the search field with the empty text passed as parameter
	        this.reset();
	    },
	
		/**
		 * Event that is invoked after the form this
		 * class is bound to was submitted.
		 * 
		 * The submit was intercepted when the search
		 * is empty or the empty text was sent.
		 *
		 * @param event The fired event
		 * @return boolean Returns true if the submit was successfull or false if the submit was stopped
		 */
	    submit : function(event) {
	    	if(this.field.value == this.emptyText || this.field.value == '') {
				
				if(this.field.value == this.emptyText)
				{	this.field.value = '';
				}
	            Event.stop(event);
	            return false;
	        }
	    	try {
		    	if(escape(document.getElementById("search").value) != "Stra%DFensuche" && document.getElementById("search").value.length > 2) {
		    		document.getElementById("street_value").value = document.getElementById("search").value;
		    	} 
	    	} catch(err) {}
	        //this.form.submit();
	        return true;
	    },
	
	    /**
	     * This method will be invoked when the search
	     * field lost the focus and it only overwrites the 
	     * method of the baseclass.
	     *
	     * @param event The event itself
	     * @return void
	     */ 
	    blur : function(event){
			if(this.field.value == '')
			{	this.field.value = this.emptyText;
			}
		},
	
		/**
		 * This method will be invoked when the search
		 * field gets the focus.
	     *
	     * @param event The event itself
	     * @return void
		 */ 
	    focus : function(event){
	        if(this.field.value == this.emptyText){
	            this.field.value='';
	        }
	    },
	
	    /**
	     * This method resets the search field and copies 
	     * the empty text if no value was entered by the 
	     * user.
	     *
	     * @return void
	     */
		reset : function() {
	        if(this.field.value == '') {
	        	this.field.value=this.emptyText;
	    	}
	    },
	
		/**
		 * This method initializes the autocomplete functionality
		 * and sends the AJAX request to load the suggestions.
		 *
		 * @param url The url to send the AJAX request to
		 * @param destinationElement The destination element
		 */
	    initAutocomplete : function(url, destinationElement){
	        new TechDivision.Autocompleter(
	            this.field,
	            destinationElement,
	            url,
	            {
	                paramName: this.field.name,
	                minChars: 5,
	                updateElement: this._selectAutocompleteItem.bind(this)
	            },
				this.dependOnField
	        );
	    },
	
	
		/**
		 * This is the callback method invoked after an 
		 * entry of the suggestions was selected by the
		 * user.
		 *
		 * The method sets the selected suggestion value
		 * in the search field.
		 *
		 * @param element The selected element
		 * @return void
		 */
	    _selectAutocompleteItem : function(element) {
	    	// check if the selected element has a title
	    	if(element.title) {
	            // if yes, copy it into the search field
	            this.field.value = element.title;
	        }

			// submit the form itself
	        this.submit();
	    }
	}
