var FX_Map = Class.create({
	initialize : function(id){
		this.id = id;
//		this.mediaBaseUrl = '/media/images/map/';
//		this.setCenter(50,7);
//		this.setZoomLevel(5);
		this.markerdict = [];
		google.load("maps", "2.x");
		this.config = {
				center : {latitude:50, longitude:7},
				zoomLevel : 6,
				mediaBaseUrl : '/media/images/map/',
		};
		Event.observe(window, "load", this.initMap.bind(this));
		//console.debug('Map loaded');
		window.fxapplication.fxmap = this;
	},
	configureListeners : function(){
		var that = this;
		GEvent.addListener(this.map, "moveend", function(){
			//console.debug('Map moved' + this.getCenter().lat());
			var bnds = this.getBounds();
			document.fire('fxmap:moved', {center_lat: this.getCenter().lat(), center_lng: this.getCenter().lng(), zoom: this.getZoom(), sw_lat:bnds.getSouthWest().lat(), sw_lng:bnds.getSouthWest().lng(), ne_lat:bnds.getNorthEast().lat(), ne_lng:bnds.getNorthEast().lng()});
		});
	},
	addMarker : function(m){
		// Erstellt das "winzige" Markierungssymbol
		var ico = new GIcon();
		if (m.href != null || true){
			ico.iconSize = new GSize(35, 35);
			ico.shadowSize = new GSize(22, 20);
			ico.iconAnchor = new GPoint(0, 32);
			ico.infoWindowAnchor = new GPoint(5, 1);
		} else {
			ico.iconSize = new GSize(60, 60);
			ico.shadowSize = new GSize(22, 20);
			ico.iconAnchor = new GPoint(0, 32);
			ico.infoWindowAnchor = new GPoint(5, 1);
		}
		ico.image = this.config.mediaBaseUrl + 'marker/' + m.icon + ".png";

		// Richtet das GMarkerOptions-Objekt ein
		markerOptions = { icon:ico };
		var point = new GLatLng(m.latitude,m.longitude);
		var mark = new GMarker(point, markerOptions);
		if (m.href != null){
			GEvent.addListener(mark, "click", function(){
				window.location.href = m.href;
			});
		}

		GEvent.addListener(mark, "mouseover", function(event){
			if (m.headline.length == 0) return;
			var point = window.fxapplication.gmap.fromLatLngToContainerPixel(this.getPoint());
			var e = $("map-control-inf").show();
			e.innerHTML = "";
			if (m.image != null){
				e.insert(new Element("img", {"src":m.image}));
			}
			//mark.setImage(this.config.mediaBaseUrl + 'marker/' + m.icon + ".png");
			e.insert(new Element("b").insert(m.headline));
			e.insert(new Element("p").insert(m.text).addClassName("small-descr"));
			e.setStyle({left: (point.x - e.clientWidth - 10) + "px", top: (point.y - e.clientHeight - 20) + "px", width:"344px"});
		});
		GEvent.addListener(mark, "mouseout", function(event){
			var point = window.fxapplication.gmap.fromLatLngToContainerPixel(this.getPoint());
			var e = $("map-control-inf").hide();
			e.setStyle({left: (point.x - e.clientWidth) + "px", top: (point.y - e.clientHeight) + "px"});
			//mark.setImage(this.config.mediaBaseUrl + 'marker/' + m.icon + ".png");
		});
		var add = true;
		this.markerdict.each(function(tmp){
//			console.debug('Latitude' + mark.getLatLng().lat());
//			console.debug('Latitude' + tmp.getLatLng().lat());
			if (tmp.getLatLng().equals(mark.getLatLng())){
				//console.debug('Marker already on map');
				add = false;
			}
		});
		if (add){
			this.map.addOverlay(mark);
			this.markerdict.push(mark);
		}
		//console.debug('Add marker');
		
	},
	// Diese Funktion aufrufen, wenn die Seite geladen ist
	initMap : function() {
		var container = $(this.id);
		var map = new google.maps.Map2(container);
		map.setCenter(new google.maps.LatLng(this.config.center.latitude, this.config.center.longitude), this.config.zoomLevel);
		this.map = window.fxapplication.gmap = map;
		var marker = window.fxapplication.settings["unserort.MapControl"][0].markers;
		marker.each(function(m){
			this.addMarker(m);
		}, this);
		if (window.fxapplication.mapcontrol != null){
			var n = window.fxapplication.mapcontrol.neighbouring;
			n.each(function(o){
				o.shapes.each(function(s){
					var cord = [];
					s.each(function(c){
							cord.push(new GLatLng(c.lat, c.lng));
						});
					var poly = new GPolygon(cord, "#000000", 1, 0.5, "#000000", 0.3);
					poly.clickable = true;
					GEvent.addListener(poly, "click", function(){window.location.href="/"+o.name+"/nachrichten"});
					GEvent.addListener(poly, "mouseover", function(e){poly.opacity=0.6;poly.redraw(true);});
					GEvent.addListener(poly, "mouseout", function(e){poly.opacity=0.3;poly.redraw(true);});
					this.map.addOverlay(poly);
				}, this);
			}, this);
		}
		this.configureListeners();
		window.fxapplication.addEventListener("mapcontrol.shapesloaded", function(e){}, this);
	}
});

