var DEBUG = 1;
	
/*
Required to load upfront
*/
google.load('visualization', '1'); // for custom SQL call to get count
google.load('search', '1', {language : 'nl'}); // for image search


/**
Writes debug messages to the console if DEBUG is true.
*/
function writeDebug (text) {
	if (DEBUG && console) {
		console.log(text);
	}
}

var MapApp = (function($, appFlavour) {
	
	var started = false;
	
	var MapRouter = Backbone.Router.extend({
	
		mapModel: undefined,
		filterModel: undefined,
		filterView: undefined,
		addressView: undefined,
		layoutView: undefined,
		layoutController: undefined,
		controlsView: undefined,
		routed: false,
		
		initialize: function(options) {

			if (appFlavour === 'bedrijven') {
				this.route(/^\s*(\d+)\s*$/, 'address', this.reset);	
			} else {
				// monuments
				// Route for monument object id
				// regex matches /12345, passing "12345"
				this.route(/^\s*(\d+)\s*$/, 'id', this.id);
				
				// multiple ids
				this.route(/^\s*((\d+)\s*\,(.*?))$/, 'ids', this.ids);
				
				// category code:
				// :CATEGORY
				// for example: :301 or :1306
				this.route(/^\s*\:(\d+)$/, 'category', this.category);
			}
			
			// first create all models
			
			this.filterModel = new FilterModel;
			this.layoutController = new LayoutController;
			
			if (google) {
				this.mapModel = new MapModel({
					dataTable: MapApp.CONFIG_SETTINGS[appFlavour].dataTable,
					dataTableFields: MapApp.CONFIG_SETTINGS[appFlavour].dataTableFields,
					filterModel: this.filterModel,
					flavour: appFlavour
				});
			}			

			// then create the views
			
			this.controlsView = new ControlsView({
				el: $('#app #controls'),
				router: this,
				templates: MapApp.TEMPLATE_SETTINGS[appFlavour]
			});
			
			this.addressView = new AddressView({
				el: $('#addressView'),
				model: this.mapModel,
				templates: MapApp.TEMPLATE_SETTINGS[appFlavour]
			});
			
			this.filterView = new FilterView({
				el: $('#filterView'),
				model: this.filterModel,
				templates: MapApp.TEMPLATE_SETTINGS[appFlavour]
			});
			
			if (google) {
				if (appFlavour === 'bedrijven') {
					this.mapView = new MapViewBedrijven({
						el: $('#mapView'),
						model: this.mapModel,
						templates: MapApp.TEMPLATE_SETTINGS[appFlavour],
						style: MapApp.CONFIG_SETTINGS.map.style,
						detailPageUrlTemplate: MapApp.CONFIG_SETTINGS[appFlavour].detailPageUrl,
						useGoogleImagesAsFallback: MapApp.CONFIG_SETTINGS[appFlavour].useGoogleImagesAsFallback
					});
				} else {
					this.mapView = new MapViewMonumenten({
						el: $('#mapView'),
						model: this.mapModel,
						templates: MapApp.TEMPLATE_SETTINGS[appFlavour],
						style: MapApp.CONFIG_SETTINGS.map.style,
						detailPageUrlTemplate: MapApp.CONFIG_SETTINGS[appFlavour].detailPageUrl,
						useGoogleImagesAsFallback: MapApp.CONFIG_SETTINGS[appFlavour].useGoogleImagesAsFallback
					});
				}
				this.mapModel.mapView = this.mapView; // don't use 'set'; it is no dynamic data
				this.mapModel.bind('change:selectedObject', this.onSelectedObjectChanged, this);
			}
			
			this.layoutView = new LayoutView({
				el: $('#app'),
				router: this,
				model: this.layoutController,
				mapView: this.mapView,
				controlsView: this.controlsView,
				filterView: this.filterView,
				addressView: this.addressView
			});
		},
		
		onSelectedObjectChanged: function (model) {
			var selectedObject = model.get('selectedObject');
			if (!selectedObject) {
				return;
			}
			this.navigate(model.get('selectedObject').id, false);
		},
		
		routes: {
			'': 'index',
			'_': 'reset',
			'*anything': 'address' // Backbone will try match the route above first
		},
		
		index: function() {
			this.filterModel.clear();
			this.controlsView.render();
			this.mapView.reset();
			this.controlsView.setSearch('');
		},
		
		reset: function() {
			this.navigate('x');
			this.index();
			this.navigate('');
		},
		
		category: function(category) {
			this.index();
			this.filterModel.setFilter(category);
		},
		
		address: function (address) {
			if (/^\s+$/i.address) {
				// must not be empty
				return;
			}
			this.mapModel.goToAddress(address);
			this.controlsView.setSearch(address);
		},
		
		id: function (id) {
			this.mapModel.goToObjectWithID(id);
			this.controlsView.setSearch(id);
		},
		
		ids: function (ids) {
			this.index();
			var idList = ids.split(/\s*\,\s*/);
			this.filterModel.set({'ids': idList});
		}
		
	});
	
	return {
		start: function (route) {
			var router = new MapRouter(appFlavour);
			if (!started) {
				// Start Backbone history a neccesary step for bookmarkable URL's
				try {
					Backbone.history.start({pushState: false});
				} catch (error) {
					console.log( "Error on backbone.history.start", error);
				}
				started = true;
			}
			if (route) {
				router.navigate(route, true);
			}
		}
	}

});
