var Map = { }

Map.Poi = Class.create({
	initialize: function(element, lat, lng, zoom, options) {
		this.element = $(element);
		this.options = options;
		this.heightTarget = 0;
		this.marker_count = 0;
		
		this.map = new GMap2(this.element);
		this.geocoder = new GClientGeocoder();
		this.marker_manager = null;
		this.poi_icon = new GIcon();
		this.directions = null;
		this.markers = new Array();
		
		this.options = options || { };
		this.options.typecontrol = this.options.typecontrol ? true : false;
		this.options.zoomcontrol = this.options.zoomcontrol ? true : false;
		this.options.iconwidth = this.options.iconwidth || 17;
		this.options.iconheight = this.options.iconheight || 21;
		this.options.anchorx = this.options.anchorx || 8;
		this.options.anchory = this.options.anchory || 21;
		
		this.map.setCenter(new GLatLng(lat, lng), zoom);
		this.map.setMapType(G_PHYSICAL_MAP);

		if(this.options.typecontrol){
			this.map.addControl(new GMapTypeControl);
		}
		if(this.options.zoomcontrol){
			this.map.addControl(new GLargeMapControl);
		}
		
		this.poi_icon.iconSize = new GSize(this.options.iconwidth,this.options.iconheight);
		this.poi_icon.iconAnchor = new GPoint(this.options.anchorx,this.options.anchory);
		
		Event.observe(window, 'unload', this.cleanup.bindAsEventListener(this));
	},
	
	zoomIn: function() {
		this.map.zoomIn();
		return false;
	},
	
	zoomOut: function() {
		this.map.zoomOut();
		return false;
	},
			
	addPanelPoi: function(Icon,Lat,Lng,Url,Title,Id) {
		if(this.marker_manager==null){
			this.marker_manager = new MarkerManager(this.map, {trackMarkers:true});
			this.marker_manager.refresh();
		}
		
		var tmp_icon = new GIcon(this.poi_icon);
		tmp_icon.image = Icon;
		var marker_options = { icon: tmp_icon };
		
		var tmp = new GMarker(new GLatLng(Lat,Lng),marker_options);
		var panel = new Map.Panel(this.map, tmp, Title, Id);
		
		tmp.panel = panel;
		tmp.url = Url;
		tmp._parent = this;
		
		this.map.addOverlay(panel);
		
		GEvent.addListener(tmp, "mouseover",function(){
			this.panel.showtip();
		});
		
		GEvent.addListener(tmp, "mouseout",function(){
			this.panel.hidetip();
		});
		
		GEvent.addListener(tmp, "mouseup",function(){
			if(this.panel.visible()){
				this.panel.hide();
			}else{
				for (var i=0;i<this._parent.marker_count;i++){
					if(this._parent.markers[i].panel!=this.panel){
						this._parent.markers[i].panel.hide();
					}
				}
				this.panel.loadcontent(this.url+'&_title='+escape(this.panel.tiptext));
			}
		});
		
		this.markers[this.marker_count] = tmp;
		this.marker_count++; 
		this.marker_manager.addMarker(tmp,0);
	},
	
	addSimplePoi: function(Icon,Lat,Lng,Url,Title) {
		if(this.marker_manager==null){
			this.marker_manager = new MarkerManager(this.map, {trackMarkers:true});
			this.marker_manager.refresh();
		}
		
		var tmp_icon = new GIcon(this.poi_icon);
		tmp_icon.image = Icon;
		var marker_options = { icon: tmp_icon, title: Title };
		
		var tmp = new GMarker(new GLatLng(Lat,Lng),marker_options);
		
		if(Url!=''){
			tmp.url = Url;		
			GEvent.addListener(tmp, "mouseup",function(){
				document.location.href = this.url;
			});
		}
			
		this.markers[this.marker_count] = tmp;
		this.marker_count++; 
		this.marker_manager.addMarker(tmp,0);
	},
	
	addTooltipPoi: function(Icon,Lat,Lng,Url,Title,Id) {
		if(this.marker_manager==null){
			this.marker_manager = new MarkerManager(this.map, {trackMarkers:true});
			this.marker_manager.refresh();
		}
		
		var tmp_icon = new GIcon(this.poi_icon);
		tmp_icon.image = Icon;
		var marker_options = { icon: tmp_icon };
		
		var tmp = new GMarker(new GLatLng(Lat,Lng),marker_options);
		var tooltip = new Map.Tooltip(this.map, tmp, Title, Id);
		
		tmp.tooltip = tooltip;
		tmp.url = Url;
		tmp._parent = this;
		
		this.map.addOverlay(tooltip);
		
		GEvent.addListener(tmp, "mouseover",function(){
			this.tooltip.showtip();
		});
		
		GEvent.addListener(tmp, "mouseout",function(){
			this.tooltip.hidetip();
		});
		
		if(Url!=''){
			tmp.url = Url;		
			GEvent.addListener(tmp, "mouseup",function(){
				document.location.href = this.url;
			});
		}
			
		this.markers[this.marker_count] = tmp;
		this.marker_count++; 
		this.marker_manager.addMarker(tmp,0);
	},
	
	extendArea: function(Padding) {
		var boundary = new GLatLngBounds();
		
		for (var i=0;i<this.marker_count;i++){
			boundary.extend(this.markers[i].getLatLng());
		}
		
		var sw = boundary.getSouthWest();
		var ne = boundary.getNorthEast();
		var center = boundary.getCenter();
		
		var min_lat = Math.min(2*center.lat() - ne.lat(), sw.lat());                
		var max_lat = Math.max(2*center.lat() - sw.lat(),  ne.lat());        
		var min_lng = Math.min(2*center.lng() - ne.lng(), sw.lng());               
		var max_lng = Math.max(2*center.lng() - sw.lng(), ne.lng());
				
		boundary.extend(new GLatLng(min_lat-Padding, min_lng-Padding));
		boundary.extend(new GLatLng(max_lat+Padding, max_lng+Padding));

		this.map.setCenter(boundary.getCenter(), this.map.getBoundsZoomLevel(boundary));
	},
		
	cleanup: function() {
		GUnload();
	}
});

