﻿
	// check for TechDivision libraries
	if(typeof TechDivision.StreetAutocompleter == 'undefined') {
		throw("td.library.streetsearchform.js requires including TechDivisions' td.library.streetautocompleter.js library");
	}
	
	/**
	 * Extendes search form to use a hidden field
	 * for storing a string necessary to control an
	 * external Object, e. g. a Flash control.
	 *
	 * @author Tim Wagner <t.wagnert@techdivision.com>
	 */
	TechDivision.streetSearchForm = Class.create();
	TechDivision.streetSearchForm.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 
	     * @param hiddenField The name of the hidden field to store the data to control the Flash control
	     * @return void
	     */
	    initialize : function(form, field, emptyText, hiddenField, ort){
	    	// initialize the member variables
	        this.form   = $(form);
	        this.field  = $(field);
	        this.emptyText = emptyText;
	        this.hiddenField = $(hiddenField);
	        this.ort = document.getElementById('search1').value;
			// 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) {
	        // check if the search field is empty or the empty text was sent
	        if (this.field.value == this.emptyText || this.field.value == ''){
	            Event.stop(event);
	            return false;
	        }
	        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){ /* do nothing here */ },
	
		/**
		 * 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.StreetAutocompleter(
	            this.field,
	            this.hiddenField,
	            destinationElement,
	            url,
	            {
	                paramName: this.field.name,
	                minChars: 2,
	                updateElement: this._selectAutocompleteItem.bind(this)
	            },
	            this.ort
	        );
	    },
	
		/**
		 * 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 and invokes the Flash control
		 * to point the the value stored in the hidden 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;
	        }
	        // check if the selected element has a point
	        if(element.id) {
	            // if yes write it to the hidden field and invoke the Flash control
	        	this.hiddenField.value = element.id;
	            MM_controlShockwave('map', '', this.hiddenField.value);
	        }
			// submit the form itself
	        this.submit();
	    }
	}
