// Hier werden dynamisch der jeweilig benoetigte GM Key verwendet um die Google Maps API version 2 einzubinden.
var gmkey;
if (document.domain == "localhost") {
	gmkey = 'ABQIAAAAPX32f84sGXKkb-ZjlYAFSxQLZ35nlTlebbyAwEdj8tsYtZ4-0hRPnp8qBZt8MWzE0x_aIsw91KPCpg';
} else if (document.domain == "corpus.geopublishing.org") {
	gmkey = 'ABQIAAAAPX32f84sGXKkb-ZjlYAFSxQLZ35nlTlebbyAwEdj8tsYtZ4-0hRPnp8qBZt8MWzE0x_aIsw91KPCpg';
} else if (document.domain == "www.sparkasse-koelnbonn.de") {
	gmkey = 'ABQIAAAAPX32f84sGXKkb-ZjlYAFSxQGJ7ZKQBiUaqmn_SSduU6-MNdX_xSwbwwGk3ovkv3wugomLnqVE_1ohA';
} else if (document.domain == "www.sskduesseldorf.de") {
	gmkey = 'ABQIAAAAPX32f84sGXKkb-ZjlYAFSxRMGS1Gg7bFcTjk7Y-IBT6jxQrqZRRURGtWaBHAuFcpP9_2oPDvYZx0bg';
} else if (document.domain == "www.frankfurter-sparkasse.de") {
	gmkey = 'ABQIAAAAPX32f84sGXKkb-ZjlYAFSxRsHXIQ0FxT7vJY-bpO5Lw_BRd3nxQwv1ef3Wxe6LUdPMcvGwG5SAadBg';
} else if (document.domain == "www.corpussireo-makler.com") {
	gmkey = 'ABQIAAAAPX32f84sGXKkb-ZjlYAFSxSzRkvMLZmN51Rr1z1urCo7DarkFBRgi9r2Ozd2p7XEiS_u_H3dq9auOQ';
}

document
		.write("<script type='text/javascript' src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=");
document.write(gmkey);
document.write("' ><\/script>");

var map;
var geocoder;
var markersLayer;
var bounds;
var stadtName;
var layerName;

/**
 * @param mapId
 *            z.B bonn1. Entspricht der id des divs, welches die Karte enhalten
 *            wird
 * @return
 */
