/***
 GLOBAL VARIABLES
***/
var map; 				// Main map
var map2; 				// Map in the detail view
//var map3;

var startLat = 50; 					// Initial center latitude
var startLng = 5; 				// Initial center longitude
var startZoom = 4;

var WMS; 				// WMS layer only
				// Initial zoom level
//var startMapType = WMS;	//Initial map type

//var MAP_WMS; 			// Map + WMS overlay
//var SATELLITE_WMS; 		// Satellite image + WMS overlay
//var HYBRID_WMS; 		// Hybrid image + WMS overlay
//var TERRAIN_WMS; 		// Terrain Image + WMS overlay

var WMSLayers='0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26';
var WMSFormat='image/png';
var WMSBaseURL='http://ims2.bkg.bund.de:8080/wmsconnector/com.esri.wms.Esrimap/ermegn4d?';

// Polygon for the bounding box
var hidemap = false;

var FeatureTab = new Array();

var Feature = [			// Feature for the detail view
	"name",
	"country",
	"countryIso3166",
	"featuretype",
	"gender",
	"alternateName",
	"longitude",
	"latitude",
	"pronounciation",
	"featureID",
	"temporalExtent",
	"administrator",
	"lastUpdateDate",
	"status",
	"grammaticalNumber",
	"language",
	"languageiso639",
	"topLeftX",
	"topLeftY",
	"bottomRightX",
	"bottomRightY",
	"alternativeGeographicIdentifier",
	"historicalStartDate",
	"historicalEndDate",
	"iPAs",
	"localStatus",
	"streetAdress",
	"postalCode",
	"dataSource",
	"nationalUniqueId",
	"featuretypelocal",
	"featureClassificationPicture",
	"custodian",
	"GeographicFeatureRelationText"
];

var detailViewLabel = [	// Label for the detail view
	"name",
	"country",
	"countryIso3166",
	"featuretype",
	"gender",
	"alternateName",
	"longitude",
	"latitude",
	"pronounciation",
	"featureID",
	"temporalExtent",
	"administrator",
	"lastUpdateDate",
	"status",
	"grammaticalNumber",
	"language",
	"languageiso639",
	"alternativeGeographicIdentifier",
	"historicalStartDate",
	"historicalEndDate",
	"iPAs",
	"localStatus",
	"streetAdress",
	"postalCode",
	"dataSource",
	"nationalUniqueId",
	"featuretypelocal",
	"custodian",
	"GeographicFeatureRelationText"
];


/***
 ON LOAD, ON UNLOAD
***/
//window.onload = initializeMap;	// On load: initialize map
window.onunload = GUnload; 		// On unload: garbage collection

function mapcenter()
{
	if(map==null)
		return;
	map.checkResize();
	map.setCenter(new GLatLng(56, 15),4, WMS);
}


/***
 INITIALIZE THE MAP
***/
function initializeMap()
{

	if(hidemap==true)
	{
		hidediv();

	}
	else
	{
		showdiv();
	}
	if(document.getElementById("map")==null)
	{
		return;
	}
	ClearOverlay();
	document.getElementById("detailInfo").style.visibility = 'hidden';
	map = new GMap2(document.getElementById("map"));


	// Set a background image which will be visible before the tiles have been loaded
	document.getElementById("map").style.backgroundImage = "url(images/background.jpg)";

	// Enable zoom modes
	map.enableDoubleClickZoom();
	map.enableContinuousZoom();
	map.enableScrollWheelZoom();

	// WMS Layer: MAP_WMS, SATELLITE_WMS, TERRAIN_WMS, WMS
	addWMS();
	// Add the map: center coordinates, zoom level, standard map type?
	//map.setCenter(new GLatLng(startLat, startLng), startZoom, WMS);
	map.checkResize();
	map.setCenter(new GLatLng(56, 15),4, WMS);
	setMapType();
	//mapTypeChanged();
	showMapTypes();
	// Standard map types: G_NORMAL_MAP, G_SATELLITE_MAP, G_HYBRID_MAP
	// Terrain map type: G_PHYSICAL_MAP (not yet part of the standard map type control)
	//map.addMapType(WMS);


	// Scale bar
	// map.addControl(new GScaleControl(), new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(70,10)));
	//map.SetMapType(WMS);
	// Overview map
	// var Tsize = new GSize(150, 150);
	// map.addControl(new GOverviewMapControl(Tsize));

	showMenu();
}

function ClearOverlay()
{
	if(map)
	map. clearOverlays ();
}





/***
 ADD FEATURE TO THE FEATURE ARRAY
***/
function AddFeatureTab()
{
	FeatureTab.push(Feature);
}


