
function ToolTip(marker, html, width) {
	this.html_ = html;
	this.width_ = (width ? width + 'px' : 'auto');
	this.marker_ = marker;
}

ToolTip.prototype = new GOverlay();

ToolTip.prototype.initialize = function(map) {
	var div = document.createElement('div');
	div.style.display = 'none';
	gmapInstance.map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
	this.map_ = map;
	this.container_ = div;
}

ToolTip.prototype.remove = function() {
	this.container_.parentNode.removeChild(this.container_);
}

ToolTip.prototype.copy = function() {
	return new ToolTip(this.html_);
}

ToolTip.prototype.redraw = function(force) {
	if(!force) return;

	var pixelLocation = this.map_.fromLatLngToDivPixel(this.marker_.getPoint());
	this.container_.innerHTML = this.html_;
	this.container_.className = 'mapToolTip';
	this.container_.style.left = pixelLocation.x + 10 + 'px';
	this.container_.style.top  = pixelLocation.y - 20 + 'px';

	//this.container_.style.whiteSpace = 'nowrap';
	if(this.width_ != 'auto') this.container_.style.overflow = 'hidden';

	this.container_.style.display = 'block';
}

GMarker.prototype.ToolTipInstance = null;
GMarker.prototype.openToolTip = function(content) {
	// nicht zeigen wenn ein custom InfoWindow da ist
	if(this.ToolTipInstance == null) {
		this.ToolTipInstance = new ToolTip(this, content);
		gmapInstance.map.addOverlay(this.ToolTipInstance);
	}
}

GMarker.prototype.closeToolTip = function() {
	// nicht zeigen wenn ein custom InfoWindow da ist
	if(this.ToolTipInstance != null) {
		gmapInstance.map.removeOverlay(this.ToolTipInstance);
		this.ToolTipInstance = null;
	}
}

var CB_GoogleMap = Class.create();
CB_GoogleMap.prototype = {
	'initialize': function(mapElement, options) {
		// Wenn Browser für GoogleMap geeignet ist, dann Map aufbauen
		if (GBrowserIsCompatible()) {
			if(options instanceof Object) {
				if(options.controlZoom == 'small') {
					var zoomControl = new GSmallMapControl();
				} else if(options.controlZoom == 'nocontrol') {
					// do nothing
				} else {
					var zoomControl = new GLargeMapControl();
				}

				if(options.controlType == 'nocontrol') {
					// do nothing
				} else {
					var typeControl = new GMapTypeControl();
				}
			} else {
				var zoomControl = new GLargeMapControl();
				var typeControl = new GMapTypeControl();
			}

			// Map mit Controls definieren
			var map = new GMap2(mapElement);
			this.map = map;

			// add zoom control
			if(zoomControl instanceof Object) {
				this.map.addControl(zoomControl);
			}

			// add type control
			if(typeControl instanceof Object) {
				this.map.addControl(typeControl);
			}

			var point = new GLatLng(startLat, startLng);
			this.map.setCenter(point, startZoom);

			// desclare variables, so that they are available in the object scope
			this.mainMarkerPoint;
			this.mainMarker;

			this.currentDirectionPoint;

			// initialize the directions extension and set the panel in which to show the direction information
			// xthis.directionsPanel = document.getElementById('directionPanel');
			this.directions = new GDirections(this.map);
			// this.directions = new GDirections(this.map, this.directionsPanel);
	    } else {
	    	mapElement.innerHTML = 'Bitte aktivieren Sie JavaScript, um die Karte zu sehen.';
	    }
	},

	'setDirection' : function(points) {
		// check whether the is already displayed
		var routeDisplayed = false;
		if(this.currentDirectionPoint instanceof GLatLng) {
			if( this.currentDirectionPoint.lat() == points[0].lat() &&
				this.currentDirectionPoint.lng() == points[0].lng()) {
				routeDisplayed = true;
				// $$('div.article_overview').first().show();
			}
		}

		// clear the current route
		this.directions.clear();
		this.currentDirectionPoint = '';

		// if the route is already displayed, then do nothing to let the map be cleared
		if(routeDisplayed != true) {
			// $$('div.article_overview').first().hide();
			this.currentDirectionPoint = points[0];
			this.directions.loadFromWaypoints(points, {preserveViewport : 1, locale : 'en_US'});
		}
	},

	// setzt einen Marker auf die googlemap dieses Objekts
	'addMarker': function(lat, lng, name, description, options) {
		if(options instanceof Object) {
			if(options.icon != undefined) {
				// Create our "tiny" marker icon
				var icon = new GIcon();
				icon.image = options.icon;
				// icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
				icon.iconSize = new GSize(21, 29);
				icon.shadowSize = new GSize(21, 29);
				icon.iconAnchor = new GPoint(11, 29);
				icon.infoWindowAnchor = new GPoint(5, 1);

				var marker = new GMarker(new GLatLng(lat, lng), icon);
			}
		}

		if(!(marker instanceof GMarker)) {
			var marker = new GMarker(new GLatLng(lat, lng));
		}

		GEvent.addListener(marker, 'mouseover',
			function() {
				marker.openToolTip(name);
			}
		);
		GEvent.addListener(marker, 'mouseout',
			function() {
				marker.closeToolTip();
			}
		);

		// if this is the main icon, than add onClickHandler
		if(options instanceof Object) {
			if(options.showInfoBox == true) {
				GEvent.addListener(marker, 'click', function() {
					marker.openInfoWindowHtml(description);
				});
			}
		}

		this.map.addOverlay(marker);

		return marker;
	},

	// setzt einen Marker auf die googlemap dieses Objekts
	'addDirectionMarker': function(lat, lng, name, description, options) {
		marker = this.addMarker(lat, lng, name, description, options);

		GEvent.addListener(marker, 'click',
			function() {
				this.setDirection([new GLatLng(lat, lng), this.mainMarkerPoint]);
			}.bind(this)
		);

		return marker;
	},
	'onAjaxError': function(request) {

	}
}