var CircleMarker;
var Circle;

function CircleDisplay( LatLng ) {
    // Add a Circle Marker to the map.
	CircleMarker = CreateMarkerFromPoint( LatLng, 'CircleCenter' );
	
	// Add a Circle overlay to the map.
    Circle = new google.maps.Circle({
    	map: Map,
    	radius: 50000, // in meters 
    	strokeColor: '#aecc5f',
    	strokeWeight: 3,
    	fillColor: "#aecc5f",
    	fillOpacity: 0.3    	  
    });        
    // Since Circle and Marker both extend MVCObject, you can bind them
    // together using MVCObject's bindTo() method.  Here, we're binding
    // the Circle's center to the Marker's position.
    // http://code.google.com/apis/maps/documentation/v3/reference.html#MVCObject
    Circle.bindTo('center', CircleMarker, 'position');	
}

function CircleAddEvents() {
    google.maps.event.addListener(CircleMarker, 'dragend', function(event) {
    	ChangeMapState( 'Circle', event.latLng );
	});
}

function RemoveCircle() {
	if( Circle != null ) {
		Circle.setMap(null);
		CircleMarker.setMap(null);
	}
	ResetMarkers();
}









function PlaceMarkers(filename) {
	alert('Wordt nog gebruikt?');
	if( Circle != null ) {
		ChangeType( 'LatLng', 1 );
		var CircleRadius = Circle.getRadius();
		var CircleCenter = Circle.getCenter();
	}
	$.get(filename, function(xml){
		$(xml).find("marker").each(function(){
	
			// create a new LatLng point for the marker
			var lat = $(this).find('lat').text();
			var lng = $(this).find('lng').text();
			var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));
			
			var Distance = google.maps.geometry.spherical.computeDistanceBetween(CircleCenter,point);
			
			if( Distance <= CircleRadius ) {
				var marker_id = $(this).find('marker_id').text();
				var marker = new google.maps.Marker({
					icon: MapMarkerIcon,
					position: point
				});
			    google.maps.event.addListener(marker, 'click', function(event) {
					DisplayProduct(marker_id);
			      });				
				Markers.push(marker);
			}
			
		});
		DisplayMarkers();
		MapChangeCenter( CircleCenter );
	});	
}



function DisplayCountry( CountryID ) {
	alert('Wordt nog gebruikt?');
	
	ChangeType( 'Country', CountryID );
	var Url = AbsPath + 'includes/Components/Map/Ajax.php';
	$.ajax({
		type: "POST",
		url: Url,
		data: {ID: CountryID, Action: 'Country'},
		dataType: "json",
		contentType: "application/x-www-form-urlencoded; charset=UTF-8;",
		timeout: 5000,
		error: function(){
			// nothing
		},
		success: function( Result ) {
			RemoveCircle();
			var lat = Result["Latitude"];
			var lng = Result["Longitude"];
			var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));			
			Map.panTo( point );
		}
	});
}
