/*|----------------------------------------------------------------------------------------------
 *|														University of Heidelberg
 *|	  _____ _____  _____      _                     	Department of Geography		
 *|	 / ____|_   _|/ ____|    (_)                    	Chair of GIScience
 *|	| |  __  | | | (___   ___ _  ___ _ __   ___ ___ 	(C) 2011
 *|	| | |_ | | |  \___ \ / __| |/ _ \ '_ \ / __/ _ \	
 *|	| |__| |_| |_ ____) | (__| |  __/ | | | (_|  __/	Berliner Strasse 48								
 *|	 \_____|_____|_____/ \___|_|\___|_| |_|\___\___|	D-69120 Heidelberg, Germany	
 *|	        	                                       	http://www.giscience.uni-hd.de
 *|----------------------------------------------------------------------------------------------*/


/**
 * Class: Adress
 * Nominatim Adresses
 * 
 * @author Oliver Roick, roick@uni-heidelberg.de
 * @version 0.2
 * @since 0.2
 */

Nominatim.Address = Class.create({
	lonlat: null,
	municipality: '',
	countrySubdivision: '',
	countryCode: '',
	type: null,
	
	initialize: function (obj) {
		this.type = obj.featuretype;
		this.lonlat = new OpenLayers.LonLat(obj.lon, obj.lat);
		this.parseAddress(obj.address);
	},
	
	parseAddress: function (address) {
		this.countryCode = address.country_code;
		this.countrySubdivision = (address.state) ? address.state : '';
		
		this.municipality = (address.island) ? address.island: this.municipality;
		this.municipality = (address.locality) ? address.locality: this.municipality;
		this.municipality = (address.suburb) ? address.suburb: this.municipality;
		this.municipality = (address.isolated_dwelling) ? address.isolated_dwelling: this.municipality;
		this.municipality = (address.hamlet) ? address.hamlet: this.municipality;
		this.municipality = (address.village) ? address.village: this.municipality;
		this.municipality = (address.town) ? address.town: this.municipality;
		this.municipality = (address.city) ? address.city: this.municipality;
		
	},
	
	toString: function () {
		var s = '';
		if (this.municipality.length > 0) {
			s += this.municipality;
		} 
		if (this.countrySubdivision.length > 0) {
			if (s.length > 0) {
				s += ', '
			}
			s += this.countrySubdivision;
		}
		if (this.countryCode.length > 0) {
			if (s.length > 0) {
				s += ', '
			}
			s += this.countryCode.toUpperCase();
		} 
		return s;
	}
});

