var Locations = new Array()
var LocDesc = new Array()
var LocIcon = new Array()
var MarkerType1 = new GIcon(false, "custom/design/maplogoaabc.png")
	MarkerType1.iconAnchor = new GPoint(14, 19)
	MarkerType1.iconSize = new GSize(30, 21);
	MarkerType1.shadowSize = new GSize(50, 21);
	MarkerType1.shadow = "custom/design/maplogoaabc_s.png"

var MarkerType2 = new GIcon(false, "custom/design/maplogoaabc_2.png")
	MarkerType2.iconAnchor = new GPoint(16, 31)
	MarkerType2.iconSize = new GSize(34, 32);

//Station nr. 1
Locations[0] = new GLatLng(57.042018, 9.917665)
LocDesc[0] = "Banegården - Busterminalen<br>Station nr. 1<br>16 Pladser"
LocIcon[0] = 1

//Station nr. 2
Locations[1] = new GLatLng(57.045076, 9.923811)
LocDesc[1] = "Frederikstorv<br>Station nr. 2<br>6 Pladser"
LocIcon[1] = 1

//Station nr. 3
Locations[2] = new GLatLng(57.048097, 9.920486)
LocDesc[2] = "Gammeltorv<br>Station nr. 3<br>8 Pladser"
LocIcon[2] = 1

//Station nr. 4
Locations[3] = new GLatLng(57.054418, 9.899463)
LocDesc[3] = "Haraldslund<br>Station nr. 4<br>8 Pladser"
LocIcon[3] = 1	

//Station nr. 5
Locations[4] = new GLatLng(57.051256, 9.920045)
LocDesc[4] = "Havnefronten - Jomfru Ane Parken<br>Station nr. 5<br>8 Pladser"
LocIcon[4] = 1

//Station nr. 6
Locations[5] = new GLatLng(57.043059, 9.930573)
LocDesc[5] = "Karolinelund<br>Station nr. 6<br>6 Pladser"
LocIcon[5] = 1

//Station nr. 7
Locations[6] = new GLatLng(57.056928, 9.903875)
LocDesc[6] = "Lystbådehavnen<br>Station nr. 7<br>8 Pladser"
LocIcon[6] = 1

//Station nr. 8
Locations[7] = new GLatLng(57.042660, 9.907243)
LocDesc[7] = "Kunsten<br>Station nr. 8<br>6 Pladser"
LocIcon[7] = 1

//Station nr. 9
Locations[8] = new GLatLng(57.0462126, 9.9331734)
LocDesc[8] = "Kjellerups Torv<br>Station nr. 9<br>8 Pladser"
LocIcon[8] = 1

//Station nr. 10
Locations[9] = new GLatLng(57.048178, 9.923016)
LocDesc[9] = "Nytorv<br>Station nr. 10<br>20 Pladser"
LocIcon[9] = 1

//Station nr. 11
Locations[10] = new GLatLng(57.059984, 9.918764)
LocDesc[10] = "Vestergade Nørresundby<br>Station nr. 11<br>6 Pladser"
LocIcon[10] = 1

//Station nr. 12
Locations[11] = new GLatLng(57.049667, 9.926705)
LocDesc[11] = "Utzon Centeret<br>Station nr. 12<br>6 Pladser"
LocIcon[11] = 1

//Station nr. 13
Locations[12] = new GLatLng(57.052922, 9.908823)
LocDesc[12] = "Vestbyens Station<br>Station nr. 13<br>6 Pladser"
LocIcon[12] = 1

//Station nr. 14
Locations[13] = new GLatLng(57.0479433, 9.9177882)
LocDesc[13] = "Algade v. Budolfi Plads<br>Station nr. 14<br>16 Pladser"
LocIcon[13] = 1

//Station nr. 15
Locations[14] = new GLatLng(57.038499, 9.900143)
LocDesc[14] = "Aalborg Zoo<br>Station nr. 15<br>6 Pladser"
LocIcon[14] = 1

//Station nr. 16
Locations[15] = new GLatLng(57.043905, 9.912310)
LocDesc[15] = "Aalborg Hallen<br>Station nr. 16<br>6 Pladser"
LocIcon[15] = 1

