/*
 This file is part of HiNii.

 Copyright (C) 2008 Carnino Claudio (jollyr0ger) <jollyr0g3r@gmail.com>
 */

/**
 * CityAutocompleter
 * @author jollyr0ger
 *
 * Attention need:
 * -Prototype
 */
var CityAutocompleter = Class.create({ });
Object.extend(CityAutocompleter, {	
	
	
	
	/**
	 * Create a Ajax City Autocompleter called city, and the needed stuff (city-xyz)
	 * @param		string					lang				User language
	 * @param		string					inputName			Input field
	 * @param		string					classFieldCorrect	Class to apply when correct
	 * @param		string					defaultValue		Default value to insert into the inputbox
	 */
	create: function(lang, inputName, classFieldCorrect, defaultValue){

		var city = $(inputName);
	
		// If has the default value, apply the class field correct
		if(defaultValue != '' && defaultValue != null){

			city.value = defaultValue;
			city.addClassName(classFieldCorrect);
		}
		
		// Create the autocompleter
		new Autocomplete(city, function() {

			this.setValue = function( val ){
				
				city.value = val;
				city.addClassName(classFieldCorrect);
			};
									
			
			// For example: require at least 1 char if this request is not fired by search icon click
			if ( this.value.length < 1 && this.isNotClick ) 
				return ;
			
			
			// Create an event to create the ajax geocondig request
			var fireEvents = function() { 
				document.fire('HN:getCoordinates');
				city.fire('hn:blur');
			};
			
			fireEvents.delay(1);
			
			return "/"+ lang +"/ajax/citiesname/search/city/name/" + this.value;
		});

		
		// Define clear field
		var fieldClear = function(event){

			city.value = '';
			city.removeClassName(classFieldCorrect);
		};

		Event.observe(city, 'click', fieldClear);
		Event.observe(city, 'change', fieldClear);
		
	}


});
	