/***
 ADD MARKERS
 As an icon for the markers the flag of the country will be used.
 If there is not country information available, the european flag will be used instead.
***/
function addMarker(iMarker)
{
	// Set icon (flag) for the marker
	if(map==null)
		return;
	var flagIcon = new GIcon(G_DEFAULT_ICON);
	flagIcon.image = FeatureTab[iMarker]["featureClassificationPicture"];
	flagIcon.iconSize = new GSize(16, 16);
	flagIcon.shadowSize = new GSize(0, 0); //no shadow
	flagIcon.iconAnchor = new GPoint(8, 6);
	markerOptions =
	{
		icon: flagIcon,
		title: FeatureTab[iMarker]["name"]
	};

	// Create marker and add it to the map
	var marker = new GMarker(new GLatLng(FeatureTab[iMarker]["latitude"], FeatureTab[iMarker]["longitude"]), markerOptions);
	map.addOverlay(marker);

	// ON MOUSEOVER: SHOW DETAIL VIEW AND BOUNDING BOX
	GEvent.addListener(marker, "mouseover", function() {
		var bounds = new GLatLngBounds();
		var topLeft = new GLatLng(FeatureTab[iMarker]["topLeftX"],FeatureTab[iMarker]["topLeftY"]);
		var bottomRight = new GLatLng(FeatureTab[iMarker]["bottomRightX"],FeatureTab[iMarker]["bottomRightY"]);
		bounds.extend(topLeft);
		bounds.extend(bottomRight);
		//map3 = new GMap2(document.getElementById("map3"));
		//map3.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
		//map3.setMapType(G_NORMAL_MAP);

		var textFeatureType 		= createText("featuretype");
		var textFeatureTypeLocal	= createText("featuretypelocal");
		var textFeatureID 			= createText("featureID");
		var textTemporalExtent 		= createText("temporalExtent");
		var textLatitude 			= createText("latitude");
		var textLongitude 			= createText("longitude");
		var textAdministrator 		= createText("administrator");
		var textLastUpdateDate 		= createText("lastUpdateDate");
		var textStatus 				= createText("status");
		var textGender 				= createText("gender");
		var textGrammaticalNumber 	= createText("grammaticalNumber");
		var textAlternateName 		= createText("alternateName");
		var textPronounciation 		= createText("pronounciation");
		var textLanguage 			= createText("language");
		var textLanguageISO639 		= createText("languageiso639");
		var textAlternativeGeographicIdentifier = createText("alternativeGeographicIdentifier");
		var textHistoricalStartDate	= createText("historicalStartDate");
		var textHistoricalEndDate 	= createText("historicalEndDate");
		var textIPAs 				= createText("iPAs");
		var textLocalStatus 		= createText("localStatus");
		var textStreetAdress 		= createText("streetAdress");
		var textPostalCode 			= createText("postalCode");
		var textDataSource 		    = createText("dataSource");
		var textNationalUniqueId	= createText("nationalUniqueId");
		var textCustodian			= createText("custodian");
		var textGeographicFeatureRelationText= createText("GeographicFeatureRelationText");

		function createText(item){
			var label;
			if(FeatureTab[iMarker][item] != ""){

				label = "<div><td align\"left\" style=\"vertical-align: top;\">"
				+ "<table cellspacing=\"0\" cellpadding=\"0\">"
				 + "<tbody>"
				 + "<tr>"
				 + "<td align=\"left\" style=\"vertical-align: top;\">"
				 + "<table cellspacing=\"0\" cellpadding=\"0\" style=\"width: 150px;\">"
				 + "<tbody>"
				 + "<tr>"
				 + "<td align=\"left\" style=\"vertical-align: top;\">"
				+ "<div class=\"gwt-Label\">"
				+ detailViewLabel[item]+": "
				+"</div></td></tr></tbody></table></td>"
				+"<td align=\"left\" style=\"vertical-align: top;\">"
				+"<div class=\"gwt-Label\">"
				+ FeatureTab[iMarker][item]
				+ "</div></td></tr></tbody></table></td></div>";
			}
			else {
				label = "";
			}
			return label;
		}

		var text = ""
		//+"<a name=\"top\"></a>" // anchor
			+"<div id=\"mapDetailInfo\">"
				+"<div id=\"mapDetailInfoTitle\">"
					+FeatureTab[iMarker]["name"]
				+"</div>"+""
				+"<div><td align\"left\" style=\"vertical-align: top;\">"
				+ "<table cellspacing=\"0\" cellpadding=\"0\">"
				 + "<tbody>"
				 + "<tr>"
				 + "<td align=\"left\" style=\"vertical-align: top;\">"
				 + "<table cellspacing=\"0\" cellpadding=\"0\" style=\"width: 150px;\">"
				 + "<tbody>"
				 + "<tr>"
				 + "<td align=\"left\" style=\"vertical-align: top;\">"
				+ "<div class=\"gwt-Label\">"
					+ detailViewLabel["country"]+ ": "
					+"</div></td></tr></tbody></table></td>"
				+"<td align=\"left\" style=\"vertical-align: top;\">"
				+"<div class=\"gwt-Label\">"
					+"<img src=\"images/countryflags/"+FeatureTab[iMarker]["countryIso3166"]+".png\">"
					+ " " + FeatureTab[iMarker]["country"]+" ("+FeatureTab[iMarker]["countryIso3166"]+")"
					+ "</div></td></tr></tbody></table></td></div>"
					+"<div><td align\"left\" style=\"vertical-align: top;\">"
				+ "<table cellspacing=\"0\" cellpadding=\"0\">"
				 + "<tbody>"
				 + "<tr>"
				 + "<td align=\"left\" style=\"vertical-align: top;\">"
				 + "<table cellspacing=\"0\" cellpadding=\"0\" style=\"width: 150px;\">"
				 + "<tbody>"
				 + "<tr>"
				 + "<td align=\"left\" style=\"vertical-align: top;\">"
				+ "<div class=\"gwt-Label\">"
					+ detailViewLabel["featuretype"]+ ": "
					+"</div></td></tr></tbody></table></td>"
				+"<td align=\"left\" style=\"vertical-align: top;\">"
				+"<div class=\"gwt-Label\">"
					+"<div class=\"gwt-Label\">"
					+"<img src=\""+FeatureTab[iMarker]["featureClassificationPicture"]+"\">"
					+ " " + FeatureTab[iMarker]["featuretype"]
					+ "</div></td></tr></tbody></table></td></div>"
					//+textFeatureType
					+textFeatureTypeLocal
					+textFeatureID
					+textTemporalExtent
					+textLatitude
					+textLongitude
					+textAdministrator
					+textLastUpdateDate
					+textStatus
					+textGender
					+textGrammaticalNumber
					+textAlternateName
					+textPronounciation
					+textLanguage
					+textLanguageISO639
					+textAlternativeGeographicIdentifier
					+textHistoricalStartDate
					+textHistoricalEndDate
					+textIPAs
					+textLocalStatus
					+textStreetAdress
					+textPostalCode
					+textDataSource
					+textNationalUniqueId
					+textCustodian
					+textGeographicFeatureRelationText
			+"</div>"
		//  "x" icon to close the detail info window
		+"<div style=\"position: absolute; top: 3px; right: 3px;\"><a href=\"javascript:hideDetailInfo()\"><img src=\"images/icons/x.gif\" border=\"0\" title=\"Close\"></div>";
		document.getElementById("detailInfo").innerHTML = text.toString();
		//document.getElementById("map3").style.visibility = 'visible';
		document.getElementById("detailInfo").style.visibility = 'visible';
		//document.location.href="#top"; // scroll to the anchor

		// DRAW BOUNDING BOX (TOP LEFT X/Y, BOTTOM RIGHT X/Y)
		drawPolygon(FeatureTab[iMarker]["topLeftX"], FeatureTab[iMarker]["topLeftY"],
		            FeatureTab[iMarker]["bottomRightX"], FeatureTab[iMarker]["bottomRightY"]);
	});

	// ON MOUSE CLICK: ZOOM TO THE BOUNDING BOX
	GEvent.addListener(marker, "click", function() {
			var bounds = new GLatLngBounds();
			var topLeft = new GLatLng(FeatureTab[iMarker]["topLeftX"],FeatureTab[iMarker]["topLeftY"]);
			var bottomRight = new GLatLng(FeatureTab[iMarker]["bottomRightX"],FeatureTab[iMarker]["bottomRightY"]);
			bounds.extend(topLeft);
			bounds.extend(bottomRight);
			map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
	});

	// ON MOUSE OUT: REMOVE BOUNDING BOX
	GEvent.addListener(marker, "mouseout", function() {
			removePolygon();
	});
}


