	/**
	 * Sorts the results of an geo coding process
	 * by their hitprobability
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
function sortByHitProbability(a, b) {
		
		hitProbabilityA = a.getHitprobability();
		hitProbabilityB = b.getHitprobability();
		
		return hitProbabilityA - hitProbabilityB;
	}

/*
 * Class MapAndRouteMap
 */
var MapAndRouteMap = function() {

	this.NAME = "name";
	
	this.LOGO = "logo";
	
	this.STREET = "street";
	
	this.HOUSENUMBER = "houseNumber";
	
	this.ZIPCODE = "zipCode";
	
	this.TOWN = "town";
	
	this.LONGITUDE = "longitude";
	
	this.LATITUDE = "latitude";
	
	this.BALLOON_WIDTH = 270;
	
	this.BALLOON_HEIGHT = 150;
	
	this.MAP_ZOOM = 9;

	this.map = null;
	
	this.location = null;
	
	this.name = null;
	
	this.logo = null;

	this.maxHits = 1;

	this.iwCountry = null;

	this.mapOverlayManager = null;
	
	this.mapLayoutManager = null;
	
	this.mapElement = null;

	this.iwGeoCoder = new IWGeocoderClient();

	this.iconPath = "typo3conf/ext/profile_pages_system/res/img/map_schild.png"; 

};

MapAndRouteMap.prototype.initialize = function(mapElement, mapCountry) {
	
	this.iwCountry = mapCountry;
	
	this.mapElement = mapElement;
	
	this.location = new IWAddress();
	
	IWEventManager.bind(this.iwGeoCoder, 'aftergeocoded', this, this.processGeoEncoding);
	
	this.initLocation();

};

MapAndRouteMap.prototype.getLocation = function() {

	return this.location;

};

MapAndRouteMap.prototype.setMaxHits = function(maxHits) {

	this.maxHits = maxHits;

};

MapAndRouteMap.prototype.getMaxHits = function() {

	return this.maxHits;

};

MapAndRouteMap.prototype.setIwCountry = function(iwCountry) {

	this.iwCountry = iwCountry;

};

MapAndRouteMap.prototype.getIwCountry = function() {

	return this.iwCountry;

};

MapAndRouteMap.prototype.initLocation = function() {

	this.location = new IWAddress();

	if (this.getIwCountry()) {

		this.location.setCountry(this.getIwCountry());

	}
	
	var name = document.getElementById(this.NAME);
	
	if (name) {
	
		this.name = name.firstChild.nodeValue;
		
	}
	
	var logo = document.getElementById(this.LOGO);
	
	if (logo) {
	
		this.logo = logo.getAttribute('src');
		
	}
	

	var street = document.getElementById(this.STREET);

	if (street) {

		this.location.setStreet(street.firstChild.nodeValue);

	} 

	var houseNumber = document.getElementById(this.HOUSENUMBER);

	if (houseNumber) {

		this.location.setHouseNumber(houseNumber.firstChild.nodeValue);

	}

	var zipCode = document.getElementById(this.ZIPCODE);

	if (zipCode) {

		this.location.setZipCode(zipCode.firstChild.nodeValue);
	}

	var town = document.getElementById(this.TOWN);

	if (town) {

		this.location.setCity(town.firstChild.nodeValue);

	}
	
	
	var locationCoords = new IWCoordinate();
	
	var longitude = document.getElementById(this.LONGITUDE);
	
	if (longitude) {
		
		locationCoords.setX(Number(longitude.firstChild.nodeValue));
		
	}
	
	var latitude = document.getElementById(this.LATITUDE);
	
	if (latitude) {
		
		locationCoords.setY(Number(latitude.firstChild.nodeValue));
		
	}
	
	this.location.setLCC(locationCoords);

};

MapAndRouteMap.prototype.doEncoding = function(location) {

	if (!location) {

		location = this.getLocation();

	}
	
	
	if (typeof this.location == "object" && this.maxHits > 0) {
		
		var locationCoords = location.getLCC();
		
		if (!locationCoords.getX() && !locationCoords.getY()) {
			
			this.iwGeoCoder.geocodeAddress(location, this.maxHits);
			
		} else {
			
			this.showMap();
			
		}

	} 
};

MapAndRouteMap.prototype.processGeoEncoding = function(geoCodingEvents) {
	
	var results = geoCodingEvents.results;

	if (results.length > 0) {
		
		// Sort by hit probability
		if (results.length > 1) {
			
			results.sort(sortByHitProbability);

		}
		
		// Get the closest coordinates
		var geoCodeResult = results.shift();

		var encodedLocation = geoCodeResult.getAddress();
		
		this.mergeLocations(encodedLocation);
		
		return this.showMap();

	}

};