Map.Panel = Class.create(GOverlay.prototype,{
	initialize: function(map,marker,tiptext,id) {
		if(marker){
			this.marker = marker;
			this.tiptext = tiptext;
			this.map = map;
			this.id = id;
			
			Element.insert(this.map.getPane(G_MAP_FLOAT_PANE), '<div id="t'+id+'" class="map-tooltip" style="display: none;">'+tiptext+'</div>');
			Element.insert(this.map.getPane(G_MAP_FLOAT_PANE), '<div id="'+id+'" class="map-panel" style="display: none;"><p class="inner">Meldungen werden geladen ...</p></div>');
			this.panel = $(id);
			this.tip = $('t'+id);
		}
	},
		
	redraw: function(force) {
		if (!force) return;
		var marker_pos = this.map.fromLatLngToDivPixel(this.marker.getPoint());
		var center_pos = this.map.fromLatLngToDivPixel(this.map.getCenter());
		var icon_anchor = this.marker.getIcon().iconAnchor;
		
		var x_pos = Math.round(marker_pos.x - this.tip.getWidth() / 2);
		var y_pos = marker_pos.y - icon_anchor.y - this.tip.getHeight() - 4;
		this.tip.style.top = y_pos + 'px';
		this.tip.style.left = x_pos + 'px';

		var x_pos = marker_pos.x+10+this.panel.getWidth()>this.map.getSize().width ? marker_pos.x-this.panel.getWidth()-10 : marker_pos.x+10;
		var y_pos = marker_pos.y+30+this.panel.getHeight()>this.map.getSize().height ? marker_pos.y-(this.panel.getHeight()-(this.map.getSize().height-marker_pos.y-30)) : marker_pos.y;
		this.panel.style.top = y_pos + 'px';
		this.panel.style.left = x_pos + 'px';
	},
	
	show: function() {
		this.hidetip();
		Effect.Appear(this.panel, { duration: 0.25 });
	},
	
	hide: function() {
		Effect.Fade(this.panel, { duration: 0.25 });
	},
	
	showtip: function() {
		if(!this.visible()){
			this.tip.show();
		}
	},
	
	hidetip: function() {
		this.tip.hide();
	},
	
	visible: function() {
		return this.panel.visible();
	},
	
	loadcontent: function(url) {
		this.show();
		new Ajax.Updater(this.panel, url, { onComplete: (function() { this.redraw(true) }).bind(this) });
	}
})

Map.Tooltip = Class.create(GOverlay.prototype,{
	initialize: function(map,marker,tiptext,id) {
		if(marker){
			this.marker = marker;
			this.tiptext = tiptext;
			this.map = map;
			
			Element.insert(this.map.getPane(G_MAP_FLOAT_PANE), '<div id="'+id+'" class="map-tooltip" style="display: none;">'+tiptext+'</div>');
			this.tip = $(id);
		}
	},
		
	redraw: function(force) {
		if (!force) return;
		var marker_pos = this.map.fromLatLngToDivPixel(this.marker.getPoint());
		var center_pos = this.map.fromLatLngToDivPixel(this.map.getCenter());
		var icon_anchor = this.marker.getIcon().iconAnchor;
		
		var x_pos = Math.round(marker_pos.x - this.tip.getWidth() / 2);
		var y_pos = marker_pos.y;
		this.tip.style.top = y_pos + 'px';
		this.tip.style.left = x_pos + 'px';
	},
	
	showtip: function() {
		this.tip.show();
	},
	
	hidetip: function() {
		this.tip.hide();
	}
})
