var map;
var isOpeningInfoWindow = false;

function initialize() {
	
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		G_NORMAL_MAP.getMinimumResolution = function () { return 4 };
		G_NORMAL_MAP.getMAximumResolution = function () { return 16 };
		map.addControl(new GLargeMapControl());
		map.enableScrollWheelZoom();

		GEvent.addListener(map, "moveend", function() {
			if (isOpeningInfoWindow)
				return;
			mapHandler();
		});

		var url = "/haba/ajaxcall_location.htm";
		findLocation(url,false);

		//var zoom = map.getBoundsZoomLevel(new GLatLngBounds(new GLatLng(x,y), new GLatLng(x,y)));
		//map.setCenter(new GLatLng(x, y), 4);
		//mapHandler();
	} else {
		var listObj = document.getElementById("list");
		listObj.innerHTML = "Sorry, the Google Maps API is not compatible with this browser"
	}
}

function getMapHandlerUrl() {
	var options = getHttpParams();
	return "/haba/ajaxcall_haendler.htm?" + options;
}

function getHttpParams() {
	bounds = map.getBounds();

	var httpParams = '';
	httpParams = "south=" + bounds.getSouthWest().lat() + "&north="
			+ bounds.getNorthEast().lat() + "&west="
			+ bounds.getSouthWest().lng() + "&east="
			+ bounds.getNorthEast().lng();

	var centerPt = map.getCenter();
	httpParams = httpParams + '&center=' + centerPt.lat() + ','
			+ centerPt.lng();
	httpParams = httpParams + '&zoom=' + map.getZoom();
	httpParams = httpParams + '&art=' + document.getElementById("art").value;
	
	return httpParams;
}

function showInfo(m) {
	isOpeningInfoWindow = true;
	markers[m].openInfoWindowHtml(markers[m].html);
}

var markers = new Array();

function mapHandler() {
	if (!isOpeningInfoWindow) {
		map.clearOverlays();
		var request = GXmlHttp.create();

		var url = getMapHandlerUrl();
		request.open("GET", url, true);
		request.onreadystatechange = function() {
			if (request.readyState == 4) {
				var listHtml = '<table width="100%">\r\n';
				if (request.status == 200) {
					
					var json = jsonParse(request.responseText);
					var listObj = document.getElementById("list");

					for (i = 0; i < json.length; i++) {
						listHtml += '  <tr>';

						var point = new GLatLng(json[i].lat, json[i].lng);
						var icon = new GIcon(G_DEFAULT_ICON);
						icon.image=json[i].art;
						icon.shadow = "/static/bilder/Fahnen_Schatten.png";
						icon.iconSize = new GSize(23, 32);
						icon.shadowSize = new GSize(32, 32);
						icon.iconAnchor = new GPoint(3, 32);
												
						var marker = new GMarker(point, icon);

						marker.html = json[i].name + "<br>";
						if(json[i].name2!=undefined){
							marker.html += json[i].name2 + "<br>";
						}
						marker.html += json[i].strasse
								+ "<br>" + json[i].plz + " " + json[i].ort
								+ "<br>";
						if(json[i].rufnummer!=undefined) {
							marker.html +="Tel. " + json[i].rufnummer + "<br>";
						}
						if(json[i].fax!=undefined){
							marker.html += "Fax. " + json[i].fax + "<br>";
						}
						if(json[i].url!=undefined) {
							marker.html += "<a href=\"http://" + json[i].url + "\" target=\"_blank\" onclick=\"ns_onclick(this,'','onclick.fh_infopopup." + json[i].url +"','clickin');return false;\" >" + json[i].url + "</a><br>"
						}
						marker.bindInfoWindowHtml(marker.html);
						GEvent.addListener(marker, "click", function() {
							isOpeningInfoWindow = true;
						});
						GEvent.addListener(marker, "infowindowclose",
								function() {
									isOpeningInfoWindow = false;
								});
						markers[i] = marker;
						listHtml += '    <td width="80%"><a href=javascript:showInfo(' + i
								+ ');>' + json[i].name + '</a>, ' + json[i].ort + '</td><td align="right" width="20%">'
								+ json[i].distance + ' km </td>\r\n';
						map.addOverlay(marker);
						listHtml += '  </tr>\r\n';
					}

					listHtml += '</table>';
					listObj.innerHTML = listHtml;
				} else {
					listHtml = '<font color=red>the server did not respond. (errorcode ' + request.status + ')</font>';
				}
			} // ready state
		} // function
		request.send(null);
	}
}

function findPlz() {
	var url = "/haba/ajaxcall_location.htm?plz="
		+ document.getElementById("plz").value + "&land=" + document.getElementById("land").value;
	findLocation(url,true);
}

function findLocation(url, move) {
	var request = GXmlHttp.create();
	request.open("POST", "/haba/ajaxcall_location.htm", true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			if (request.status == 200) {
				var json = jsonParse(request.responseText);
				if(json._latitude==undefined) {
					var listObj = document.getElementById("list");
					listObj.innerHTML = "Sorry, Google Maps API couldn't find a location!"
					return;
				}
				if(move) {
					map.setCenter(new GLatLng(json._latitude, json._longitude), 11);
				} else {
					isOpeningInfoWindow=true;
					map.setCenter(new GLatLng(json._latitude, json._longitude), 5);
					isOpeningInfoWindow=false;
				}
			}
		}
	}
	data="plz=" + document.getElementById("plz").value + "&land=" + document.getElementById("land").value;
	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	request.send(data);
}


