var Locations = new Array()
var LocAddr = new Array()
var LocIcon = new Array()
var LocDates = new Array()
var MarkerType1 = new GIcon(false, "custom/design/maplogo_r_ms.png")
	MarkerType1.iconAnchor = new GPoint(14, 8)
	MarkerType1.iconSize = new GSize(30, 21);
	//MarkerType1.shadowSize = new GSize(30, 21);
	//MarkerType1.shadow = "custom/design/maplogo_r_s.png"
var MarkerType2 = new GIcon(false, "custom/design/maplogo_genb.png")
	MarkerType2.iconAnchor = new GPoint(14, 10)
	MarkerType2.iconSize = new GSize(30, 21);

var omr_aalborg = new GLatLng(57.032435373818195, 9.95361328125) // zoom = 11
var omr_nordvest = new GLatLng(57.13281071631751, 9.98931884765625)
var omr_nordoest = new GLatLng(57.03878683961362, 10.245437622070312)
var omr_sydvest = new GLatLng(56.991685466818254, 9.628143310546875)
var omr_sydoest = new GLatLng(56.95276318493485, 10.119781494140625)
var omr_helekommunen = new GLatLng(57.09775927054997, 9.86846923828125) // zoom = 9

var thisTemplateCentre = 0

var myGoogleMap
var geocoder
function loadMap() {
	if (GBrowserIsCompatible()) {
		myGoogleMap = new GMap2(document.getElementById("DIVmap"));
		myGoogleMap.addControl(new GLargeMapControl());
		myGoogleMap.addControl(new GMapTypeControl());
		myGoogleMap.enableScrollWheelZoom()

		if (document.getElementById("dynMapRapskrDiv")) {
			myGoogleMap.setCenter(new GLatLng(57.032435373818195, 9.95361328125), 11);
		} else if (document.getElementById("dynMapPladserDiv")) {
			myGoogleMap.setCenter(new GLatLng(57.04108161930166, 9.99933984375), 9);
		} else if (document.getElementById("dynMapCentreDiv")) {
			myGoogleMap.setCenter(Locations[thisTemplateCentre], 12);
		}
		//myGoogleMap.setMapType(G_SATELLITE_TYPE);
		
		// Create new geocoding object
		geocoder = new GClientGeocoder();
		
		setLocations()
		document.body.onmousemove = function(event) {captureMousePos(event)}

	}
}

function setLocations() {
	for (var intI=0; intI < Locations.length; intI++) {
		myGoogleMap.addOverlay(createMarker(Locations[intI], intI))
	}
}

function createMarker(point, index) {
  // Set up our GMarkerOptions object
	var marker
	if (LocIcon[index] == 1)
		marker = new GMarker(point, MarkerType1)
	else if (LocIcon[index] == 2)
		marker = new GMarker(point, MarkerType2)

  GEvent.addListener(marker, "mouseover", function() {
    showLocMessage(index);
  });
  GEvent.addListener(marker, "mouseout", function() {
    hideLocMessage();
  });
  GEvent.addListener(marker, "click", function() {
    panmapWithZoom(index);
  });
	return marker;
}

function panmapToArea(omr,zoom) {
	myGoogleMap.setZoom(zoom)
	myGoogleMap.panTo(omr)
}

function panmapWithZoom(index) {
	if (document.getElementById("dynMapRapskrDiv") || document.getElementById("dynMapCentreDiv")) {
		myGoogleMap.setZoom(15)
		myGoogleMap.panTo(Locations[index])

	} else if (document.getElementById("dynMapPladserDiv")) {

		myGoogleMap.setZoom(15)
		var thisIndex = index
		if (index.length > 2) { //if index is a string
			for (var intI = 0; intI < LocAddr.length; intI++) {
				if (LocAddr[intI].indexOf(index) > -1) { thisIndex = intI }
			}
		} else { //is numeric, also show PladsDiv
			showGenbPlads(LocDates[index])
		}
		myGoogleMap.panTo(Locations[thisIndex])
	} 
}

var MousePosX = 0
var MousePosY = 0
function captureMousePos(e) {
	if (!mi) {
		MousePosX = e.pageX
		MousePosY = e.pageY
	} else {
		MousePosX = event.clientX
		MousePosY = event.clientY
	}
}

