

function centremap(type, address, retryaddress, llpoint, showthisid, showurl) 
{
	/*if (GBrowserIsCompatible()) 
	{*/
		 var mapOptions = {
			  zoom: 15,
			  center: google.maps.LatLng(0,0),
			  mapTypeId: google.maps.MapTypeId.ROADMAP
			};
		map = new google.maps.Map(document.getElementById("googlemap"), mapOptions);
		bounds = new google.maps.LatLngBounds();
		if(type != 'small') {
			//map.addControl(new GSmallMapControl());
			//map.addControl(new GMapTypeControl());
			
			map.bounds_changed = function() {
				if (markersArray) 
				{
					for (i in markersArray) 
					{
						markersArray[i].setMap(null);
					}
				}
				if(map.getZoom() < 9){
					alert('U bent te ver uitgezoomd, zoom in voor meer gegevens.');
				}
				else {
					placevisible(type, map.getBounds(), showthisid);
				}
			}
		}
		else {
			document.getElementById("googlemap").onclick = function() {
				//document.location.href = 'articlemap.asp?id=' + showthisid;	
				document.location.href = showurl; //'/map/' + showthisid;					
			}
			map.draggable = false;
			/*map.disableInfoWindow();
			map.disableDoubleClickZoom();
			map.disableContinuousZoom();*/
			map.scrollwheel = false;
		}
		geocoder = new google.maps.Geocoder();
				
		if (llpoint == false)
		{
			
			geocoder.geocode({ 'address': address }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
				centremap(type, false, false, results[0].geometry.location, showthisid, showurl);
			}
            else if(retryaddress != false)
				centremap(type, retryaddress, false, false, showthisid, showurl);
          	});

			
			/*
			geocoder.geocode
			(
				address, 
				function(point) 
				{
					if (!point) 
					{
						if(retryaddress != false)
						{
							/*try again only with the retry* /
							centremap(type, retryaddress, false, false, showthisid, showurl);
						}
						else
						{
							/*place not found text* /
							document.getElementById("googlemap").style.display = 'none';
							document.getElementById("googlemapnotfound").style.display = 'block';
						}
					} 
					else 
					{
						if(retryaddress == false)
						{
							/*retry address, zoom out* /
							map.zoom = 13;
						}
						else
						{
							/*orginal address, zoom in* /
							if(type != 'small') {map.zoom = 15;} else {map.zoom = 14;}
						}	 
						map.setCenter(point); //G_HYBRID_MAP
						bounds.extend(point);
						google.maps.event.addListener(map, "moveend", function() {
							map.clearOverlays();
							if(map.getZoom() < 9){
								alert('U bent te ver uitgezoomd, zoom in voor meer gegevens.');
							}
							else {
								placevisible(type, map.getBounds(), showthisid);
							}
						});
						placevisible(type, map.getBounds(), showthisid); 
					}
				}
			);
			*/
		}
		else
		{
			var point = llpoint;
			if(type != 'small' && type != 'walk') {
				map.zoom = 15;
			} 
			else {
				map.zoom = 14;
			}
			map.setCenter(point);
			bounds.extend(point);
			
			if(type != 'walk') {
				google.maps.event.addListener(map, "moveend", function() {
					map.clearOverlays();
					if(map.getZoom() < 9){
						alert('U bent te ver uitgezoomd, zoom in voor meer gegevens.');
					}
					else {
						placevisible(type, map.getBounds(), showthisid);
					}
				});
				placevisible(type, map.getBounds(), showthisid);
			}
		}
	/*}*/
}



