
var map = null;
var geocoder = null;
var bounds = null;

function initGoogleMaps() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById('map'));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl(),
		new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(90, 10)));
		map.setCenter(new GLatLng(0,0),0);
		geocoder = new GClientGeocoder();
		$('body').unload(GUnload);
	}
}

function showMap() {
	if ($('#anwaltsuche-suchergebnis').html()!=null)
		$('#content').append('<div id="google-map"><div id="map"></div></div>');
	else
		$('#anwaltsuche-profil').after('<div id="google-map"><div id="map"></div></div>');
	$('#google-map').append('<a href="#" id="closeMapLink" title="Stadtplan schließen">Fenster schließen</a>');
	$('#closeMapLink').click(function() {
		$('#google-map').remove();
		$('#addedRow').remove();
		map = null;
		return false;
	});
}

function addressNotFound(address) {
	mapDiv = $('#google-map');
	map.setCenter(new GLatLng(50.205033,9.931641), 5);
	$('#routeForm').remove();
	$('#address').html('Die Adresse <strong>'+address+'</strong> wurde nicht gefunden.');
	onlyPLZ = address.replace(/(.*)\, (.*)/,'$2');
	if (onlyPLZ!='') {
		bounds = new GLatLngBounds();
		geocoder.getLatLng(onlyPLZ, function(point) {
			if (point) {
				map.setCenter(point, 11);
				$('#address').html('Die Adresse <strong>'+address+'</strong> konnte nicht genau lokalisiert werden.');
			}
		});
	}
}

function showAddressOnMap(obj) {
	address = $(obj).siblings('span.address').html();
	if ($('#google-map').html()==null) 	showMap();
	if (map==null)						initGoogleMaps();
	if ($('#anwaltsuche-suchergebnis').html()!=null) {
		rowNum = parseInt($(obj).attr('id').replace(/mapLink/,''));
		positionMap(rowNum);
	}
	if (geocoder) {
		map.clearOverlays();
		geocoder.getLatLng(address, function(point) {
			if (!point) {
				addressNotFound(address);
			} else {
				map.addOverlay(new GMarker(point));
				map.setCenter(point, 15);
			}
		});
	}
	addRouteForm(address);
}

function addRouteForm(address) {
	$('#routeForm').remove();
	$('#google-map').append('<form id="routeForm" action="http://maps.google.de/maps" method="get" target="_blank"></form>');
	$('#routeForm').append('<fieldset><legend>Routenplaner</legend><label for="saddr">Startadresse <span>(Straße, Hausnummer, PLZ Ort)</span></label><input type="text" name="saddr" id="saddr"><input type="submit" value="Route berechnen" class="button"><input type="hidden" name="daddr" value="'+address+'">');
}

function positionMap(rowNum) {
	$('#addedRow').remove();
	// position map
	$('#mapLink'+rowNum).parent().parent().after('<tr id="addedRow"><td colspan="3"><div style="height:450px;width:10px" id="placeholder"></div></td></tr>');
	// get new position
	coords = $('#placeholder').offset();
	$('#google-map').css({ 'position':'absolute','top': ( coords.top + 20 ) + 'px','left': ( coords.left ) + 'px', 'width': $('#content').width() + 'px' });
}

function createMapLinks() {
	if ($('#anwaltsuche-suchergebnis').html()!=null)	id = '#anwaltsuche-suchergebnis';
	else												id = '#anwaltsuche-profil';
	$.each($(id+' span.address'), function(key,obj) {
		if (key==0 && id=='#anwaltsuche-suchergebnis')
			$(obj).append('&nbsp;&middot; <img src="/images/layout/icon-maps.gif" alt=""> Stadtplan');
		else
			$(obj).after('<a href="#" class="mapLink" id="mapLink'+key+'" title="Adresse auf dem Stadtplan zeigen">&nbsp;&middot; <img src="/images/layout/icon-maps.gif" alt=""></a>');
	});
	$.each($(id+' a.mapLink'), function(key,obj) {
		$(obj).click(function() { showAddressOnMap(this); return false; });
	});
}

// call functions
$(document).ready(function() {
	createMapLinks();
});
