var map = null;
var geocoder = null;
var host;

var com_map;

function initialize() {
	
	com_map = document.getElementById('com_map');	
	
  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map_canvas"));
	//map.setCenter(new GLatLng(37.4419, -122.1419), 13);
	geocoder = new GClientGeocoder();
  }

  var target = com_map.target.value;
  
  var zoom = com_map.zoom.value;
  
  var info = com_map.info.value;
 
    var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(5,25));

 	map.addControl(new GMapTypeControl(), bottomRight);
	
	var bottomLeft = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(10,40));

map.addControl(new GSmallMapControl(), bottomLeft);

    
  
  showAddress(target, zoom, info);
  
}

function showAddress(address, zoom, info) {
	
	if(!zoom) {
			
		zoom = 10;	
			
	}
	
if(!info) {
	info = 'Wij zijn te vinden aan de '+address;
}

info = '<div class="mapinfo">'+info+'</div>';
	
  if (geocoder) {
	geocoder.getLatLng(
	  address,
	  function(point) {
		if (!point) {
		  alert(address + " not found");
		} else {
		  map.setCenter(point, parseInt(zoom));
		  var marker = new GMarker(point);
		  map.addOverlay(marker);
		  marker.openInfoWindowHtml(info);
		}
	  }
	);
  }
}

function getRoute() {
		
	//alert(com_map.target.value);	
	//alert(com_map.custom.value);	
		
	var target = com_map.target.value;
	var custom = com_map.custom.value;
	
	var xlocation;
	
	if(custom) {
		
		xlocation = custom;
		
	} else {
		
		if(radio = com_map.location) {
		
			for(i=0;i<radio.length;i++) {
					
				if(radio[i].checked) {
					
					xlocation = radio[i].value;
					
					break;
					
				}
					
			}
			
		}
		
	}
	
	if(xlocation) {

		directionsPanel = document.getElementById("route");
		directions = new GDirections(map, directionsPanel);
		
		GEvent.addListener(directions, "error", handleErrors);
	
		node = directionsPanel;
		
		if(node.hasChildNodes()) {
			
			while(node.childNodes.length >= 1 ) {
			
				node.removeChild(node.firstChild);
			
			}
		
		}
		
		directions.load("from: "+xlocation+", The Netherlands to: "+target);
	
	}

}

function handleErrors(){
  
  alert("Er is geen routbeschrijving gevonden vanaf de opgegeven locatie");
   
}