//Station nr. 17
Locations[16] = new GLatLng(57.057686, 9.922680)
LocDesc[16] = "Nørresundby Torv<br>Station nr. 17<br>8 Pladser"
LocIcon[16] = 1

//Station nr. 18
Locations[17] = new GLatLng(57.027363, 9.945184)
LocDesc[17] = "AAU - Sohngårdsholmsvej<br>Station nr. 18<br>8 Pladser"
LocIcon[17] = 1

//Station nr. 19
Locations[18] = new GLatLng(57.016168, 9.977531)
LocDesc[18] = "AU - Fibigerstræde<br>Station nr. 19<br>10 Pladser"
LocIcon[18] = 1

//Station nr. 20
Locations[19] = new GLatLng(57.047649, 9.926147)
LocDesc[19] = "Friis<br>Station nr. 20<br>6 Pladser"
LocIcon[19] = 1

//Station nr. 21
Locations[20] = new GLatLng(57.053364, 9.911765)
LocDesc[20] = "Strandvejen <br>Station nr. 21<br>6 Pladser"
LocIcon[20] = 1


var myGoogleMap
var myMapEventListener
function loadMap(little) {
	if (GBrowserIsCompatible() && !little) {
		myGoogleMap = new GMap2(document.getElementById("MapsDiv"));
		myGoogleMap.addControl(new GLargeMapControl());
		myGoogleMap.addControl(new GMapTypeControl());
		myGoogleMap.enableScrollWheelZoom();
		myGoogleMap.setCenter(new GLatLng(57.038499, 9.930573), 13);
		setLocations()
		document.body.onmousemove = function (event) { captureMousePos(event) }
	} else if (GBrowserIsCompatible() && little) {
		myGoogleMap = new GMap2(document.getElementById("MapsDiv"));
		myGoogleMap.setMapType(G_HYBRID_MAP);
		myGoogleMap.addControl(new GLargeMapControl());
		myGoogleMap.addControl(new GMapTypeControl());
		myGoogleMap.enableScrollWheelZoom();
		myGoogleMap.setCenter(new GLatLng(57.038499, 9.930573), 12);

		if (location.href.indexOf('lat=') > 0) {
			//alert(getValFromQuery('lat'))
			//alert(getValFromQuery('lng'))
			var latlng = new GLatLng(getValFromQuery('lat'), getValFromQuery('lng'))

			var marker = new GMarker(latlng, { draggable: false, icon: MarkerType2 })
			myGoogleMap.addOverlay(marker);

			myGoogleMap.setZoom(17)
			myGoogleMap.panTo(latlng)

		} else {
			myMapEventListener = GEvent.addListener(myGoogleMap, "click", function (overlay, latlng) {
				addMarkerAndZoom(latlng);
			});
		}

	}
}

function setLocations() {
	for (var intI = 0; intI < Locations.length; intI++) {
		myGoogleMap.addOverlay(createMarker(Locations[intI], intI))
	}
}

function createMarker(point, index) {
	var marker
	if (LocIcon[index] == 1)
		marker = new GMarker(point, MarkerType1)
	else if (LocIcon[index] == 2)
		marker = new GMarker(point, MarkerType2)

  /*GEvent.addListener(marker, "click", function() {
    mapGotoPage(index);
  });*/
  GEvent.addListener(marker, "click", function() {
		panmapWithZoom(index);
  });
  GEvent.addListener(marker, "mouseover", function() {
		mapDisplayTitle(index);
  });
  GEvent.addListener(marker, "mouseout", function() {
	  mapHideTitle(index);
  });
  return marker;
}

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 mapGotoPage(index) {
	if (LocPage[index]) {
		location.href = LocPage[index]
	}
}

function panmapWithZoom(index) {
	if (index > -1) {
		myGoogleMap.setZoom(17)
		myGoogleMap.panTo(Locations[index])
	}
}

function mapDisplayTitle(index) {
	if (LocDesc[index] && document.getElementById("MapsTitle")) {
		//document.getElementById("MapsTitle").innerHTML = LocDesc[index]
		document.getElementById("MapsTitle").innerHTML = "<b>" + LocDesc[index] + "</b><br><br>Klik på ikon for at zoome ind."

		document.getElementById("MapsTitle").style.left = MousePosX + 20
		if (mi)
			document.getElementById("MapsTitle").style.top = MousePosY + document.body.scrollTop + 1
		else
			document.getElementById("MapsTitle").style.top = MousePosY
		document.getElementById("MapsTitle").style.visibility = "visible"
	}
}