/***
 BOUNDING BOX
 Draw the bounding box of the selected item on top of the map.
***/
function drawPolygon(x1,y1,x2,y2){
	// GPolygon(points,  strokeColor?,  strokeWeight?,  strokeOpacity?,  fillColor?,  fillOpacity?,  opts?)
	polygon = new GPolygon([
		new GLatLng(x1, y1),
		new GLatLng(x2, y1),
		new GLatLng(x2, y2),
		new GLatLng(x1, y2)
	], "#3a8dc3", 1, .6, "#3a8dc3", .4);
	map.addOverlay(polygon);
}
function removePolygon(){
	map.removeOverlay(polygon);
}


/***
 MAP CONTROL MENUE
***/
function showMenu(){
	function TextualZoomControl() {
	}
	TextualZoomControl.prototype = new GControl();
	TextualZoomControl.prototype.initialize = function(map) {
		var container = document.createElement("div");
		var zoomInDiv = document.createElement("div");
		this.setButtonStyle_(zoomInDiv);
		container.appendChild(zoomInDiv);
		zoomInDiv.innerHTML = '<img src="images/icons/zoom-in.gif" title="Zoom In">';
		GEvent.addDomListener(zoomInDiv, "click", function() {
			map.zoomIn();
		});
		var zoomOutDiv = document.createElement("div");
		this.setButtonStyle_(zoomOutDiv);
		container.appendChild(zoomOutDiv);
		zoomOutDiv.innerHTML = '<img src="images/icons/zoom-out.gif" title="Zoom Out">';
		GEvent.addDomListener(zoomOutDiv, "click", function() {
			map.zoomOut();
		});
		var panLeftDiv = document.createElement("div");
		this.setButtonStyle_(panLeftDiv);
		container.appendChild(panLeftDiv);
		panLeftDiv.innerHTML = '<img src="images/icons/arrow-left.gif" title="Pan Left">';
		GEvent.addDomListener(panLeftDiv, "click", function() {
			map.panDirection(1, 0);
		});
		var panRightDiv = document.createElement("div");
		this.setButtonStyle_(panRightDiv);
		container.appendChild(panRightDiv);
		panRightDiv.innerHTML = '<img src="images/icons/arrow-right.gif" title="Pan Right">';
		GEvent.addDomListener(panRightDiv, "click", function() {
			map.panDirection(-1, 0);
		});
		var panUpDiv = document.createElement("div");
		this.setButtonStyle_(panUpDiv);
		container.appendChild(panUpDiv);
		panUpDiv.innerHTML = '<img src="images/icons/arrow-up.gif" title="Pan Up">';
		GEvent.addDomListener(panUpDiv, "click", function() {
			map.panDirection(0, 1);
		});
		var panDownDiv = document.createElement("div");
		this.setButtonStyle_(panDownDiv);
		container.appendChild(panDownDiv);
		panDownDiv.innerHTML = '<img src="images/icons/arrow-down.gif" title="Pan Down">';
		GEvent.addDomListener(panDownDiv, "click", function() {
			map.panDirection(0, -1);
		});
		var resetDiv = document.createElement("div");
		this.setButtonStyle_(resetDiv);
		container.appendChild(resetDiv);
		resetDiv.innerHTML = '<img src="images/icons/move.gif" title="Reset Zoom and Position">';
		GEvent.addDomListener(resetDiv, "click", function() {
			map.setCenter(new GLatLng(startLat, startLng), startZoom);
		});
		map.getContainer().appendChild(container);
		return container;
	}
	TextualZoomControl.prototype.getDefaultPosition = function() {
		return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
	}
	TextualZoomControl.prototype.setButtonStyle_ = function(button) {
		button.style.color = "#0000cc";
		button.style.backgroundColor = "white";
		button.style.padding = "2px";
		button.style.marginBottom = "3px";
		button.style.width = "16px";
		button.style.height = "16px";
		button.style.cursor = "pointer";
	}
	map.addControl(new TextualZoomControl());
}