function showLocMessage(index) {
	if (document.getElementById("dynMapRapskrDiv")) {
		var datoer = LocDates[index].split(",")
		var strDatoer = '<table cellspacing="0" cellpadding="0"><tr><td colspan="2" class="rsHead">Forår 08</td><td colspan="2" class="rsHead">Efterår 08</td><td colspan="2" class="rsHead">Vinter 09</td></tr>'
		strDatoer += '<tr><td class="rsRow">' + datoer[0] + '</td><td class="rsRow">' + datoer[1] + '</td><td class="rsRow">' + datoer[2] + '</td><td class="rsRow">' + datoer[3] + '</td><td class="rsRow">' + datoer[4] + '</td><td class="rsRow">' + datoer[5] + '</td></tr>'
		strDatoer += '<tr><td class="rsRow">' + datoer[6] + '</td><td class="rsRow">' + datoer[7] + '</td><td class="rsRow">' + datoer[8] + '</td><td class="rsRow">' + datoer[9] + '</td><td class="rsRow">' + datoer[10] + '</td><td class="rsRow">' + datoer[11] + '</td></tr>'
		strDatoer += '<tr><td class="rsRow">' + datoer[12] + '</td><td class="rsRow">' + datoer[13] + '</td><td class="rsRow">' + datoer[14] + '</td><td class="rsRow">' + datoer[15] + '</td><td class="rsRow">' + datoer[16] + '</td><td class="rsRow">' + datoer[17] + '</td></tr>'
		strDatoer += '<tr><td class="rsRow">' + datoer[18] + '</td><td class="rsRow">' + datoer[19] + '</td><td class="rsRow">' + datoer[20] + '</td><td class="rsRow">' + datoer[21] + '</td><td class="rsRow">' + datoer[22] + '</td><td class="rsRow">' + datoer[23] + '</td></tr></table>'
		//alert(strDatoer)
		document.getElementById("dynMapRapskrDiv").innerHTML = LocAddr[index] + "<br><br>" + strDatoer + "<br>Klik for at zoome ind."
		
		/*alert(e.pageX)
		if (e != null) {
			if (mi) {
				document.getElementById("dynMapRapskrDiv").style.left = event.clientX + 30
				document.getElementById("dynMapRapskrDiv").style.top = event.clientY + document.body.scrollTop + 1
			} else {
				document.getElementById("dynMapRapskrDiv").style.left = e.pageX + 30
				document.getElementById("dynMapRapskrDiv").style.top = e.pageY
			}
			document.getElementById("dynMapRapskrDiv").style.visibility = "visible"
		}*/
		document.getElementById("dynMapRapskrDiv").style.left = MousePosX + 30
		if (mi)
	 		document.getElementById("dynMapRapskrDiv").style.top = MousePosY + document.body.scrollTop + 1
		else
			document.getElementById("dynMapRapskrDiv").style.top = MousePosY
		document.getElementById("dynMapRapskrDiv").style.visibility = "visible"
	
	} else if (document.getElementById("dynMapPladserDiv") || document.getElementById("dynMapCentreDiv")) {
		var tmpMapDiv = document.getElementById("dynMapPladserDiv") ? document.getElementById("dynMapPladserDiv") : document.getElementById("dynMapCentreDiv")
		tmpMapDiv.innerHTML = "<b>" + LocAddr[index] + "</b><br><br>Klik for at zoome ind."
		tmpMapDiv.style.left = MousePosX + 30
		if (mi)
	 		tmpMapDiv.style.top = MousePosY + document.body.scrollTop + 1
		else
			tmpMapDiv.style.top = MousePosY
		tmpMapDiv.style.visibility = "visible"
	}
}

function hideLocMessage() {
	if (document.getElementById("dynMapRapskrDiv")) {
		document.getElementById("dynMapRapskrDiv").style.visibility = "hidden"
	} else if (document.getElementById("dynMapPladserDiv")) {
		document.getElementById("dynMapPladserDiv").style.visibility = "hidden"
	} else if (document.getElementById("dynMapCentreDiv")) {
		document.getElementById("dynMapCentreDiv").style.visibility = "hidden"
	}
}

function zoommap() {
	if (document.getElementById("bynavn").value != "") {
		// Retrieve location information, pass it to addToMap()
		geocoder.getLocations(document.getElementById("bynavn").value + ",Nordjylland Denmark", addToMap);
	}
}

function addToMap(response) {
	if (response.Placemark) {
		// Retrieve the object
		var place = response.Placemark[0];
		// Retrieve the latitude and longitude
		var newpoint = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		// Center the map on this point
		myGoogleMap.setCenter(newpoint, 14);
	} else {
		alert("Din søgning gav ingen resultat!")
	}
}

function getMapCenter() {
	var bounds = myGoogleMap.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	alert(((northEast.y+southWest.y)/2) + ', ' + ((northEast.x+southWest.x)/2) + ', zoom = ' + myGoogleMap.getZoom())
}