function makeroute() {
	var startpoint = markersArray[0];
	var endpoint = markersArray[markersArray.length-1];
	var routediv = document.getElementById('makeroutediv');
	var routedivtable = document.createElement('table');
	routedivtable.border = 1;
	var routedivtbody = document.createElement('tbody');
	var waypts = [];
	
	directionsService = new google.maps.DirectionsService();
	directionsDisplay = new google.maps.DirectionsRenderer({suppressMarkers: true});
	directionsDisplay.setMap(map);
	
	routedivtr = document.createElement('tr');
	routedivtd = document.createElement('td');
	routedivtd.innerHTML = '1.'
	routedivtr.appendChild(routedivtd);
	routedivtd = document.createElement('td');
	routedivtd.innerHTML = startpoint.omtitle
	routedivtr.appendChild(routedivtd);
	routedivtd = document.createElement('td');
	routedivtr.appendChild(routedivtd);
	routedivtd = document.createElement('td');
	routedivtd.innerHTML = '<a href=\'javascript:sortmarkers(0, 1);\'>down</a>';
	routedivtr.appendChild(routedivtd);
	routedivtbody.appendChild(routedivtr);
	routedivtable.appendChild(routedivtbody);
	routediv.appendChild(routedivtable);
	
	if(markersArray.length > 2) {
		for (var i = 1; i < markersArray.length-1; i++) {
			/*
			routedivli = document.createElement('li');
			routedivli.innerHTML = markersArray[i].omtitle + ' - <a href=\'javascript:sortmarkers('+i+',-1);\'>up</a>' + ' <a href=\'javascript:sortmarkers('+i+',1);\'>down</a>';
			routedivol.appendChild(routedivli);
	*/
			waypts.push({
            	location:markersArray[i].position,
            	stopover:true});
		}
	}
	/*
	routedivli = document.createElement('li');
	routedivli.innerHTML = endpoint.omtitle + ' - <a href=\'javascript:sortmarkers('+(markersArray.length-1)+',-1);\'>up</a>';
	routedivol.appendChild(routedivli);
	routediv.appendChild(routedivol);
	*/
	var request = {
        origin: startpoint.position, 
        destination: endpoint.position,
        waypoints: waypts,
        optimizeWaypoints: true,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
	
	directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
        var route = response.routes[0];
        /*var summaryPanel = document.getElementById("directions_panel");
        summaryPanel.innerHTML = "";
		*/
        // For each route, display summary information.
        for (var i = 0; i < route.legs.length; i++) {
          /*var routeSegment = i + 1;
          summaryPanel.innerHTML += "<b>Route Segment: " + routeSegment + "</b><br />";
          summaryPanel.innerHTML += route.legs[i].start_address + " to ";
          summaryPanel.innerHTML += route.legs[i].end_address + "<br />";
          summaryPanel.innerHTML += route.legs[i].distance.text + "<br /><br />";*/
		  
			routedivtr = document.createElement('tr');
			routedivtd = document.createElement('td');
			routedivtd.innerHTML = (i+2) + '.'
			routedivtr.appendChild(routedivtd);
			routedivtd = document.createElement('td');
			routedivtd.innerHTML = markersArray[i+1].omtitle
			routedivtr.appendChild(routedivtd);
			routedivtd = document.createElement('td');
			routedivtd.innerHTML = '<a href=\'javascript:sortmarkers('+(i+1)+', -1);\'>up</a>';
			routedivtr.appendChild(routedivtd);
			routedivtd = document.createElement('td');
			if(i+1 < route.legs.length) {
				routedivtd.innerHTML = '<a href=\'javascript:sortmarkers('+(i+1)+', 1);\'>down</a>';
			}
			routedivtr.appendChild(routedivtd);
			routedivtd = document.createElement('td');
			routedivtd.innerHTML = route.legs[i].start_address;
			routedivtr.appendChild(routedivtd);
			routedivtd = document.createElement('td');
			routedivtd.innerHTML = route.legs[i].end_address;
			routedivtr.appendChild(routedivtd);
			routedivtd = document.createElement('td');
			routedivtd.innerHTML = route.legs[i].distance.text;
			routedivtr.appendChild(routedivtd);
			routedivtbody.appendChild(routedivtr);
		  
        }
      }
    });
	
}

function CanvasProjectionOverlay() {}
CanvasProjectionOverlay.prototype = new google.maps.OverlayView();
CanvasProjectionOverlay.prototype.constructor = CanvasProjectionOverlay;
CanvasProjectionOverlay.prototype.onAdd = function(){};
CanvasProjectionOverlay.prototype.draw = function(){};
CanvasProjectionOverlay.prototype.onRemove = function(){};