/***
 MAP TYPE MENU
***/
function showMapTypes(){
	function mapTypeControl() {
	}
	mapTypeControl.prototype = new GControl();
	mapTypeControl.prototype.initialize = function(map) {
		var mapTypeContainer = document.createElement("div");
		this.setContainerStyle_(mapTypeContainer);

		var wmsDiv = document.createElement("div");
		if (map.getCurrentMapType() == G_SATELLITE_MAP ||
		     map.getCurrentMapType() == G_PHYSICAL_MAP ||
		    map.getCurrentMapType() == G_HYBRID_MAP ||
		    map.getCurrentMapType() == G_NORMAL_MAP){
			this.setNotActiveStyle_(wmsDiv);
		}
		else{
			this.setSelectedStyle_(wmsDiv);
		}
		mapTypeContainer.appendChild(wmsDiv);
		wmsDiv.appendChild(document.createTextNode("EuroRegionalMap"));
		GEvent.addDomListener(wmsDiv, "click", function()
		{
			map.setMapType(WMS);
			map.getContainer().removeChild(mapTypeContainer);
			showMapTypes();
		});

		var spacerDiv = document.createElement("div");
		this.setSpacerStyle_(spacerDiv);
		mapTypeContainer.appendChild(spacerDiv);
		spacerDiv.appendChild(document.createTextNode(""));

		var satDiv = document.createElement("div");
		if (map.getCurrentMapType() == WMS){
			this.setNotActiveStyle_(satDiv);
		}
		else if (map.getCurrentMapType() == G_SATELLITE_MAP ){
			this.setSelectedStyle_(satDiv);
		}
		else{
			this.setNotSelectedStyle_(satDiv);
		}
		mapTypeContainer.appendChild(satDiv);
		satDiv.appendChild(document.createTextNode("Satellite"));
		GEvent.addDomListener(satDiv, "click", function() {
			map.setMapType(G_SATELLITE_MAP);
			map.getContainer().removeChild(mapTypeContainer);
			showMapTypes();
		});

		var mapDiv = document.createElement("div");
		if (map.getCurrentMapType() == WMS){
			this.setNotActiveStyle_(mapDiv);
		}
		else if (map.getCurrentMapType() == G_NORMAL_MAP){
			this.setSelectedStyle_(mapDiv);
		}
		else{
			this.setNotSelectedStyle_(mapDiv);
		}
		mapTypeContainer.appendChild(mapDiv);
		mapDiv.appendChild(document.createTextNode("Map"));
		GEvent.addDomListener(mapDiv, "click", function() {
			map.setMapType(G_NORMAL_MAP);
			map.getContainer().removeChild(mapTypeContainer);
			showMapTypes();
		});

		var hybridDiv = document.createElement("div");
		if (map.getCurrentMapType() == WMS){
			this.setNotActiveStyle_(hybridDiv);
		}
		else if (map.getCurrentMapType() == G_HYBRID_MAP){
			this.setSelectedStyle_(hybridDiv);
		}
		else{
			this.setNotSelectedStyle_(hybridDiv);
		}
		mapTypeContainer.appendChild(hybridDiv);
		hybridDiv.appendChild(document.createTextNode("Hybrid"));
		GEvent.addDomListener(hybridDiv, "click", function() {
			map.setMapType(G_HYBRID_MAP);
			map.getContainer().removeChild(mapTypeContainer);
			showMapTypes();
		});

		var terrainDiv = document.createElement("div");
		if (map.getCurrentMapType() == WMS){
			this.setNotActiveStyle_(terrainDiv);
		}
		else if (map.getCurrentMapType() == G_PHYSICAL_MAP ){
			this.setSelectedStyle_(terrainDiv);
		}
		else{
			this.setNotSelectedStyle_(terrainDiv);
		}
		mapTypeContainer.appendChild(terrainDiv);
		terrainDiv.appendChild(document.createTextNode("Terrain"));
		GEvent.addDomListener(terrainDiv, "click", function() {
			map.setMapType(G_PHYSICAL_MAP);
			map.getContainer().removeChild(mapTypeContainer);
			showMapTypes();
		});

		/*if (map.getCurrentMapType() != WMS){
			var spacerDiv2 = document.createElement("div");
			this.setSpacerStyle_(spacerDiv2);
			mapTypeContainer.appendChild(spacerDiv2);
			spacerDiv2.appendChild(document.createTextNode(""));
		}

		/*var wmsOverlayDiv = document.createElement("div");
		if (map.getCurrentMapType() == WMS){
			this.setNotActiveStyle_(wmsOverlayDiv);
		}
		else if (map.getCurrentMapType() == SATELLITE_WMS ||
				 map.getCurrentMapType() == TERRAIN_WMS ||
				 map.getCurrentMapType() == HYBRID_WMS ||
				 map.getCurrentMapType() == MAP_WMS){
			this.setSelectedStyle_(wmsOverlayDiv);
		}
		else{
			this.setNotSelectedStyle_(wmsOverlayDiv);
		}
		// if the map type "WMS only" is selected, the option to
		// add or remove the WMS will not be shown in the menu.
		if (map.getCurrentMapType() != WMS){
			mapTypeContainer.appendChild(wmsOverlayDiv);
			if (activeWMS == false){
				wmsOverlayDiv.appendChild(document.createTextNode("Add ERM"));
				GEvent.addDomListener(wmsOverlayDiv, "click", function() {
					activateWMS(); // set activeWMS to true
				 	setMapType(); // add the WMS on top of the selected map type
				 	map.getContainer().removeChild(mapTypeContainer);
				 	showMapTypes(); // reload menue, so that the button will be updated
				});
			}
			if (activeWMS == true){
				wmsOverlayDiv.appendChild(document.createTextNode("Remove ERM"));
				GEvent.addDomListener(wmsOverlayDiv, "click", function() {
				 	deactivateWMS();
				 	setMapType();
				 	map.getContainer().removeChild(mapTypeContainer);
				 	showMapTypes();
				});
			}
		}*/
		map.getContainer().appendChild(mapTypeContainer);
		return mapTypeContainer;
	}
	mapTypeControl.prototype.getDefaultPosition = function() {
		return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(40, 7));
	}
	// The styles "setSelectedStyle" and "setNotSelectedStyle"
	// inherit properties from the "setHeaderStyle"
	mapTypeControl.prototype.setContainerStyle_ = function(container) {
		container.style.textDecoration = "none";
		container.style.color = "#000000";
		container.style.backgroundColor = "white";
		container.style.font = "8pt Trebuchet MS, Helvetica, Arial, sans-serif";
		container.style.border = "2px solid white";
		container.style.textAlign = "left";
		container.style.cursor = "pointer";
		container.style.width = "100px";
	}
	mapTypeControl.prototype.setSpacerStyle_ = function(header) {
		header.style.backgroundColor = "white";
		header.style.height = "2px";
	}
	mapTypeControl.prototype.setSelectedStyle_ = function(selected) {
		selected.style.color = "#3a8dc3";
		selected.style.backgroundColor = "white";
		selected.style.fontWeight = "bold";
		selected.style.border = "0px";
		selected.style.paddingLeft = "5px";
	    selected.style.paddingRight = "5px";
	    selected.style.paddingBottom = "5px";
		selected.style.height = "12px";
		selected.style.width = "90px";
	}
	mapTypeControl.prototype.setNotSelectedStyle_ = function(notSelected) {
		notSelected.style.color = "white";
		notSelected.style.backgroundColor = "#3a8dc3";
		notSelected.style.fontWeight = "bold";
		notSelected.style.border = "0px";
		notSelected.style.paddingLeft = "5px";
		notSelected.style.paddingRight = "5px";
		notSelected.style.paddingBottom = "5px";
		notSelected.style.height = "12px";
		notSelected.style.width = "90px";
	}
	mapTypeControl.prototype.setNotActiveStyle_ = function(notActive) {
		notActive.style.color = "#3a8dc3";
		notActive.style.backgroundColor = "#cbe5f5";
		notActive.style.border = "0px";
		notActive.style.paddingLeft = "5px";
	    notActive.style.paddingRight = "5px";
	    notActive.style.paddingBottom = "5px";
		notActive.style.height = "12px";
		notActive.style.width = "90px";
	}
	map.addControl(new mapTypeControl());
	map.addControl(new GScaleControl()) ;
}


