var directionDisplay;
var directionsService;
var map;
var geocoder;
var query = "Maastricht";

function ZoomIn(controlDiv, map) {

	// Set CSS styles for the DIV containing the control
	// Setting padding to 5 px will offset the control
	// from the edge of the map
	controlDiv.style.paddingLeft = '30px';
	controlDiv.style.paddingTop = '10px';

	// Set CSS for the control border
	var controlUI = document.createElement('DIV');
	controlUI.style.backgroundColor = 'white';
	controlUI.style.borderStyle = 'solid';
	controlUI.style.borderWidth = '2px';
	controlUI.style.cursor = 'pointer';
	controlUI.style.textAlign = 'center';
	controlUI.title = 'Zoom in';
	controlDiv.appendChild(controlUI);

	// Set CSS for the control interior
	var controlText = document.createElement('DIV');
	controlText.style.fontFamily = 'Courier';
	controlText.style.fontWeight = 'bold';
	controlText.style.fontSize = '12px';
	controlText.style.paddingLeft = '4px';
	controlText.style.paddingRight = '4px';
	controlText.innerHTML = '+';
	controlUI.appendChild(controlText);

	// Setup the click event listeners: simply set the map to
	// Chicago
	google.maps.event.addDomListener(controlUI, 'click', function() {
		map.setZoom(map.getZoom() + 1);

	});
}
function ZoomOut(controlDiv, map) {

	// Set CSS styles for the DIV containing the control
	// Setting padding to 5 px will offset the control
	// from the edge of the map
	controlDiv.style.paddingLeft = '30px';
	controlDiv.style.paddingTop = '5px';


	// Set CSS for the control border
	var controlUI = document.createElement('DIV');
	controlUI.style.backgroundColor = 'white';
	controlUI.style.borderStyle = 'solid';
	controlUI.style.borderWidth = '2px';
	controlUI.style.cursor = 'pointer';
	controlUI.style.textAlign = 'center';
	controlUI.title = 'Zoom out';
	controlDiv.appendChild(controlUI);

	// Set CSS for the control interior
	var controlText = document.createElement('DIV');
	controlText.style.fontFamily = 'Courier';
	controlText.style.fontWeight = 'bold';
	controlText.style.fontSize = '16px';
	controlText.style.paddingLeft = '4px';
	controlText.style.paddingRight = '4px';
	controlText.innerHTML = '-';
	controlUI.appendChild(controlText);

	// Setup the click event listeners: simply set the map to
	// Chicago
	google.maps.event.addDomListener(controlUI, 'click', function() {
		map.setZoom(map.getZoom() - 1);

	});
}


function initialize(bezoekAdres) {



	geocoder = new google.maps.Geocoder();

	directionsDisplay = new google.maps.DirectionsRenderer();
	var myOptions = {
		zoom: 7,
		navigationControl: false,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	directionsDisplay.setMap(map);
	directionsDisplay.setPanel(document.getElementById("directionsPanel"));


	if (!(bezoekAdres=='@vestiging.js@')) query = bezoekAdres;
	codeAddress();
	var homeControlDiv = document.createElement('DIV');
	var homeControl = new ZoomIn(homeControlDiv, map);

	homeControlDiv.index = 1;
	map.controls[google.maps.ControlPosition.TOP_LEFT].push(homeControlDiv);

	var homeControlDiv2 = document.createElement('DIV');
	var homeControl2 = new ZoomOut(homeControlDiv2, map);
	homeControlDiv2.index = 2;
	map.controls[google.maps.ControlPosition.LEFT].push(homeControlDiv2);
	google.maps.event.trigger(this.map, 'resize');
	this.map.setZoom(this.map.getZoom() - 1);
	this.map.setZoom(this.map.getZoom() + 1);

	//setTimeout(function() { google.maps.event.trigger(map, 'resize'); }, 10);

}

function calcRoute() {

	var start = $('#gm_adres').val() + ' ' + $('#gm_woonplaats').val() + ' ' + $('#gm_postcode').val();
	var end = $('#gm_vestiging').val();
	var test = new google.maps.LatLng(5.4750567, 52.4440975);
	var latlng = new google.maps.LatLng(5.4750567, 51.4440975);

	
	var request = {
	origin: start,
		destination: end,
		travelMode: google.maps.DirectionsTravelMode.DRIVING
	};

	directionsService = new google.maps.DirectionsService();
	directionsService.route(request, function(response, status) {
		if (status == google.maps.DirectionsStatus.OK) {
			directionsDisplay.setDirections(response);
			$('.noDirections .text').hide();
		}
	});
	var directionsLink = 'http://maps.google.nl/maps?f=d&source=s_d&saddr=' + start.replace(/ /g, ', ') + '&daddr=' + end + '&hl=' + taal + '&pw=2';
	$('#directionsPanel .print').attr('href', directionsLink);
	
}

function codeAddress() {
	var address = query;
	if (geocoder) {
		geocoder.geocode({ 'address': address }, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				map.setCenter(results[0].geometry.location);
				var marker = new google.maps.Marker({
					map: map,
					position: results[0].geometry.location
				});
			} else {
				alert("Geocode was not successful for the following reason: " + status);
			}
		});
	}
}
