
function changein(id) {
        if (document.layers) document.layers[id].visibility = "show"
        else if (document.getElementById) document.getElementById(id).style.visibility = "visible"
        else if (document.all && !(document.getElementById)) document.all(id).style.visibility = "visible"
}

function changeout(id) {
        if (document.layers) document.layers[id].visibility = "hide"
        else if (document.getElementById) document.getElementById(id).style.visibility = "hidden"
        else if (document.all && !(document.getElementById)) document.all(id).style.visibility = "hidden"
}


function nothing(){
}

function retarget()
{	var linkobj = document.links;
	var hnaam = document.location.hostname;
	for (i=0; i < linkobj.length;)
	{	if(linkobj[i].hostname.indexOf(hnaam))
		{	linkobj[i].target ="_blank";
		}
		i++;
	}
}


var alertNL="U moet eerst een zoekopdracht invullen!";

function checkform ()
{
  if (document.forms[0].SearchQuery.value == "") {
alert(alertNL);
    document.forms[0].SearchQuery.focus();
    return false ;
  }
  return true ;
}






function createXmlHttpRequest() {
 try {
   if (typeof ActiveXObject != 'undefined') {
     return new ActiveXObject('Microsoft.XMLHTTP');
   } else if (window["XMLHttpRequest"]) {
     return new XMLHttpRequest();
   }
 } catch (e) {
   changeStatus(e);
 }
 return null;
};

function downloadUrl(url, callback) {
 var status = -1;
 var request = createXmlHttpRequest();
 if (!request) {
   return false;
 }

 request.onreadystatechange = function() {
   if (request.readyState == 4) {
     try {
       status = request.status;
     } catch (e) {
       // Usually indicates request timed out in FF.
     }
     if ((status == 200) || (status == 0)) {
       callback(request.responseText, request.status);
       request.onreadystatechange = function() {};
     }
   }
 }
 request.open('GET', url, true);
 try {
   request.send(null);
 } catch (e) {
   changeStatus(e);
 }
};

function xmlParse(str) {
  if (typeof ActiveXObject != 'undefined' && typeof GetObject != 'undefined') {
    var doc = new ActiveXObject('Microsoft.XMLDOM');
    doc.loadXML(str);
    return doc;
  }

  if (typeof DOMParser != 'undefined') {
    return (new DOMParser()).parseFromString(str, 'text/xml');
  }

  return createElement('div', null);
}

/**
 * Appends a JavaScript file to the page.
 * @param {string} url
 */
function downloadScript(url) {
  var script = document.createElement('script');
  script.src = url;
  document.body.appendChild(script);
}
 


var gmarkers = [] ;
var map = null;
var infowindow = new google.maps.InfoWindow({size: new google.maps.Size(400,50)});

 
//A function to create the marker and set up the event window function 
function createMarker(latlng, name, html, open) {

	var contentString = unescape(html) ;
	var marker = new google.maps.Marker({
						position: latlng,
						visible: true,
						map: map,
						content: contentString,
						zIndex: Math.round(latlng.lat()*-100000)<<5
			});

	google.maps.event.addListener(marker, 'click', function() {
					infowindow.setContent(contentString); 
					infowindow.open(map,marker);
			});
	gmarkers.push(marker) ;

	if (open == true) {
		infowindow.setContent(contentString); 
		infowindow.open(map, marker);
	}
	
	return marker ;
}

//This function picks up the click and opens the corresponding info window
function myclick(i) {
	google.maps.event.trigger(gmarkers[i], "click");
}

function initialize(regio, address, lat, lng) {
	//console.debug("initialize google maps:" + address + " lat:" + lat + " lng:" + lng );
 	if (lat == '') {
		var geocoder = new google.maps.Geocoder();
		//console.debug("get address primary: "+ unescape(address));
		geocoder.geocode( {'address' : unescape(address) }, function(results, status) {
						if (status == google.maps.GeocoderStatus.OK) {
							buildMap(regio, results[0].geometry.location.lat(), results[0].geometry.location.lng()) ;
						} ;
					});
	} else {
		buildMap(regio, lat, lng) ;
	}
} ;

function buildMap(regio, ctrLat, ctrLng) {
	//console.debug("build map:" + regio + " lat:" + ctrLat + " lng:" + ctrLng );
	var myOptions = {
			zoom: 7,				
			center: new google.maps.LatLng(ctrLat, ctrLng),
			mapTypeControl: true,
			navigationControl: true,
			mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

	google.maps.event.addListener(map, 'click', function() {infowindow.close() ; }) ;

	downloadUrl("google-maplocations?Openpage&regio=" + regio , function(doc) {
			var xmlDoc = xmlParse(doc) ;
			var markers = xmlDoc.documentElement.getElementsByTagName("marker") ;
			for (var i = 0; i < markers.length; i++) {
				if (markers[i].getAttribute("lat") ==  '') {
					if (markers[i].getAttribute("address") != '') {
						var searchAndPlace = function(m, e) { 
							var geocoder = new google.maps.Geocoder() ;
							//console.debug("get address: "+ unescape(markers[e].getAttribute("address")));
							geocoder.geocode( {'address': unescape(markers[e].getAttribute("address")) }, function(results, status) {
								if (status == google.maps.GeocoderStatus.OK) {
									var lat = results[0].geometry.location.lat() ;
									var lng = results[0].geometry.location.lng() ;
									var point = new google.maps.LatLng(lat, lng) ;
									var html = m[e].getAttribute("html") ;
									var label = m[e].getAttribute("label") ;		
									//console.debug("create marker:" + label + " lat:" + lat + " lng:" + lng );													
									if (lat != '' | lng != '') { createMarker(point, label, html, ((ctrLat == lat) & (ctrLng == lng))) ; }
								} else {
									//console.debug("nie gevonden")
								}
							}) ;	
						} ;
						searchAndPlace(markers, i) ;						
					}
				} else {
					var lat = parseFloat(markers[i].getAttribute("lat")) ;
					var lng = parseFloat(markers[i].getAttribute("lng")) ;
					var point = new google.maps.LatLng(lat,lng) ;
					var html = markers[i].getAttribute("html") ;
					var label = markers[i].getAttribute("label") ;
					createMarker(point, label, html, ((ctrLat == lat) & (ctrLng == lng))) ;
				}	
				
			}
			  
	}) ;
	
}