/***
 WMS Overlay
 For more information visit http://johndeck.blogspot.com
 The script uses the javascript file "wms_gm.js"
***/
function addWMS()
{

	// COPYRIGHT INFORMATION
       	// ============================================================
      	// ====== Create a copyright entry =====
	var WMScopyright = new GCopyright(1,new GLatLngBounds(new GLatLng(-180,-90),new GLatLng(180,90) ),0, "EuroRegional Map - Copyright");

      	// ============================================================
      	// ====== Create a copyright collection =====
      	// ====== and add the copyright to it   =====
      	var WMScopyrightCollection = new GCopyrightCollection('');
	WMScopyrightCollection.addCopyright(WMScopyright);

     WMSCustomGetTileUrl=function(a,b)
	  {
		//  msg = "A.X =  "+a.x+"   A.Y =  "+a.y+"   Zoomlevel = "+b;
		//  alert (msg);
		// return "http://ims3.bkg.bund.de/test/ermegn/"+b+"/egn_"+a.x+"_"+a.y+"_"+b+".png";

		server = "http://egncs1.eurogeonames.com/ermegn/";

		//to have the light, add filament,bulb and battery
		filament=server+b;
		bulb="";
		battery="/egn_"+a.x+"_"+a.y+"_"+b+".png";

		switch(b)
		{
			case 10:
						if ((a.x >= "0") && (a.x < "400"))
							bulb="/03xx";
						if ((a.x >= "400") && (a.x < "500"))
							bulb="/04xx";
						if ((a.x >= "500") && (a.x < "600"))
							bulb="/05xx";
						if ((a.x >= "600") && (a.x < "700"))
							bulb="/06xx";
						break;
			case 11:
						if ((a.x >=  "600") && (a.x <  "700"))
							bulb="/07xx";
						if ((a.x >=  "800") && (a.x <  "900"))
							bulb="/08xx";
						if ((a.x >=  "900") && (a.x < "1000"))
							bulb="/09xx";
						if ((a.x >= "1000") && (a.x < "1100"))
							bulb="/10xx";
						if ((a.x >= "1100") && (a.x < "1200"))
							bulb="/11xx";
						if ((a.x >= "1200") && (a.x < "1300"))
							bulb="/12xx";
						break;
			case 12:
						if ((a.x >= "1600") && (a.x < "1700"))
							bulb="/16xx";
						if ((a.x >= "1700") && (a.x < "1800"))
							bulb="/17xx";
						if ((a.x >= "1800") && (a.x < "1900"))
							bulb="/18xx";
						if ((a.x >= "1900") && (a.x < "2000"))
							bulb="/19xx";
						if ((a.x >= "2000") && (a.x < "2100"))
							bulb="/20xx";
						if ((a.x >= "2100") && (a.x < "2200"))
							bulb="/21xx";
						if ((a.x >= "2200") && (a.x < "2300"))
							bulb="/22xx";
						if ((a.x >= "2300") && (a.x < "2400"))
							bulb="/23xx";
						if ((a.x >= "2400") && (a.x < "2500"))
							bulb="/24xx";
					/***		
						if ((a.x >= "2500") && (a.x < "2600"))
							bulb="/25xx";
						if ((a.x >= "2600") && (a.x < "2700"))
							bulb="/26xx";
						if ((a.x >= "2700") && (a.x < "2800"))
							bulb="/27xx";
					***/
						break;
			case 13:
						if ((a.x >= "3400") && (a.x < "3500"))
							bulb="/34xx";
						if ((a.x >= "3500") && (a.x < "3600"))
							bulb="/35xx";
						if ((a.x >= "3600") && (a.x < "3700"))
							bulb="/36xx";
						if ((a.x >= "3700") && (a.x < "3800"))
							bulb="/37xx";
						if ((a.x >= "3800") && (a.x < "3900"))
							bulb="/38xx";
						if ((a.x >= "3900") && (a.x < "4000"))
							bulb="/39xx";
						if ((a.x >= "4000") && (a.x < "4100"))
							bulb="/40xx";
						if ((a.x >= "4100") && (a.x < "4200"))
							bulb="/41xx";
						if ((a.x >= "4200") && (a.x < "4300"))
							bulb="/42xx";
						if ((a.x >= "4300") && (a.x < "4400"))
							bulb="/43xx";
						if ((a.x >= "4400") && (a.x < "4500"))
							bulb="/44xx";
						if ((a.x >= "4500") && (a.x < "4600"))
							bulb="/45xx";
						if ((a.x >= "4600") && (a.x < "4700"))
							bulb="/46xx";
						if ((a.x >= "4700") && (a.x < "4800"))
							bulb="/47xx";
						if ((a.x >= "4800") && (a.x < "4900"))
							bulb="/48xx";							
						break;
		/***
			case 14:
						if ((a.x >= "8500") && (a.x < "8600"))
							bulb="/85xx";
						if ((a.x >= "8600") && (a.x < "8700"))
							bulb="/86xx";
						if ((a.x >= "8800") && (a.x < "8900"))
							bulb="/88xx";
						if ((a.x >= "8900") && (a.x < "9000"))
							bulb="/89xx";
						if ((a.x >= "9000") && (a.x < "9100"))
							bulb="/90xx";
						if ((a.x >= "9100") && (a.x < "9200"))
							bulb="/91xx";
						if ((a.x >= "9200") && (a.x < "9300"))
							bulb="/92xx";
						if ((a.x >= "9300") && (a.x < "9400"))
							bulb="/93xx";
						if ((a.x >= "9400") && (a.x < "9500"))
							bulb="/94xx";
						break;
		***/
			default:
						bulb="";
						break;
		}

		return filament+bulb+battery;

      }
  	// ============================================================
      	// ===== Create the GTileLayer =====
      	// ===== adn apply the CustomGetTileUrl to it
      	var WMStilelayers = [new GTileLayer(WMScopyrightCollection,1,18)];

	WMStilelayers[0].getTileUrl = WMSCustomGetTileUrl;
	// Set opacity of the WMS layer (0.0 = 0% covered, 1.0 = 100% covered)
	WMStilelayers[0].getOpacity = function()
	{
		return 0.6;
	}


	//var layerMapWMS=[G_NORMAL_MAP.getTileLayers()[0],WMStilelayers[0]];
	// GMapType(layers,  projection,  name,  opts?)
	//MAP_WMS  = new GMapType(layerMapWMS, G_NORMAL_MAP.getProjection(), "WMS + Map",G_NORMAL_MAP);

	//var layerSatWMS=[G_SATELLITE_MAP.getTileLayers()[0],WMStilelayers[0]];
	//SATELLITE_WMS = new GMapType(layerSatWMS, G_SATELLITE_MAP.getProjection(), "WMS + Sat.",G_SATELLITE_MAP);

	//var layerHybridWMS=[G_HYBRID_MAP.getTileLayers()[0],WMStilelayers[0]];
	//HYBRID_WMS = new GMapType(layerHybridWMS, G_HYBRID_MAP.getProjection(), "WMS + Hyb.",G_HYBRID_MAP);

	//var layerTerWMS=[G_PHYSICAL_MAP.getTileLayers()[0],WMStilelayers[0]];
	//TERRAIN_WMS = new GMapType(layerTerWMS, G_PHYSICAL_MAP.getProjection(), "WMS + Ter.",G_PHYSICAL_MAP);

	var layerWMS=[G_PHYSICAL_MAP.getTileLayers()[0],WMStilelayers[0]];
	WMS = new GMapType(layerWMS, G_PHYSICAL_MAP.getProjection(), "ERM WMS",G_PHYSICAL_MAP);
	//var layerWMS=WMStilelayers;
	//WMS = ERM2;// new GMapType(tileWMS, G_SATELLITE_MAP.getProjection(), "ERM WMS", G_SATELLITE_MAP);
}