MapAndRouteMap.prototype.mergeLocations = function (encodedLocation) {
	
	if (encodedLocation.getStreet != "") {
		
		this.location.setStreet(encodedLocation.getStreet());
		
	}
	
	if (encodedLocation.getCity != "") {
		
		this.location.setCity(encodedLocation.getCity());
		
	}
	
	if (encodedLocation.getZipCode() != "") {
		
		this.location.setZipCode(encodedLocation.getZipCode());
		
	}
	
	if (encodedLocation.getHouseNumber != "") {
		
		this.location.setHouseNumber(encodedLocation.setHouseNumber());
		
	}
	
	// Overwrite coordinates
	
	var encodedCoordinates = encodedLocation.getLCC();

	if (encodedCoordinates.getX() != "" && encodedCoordinates.getY() != "") {
		
		this.location.setLCC(encodedCoordinates);
		
		// Flag that the address can be shown in 
		// the map 
		
		return true;
		
	} else {
		
		this.location.setLCC(null);
		
		return false;
		
	}
	
};

MapAndRouteMap.prototype.sortByHitProbability = function(a, b) {

	hitProbabilityA = a.getHitprobability();

	hitProbabilityB = b.getHitprobability();

	return hitProbabilityA - hitProbabilityB;

};

MapAndRouteMap.prototype.showMap = function()
{

	// Create map
	
	this.map = new IWMap(this.mapElement);
	
	// Set default view
	
	this.map.setCenter(this.location.getLCC(), this.MAP_ZOOM);
	
	IWEventManager.bind(this.map, 'afterinitialized', this, this.setOverlays());
	
	return true;
	
};

MapAndRouteMap.prototype.setOverlays = function() {
	
	// Get necessary managers to add overlays and controls
	
	var mapOverlayManager = this.map.getOverlayManager().getLayer(0);

	var mapLayoutManager = this.map.getLayoutManager().getLayer(0);
	
	var addressCoordinates = this.location.getLCC();
	
	
	// Set Position Marker
			 
	if (addressCoordinates != null) {
		
		var addressMarker = new IWMarker(this.map, addressCoordinates);
	
		var icon = new IWIcon(this.icon, new IWPoint(15, 17), new IWSize(30, 35));
		
		var scalableIcon = new IWScaleDependentIcon(this.iconPath, new IWPoint(15, 13), new IWSize(30, 35), new IWRange(1, 9));  
		
		addressMarker.setDefaultIcon(icon);
		
		addressMarker.addScaleDependentIcon(scalableIcon);
	
		mapOverlayManager.addOverlay(addressMarker);
		
	} 	
	
	// Set balloon if it is a full address
	
	if (this.name != "" && this.location.getStreet() && this.location.getCity()) {
		
		
		// Slider- and scalar-control

		mapLayoutManager.addControl(new IWSliderControl(this.map), IWAlignment.RIGHT, IWAlignment.BOTTOM, 0, 0);

		mapLayoutManager.addControl(new IWScalarControl(this.map), IWAlignment.BOTTOM, IWAlignment.LEFT, 0, 0);
		

		// Balloon with address and logo
		
		var balloonHTML = this.getBalloon();

		var balloon = new IWInfoBalloon(this.map, addressCoordinates, balloonHTML);
		
		var balloonSize = new IWSize(this.BALLOON_WIDTH, this.BALLOON_HEIGHT);
		
		balloon.setSize(balloonSize);
		
		this.map.addWindowOverlay(balloon);
		
	}

};

MapAndRouteMap.prototype.getBalloon = function() {
	
	var i = 0;
	
	var content = new Array();
	
	var name = '';
	
	if (this.name != null) {
		
		name = this.name;
		
	}
	
	var street = '';
	
	if (typeof this.location.getStreet() != 'undefined') {
	
		street = this.location.getStreet();
		
	}

	var houseNumber = '';
	
	if (typeof this.location.getHouseNumber() != 'undefined') {
		
		houseNumber = this.location.getHouseNumber();
		
	}
	
	var zipCode = '';
	
	if (typeof this.location.getZipCode() != 'undefinde') {
		 
		zipCode = this.location.getZipCode();
		
	}	
	
	var city = '';
	
	if (typeof this.location.getCity() != 'undefined') {
		
		city = this.location.getCity();
		
	}
	
	
	content[i++] = '<span id="jsName">' + name + '</span><br>';
	
	content[i++] = '<span id="jsStreet">' + street + ' ' + houseNumber + '</span><br>';
	
	content[i++] = '<br><span id="jsCity">' + zipCode + ' ' + city + '</span><br>';

	var balloonHTML = content.join("\n");
	
	if (this.logo) {
		
		// TODO: Prüfen, ob die Source auch aus dem Firmenlogo im HTML gezogen wird!
		
		balloonHTML = '<img src="' + this.logo + '" border="0" width="120" id="jsCoImage"/>' + balloonHTML;
		
	}
	
	return '<span id="jsAddress">' + balloonHTML + '</span>';	
	
}