function placemap() 
{
	 var mapOptions = {
          zoom: 15,
          center: google.maps.LatLng(0,0),
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
	
	
	map = new google.maps.Map(document.getElementById("googlemap"), mapOptions);
	canvasProjectionOverlay = new CanvasProjectionOverlay();
	canvasProjectionOverlay.setMap(map);
	//map.addControl(new GSmallMapControl());
	//map.addControl(new GMapTypeControl());
	geocoder = new google.maps.Geocoder();
	
	bounds = new google.maps.LatLngBounds();
}
function placemaprender() 
{
	//map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds), G_NORMAL_MAP);
	map.setCenter(bounds.getCenter());
	map.fitBounds(bounds); 
}


function placevisible(type, bounds, showthisid)
{
	//alert(bounds);
	google.maps.event.addListenerOnce(map, 'bounds_changed', function(){
		bounds = this.getBounds();
		loadXML('/searchmap.asp?type=' + type + '&bounds=' + bounds + '&showthisid=' + showthisid);
	});


}

function addmap(type, showthisid, iconurl, llpoint, title, text, img, objclass, showurl) 
{
	icon = new google.maps.MarkerImage();
	icon.url = iconurl //"images/i/map_museum.png";
	//icon.shadow = "";
	icon.size = new google.maps.Size(20, 34);
	/*
	if(bounds != null)
	{
		bounds.extend(llpoint);
	}
	*/
	//var point = llpoint;
	//map.setCenter(point, 15, G_HYBRID_MAP);
	var marker = new google.maps.Marker({position: llpoint, map: map, icon: icon});
	marker.omtitle = title;
	markersArray.push(marker);
	bounds.extend(llpoint);
	//alert(marker);
	//map.addOverlay(marker);
	
	if(type!='small'){
		google.maps.event.addListener(marker, "click", function() {
			//alert('click: ' + title);
			if(currentmarker != null)
			{
				closebox();	
			}
													 
			map.draggable = false;
			map.setCenter(llpoint);
			
			var obj = document.createElement('div');
			obj.id = 'infobox';
			obj.className = objclass;
			
			var h3 = document.createElement('h3');
			h3.innerHTML = title;
			obj.appendChild(h3);
			var p = document.createElement('p');
			if(img != null) {
				p.innerHTML = '<img src="'+img+'" alt="" title="" width="100">' + text + ' <a class="close" href="javascript: void(0);" onclick="return closebox();"><span>[sluiten]</span></a>';
			}
			else {
				p.innerHTML = text + ' <a class="close" href="javascript: void(0);" onclick="return closebox();"><span>[sluiten]</span></a>';
			}
			obj.appendChild(p);
			
			document.getElementById('googlemap').appendChild(obj);
			//alert(canvasProjectionOverlay.getProjection().fromLatLngToContainerPixel(marker.position.lat()));
			//obj.style.marginTop = (canvasProjectionOverlay.getProjection().fromLatLngToContainerPixel(marker.position.lat()) - 145) + 'px';
			//obj.style.marginLeft = (canvasProjectionOverlay.getProjection().fromLatLngToContainerPixel(marker.position.lng()) - 5) + 'px';
			
			obj.style.marginTop = '100px';
			obj.style.marginLeft = '475px';
			
			currentmarker = marker;
		});
	}
	else {
		google.maps.event.addListener(marker, "click", function() {
			//document.location.href = 'articlemap.asp?id=' + showthisid;	
			document.location.href = showurl; //'/map/' + showthisid;	
		});
	}
}
function closebox()
{
	var obj = document.getElementById('infobox');
	obj.parentNode.removeChild(obj);
	currentmarker=null;
	map.draggable = true;
	return false;
}
function loadXML(page)
{
	//alert(page);
	var req = null; 
	if(window.XMLHttpRequest)
	{
		req = new XMLHttpRequest(); 
	}
	else if (window.ActiveXObject)
	{
		req  = new ActiveXObject("Microsoft.XMLHTTP"); 
	}
	req.onreadystatechange = function()
	{ 
		if(req.readyState == 4)
		{
			if(req.status == 200)
			{
				//goed
				//eval(followfunction);
				//alert(req.responseText);
				eval(req.responseText);
			}
			else
			{
				//fout
				alert(req.status + " " + req.statusText);
			}
		} 
	}; 
	req.open("GET", page, true); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	req.send(null); 
}
function init()
{
	// ===== menu's =========================================================
	var menu = document.getElementById('mainmenu');
	var lis = menu.childNodes;
	
	for(var i=0;i<lis.length;i++)
	{
		if(lis[i].getElementsByTagName) 
		{
			var submenu = lis[i].getElementsByTagName('ul')[0];
			if(submenu)
			{
				lis[i].onmouseover = function()
				{
					var thesubm = this.getElementsByTagName('ul')[0];
					if(thesubm)
					{
						thesubm.style.left = (findPos(this)[0]) + 'px';
						thesubm.style.display = 'block';
					}
				}
				lis[i].onmouseout = function()
				{
					var thesubm = this.getElementsByTagName('ul')[0];
					if(thesubm)
					{
						thesubm.style.display = 'none';
					}
				}
			}
		}
	}
	
	var pictviewer = document.getElementById('pictviewer');
	if(pictviewer) {/*
		var newimg = pictviewer.getElementsByTagName('img')[0]
		newimg.onmouseover = new Function("showlic('"+newimg.className+"')");
		newimg.onmouseout = new Function("hidelic()")
	
		var license = document.getElementById('license');
		license.onmouseover = new Function("showlic('"+newimg.className+"')");
		license.onmouseout = new Function("hidelic()")*/
		
		var newimg = pictviewer.getElementsByTagName('img')[0].width;
		if(pictviewer.getElementsByTagName('span')[0]) {
			pictviewer.getElementsByTagName('span')[0].style.width = newimg + 'px';
		}
		
		var allas = pictviewer.getElementsByTagName('a');
		var curi = 0;
		for(var i=0;i<allas.length;i++)
		{
			if(allas[i].className != 'cc') {
				var src = allas[i].getElementsByTagName('img')[0].src;
				var cls = allas[i].getElementsByTagName('img')[0].className;
				src = src.substr(src.lastIndexOf('/')+1);
				
				allas[i].onclick = new Function("showimg('"+src+"','"+cls+"',"+curi+");return false;");
				curi = curi + 1;
			}
		}
	}
	// ===== menu's =========================================================
	
	// ===== europe map =====================================================
	var europemap = document.getElementById('map');
	if(europemap == null) { europemap = document.getElementById('countrymap') }
	var maptitle = document.getElementById('maptitle');
	
	if(europemap != null && maptitle != null) {
		var areas = europemap.getElementsByTagName('area');
		for(var i=0;i<areas.length;i++) {
			areas[i].onmouseover = new Function("document.getElementById('maptitle').innerHTML = '&nbsp;-&nbsp;' + this.title;");
			areas[i].onmouseout = new Function("document.getElementById('maptitle').innerHTML = '';");
		}
	}
	// ===== europe map =====================================================
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}


function showimg(imgsrc, imgcls, curi)
{
	var pictviewer = document.getElementById('pictviewer');
	var alllis = pictviewer.getElementsByTagName('li');
	var firstli = alllis[0];
	var firstimg = firstli.getElementsByTagName('img')[0];
	var firstp = firstli.getElementsByTagName('p')[0];
	
	//alert(curi);
	
	curi = curi + 1;
	for(var i=0;i<alllis.length;i++)
	{
		if(alllis[i].className == 'active')
		{
			alllis[i].className = '';
		}
	}
	pictviewer.getElementsByTagName('li')[curi].className = 'active';
	var oldp = pictviewer.getElementsByTagName('li')[curi].getElementsByTagName('p')[0];
	if(oldp != null) {
		var newp = document.createElement('p');
		newp.className = 'license';
		newp.innerHTML = oldp.innerHTML;
	}
	
	var newimg = document.createElement('img');
	newimg.src = 'http://www.oorlogsmusea.nl/upload/' + imgsrc;
	if(imgcls.length > 0) {
		newimg.className = imgcls;
		//newimg.onmouseover = new Function("showlic('"+imgcls+"')");
		//newimg.onmouseout = new Function("hidelic()")
	}
	firstli.removeChild(firstimg);
	if(firstp != null) { firstli.removeChild(firstp); }
	firstli.appendChild(newimg);
	if(newp != null) {
		newp.getElementsByTagName('span')[0].style.width = newimg.width + 'px';
		firstli.appendChild(newp);
	}
	else {
		newimg.className = 'nocc';
		//newimg.style.borderBottom = '1px solid #D2D2D2';
		//newimg.style.paddingBottom = '5px';
	}
	/*
	var license = document.getElementById('license');
	license.onmouseover = new Function("showlic('"+imgcls+"')");
	license.onmouseout = new Function("hidelic()")*/
}
/*
function showlic(imgcls) {
	var pictviewer = document.getElementById('pictviewer');
	var alllis = pictviewer.getElementsByTagName('li');
	var firstli = alllis[0];
	var firstimg = firstli.getElementsByTagName('img')[0];
	
	var license = document.getElementById('license');
	var newlimg = license.getElementsByTagName('a')[0];
	if(newlimg) {
		license.removeChild(newlimg);
	}
	
	
	if(imgcls.length > 0) {
	
		var newa = document.createElement('a');
		newa.href='http://www.stiwot.nl';
		newa.target='_blank';
		license.appendChild(newa);
		
		var newlimg = document.createElement('img');
		newlimg.src = 'http://www.oorlogsmusea.nl/images/i/' + imgcls + '.png';
		newlimg.className = imgcls;
		newa.appendChild(newlimg);
		license.style.display = 'block';
		license.style.left= (findPos(firstimg)[0] + 5) + 'px';
		license.style.top= (findPos(firstimg)[1] + 5) + 'px';
	}
	else {
		license.style.display = 'none';	
	}
}

function hidelic() {
	var license = document.getElementById('license');
	license.style.display = 'none';	
	
}*/

function checkfeedback()
{
	var name = document.getElementById('a1').value;
	var email = document.getElementById('a2').value;
	var message = document.getElementById('a3').value;
	
	
	if(name == '' || email == '' || message == '') {
		alert('Alle velden zijn verplicht.');	
		return false;
	}
	else if(email.indexOf('@') == -1 || email.indexOf('.') == -1) {
		alert('Een geldig e-mailadres is verplicht.');	
		return false;
	}
	
	return true;
}
function checkcontact()
{
	var name = document.getElementById('naam').value;
	var email = document.getElementById('email').value;
	var subject = document.getElementById('onderwerp').value;
	var message = document.getElementById('bericht').value;
	
	
	if(name == '' || email == '' || message == '' || subject == '') {
		alert('Alle velden zijn verplicht.');	
		return false;
	}
	else if(email.indexOf('@') == -1 || email.indexOf('.') == -1) {
		alert('Een geldig e-mailadres is verplicht.');	
		return false;
	}
	
	return true;
}

function timediff(starttime)
{
	return ((new Date().getTime()-starttime)/1000).toString().replace('.',',');
}
function getdis_v2(id, here, there) {
	/*
	var gDirections = new GDirections(null, null);
	
	google.maps.event.addListener(gDirections, 'load', function(){ // Listnener fordirections on load
		var res = gDirections.getDistance().meters / 1000;
		res = res.toFixed(1);
		res = res.toString();
		if(res.indexOf('.') == -1) {
			res = res + '.0';
		}
		
		res = res + ' km';
		document.getElementById(id).innerHTML = res;
	});
	
	gDirections.loadFromWaypoints([here,there], {getSteps:true})
  
		*/
		
	var res = google.maps.geometry.spherical.computeDistanceBetween(here, there) / 1000;
	res = res.toFixed(1);
	res = res.toString();
	if(res.indexOf('.') == -1) {
		res = res + '.0';
	}
	
	res = res + ' km';
	document.getElementById(id).innerHTML = res;
}
function doem(thisid, t1,t2)
{
	thisid.href = 'mailto: ' + t1 + '@' + t2;
}

function doprint()
{
	window.print();
	return false;
}

var gbl_helptitle = '';
function dohelp(elm) 
{
	elm.onclick = function() { return false; }
	elm.onmouseout = function() { exithelp(elm); }
	gbl_helptitle = elm.title;
	elm.title = '';
	var dh_popup = document.createElement('div');
	dh_popup.id = 'popup';
	dh_title = document.createElement('h3');
	dh_title.innerHTML = 'Oorlogsmusea.nl - Help';
	dh_popup.appendChild(dh_title);
	dh_text = document.createElement('p');
	dh_text.innerHTML = gbl_helptitle;
	dh_popup.appendChild(dh_text);
	dh_popup.style.left = (findPos(elm)[0] - 300) + 'px'
	dh_popup.style.top = (findPos(elm)[1] - 20) + 'px'
	document.getElementsByTagName('body')[0].appendChild(dh_popup);
}
function exithelp(elm) 
{
	elm.title = gbl_helptitle;
	document.getElementsByTagName('body')[0].removeChild(document.getElementById('popup'));
}


function checkfields()
{
	var pass=true;
	var arguments=checkfields.arguments;
	if (arguments.length > 0)
	{
		for (i=0; i < arguments.length; i++)
		{
			if (document.getElementById(arguments[i]).value=="" && pass==true)
			{
				pass=false;
			}
		}
	}
	if (pass==false)
	{
		alert("U heeft niet alle velden gevuld!");
		return false;
	}
	else
	{
		return true;
	}
}

function searchcheck() {
	var sfld = document.getElementById('searchtxt');
	if(sfld.value.length == 0) {
		alert('Een zoekterm is verplicht.');
		return false;
	}
	else {
		return true;	
	}
}


function popupimage(pagina,tekst) {
	popupclose();
  	var div = document.createElement('div');
	div.id = 'picturepopup';
	div.onclick = function() { popupclose() };
	var img = document.createElement('img');
	img.src = pagina;
	img.alt = tekst;
	img.title = tekst;
	div.appendChild(img);
	window.location.hash="top";
	document.getElementsByTagName('body')[0].appendChild(div);
  
}    
function popupclose() {
var div = document.getElementById('picturepopup');
if(div) {
	document.getElementsByTagName('body')[0].removeChild(div);
}
}

//set global vars
var markersArray = [];
var map = null;
var canvasProjectionOverlay = null;
var geocoder = null;
var currentmarker = null;
var bounds = null;
var directionsDisplay = null;  
var directionsService = null;
//set init

window.onload = init;