/***
 ACTIVATE OR DEACTIVATE WMS:
 When the user clicks on "Add WMS", the WMS will be shown on top of
 the layer which is currently selected in Google Maps.
***/
var activeWMS = false;
function deactivateWMS(){ activeWMS = false; }
function activateWMS(){ activeWMS = true; }

function setMapType(){
	// alert(map.getCurrentMapType().getName());
	if (map.getCurrentMapType() == G_NORMAL_MAP){
		map.setMapType(G_NORMAL_MAP);
		/*if (activeWMS == true){
			map.setMapType(MAP_WMS);
		}*/
	}
	else if (map.getCurrentMapType() == G_SATELLITE_MAP){
		map.setMapType(G_SATELLITE_MAP);
		/*if (activeWMS == true){
			map.setMapType(SATELLITE_WMS);
		}*/
	}
	else if (map.getCurrentMapType() == G_HYBRID_MAP){
		map.setMapType(G_HYBRID_MAP);
		/*if (activeWMS == true){
			map.setMapType(HYBRID_WMS);
		}*/
	}
	else if (map.getCurrentMapType() == G_PHYSICAL_MAP){
		map.setMapType(G_PHYSICAL_MAP);
		/*if (activeWMS == true){
			map.setMapType(TERRAIN_WMS);
		}*/
	}
	else
	{
	map.setMapType(WMS);
	}
}

