function KaartManager() {
	this.mapVernieuw 	= true;
	this.mapPadding		= 50;
	this.mapType	 	= false;
	this.dragTimeout 	= false;
	this.filters	 	= false;
	this.boundZoom   	= false;
	this.afterVernieuw	= false;
	
	// Init
	this.init = function( Args ) {
		this.mapType    = ( Args.mapType ? Args.mapType : 'canvas' );
		this.mapPadding = ( Args.mapPadding ? Args.mapPadding : this.mapPadding );
		this.filters    = ( Args.filters ? Args.filters : {} );
		
		// Kaart laden
		mapInit( Args );
		
		// Iconen laden
		mapInitIconen();
		
		GEvent.addListener( map, "dragend", function() {
			krtManager.herlaad( 500 );
		} );
		GEvent.addListener( map, "zoomend", function() {
			krtManager.herlaad( 500 );
		} );
		
		return this;
	};
	
	// Filters
	this.setFilterWaarde = function( FilterId, Waarde ) {
		this.filters[ FilterId ] = Waarde;
		return this;
	};
	
	this.herlaad = function( Tijdsduur ) {
		clearTimeout( this.dragTimeout );
		
		if ( !this.mapVernieuw ) {
			return;
		}
		
		this.dragTimeout = setTimeout( function() { 
			mapUpdatePos();
			krtManager.vernieuw();
		}, Tijdsduur, {} );
	};
	
	this.vernieuw = function() {
		var bounds = new GLatLngBounds( mapGetBoundSw( this.mapPadding ), mapGetBoundNe( this.mapPadding ) );  
		$.getJSON(
			'/jx.php',
			'map='+this.mapType+'&bbox='+bounds.getSouthWest().toUrlValue()+','+bounds.getNorthEast().toUrlValue()+'&lvl='+map.getZoom()+( $.param( this.filters ) ? '&'+$.param( this.filters ) : '' ),
			function( Data ) {
				krtManager.herteken( Data );
			}
		);
	};
	
	this.herteken = function( Data ) {
		map.clearOverlays();
		
		mapBounds = new GLatLngBounds();
		for ( var i = 0; i < Data.items.length; i++ ) {
			jxItem = Data.items[ i ];
			
			var icoonId = jxItem.markerType;
			if ( jxItem.markerType == 'hotel' ) {
				icoonId += '-'+jxItem.sterren;
			} else if ( jxItem.markerType == 'evenement' ) {
				icoonId += '-'+jxItem.feestdag;
			}
			
			if ( jxItem.miniLogo ) {
				var labelTekst = '<img src="'+jxItem.miniLogo+'" alt="" />';
				var marker = new LabeledMarker( new GLatLng( parseFloat( jxItem.lat ), parseFloat( jxItem.lng ) ), { icon: mapIcons["markers"][ 'evenement-logo' ], labelText: labelTekst } );
				marker.icoonId = 'evenement-logo';
			} else {
				var marker = new KaMarker( new GLatLng( parseFloat( jxItem.lat ), parseFloat( jxItem.lng ) ), { icon: mapIcons["markers"][ icoonId ] } );
			}
			marker.title = jxItem.naam;
			marker.html = this.getMarkerHtml( jxItem );
			marker.initSettings();
			map.addOverlay( marker );
			
			mapBounds.extend( marker.getLatLng() );
		}
		
		if ( this.boundZoom ) {
			map.setCenter( mapBounds.getCenter(), map.getBoundsZoomLevel( mapBounds ) );  
		}
		
		this.boundZoom = false;
		
		if ( this.afterVernieuw ) {
			this.afterVernieuw();
		}
	};
	
	this.getMarkerHtml = function( JxItem ) {
		var html = '';
		html = '<table id="marker-window" class="'+JxItem.markerType+'">';
		
		// Foto
		html += '<td class="foto"><a href="'+JxItem.url+'">';
		if ( JxItem.foto ) {
			html += '<img src="'+JxItem.foto+'" alt="" width="80" onerror="$(this).attr(\'src\', \'/images/misc/geen-80.gif\' );" />';
		} else {
			html += '<img src="/images/misc/geen-80.gif" alt="" />';
		}
		html += '</a></td>';
		
		// Naam
		html += '<td class="info"><a href="'+JxItem.url+'" class="titel"><strong>'+JxItem.naam+'</strong></a>';
		
		// Subnaam
		if ( JxItem.subnaam ) {
			html += '<em>'+JxItem.subnaam+'</em>';
		}
		
		// Omschrijving
		if ( JxItem.omschrijving ) {
			html += '<div class="omschrijving">'+JxItem.omschrijving+'</div>';
		}
		
		html += '</td></tr></table>';
		
		return html;
	};
}

var krtManager = new KaartManager();