function mapHideTitle(index) {
	if (document.getElementById("MapsTitle")) {
		document.getElementById("MapsTitle").style.visibility = "hidden"
	}
}

//**** bortkomne cykler ***

function addMarkerAndZoom(latlng) {
	if (latlng) {
		GEvent.removeListener(myMapEventListener);

		myGoogleMap.setZoom(17)
		myGoogleMap.panTo(latlng)

		updateForm(latlng)

		var marker = new GMarker(latlng, { draggable: true, icon: MarkerType2 })

		GEvent.addListener(marker, "dragend", function (latlng) {
			updateForm(latlng);
		});
		GEvent.addListener(marker, "dragstart", function () {
			hideDescription();
		});

		myGoogleMap.addOverlay(marker);
		showDescription()
	}
}

function showDescription() {
	if (document.getElementById("MapsTitle")) {
		var mapPos = getElementPosition(document.getElementById("MapsDiv"))

		document.getElementById("MapsTitle").innerHTML = "<b>Placering</b><br />Er dette ikke den helt korrekte placering, kan du flytte cyklen ved at trække i den med musen."
		document.getElementById("MapsTitle").style.left = mapPos.left + 155
		document.getElementById("MapsTitle").style.top = mapPos.top + 120
		document.getElementById("MapsTitle").style.visibility = "visible"
	}
}

function hideDescription() {
	if (document.getElementById("MapsTitle")) {
		document.getElementById("MapsTitle").style.visibility = "hidden"
	}
}

function updateForm(latlng) {
	if (document.getElementById('meddelelse')) {
		if (document.getElementById('meddelelse').value == '')
			document.getElementById('meddelelse').value = 'Jeg har den ' + new Date().getDate() + '. ' + getMonth(new Date()) + ' ' + new Date().getFullYear() + ' set en bycykel på følgende placering\n\nMvh\n';
	}
	if (document.getElementById('placering')) {
		document.getElementById('placering').value = 'http://www.aalborgbycyklen.dk/default.aspx?m=2&i=40&lat=' + latlng.lat() + '&lng=' + latlng.lng();
		document.getElementById('placering').readOnly = true;
	}
}

function getValFromQuery(val) {
	var retVal = ''
	var curLoc = location.href
	var symbol = "&"
	if (curLoc.indexOf(symbol) == -1) { symbol = "?" }
	var pos1 = curLoc.indexOf(symbol + val + '=')
	if (pos1 > 0) {
		curLoc = curLoc.substring(pos1)
		var pos2 = curLoc.indexOf(symbol, 1)
		if (pos2 > 0)
			curLoc = curLoc.substring(0, pos2)
		retVal = curLoc.replace(symbol + val + '=', '')
	}
	return retVal
}

function getMonth(dt) {
	var retVal = ''
	switch (dt.getMonth()) {
		case 0:
			retVal = 'januar'
			break;
		case 1:
			retVal = 'februar'
			break;
		case 2:
			retVal = 'marts'
			break;
		case 3:
			retVal = 'april'
			break;
		case 4:
			retVal = 'maj'
			break;
		case 5:
			retVal = 'juni'
			break;
		case 6:
			retVal = 'juli'
			break;
		case 7:
			retVal = 'august'
			break;
		case 8:
			retVal = 'september'
			break;
		case 9:
			retVal = 'oktober'
			break;
		case 10:
			retVal = 'november'
			break;
		case 11:
			retVal = 'december'
			break;
	}
	return retVal
}

function getElementPosition(offsetTrail) {
	var offsetLeft = 0;
	var offsetTop = 0;
	while (offsetTrail) {
		if (typeof (offsetTrail.offsetLeft) != 'undefined' && offsetTrail.tagName != 'TR' && offsetTrail.tagName != 'SPAN') {
			offsetLeft += offsetTrail.offsetLeft;
			offsetTop += offsetTrail.offsetTop;
		}
		offsetTrail = offsetTrail.offsetParent;
	}
	return { left: offsetLeft, top: offsetTop };
}

