/*
window.addEvent('unload', function(){
	if ($('map_container')) {
		google.maps.Unload()
	}
});
*/

var route = new Class({
	versionInfo: '1.0 beta',
	routeInfo: {
		'time': 0,
		'distance': 0,
		'route': ''
	},
	initialize: function(mapObj) {		
		this.dirObj = new google.maps.Directions(mapObj);
		
		google.maps.Event.addListener(this.dirObj, "load", this.onDirectionsLoad.bind(this));
		google.maps.Event.addListener(this.dirObj, "error", this.onDirectionsError.bind(this));
		google.maps.Event.addListener(this.dirObj, "addoverlay", this.hideDirMarkers.bind(this));
		
		this.start_location = null;
		this.destinations = [];
		this.stops = [];
		
		this.distance = null;
		this.duration = null;
	},
	loadDirections: function(points) {

		this.fireEvent('onDirectionsLoading');
		this.dirObj.clear();
		this.dirObj.loadFromWaypoints(points,{locale:"nl",getPolyline:true,getSteps:true});
	},
	onDirectionsLoad: function() {
		
		
		var summaryHTML = this.dirObj.getSummaryHtml();
		this.distance = this.dirObj.getDistance();
		this.duration = this.dirObj.getDuration();
		var numRoutes = this.dirObj.getNumRoutes();
		var startLatLng = this.dirObj.getRoute(0).getStep(0).getLatLng();
		var endLatLng = this.dirObj.getRoute(numRoutes-1).getEndLatLng();
		
		this.route = this.dirObj.getPolyline();
		
		this.fireEvent('onDirectionsLoaded');
	},
	onDirectionsError: function() {
		
	},
	hideDirMarkers: function() {
		var numMarkers = this.dirObj.getNumGeocodes();
		for (var i = 0; i < numMarkers; i++) { 
			var marker = this.dirObj.getMarker(i); 
	 		if (marker != null) {
				marker.hide(); 
			}
			else {
				alert("Marker is null"); 
			}
		}
	},
	toggleRoute: function() {
		
	},
	showRoute: function(map, args) {
		
		$each(args, function(item, index) {
		//	window['this.route.'+index] = item;
		}.bind(this));
		//this.route.color = '#000';
		
		this.routeObj = map.mapObj.addOverlay(this.route);
	},
	hideRoute: function(map) {
		map.mapObj.removeOverlay(this.route);
	},
	resetRoute: function(map) {
		if (this.route) map.mapObj.removeOverlay(this.route);
	}
});
route.implement(new Events);


var map = new Class({
	versionInfo: '1.0 beta',
	initialize: function() {
		google.load("maps", "2", {"language": "nl_NL","callback" : this.mapsLoaded.bind(this)});
	},
	mapsLoaded: function() {
		if (google.maps.BrowserIsCompatible()) {
			this.geocoder = new google.maps.ClientGeocoder();
			this.geocoder.setBaseCountryCode('BE');		
			this.fireEvent('onMapsLoaded')
		}
		else {
			this.fireEvent('onMapsNotCampatible');			
		}
	},
	loadMaps: function(dest) {
		this.mapObj = new google.maps.Map2(dest);		
		
		this.mapObj.setCenter(new google.maps.LatLng(51.23071548460618, 4.44185320095403), 13);
				
		this.mapObj.enableContinuousZoom();
		this.mapObj.enableScrollWheelZoom();
		
		google.maps.Event.addListener(this.mapObj, "moveend", this.moveMap.bind(this));		

	},	
	moveMap: function() {
		var center = this.mapObj.getCenter();
	},
	zoomMap: function(locations) {
		var bounds = new GLatLngBounds();
		
		locations.each(function(location, index) {
			bounds.extend(location);
		});
		
		this.mapObj.setZoom(this.mapObj.getBoundsZoomLevel(bounds));
		this.mapObj.setCenter(bounds.getCenter());
	},
	controls: function(input){
		switch(input){
			case 'zoom_in':
				this.mapObj.zoomIn();
			break;
			case 'zoom_out':
				this.mapObj.zoomOut();
			break;
			case 'up':
				this.mapObj.panDirection(0, 1);
			break;
			case 'down':
				this.mapObj.panDirection(0, -1);
			break;
			case 'left':
				this.mapObj.panDirection(1, 0);
			break;
			case 'right':
				this.mapObj.panDirection(-1, 0);
			break;
		}
		return false;
	},
	createIcon: function() {
		
	},
	createMarker: function() {
		
	},
	removeMarker: function() {
		
	},
	resetMap: function() {
		
	}
});
map.implement(new Events);
	
