var AgencyMapRef; 
	
function agencyMouseOverCatcher(link) {
	AgencyMapRef.onLinkClick(link);
}

function AgencyMap () {
		
}
	
AgencyMap.prototype = {
    map: null
    ,bounds: null		// bounds that
    ,geocoder: null
    ,items: []
    ,init: function(center){		

		this.geocoder = new GClientGeocoder();
		this.center = center;
		
        if (!GBrowserIsCompatible()) return false;
	
		// create map
		this.map = new GMap2(document.getElementById("AgencyMap"));
			
		// add controlls
		this.map.addControl(new GLargeMapControl());
		this.map.addControl(new GMapTypeControl());
			
		this.map.enableScrollWheelZoom();
	}
	,setCenter: function(center, zoom) {
		if (zoom == undefined) zoom = 6;
		if (!center) {
			this.center = {
				geo_b: '51.3171621795287' 
				,geo_l: '9.49079659015147' 
			};
		}
		else {
			this.center = center;			
		}
		this.map.setCenter(new GLatLng(Number(this.center.geo_b), Number(this.center.geo_l)), zoom);
	}
	,populate: function(items) {
		this.items = items;
		this.boundsHas = 0;
		for (var i in this.items) {
			this.createMarker(this.items[i]);
		}
		this.bounds = new GLatLngBounds();
		//this.map.setZoom(this.map.getBoundsZoomLevel(this.bounds));
		//this.map.setZoom(7);
		//this.map.setCenter(this.bounds.getCenter());
		this.map.setCenter();
	}
	,createMarker: function(item) {
		var app = this;
		this.geocoder.getLatLng(item.address, function(point) {
			app.onGetGeoXYZ(point, item);
		});
	}
	,onGetGeoXYZ: function(point, item) {
		if (!point) {
		// wenn er trotz erfolgloser suche manuelle geo daten hat
			if (item.Lat && item.Lng) {
				var point = new GLatLng(item.Lat, item.Lng);
			}
			else {					
				return false;
			}
		}
		if (this.boundsHas <= this.boundsMax) {
			this.bounds.extend(point);
			this.boundsHas++;
		}
			
		var AgencyIcon = new GIcon(G_DEFAULT_ICON);
		AgencyIcon.image = _path + 'app/templates/img/logo.png';
		AgencyIcon.iconSize = new GSize(67, 43);
		AgencyIcon.shadow = _path + 'app/templates/img/schatten.png';
		AgencyIcon.shadowSize = new GSize(67, 43);
		AgencyIcon.iconAnchor = new GPoint(5, 40);
		AgencyIcon.infoWindowAnchor = new GPoint(10,20);
		AgencyIcon.imageMap = [0,27,11,14,21,20,37,2,63,2,63,16,42,30,23,28,11,38];
	
			var markerOptions = {
				icon: AgencyIcon
			};
			
			var marker = new GMarker(point, markerOptions);
			
			item.marker = marker;
			
			marker.item = item;
			
			var app = this;
			
			GEvent.addListener(marker, "click", function(point) {
				app.showPopup(this);
			});
			
			this.map.addOverlay(marker);
		}
		,showPopup: function(marker) {
			//console.log(marker);
			var item = marker.item;
			
			var html = '<div class="mapPopUp">'+item.text;
			html += '<br /><a href="'+item.link+'">Weitere Informationen</a>';					
			html += '</div>';
			marker.openInfoWindowHtml(html);
		}
		,onLinkClick: function(link) {
			var data = link.getAttribute('id').split("_");
			var type = data[0];
			var ID = data[1];
			
			for (var index in this.items) {
				var item = this.items[index];
				if (item.id == ID) {
					this.showPopup(item.marker);
				}
			}
		}
		,clearMap: function() {
			for (var i in this.items) {
				this.removeItem(this.items[i]);
			}
			this.items = [];
		}
		,removeItem: function(item) {
			// TODO: remove listeners on marker
			try {				
				this.map.removeOverlay(item.marker);
			} catch (e) {
				// TODO: handle exception
				//console.log("big error");
			}
		}
	}