function mapTypeChanged()
{
	GEvent.addListener(map, "maptypechanged", function() {
		if (map.getCurrentMapType() == G_NORMAL_MAP){
			map.setMapType(G_NORMAL_MAP);
			/*if (activeWMS == true){
				map.setMapType(MAP_WMS);
			}*/
		}
		if (map.getCurrentMapType() == G_SATELLITE_MAP){
			map.setMapType(G_SATELLITE_MAP);
			/*if (activeWMS == true){
				map.setMapType(SATELLITE_WMS);
			}*/
		}
		if (map.getCurrentMapType() == G_HYBRID_MAP){
			/*map.setMapType(G_HYBRID_MAP);
			if (activeWMS == true){
				map.setMapType(HYBRID_WMS);
			}*/
		}
		if (map.getCurrentMapType() == G_PHYSICAL_MAP){
			map.setMapType(G_PHYSICAL_MAP);
			/*if (activeWMS == true){
				map.setMapType(TERRAIN_WMS);
			}*/
		}
	});

}

var PointsTab;
var gll;

function CreatePointsTable()
{
	PointsTab = new Array();
}

function AddPointToPointsTable(latitude,longitude)
{
	gll = new GLatLng(latitude, longitude);
	PointsTab.push(gll);
}


function CreatePolygon()
{
	// GPolygon(points,  strokeColor?,  strokeWeight?,  strokeOpacity?,  fillColor?,  fillOpacity?,  opts?)
	var polygonExtent = new GPolygon(PointsTab, "#fff523", 1, .6, "#fff523", .4);
	map.addOverlay(polygonExtent);

}