function init(mapId) {

	if (mapId == 'bonn1' || mapId == 'bonn2'|| mapId == 'bonnM2011') {
		bounds = new OpenLayers.Bounds(7.027, 50.65, 7.206, 50.767);
		stadtName = "Bonn";
		if (mapId == 'bonn1') {
			layerName = "bnetwefh09_reclasse";
			sldName = "corpus_8";
			layerTitle = "Kaufpreise";
		}
		if (mapId == 'bonn2') {
			layerName = "bnmw09_8klassen";
			sldName = "corpus_8";
			layerTitle = "Mietpreise";
		}
		if (mapId == 'bonnM2011') {
			layerName = "bn_m_2011";
			sldName = "corpus2011_9";
			layerTitle = "Mietpreise";
		}
	} else if (mapId == 'koeln1' || mapId == 'koeln2'|| mapId == 'koelnM2011') {
		bounds = new OpenLayers.Bounds(6.6700744, 50.81938, 7.32788, 51.0595231);
		//left-bottom=(6.6700744628906,50.81938446414) right-top=(7.327880859375,51.059523193901) 
		stadtName = "Koeln";
		if (mapId == 'koeln1') {
			layerName = "kefhetw0809_8Klassen";
			sldName = "corpus_8";
			layerTitle = "Kaufpreise";
		}
		if (mapId == 'koeln2') {
			layerName = "kmw09_9Klassen";
			sldName = "corpus_9";
			layerTitle = "Mietpreise";
		}
		if (mapId == 'koelnM2011') {
			layerName = "k_m_2011";
			sldName = "corpus2011_9";
			layerTitle = "Mietpreise";
		}
	} else if (mapId == 'dd1' || mapId == 'dd2'|| mapId == 'ddM2011') {
		bounds = new OpenLayers.Bounds(6.685, 51.124, 6.938, 51.349);

		stadtName = "Duesseldorf";
		if (mapId == 'dd1') {
			layerName = "ddefhetw0809_8klassen";
			sldName = "corpus_8";
			layerTitle = "Kaufpreise";
		}
		if (mapId == 'dd2') {
			layerName = "ddmw09_9klassen";
			sldName = "corpus_9";
			layerTitle = "Mietpreise";
		}
		if (mapId == 'ddM2011') {
			layerName = "dd_m_2011";
			sldName = "corpus2011_9";
			layerTitle = "Mietpreise";
		}
	} else if (mapId == 'ff1' || mapId == 'ff2' || mapId == 'ff_m_2011') {
		bounds = new OpenLayers.Bounds(8.493,50.08, 8.822,50.190);
		// 8.4934616088867,50.077413243222) right-top=(8.8223648071289,50.199539422473)

		stadtName = "Frankfurt am Main";
		if (mapId == 'ff1') {
			layerName = "ffefhetw0809_8klassen";
			sldName = "corpus_8";
			layerTitle = "Kaufpreise";
		}
		if (mapId == 'ff2') {
			layerName = "ffmw09_8klassen";
			sldName = "corpus_8";
			layerTitle = "Mietpreise";
		}
		if (mapId == 'ff_m_2011') {
			layerName = "ff_m_2011";
			sldName = "corpus_8";
			layerTitle = "Mietpreise";
		}
	}

	OpenLayers.IMAGE_RELOAD_ATTEMPTS = 5;

	// make OL compute scale according to WMS spec
	OpenLayers.DOTS_PER_INCH = 25.4 / 0.28;

	options = {
		maxExtent : bounds,
		maxResolution : 1,
		minResolution : 0.0001,
		projection : "EPSG:4326",
		units : 'degrees'
	};

	map = new OpenLayers.Map(mapId, options);

	geocoder = new OpenLayers.Control.Geocoder();

	map.addControl(geocoder);
	map.addControl(new OpenLayers.Control.ScaleLine());

	// setup single tiled layer
	corpusLayer = new OpenLayers.Layer.WMS(layerTitle,
			"http://corpus.geopublishing.org/geoserver/wms", {
				layers : 'corpus:' + layerName,
				styles : sldName,
				height : '403',
				width : '512',
				srs : 'EPSG:4326',
				format : 'image/png',
				transparent : true
			}, {
				singleTile : true,
				ratio : 1.,
				'isBaseLayer' : false
			});

	var gphy = new OpenLayers.Layer.Google("Google topogr. Karte", {
		type : G_PHYSICAL_MAP, numZoomLevels : 20
	});
	
	var gmap = new OpenLayers.Layer.Google("Google Stra&szlig;enkarte", {
		numZoomLevels : 20
	});
	
	var ghyb = new OpenLayers.Layer.Google("Google Hybridkarte", {
		type : G_HYBRID_MAP,
		numZoomLevels : 20
	});
	
	var gsat = new OpenLayers.Layer.Google("Google Luftbild", {
		type : G_SATELLITE_MAP,
		numZoomLevels : 20
	});

	markersLayer = new OpenLayers.Layer.Markers();

	ls = new OpenLayers.Control.LayerSwitcher();
	map.addControl(ls);
	ls.maximizeControl();
	ls.showControls();
	corpusLayer.displayInLayerSwitcher = false;
	markersLayer.displayInLayerSwitcher = false;

	// Maus-on-Over-Effect
	var hoverControl = new OpenLayers.Control.WMSGetFeatureInfo( {
		url : 'http://corpus.geopublishing.org/geoserver/wms',
		title : 'Mietpreis mit der Maus abfragen',
		layers : [ corpusLayer ],
		hover : true,
		info_format : 'text/plain'
	});
	map.addControl(hoverControl);
	hoverControl.events.register("getfeatureinfo", map, showInfo);
	hoverControl.activate();

	map.events.register("mouseout", map, markLegend);

	map.addLayers( [ ghyb, gphy, gmap, gsat, corpusLayer, markersLayer ]);
	map.zoomToExtent(bounds);
}

function showInfo(evt) {
	Ausdruck = /.*?<td>(.+?)<\/td>.*/;
	Ausdruck.exec(evt.text);
	wert = RegExp.$1;
	// OpenLayers.Util.getElement('nodeList').innerHTML = "Wert unter dem
	// Mauscursor: "+wert;
	markLegend(wert);
};

function markLegend(wert) {
	for (i = 0; i <= 9; i++) {
		tdElement = document.getElementById('leg' + i);
		if (tdElement == null)
			continue;
		if ((i + ".0") == wert) {
			tdElement.style.visibility = 'visible';
		} else {
			tdElement.style.visibility = 'hidden';
		}
		
		tdElement = document.getElementById('legval' + i);
		if (tdElement == null)
			continue;
		if ((i + ".0") == wert) {
			tdElement.style.color ='E3001B';
		} else {
			tdElement.style.color ='black';
		}
	}
}

String.prototype.endsWith = function(str) {
	return (this.match(str + "$") == str);
};

function showAddress(address) {
	if (geocoder) {
		if (address.endsWith("Hausnummer")) {
			alert("Bitte tragen Sie eine Adresse in das Suchfeld ein.");
			return;
		}
		geocoder
				.getLocation(address + ", " + stadtName + ", Deutschland",
						function(lonlat) {
							if (!lonlat) {
								alert("Kein Treffer in " + stadtNae
										+ " gefunden.");
							} else {
								if ((lonlat.lon > bounds.right)
										|| (lonlat.lat > bounds.top)
										|| (lonlat.lon < bounds.left)
										|| (lonlat.lat < bounds.bottom)) {
									alert("Kein Treffer in " + stadtName
											+ " gefunden.");
									return;
								}

								map.setCenter(lonlat, 17);

								var feature = new OpenLayers.Feature(
										markersLayer, lonlat);

								var marker = feature.createMarker();

								markersLayer.addMarker(marker);
							}
						});
	}
}

