window.onload = function() { initialize(); };
window.onunload = function() { GUnload(); };

var map = null; //kaart
var bounds = null;
var geocoder = null; //object voor geocoding
var activetab = '';
var ple = null; //polyline encoder
var baseurl = 'http://www.doehits.nl/';
var iconurl = baseurl + 'mapicons/';

function initialize() {
	if (GBrowserIsCompatible()) {
		geocoder = new GClientGeocoder();
		geocoder.setBaseCountryCode('nl');
		map = new GMap2(document.getElementById("bigmap"));
		map.enableContinuousZoom();
		map.enableScrollWheelZoom();
		map.setCenter(new GLatLng(52.0, 5.0), 8 );
		map.addMapType(G_PHYSICAL_MAP);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setMapType(G_HYBRID_MAP);
		bounds = new GLatLngBounds();
		ple = new PolylineEncoder();
		GEvent.addListener(map, "click", mapClick);
		//laad de punten op de kaart
		var oid = document.getElementById("oid").value;
		var ostyle = document.getElementById("ostyle").value;
		if (oid) {
			GDownloadUrl('xhr.php?act=showobject&id='+oid+'&ostyle='+ostyle, function(data, responseCode) {
				eval(data);
				zoomlevel = map.getBoundsZoomLevel(bounds);
				if (zoomlevel > 16) zoomlevel = 16;
				map.setCenter(bounds.getCenter(), zoomlevel);
			});
		}
	}
	else {
		alert('Jouw browser is niet geschikt om te gebruiken voor het tonen van deze objecten!');
	}
} 


function mapClick(overlay, latlng) {
	if (overlay) {
		//iets met overlay
		if (overlay instanceof GMarker) { 
			if (overlay.url) window.location = overlay.url;
		} 
		else if (overlay instanceof GPolygon) { }
		else if (overlay instanceof GPolyline) { 
			// op een lijn geklikt, 
		} 
	}
	else if (!overlay && latlng) {
		//geklikt op een een punt ergens op de kaart
	}
}

function addPoint(latlng, title, url, icon) {
	var markericon = new GIcon(); //icon voor marker gebaseerd op type
	markericon.image = iconurl+icon;
	markericon.iconSize = new GSize(24,24);
	markericon.iconAnchor = new GPoint(12,24);
				
	locationmarker = new GMarker(latlng, {icon: markericon, title: title});
	locationmarker.url = url;

	map.addOverlay(locationmarker);
	bounds.extend(latlng); //vergroot area op basis van locatie icons
}

function addStart(latlng) {
	var starticon = new GIcon();
	starticon.image = iconurl+'routestart.png';
	starticon.iconSize = new GSize(16,16);
	starticon.iconAnchor = new GPoint(8,8);
	startmarker = new GMarker(latlng, {icon: starticon, title: 'Startpunt route'});
	map.addOverlay(startmarker);
}

function addStop(latlng) {
	var stopicon = new GIcon();
	stopicon.image = iconurl+'routestop.png';
	stopicon.iconSize = new GSize(16,16);
	stopicon.iconAnchor = new GPoint(8,8);
	stopmarker = new GMarker(latlng, {icon: stopicon, title: 'Eindpunt route'});
	map.addOverlay(stopmarker);
}

function addRoute(polyline) {
	addPolyline(polyline);
	//addStart(polyline.getVertex(0));
	//addStop(polyline.getVertex(polyline.getVertexCount()-1));
	//bounds = polyline.getBounds();
}

function addPolyline(polyline) {
	map.addOverlay(polyline);
	var linebounds = polyline.getBounds();
	bounds.extend(linebounds.getSouthWest());
	bounds.extend(linebounds.getNorthEast());
}
