/*|----------------------------------------------------------------------------------------------
 *|														University of Heidelberg
 *|	  _____ _____  _____      _                     	Department of Geography		
 *|	 / ____|_   _|/ ____|    (_)                    	Chair of GIScience
 *|	| |  __  | | | (___   ___ _  ___ _ __   ___ ___ 	(C) 2011
 *|	| | |_ | | |  \___ \ / __| |/ _ \ '_ \ / __/ _ \	
 *|	| |__| |_| |_ ____) | (__| |  __/ | | | (_|  __/	Berliner Strasse 48								
 *|	 \_____|_____|_____/ \___|_|\___|_| |_|\___\___|	D-69120 Heidelberg, Germany	
 *|	        	                                       	http://www.giscience.uni-hd.de
 *|----------------------------------------------------------------------------------------------*/

 Search = Class.create({
 	geocoder: null,
	
	placeField: null,
	
	requestButton: null,
	
	content: null,
	
	resultList: null,
	
	initialize: function (evtSrc) {
//		this.geocoder = new OSMatrix.Geocoder('http://openls.geog.uni-heidelberg.de/osm/eu/geocoding');
		this.geocoder = new Nominatim();
		this.placeField= $('searchField'),
		this.requestButton = $('searchButton'),
		this.content = $('searchResults'),
		
		
//		this.htmlId = 'geocoder';
//		this.eventSource = $(evtSrc);
//		this.initGui();
		
//		this.content.insert(this.placeField);
		
		// Binding event listeners
//		this.eventSource.observe('click', this.toggleDisplay.bindAsEventListener(this));
		this.placeField.observe('focus', function(ev) {
			this.setValue('');
		});
		this.placeField.observe('keydown', this.stopPositionKeys.bindAsEventListener(this));
		this.placeField.observe('keyup', this.getResults.bindAsEventListener(this));
		
		this.requestButton.observe('click', this.getResults.bindAsEventListener(this));
	},
	stopPositionKeys:function(ev){
		var positionKeys = [9,37,38,39,40,33,34,35,36];
		if (positionKeys.indexOf(ev.keyCode) != -1){
			ev.stop();
		}
	},
	getResults: function(ev) {
		
		
		if ((ev.type == "keyup" && ev.keyCode == 13) || ev.type == "click") {
//			if (this.resultList) {
//				this.resultList.remove();
//			}
			if (this.content.firstChild) {
				this.content.firstChild.remove();
			}
			
			this.placeField.addClassName('loading');
			this.geocoder.geocode(this.placeField.getValue(), this.displayResults.bind(this), this.geocodeFail.bind(this));
		} 
	},
	
	displayResults: function(response) {
		this.placeField.removeClassName('loading');
		var places = this.geocoder.parseGeocodeResponse(response.responseText.replace(/class/g, 'featuretype').evalJSON(true));

		if (typeof places == 'object') {
			if (places.length > 0) {
				this.resultList = new Element('ul');
				
				places.each(function(p) {
					if (p.type == 'place' || p.type == 'boundary') {
						var resultListItem = new Element('li');
						var link = new Element('a', {
							'href': '#'
						});
						link.update(p.toString());
						link.observe('click', function() {
							var lonLat = p.lonlat.transform(
								new OpenLayers.Projection("EPSG:4326"), 
								new OpenLayers.Projection("EPSG:900913")
							);
							map.setCenter(lonLat);
							map.zoomTo(11);
							addMarkerAtLonLat(lonLat);
//							this.remove();
							this.resultList.remove();
						}.bind(this));
						
						resultListItem.insert(link);
						this.resultList.insert(resultListItem);	
					}
				}.bind(this));

				this.content.insert(this.resultList);
			} else {
				this.content.insert('<p>No places have been found.</p>');
			}
		} else {
			this.content.insert('<p>' + places + '</p>');
		}
	},
	
	geocodeFail: function() {
		this.placeField.removeClassName('loading');
		this.content.insert('<p>An error occurred while requesting geodcoding results.</p>');
	}
 });

