GPolygon.prototype.getBounds = function(){
	var bounds = new GLatLngBounds();
	for(i=0; i< this.getVertexCount(); i++) {
		vertex = this.getVertex(i);
		bounds.extend(vertex);
	}
	return bounds;
}

function MDC_GMask(options) {
	
	// dummy Polyline zur Ermittlung der Bounds des inneren Masken Polygons
	var inner_polyline = new GPolyline.fromEncoded({
		color: "#000000",
		weight: 1,
		points: options.inner_polyline.encoded_points,
		levels: options.inner_polyline.encoded_levels,
		zoomFactor: 2,
		numLevels: 18
	});
	
	var inner_bounds = inner_polyline.getBounds();
	delete(inner_polyline);
	
	var mask_polygon = new GPolygon.fromEncoded({
	  polylines: [
	    {
	    	points:     options.outer_polyline.encoded_points,
				levels:     options.outer_polyline.encoded_levels,
				color:      options.stroke_color,
				opacity:    1,
				weight:     2,
				numLevels:  18,
				zoomFactor: 2
			},
	    {
	    	points:     options.inner_polyline.encoded_points,
				levels:     options.inner_polyline.encoded_levels,
				color:      options.stroke_color,
				opacity:    0,
				weight:     0.5,
				numLevels:  18,
				zoomFactor: 2
			}
		],
	  fill:    true,
	  color:   options.fill_color,
	  opacity: options.fill_opacity,
	  outline: true
	});
	
	mask_polygon.getBounds = function() {
		return inner_bounds;
	}
	
	return mask_polygon;
}

function MDC_build_markers(markers_type, markers_data, g_icon_options) {
	var markers = [];
	for(var i = 0; i < markers_data.length; i++) {
		var marker = markers_data[i];
		markers[i] = new window['MDC_GMarker_' + markers_type](marker, g_icon_options);	
	}
	
	return markers;
}


GMap2.prototype.zoom_to_bounds = function(bounds) {
	zoom = this.getBoundsZoomLevel(bounds);
	if(zoom > 18) zoom = 18;
	this.setCenter(bounds.getCenter());
	this.setZoom(zoom);
}

GMap2.prototype.pan_to_than_zoom = function(bounds) {
	var zoom = this.getBoundsZoomLevel(bounds);
	if(zoom > 18) zoom = 18;
	var center = bounds.getCenter();
	this.MDC_zoom_to_target = zoom;
	
	this.panTo(bounds.getCenter());
}

GMap2.prototype.zoom_to_prepared_center = function() {
	if(this.MDC_zoom_to_target) {
		var zoom = this.MDC_zoom_to_target + 0;
		this.MDC_zoom_to_target = null;
		this.setZoom(zoom);
	}
}

GMap2.prototype.MDC_add_mask_x = function(mask_name, zoom_to) {
	if(! zoom_to) zoom_to = false;
	
	var the_map = this;
	
	GDownloadUrl('MDC_GMap2_xhr.php?action=get_mask&mask_name=' + mask_name, function(result) {
		eval('var mask = ' + result); // json string nach mask aufloesen
		the_map.MDC_named_elements.masks[mask_name] = new MDC_GMask({
			outer_polyline: {
				encoded_points: mask.outer_encoded_points,
				encoded_levels: mask.outer_encoded_levels
			},
			inner_polyline: {
				encoded_points: mask.inner_encoded_points,
				encoded_levels: mask.inner_encoded_levels
			},
			stroke_color: mask.stroke_color,
			fill_color:   mask.fill_color,
			fill_opacity: mask.fill_opacity
		});
		the_map.addOverlay(the_map.MDC_named_elements.masks[mask_name]);
		
		the_map.MDC_visible_elements.masks[mask_name] = the_map.MDC_named_elements.masks[mask_name];
		
		the_map.zoom_target = the_map.MDC_named_elements.masks[mask_name];

		if(zoom_to == 1) {
			var mask_bounds = the_map.MDC_named_elements.masks[mask_name].getBounds();
			the_map.pan_to_than_zoom(mask_bounds);
		} else if(zoom_to == 2) {
			var mask_bounds = the_map.MDC_named_elements.masks[mask_name].getBounds();
			the_map.zoom_to_bounds(mask_bounds);
		}
	});
}

GMap2.prototype.MDC_remove_mask = function(mask_name) {
	try {
		this.removeOverlay(this.MDC_named_elements.masks[mask_name]);
		this.MDC_visible_elements.masks[mask_name] = false;
	} catch(e){}
}

GMap2.prototype.MDC_remove_all_masks = function() {
	var the_map = this;
	Object.values(this.MDC_visible_elements.masks).each(function(mask, value) {
 		the_map.removeOverlay(mask);
	});
}


GMap2.prototype.MDC_add_marker_group_x = function(marker_group_name, g_icon_options) {
	var the_map = this;
	
	GDownloadUrl('MDC_GMap2_xhr.php?action=get_marker_group&marker_group_name=' + marker_group_name, function(result) {
		try {
			eval('var marker_group = ' + result); // json string nach mask aufloesen
			the_map.MDC_named_elements.marker_groups[marker_group_name] = new MarkerManager(the_map);
			the_map.MDC_named_elements.marker_groups[marker_group_name].addMarkers(
				MDC_build_markers(
					marker_group.type,
					marker_group.markers,
					g_icon_options
				),
				1
			);
			the_map.MDC_named_elements.marker_groups[marker_group_name].refresh();
			the_map.MDC_visible_elements.marker_groups[marker_group_name] = the_map.MDC_named_elements.marker_groups[marker_group_name];
		} catch(e) {}
	});
}

GMap2.prototype.MDC_remove_marker_group = function(marker_group_name) {
	try { 
		this.MDC_named_elements.marker_groups[marker_group_name].clearMarkers();
	} catch(e){}
}

GMap2.prototype.MDC_remove_all_marker_groups = function() {
	var the_map = this;
	Object.values(this.MDC_visible_elements.marker_groups).each(function(marker_group, value) {
 		marker_group.clearMarkers();
	});
}
