var maininfowindow = new google.maps.InfoWindow();
var map;
var geocoder;
var bounds;
var center;
var directionsService;
var routes = [];
var globalIcon;

function initMap()
{
	center = new google.maps.LatLng(51.2289, 5.4316);
	var options = {
		center: center,
		zoom: 13,
		mapTypeId: google.maps.MapTypeId.ROADMAP						 
	};
	map = new google.maps.Map(document.getElementById('map_canvas'), options );
	bounds = new google.maps.LatLngBounds();
}

function setIcon( iconUrl, iconWidth, iconHeight )
{
	globalIcon = new google.maps.MarkerImage( 
		iconUrl,
		new google.maps.Size(iconWidth, iconHeight),
		new google.maps.Point(0, 0),
		new google.maps.Point(16, 32)
	);
}

function addMarker( gCoordinate, content )
{
	bounds.extend( gCoordinate );
	if ( globalIcon )
	{
		var marker = new google.maps.Marker( { position:gCoordinate, map:map, icon:globalIcon } );
	}
	else
	{
		var marker = new google.maps.Marker( { position:gCoordinate, map:map } );
	}
	
	addMessage( marker, content );
}

function addIconMarker( gCoordinate, content, iconUrl, iconWidth, iconHeight )
{
	bounds.extend( gCoordinate );
	var icon = new google.maps.MarkerImage( 
		iconUrl,
		new google.maps.Size(iconWidth, iconHeight),
		new google.maps.Point(9, 12)
	);
	
	var marker = new google.maps.Marker( { position:gCoordinate, map:map, icon:icon } );
	addMessage( marker, content );
}

function addPolygon( coords, content )
{
	if ( !coords || coords.length < 1 ) {
		return false;
	}
	
	var gCoords = [];
	
	var tempBounds = new google.maps.LatLngBounds();
	for ( var j=0; j<coords.length; j++ ) {
		var gCoordinate = new google.maps.LatLng( coords[j][0], coords[j][1] );
		gCoords.push( gCoordinate );
		bounds.extend( gCoordinate );
		tempBounds.extend( gCoordinate );
	}
  
	polygon = new google.maps.Polygon({
		paths: gCoords,
		fillOpacity: 0.2,
		strokeWeight: 1,
		strokeColor: '#000000',
		fillColor: '#2222FF',
		map: map
	});
	
	if ( globalIcon )
	{
		icon = globalIcon;
	}
	else
	{	
		var icon = new google.maps.MarkerImage(
			'http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png',
			new google.maps.Size(35, 35)
		);
	}
	var marker = new google.maps.Marker({
		position: tempBounds.getCenter(), 
        map: map,
        icon: icon
    });
	tempBounds = null;
      
    addMessage( marker, content );
}

function addRoute( coords, content )
{
	if ( !coords || coords.length < 1 ) {
		return false;
	}
	
	if ( !directionsService )
	{
		directionsService = new google.maps.DirectionsService();
	}
	var color = get_random_color();
	var directionsDisplay = new google.maps.DirectionsRenderer( {
		polylineOptions: {strokeColor: color}, 
		preserveViewport: true,
		suppressInfoWindows: true,
		suppressMarkers: true,
		map: map
	} );
	
	var routeCoordinates = [];
	var routeCoordinatesWaypoints = [];
	for( var x = 0; x < coords.length; x++ )
	{
		var latLng = new google.maps.LatLng( coords[x][0], coords[x][1] );
		bounds.extend( latLng );
		routeCoordinates.push( latLng );
		if ( x != 0 && x != coords.length-1 )
		{
			routeCoordinatesWaypoints.push( {location: latLng, stopover:false} );
		}
	}
	if ( routeCoordinates.length > 0 )
	{
		var start = routeCoordinates.shift();
	    var end = routeCoordinates.pop();
	    var request = {
	        origin:start, 
	        destination:end,
	        waypoints: routeCoordinatesWaypoints,
	        travelMode: google.maps.DirectionsTravelMode.DRIVING
	    };
	    
	    directionsService.route(request, function(response, status) {
		    if ( status == google.maps.DirectionsStatus.OK )
		    {
		    	var myRoute = response.routes[0].legs[0];
		    	directionsDisplay.setDirections( response );
		    	
		    	if ( globalIcon )
		    	{
		    		var icon = globalIcon;
		    	}
		    	else
		    	{
			    	var icon = new google.maps.MarkerImage(
		        		'http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png',
		        		new google.maps.Size(35, 35)
		        	);
		    	}
		    	var marker = new google.maps.Marker({
		    		position: myRoute.steps[0].start_point, 
			        map: map,
			        icon: icon
			    });
			      
			    addMessage( marker, content ); 
		    }
	    });
	}
}

function showAddress( address )
{
	var geocoder = new google.maps.Geocoder();
	geocoder.geocode( 
		{ address: address },
	    function(results, status) {
	      	if (status == google.maps.GeocoderStatus.OK ) {
	        	var greenIcon = new google.maps.MarkerImage(
	        		'http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png',
	        		new google.maps.Size(35, 35)
	        	);
				
	        	var marker = new google.maps.Marker( { 
	        		position: results[0].geometry.location, 
	        		map: map, 
	        		icon:greenIcon 
	        	} );
	        	
	        	var content = results[0].formatted_address;
	        	addMessage( marker, content );
	        	maininfowindow.setContent( content );
	    		maininfowindow.open(map, marker);
	      	} else {
	      		alert('Straat niet gevonden');
	      	}
	    }
	);
}

function addMessage( marker, content )
{
	var infowindow = new google.maps.InfoWindow({
		content: content
	});
	
	google.maps.event.addListener(marker, 'click', function( event ) {
		maininfowindow.setContent( infowindow.getContent() );
		maininfowindow.open(map, marker);
	});
}

function addPolygonMessage( polygon, content )
{
	var infowindow = new google.maps.InfoWindow({
		content: content
	});
	
	google.maps.event.addListener(polygon, 'click', function( event ) {
		maininfowindow.setPosition(event.latLng);
		maininfowindow.setContent( infowindow.getContent() );
		maininfowindow.open(map);
	});
}

function fitBounds()
{
	if ( !bounds.isEmpty() )
	{
		bounds.extend( center );
		map.fitBounds( bounds );
	}
}

function get_random_color() 
{
    var r = function () { return Math.floor(Math.random()*256) };
    return "rgb(" + r() + "," + r() + "," + r() + ")";
}
