if(!JS_MAP) { var JS_MAP = new Object(); }
JS_MAP.core = {
	map : null,
	geocoder : null,
	directions: null,
	center: null,
	zoomlevel: 8,
	points: null,
	baseicon: null,
	in_the_picture: null,

	init: function() {
		if(GBrowserIsCompatible()) {
			JS_MAP.core.map = new GMap2(document.getElementById('map'));
			JS_MAP.core.geocoder = new GClientGeocoder();
			JS_MAP.core.center = new GLatLng(51.0540096, 3.7216956);
			JS_MAP.core.points = new Array();

			JS_MAP.core.map.enableScrollWheelZoom();
			JS_MAP.core.map.setCenter(JS_MAP.core.center, JS_MAP.core.zoomlevel);
			
			JS_MAP.core.directions = new GDirections(JS_MAP.core.map, document.getElementById("route"));
			
			JS_MAP.core.baseicon = new GIcon(null, "http://frietfindr.be/modules/core/layout/images/marker.png");
			JS_MAP.core.baseicon.shadow = "http://frietfindr.be/modules/core/layout/images/marker_shadow.png";
			JS_MAP.core.baseicon.iconSize = new GSize(25, 33);
			JS_MAP.core.baseicon.shadowSize = new GSize(49, 33);
			JS_MAP.core.baseicon.iconAnchor = new GPoint(25, 34);
			JS_MAP.core.baseicon.infoWindowAnchor = new GPoint(9, 2);
		}
	},
	
	addAddress: function(address, title, url, extraOptions) {
		JS_MAP.core.geocoder.getLatLng(address, function(point) { 
			if(point) {
				JS_MAP.core.points.push(point);
				
				if(extraOptions == null) extraOptions = new Object();
				if(url == null) extraOptions.clickable = false;
				
				var options = extraOptions;
				if(title != null) options.title = title;

				var icon = new GIcon(JS_MAP.core.baseicon);
				if(options.large == true) {
					icon.image = "http://frietfindr.be/modules/core/layout/images/marker_large.png";
					icon.shadow = "http://frietfindr.be/modules/core/layout/images/marker_shadow_large.png";
					icon.iconSize = new GSize(34, 45);
					icon.shadowSize = new GSize(67, 45);
					icon.iconAnchor = new GPoint(34, 45);
					JS_MAP.core.in_the_picture = point;
				}
				options.icon = icon;
				var marker = new GMarker(point, options);
				JS_MAP.core.map.addOverlay(marker);
				
				if(url != null) {
					GEvent.bind(marker, 'click', this, function() { 
						window.location = url;
					});
				}
				if(options.center == true) JS_MAP.core.map.panTo(point);
				JS_MAP.core.reZoom();
			}
		});
	},

	addPoint: function(latitude, longitude, title, url, extraOptions) {
		point = new GLatLng(latitude, longitude);
		JS_MAP.core.points.push(point);
			
		if(extraOptions == null) extraOptions = new Object();
		if(url == null) extraOptions.clickable = false;
			
		var options = extraOptions;
		if(title != null) options.title = title;

		var icon = new GIcon(JS_MAP.core.baseicon);
		if(options.large == true) {
			icon.image = "http://frietfindr.be/modules/core/layout/images/marker_large.png";
			icon.iconSize = new GSize(34, 45);
			icon.iconAnchor = new GPoint(34, 45);
			JS_MAP.core.in_the_picture = point;
		}
		options.icon = icon;
		var marker = new GMarker(point, options);
		JS_MAP.core.map.addOverlay(marker);
			
		if(url != null) {
			GEvent.bind(marker, 'click', this, function() { 
				window.location = url;
			});
		}
		if(options.center == true) JS_MAP.core.map.panTo(point);
		JS_MAP.core.reZoom();
	},
	
	showRoute: function(from, to) {
		JS_MAP.core.directions.load('from: '+ from +' to: '+ to, {'locale':'nl_BE'});
		return JS_MAP.core.directions.getStatus();
	},
	
	reZoom: function() {
		if(JS_MAP.core.in_the_picture != null) {
			JS_MAP.core.map.panTo(JS_MAP.core.in_the_picture);			
		} else if(JS_MAP.core.points.length > 1) {
			var bounds = new GLatLngBounds();
			for(var i=0; i < JS_MAP.core.points.length; i++) bounds.extend(JS_MAP.core.points[i]);
			
			JS_MAP.core.map.setZoom(JS_MAP.core.map.getBoundsZoomLevel(bounds));
			JS_MAP.core.map.setCenter(bounds.getCenter());
		} else JS_MAP.core.map.setCenter(JS_MAP.core.points[0], 13);
	},
	
	_eoo: true
}

$(document).ready(function() { JS_MAP.core.init() });
