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.options.labeledMarker = this.options.labeledMarker ? true : false;
		
		this.options.movepanelx = this.options.movepanelx || 70;
		this.options.movepanely = this.options.movepanely || 60;
		
		this.map.setCenter(new GLatLng(lat, lng), zoom);

		if(this.options.normalMap){
			this.map.setMapType(G_NORMAL_MAP);
		}else{
			this.map.setMapType(G_PHYSICAL_MAP);
		}

		if(this.options.typecontrol){
			this.map.addControl(new GMapTypeControl);
		}
		if(this.options.zoomcontrol){
			this.map.addControl(new GSmallZoomControl);
		}
		
		this.poi_icon.iconSize = new GSize(this.options.iconwidth,this.options.iconheight);
		this.poi_icon.iconAnchor = new GPoint(this.options.anchorx,this.options.anchory);
		this.poi_icon.transparent = null;
		this.poi_icon.shadow = null;
		
		Event.observe(window, 'unload', this.cleanup.bindAsEventListener(this));
		
		//mmueller: count map views
		new Ajax.Request('/scripts/update_map_counter.php',
		  {
			method:'get'
		  });		
	},
	
	zoomIn: function() {
		this.map.zoomIn();
		return false;
	},
	
	zoomOut: function() {
		this.map.zoomOut();
		return false;
	},
	
	addHtmlPoiWithClassName: function(Icon,Lat,Lng,Title,Html,Id,classname) {
		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, this.options.movepanelx, this.options.movepanely, classname);
		
		tmp.panel = panel;
		tmp.html = Html;
		tmp._parent = this;
		this.map.addOverlay(panel);
		
		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.setcontent(this.html);
			}
		});
		
		this.markers[this.marker_count] = tmp;
		this.marker_count++; 
		this.marker_manager.addMarker(tmp,0);
		return tmp;
	},
	
	addHtmlPoi: function(Icon,Lat,Lng,Title,Html,Id,options) {
		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 };
		
		//begin
		var tmp = null;
		if(this.options.labeledMarker) {
			s = null;
			if(options.counter < 10) {
				s = new GSize(6, -26);
			}else if(options.counter < 100) {
				s = new GSize(3, -26);
			}else if(options.counter < 1000) {
				s = new GSize(0, -26);
			}
			
			marker_options = { 
			  "icon": tmp_icon,
			  "clickable": true,
			  "title": options.labeledtitle,
			  "labelText": options.counter ,
			  "labelClass": "labeledMarker",
			  "labelOffset": s
			};
			tmp = new LabeledMarker(new GLatLng(Lat,Lng), marker_options);		
		} else {
			tmp = new GMarker(new GLatLng(Lat,Lng),marker_options);
		}	
		//end
		var panel = new Map.Panel(this.map, tmp, Title, Id, this.options.movepanelx, this.options.movepanely, '');

		tmp.panel = panel;
		tmp.html = Html;
		tmp._parent = this;
		this.map.addOverlay(panel);
		
		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.setcontent(this.html);
			}
		});
		
		this.markers[this.marker_count] = tmp;
		this.marker_count++; 
		this.marker_manager.addMarker(tmp,0);
		return tmp;
	},
	
	closeHtmlPoi: function(options) {
		opts = options || { };
		for (var i=0;i<this.marker_count;i++){
			if(this.markers[i].panel!=this.panel){
				this.markers[i].panel.hide();
			}
		}
		
		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, this.options.classname);
		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);
		return tmp;
	},
	
	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);
		return tmp;
	},
	
	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);
		return tmp;
	},
	
	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();
	},
	
	getDirections: function(From, To, Summary, Description, Print, IconStart, IconEnd) {
		if(this.directions==null){
			this.directions = new GDirections(gmap_lightbox.map);
			this.directions.parent = this;
			this.directions.from = From;
			this.directions.to = To;
			this.directions.summary_element = Summary;
			this.directions.description_element = Description;
			this.directions.print_element = Print;
			this.directions.icon_start = IconStart;
			this.directions.icon_end = IconEnd;
			
			GEvent.addListener(this.directions, "load", this.onDirectionsLoad);
   			GEvent.addListener(this.directions, "error", this.handleErrors);
   		}
   		
   		this.directions.clear();
   		if(this.directions.print_element) this.directions.print_element.hide();
   		this.directions.summary_element.innerHTML = '';
   		this.directions.description_element.innerHTML = '';
   		this.directions.load("from: " + From + " to: " + To, { "locale": "de_DE", "getSteps": true });
	},
	
	onDirectionsLoad: function() {
		this.getMarker(0).getIcon().image = this.icon_start;
		this.getMarker(0).getIcon().iconSize = this.parent.poi_icon.iconSize;
		this.getMarker(0).getIcon().iconAnchor = this.parent.poi_icon.iconAnchor;
		this.getMarker(0).getIcon().shadow = '';
		
		this.getMarker(1).getIcon().image = this.icon_end;
		this.getMarker(1).getIcon().iconSize = this.parent.poi_icon.iconSize;
		this.getMarker(1).getIcon().iconAnchor = this.parent.poi_icon.iconAnchor;
		this.getMarker(1).getIcon().shadow = '';
		
		this.getPolyline().setStrokeStyle({"color": '#d60027',"opacity": 0.8});
		
		this.summary_element.innerHTML = this.getSummaryHtml();
		
		var description = '<dl class="table">';
		for(var i=0;i<this.getRoute(0).getNumSteps();i++){
			description += '<dt'+(i%2==0 ? ' class="hot"' : '')+'>'+this.getRoute(0).getStep(i).getDescriptionHtml()+'</dt><dd>'+this.getRoute(0).getStep(i).getDistance().html+'</dd>';
		}
		
		this.description_element.innerHTML = description+'</dl>';
		
		if(this.print_element)  this.print_element.show();
		
		var date = new Date();
		date.setTime(date.getTime()+(60*60*1000));
		document.cookie = "directions="+this.from+"|"+this.to+"; expires="+date.toGMTString()+"; path=/";
	},
	
	handleErrors: function() {
		this.summary_element.innerHTML = 'Die Route konnte nicht berechnet werden, da Abfahrt- oder Zielort nicht gefunden wurde.';
		this.description_element.innerHTML = '';
		this.print_element.hide();
	}
});

Map.Panel = Class.create(GOverlay.prototype,{
	initialize: function(map,marker,tiptext,id,movepanelx,movepanely,classname) {
		if(marker){
			this.marker = marker;
			this.tiptext = tiptext;
			this.map = map;
			this.id = id;
			this.movepanelx=movepanelx;
			this.movepanely=movepanely;
			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 ' + classname + '" 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-50;
		var y_pos = marker_pos.y-this.panel.getHeight()-40;
		this.panel.style.top = y_pos + 'px';
		this.panel.style.left = x_pos + 'px';
	},
	
	show: function() {
		this.hidetip();
		
		var marker_pos = this.map.fromLatLngToDivPixel(this.marker.getPoint());
		marker_pos.x += this.movepanelx;//70;
		marker_pos.y -= this.movepanely;//60;

		this.map.panTo(this.map.fromDivPixelToLatLng(marker_pos));
		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();
	},
	
	setcontent: function(Html) {
		this.panel.innerHTML = Html;
		this.show();
	},
	
	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 + icon_anchor.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();
	}
})