function initializeWMSMap(topLeftX, topLeftY,bottomRightX,bottomRightY)
{

	var lBbox=topLeftY +","+bottomRightX+","+bottomRightY+","+topLeftX;

	var lURL=WMSBaseURL;
	lURL+="REQUEST=GetMap";
	lURL+="&SERVICE=WMS";
	lURL+="&VERSION=1.1.1";
	lURL+="&LAYERS="+WMSLayers;
	//lURL+="&STYLES="+this.myStyles;
	lURL+="&FORMAT="+WMSFormat;
	//lURL+="&BGCOLOR=0xFFFFFF";
	lURL+="&TRANSPARENT=TRUE";
	lURL+="&SRS=EPSG:4326";
	lURL+="&BBOX="+lBbox;
	lURL+="&WIDTH=250";
	lURL+="&HEIGHT=250";
	lURL+="&REASPECT=FALSE";



	var obj=document.getElementById("map2a").src = lURL;
	//alert(" url is " + lURL);



}

      // ============================================================
      // ====== Create a copyright entry =====
      var erm2copyright = new GCopyright(1,new GLatLngBounds(new GLatLng(-180,-90),new GLatLng(180,90) ),0, "EuroRegional Map - Copyright");

      // ============================================================
      // ====== Create a copyright collection =====
      // ====== and add the copyright to it   =====
      var erm2copyrightCollection = new GCopyrightCollection('');
			erm2copyrightCollection.addCopyright(erm2copyright);

     // ============================================================
      // == Write our own getTileUrl function ========
	  // Hier die URL des Webservers mit den Kacheln eintragen
	  // http://ims.bkg.bund.de/website/ermegn/
	  // oder
	  // http://ims3.bkg.bund.de/test/ermegn/
	  // Die Ordner b sind nach Zoomstufen eingeteilt, b=Zoomstufe (nur bei ims3)
      // Dateiname ist: egn_x_y_z.png, z.B. egn_132_34_8.png
     CustomGetTileUrl=function(a,b)
	  {
		//  msg = "A.X =  "+a.x+"   A.Y =  "+a.y+"   Zoomlevel = "+b;
		//  alert (msg);
		// return "http://ims3.bkg.bund.de/test/ermegn/"+b+"/egn_"+a.x+"_"+a.y+"_"+b+".png";

		server = "http://egncs1.eurogeonames.com/ermegn/";

		//to have the light, add filament,bulb and battery
		filament=server+b;
		bulb="";
		battery="/egn_"+a.x+"_"+a.y+"_"+b+".png";

		switch(b)
		{
			case 10:
						if ((a.x >= "400") && (a.x < "500"))
							bulb="/04xx";
						if ((a.x >= "500") && (a.x < "600"))
							bulb="/05xx";
						if ((a.x >= "600") && (a.x < "700"))
							bulb="/06xx";
						break;
			case 11:
						if ((a.x >=  "800") && (a.x <  "900"))
							bulb="/08xx";
						if ((a.x >=  "900") && (a.x < "1000"))
							bulb="/09xx";
						if ((a.x >= "1000") && (a.x < "1100"))
							bulb="/10xx";
						if ((a.x >= "1100") && (a.x < "1200"))
							bulb="/11xx";
						if ((a.x >= "1200") && (a.x < "1300"))
							bulb="/12xx";
						break;
			case 12:
						if ((a.x >= "1600") && (a.x < "1700"))
							bulb="/16xx";
						if ((a.x >= "1700") && (a.x < "1800"))
							bulb="/17xx";
						if ((a.x >= "1800") && (a.x < "1900"))
							bulb="/18xx";
						if ((a.x >= "1900") && (a.x < "2000"))
							bulb="/19xx";
						if ((a.x >= "2000") && (a.x < "2100"))
							bulb="/20xx";
						if ((a.x >= "2100") && (a.x < "2200"))
							bulb="/21xx";
						if ((a.x >= "2200") && (a.x < "2300"))
							bulb="/22xx";
						if ((a.x >= "2300") && (a.x < "2400"))
							bulb="/23xx";
						if ((a.x >= "2400") && (a.x < "2500"))
							bulb="/24xx";
						if ((a.x >= "2500") && (a.x < "2600"))
							bulb="/25xx";
						if ((a.x >= "2600") && (a.x < "2700"))
							bulb="/26xx";
						if ((a.x >= "2700") && (a.x < "2800"))
							bulb="/27xx";
			case 13:
						if ((a.x >= "4200") && (a.x < "4300"))
							bulb="/42xx";
						if ((a.x >= "4300") && (a.x < "4400"))
							bulb="/43xx";
						if ((a.x >= "4400") && (a.x < "4500"))
							bulb="/44xx";
						if ((a.x >= "4500") && (a.x < "4600"))
							bulb="/45xx";
						if ((a.x >= "4600") && (a.x < "4700"))
							bulb="/46xx";
						if ((a.x >= "4700") && (a.x < "4800"))
							bulb="/47xx";
						break;
			case 14:
						if ((a.x >= "8500") && (a.x < "8600"))
							bulb="/85xx";
						if ((a.x >= "8600") && (a.x < "8700"))
							bulb="/86xx";
						if ((a.x >= "8800") && (a.x < "8900"))
							bulb="/88xx";
						if ((a.x >= "8900") && (a.x < "9000"))
							bulb="/89xx";
						if ((a.x >= "9000") && (a.x < "9100"))
							bulb="/90xx";
						if ((a.x >= "9100") && (a.x < "9200"))
							bulb="/91xx";
						if ((a.x >= "9200") && (a.x < "9300"))
							bulb="/92xx";
						if ((a.x >= "9300") && (a.x < "9400"))
							bulb="/93xx";
						if ((a.x >= "9400") && (a.x < "9500"))
							bulb="/94xx";
						break;
			default:
						bulb="";
						break;
		}

		return filament+bulb+battery;

      }
  // ============================================================
      // ===== Create the GTileLayer =====
      // ===== adn apply the CustomGetTileUrl to it
      var erm2tilelayers = [new GTileLayer(erm2copyrightCollection,10,10)];
			erm2tilelayers[0].getTileUrl = CustomGetTileUrl;

      // ============================================================
      // ===== Create the GMapType =====
      // ===== and add it to the map =====
      // var custommap = new GMapType(tilelayers, new GMercatorProjection(18), "ERM");
	  var ERM2 = new GMapType(erm2tilelayers, G_NORMAL_MAP.getProjection(), "ERM2");


/***
 INITIALIZE THE SMALL MAP IN THE DETAIL VIEW
***/
function initializeSmallMap(latitude,longitude,name)
{
	map2 = new GMap2(document.getElementById("map2"));
		map2.setCenter(new GLatLng(latitude,longitude), 10, ERM2);
	//map2.setCenter(new GLatLng(latitude,longitude), 10, WMS);
	map2.enableScrollWheelZoom();
		// Set icon (flag) for the marker
	var flagIcon = new GIcon(G_DEFAULT_ICON);
	//flagIcon.image = FeatureTab[iMarker]["featureClassificationPicture"];
	flagIcon.iconSize = new GSize(16, 16);
	flagIcon.shadowSize = new GSize(0, 0); //no shadow
	flagIcon.iconAnchor = new GPoint(8, 6);
	markerOptions =
	{
		icon: flagIcon,
		title: name
	};

	// Create marker and add it to the map
	var marker = new GMarker(new GLatLng(latitude,longitude), markerOptions);
	map2.addOverlay(marker);


}



