// Global Map variables
var defZoom = 13

function DetectBrowser() {
	var a=navigator.userAgent.toLowerCase()
	this.isOpera=(a.indexOf("opera")!=-1)
	this.isKonq=(a.indexOf('konqueror')!=-1)
	this.isSafari=(a.indexOf('safari')!=-1)&&(a.indexOf('mac')!=-1)
	this.isKhtml=this.isSafari || this.isKonq || (a.indexOf('khtml')!=-1)
	this.isFF=(a.indexOf('firefox')!=-1)
	this.isIE=(a.indexOf("msie")!=-1)&&!this.isOpera
	this.isWinIE=this.isIE&&(a.indexOf("win")!=-1)
	this.isWinIE7=this.isWinIE && (a.indexOf("msie 7")!=-1)
	this.isWinIEVista=this.isWinIE && (a.indexOf("windows nt 6")!=-1)
	this.isCSS1Compat=(!this.isIE)||(document.compatMode&&document.compatMode=="CSS1Compat")
}

var browserDetect = new DetectBrowser()

function initMap(mapHolderID, latitude, longitude, zoomlevel, maptype, calculateRoute) {
	if (GBrowserIsCompatible()) {
		GUnload()
		// create and init map
		gMap = new GMap2(document.getElementById(mapHolderID))
		// create gecoder
		gGeocoder = new GClientGeocoder()
		// add location to map
		showCoordsOnMap(latitude, longitude, zoomlevel, maptype)
		// add map controls
		addMapControls(calculateRoute)
		// add map events
		addMapEvents()
		// save init position
		gMap.savePosition()
	}
}

function addMapControls(calculateRoute) {
	if (!calculateRoute) calculateRoute = false
	if (calculateRoute) {
		gMap.addControl(new GSmallMapControl())
		gMap.addControl(new GMapTypeControl())
		gMap.addControl(new GOverviewMapControl())
		gMap.addControl(new GScaleControl())
		gMap.enableDoubleClickZoom()
		gMap.enableContinuousZoom()
	} else {
		gMap.addControl(new GSmallZoomControl())
		gMap.enableDoubleClickZoom()
		gMap.enableContinuousZoom()
	}
}

function addMapEvents() {
	// add zoom event handler
	//GEvent.addListener(gMap, "zoomend", zoomHandler)
}

function showPointOnMap(point, marker, zoomlevel, maptype) {
	if (!point) {
		//alert("Adres niet gevonden")
		showAddressOnMap('Netherlands')
	} else {
		gMap.setCenter(point, defZoom)				
		if (maptype == 0 || maptype == 1 || maptype == 2) {
			var aMapTypes = gMap.getMapTypes()
			 gMap.setMapType(aMapTypes[maptype])
		}
		gMap.addOverlay(marker)
		if (zoomlevel > 0) gMap.setZoom(zoomlevel)
	}
}

function showCoordsOnMap(latitude, longitude, zoomlevel, maptype) {
	var point = new GLatLng(latitude, longitude)
	var marker = new GMarker(point)
	showPointOnMap(point, marker, zoomlevel, maptype)
}

function initDirections(){
	// init directions
	gDirectionsPanel = document.getElementById("gmdirections")
	gDirections = new GDirections(gMap, gDirectionsPanel)
}

function showDirectionsOnMap(from, to) {
	// show directions
	gDirections.load("from: " + from + " to: " + to)
}

function calculateDirections(from, to_latitude, to_longitude) {
	var to = to_latitude + "," + to_longitude
	var coords_to = new GLatLng(parseFloat(to_latitude), parseFloat(to_longitude))
	
	gGeocoder.getLatLng(from, function(point) {
		if (!point) {
			alert(from + " niet gevonden.")
		} else {
			var from_latitude = point.lat()
			var from_longitude = point.lng()
			var from = from_latitude + "," + from_longitude
			// show directions
			showDirectionsOnMap(from, to)
		}
	})
}

