(function(){
	Tour = {
		gmap: null,
		map_control : null,
		map_type_control : null,
		locales : {},
		dossier : "",
		dossier_lru_cache : {"anthony":1},
		dossier_lru_index : 2 
	};
	Tour.hide_dossier = function(cb) { 
		var name = Tour.dossier;
		var afterwards = function() { 
			var name = Tour.dossier;

			var photo = document.getElementById("photo-"+name);
			var tab = document.getElementById("tab-"+name);
			var dossier = document.getElementById("dossier-"+name);
			var paperclip = document.getElementById("paper-clip");

			if (photo) { 
				photo.className = "hidden";
				paperclip.className = "hidden";
			}
			if (tab) tab.className = ""; // if hidden will close the tab completely
			if (dossier) dossier.className = "hidden";


			// now we want to keep only 4 tabs open at a time
			// closing the least recently used tab
			var count = 0;
			var longest_time = 100000;
			var candidate = null;
			for (var tab in Tour.dossier_lru_cache) { 
				// set to null rather than delete to work around old browsers
				if (Tour.dossier_lru_cache[tab]) { 
					count++;
					if (longest_time > Tour.dossier_lru_cache[tab]) {
						longest_time = Tour.dossier_lru_cache[tab];
						candidate = tab;
					}
				}	
			}
			if (count > 4 && candidate) {
				var hidden_tab = document.getElementById("tab-"+candidate);
				if (hidden_tab) hidden_tab.className = "hidden";
				Tour.dossier_lru_cache[candidate] = null;
			}

			// finally issue the callback
			if (cb) cb();
		};
		if (name == "") { 
			if (cb) cb();
		} else { 
			// do animation stuff to cause it to leave then call
			//document.getElementById("folder").className = "hidden";
			afterwards();
		}
	};

	Tour.show_locale = function(name) {
		if (Tour.locales && Tour.locales[name]) Tour.locales[name].click();
	};

	Tour.show_dossier = function(name,hometown,callback) { 
		if (hometown) Tour.show_locale(hometown); // show locale
		if (Tour.dossier == name) { 
			// already showing!	
			return false;
		}
		Tour.dossier_lru_cache[name] = Tour.dossier_lru_index++
		var afterwards = function() { 
			Tour.dossier = name;
			// swap the dossier contents.
			var photo = document.getElementById("photo-"+name);
			var tab = document.getElementById("tab-"+name);
			var dossier = document.getElementById("dossier-"+name);
			var paperclip = document.getElementById("paper-clip");
			if (photo) { 
				photo.className = "clip-photo-image png";
				paperclip.className = "paper-clip png";
			}
			if (tab) tab.className = "current";
			if (dossier) dossier.className = "";

			// ensure its visibility
			//document.getElementById("folder").className = "folder";

			// then callback
			if (callback) callback();
		};
		
		Tour.hide_dossier(afterwards);
	};


	Tour.show_controls = function() { 
		if (Tour.gmap) { 
			Tour.gmap.addControl(Tour.map_control); 
			Tour.gmap.addControl(Tour.map_type_control);
		}		
	};

	Tour.hide_controls = function() { 
		if (Tour.gmap) { 
			Tour.gmap.removeControl(Tour.map_control);
			Tour.gmap.removeControl(Tour.map_type_control);
		}
	};


	Tour.locale = function(name, lat,lng,maxzoom,zoom,text) { 
	    try { 
		if (Tour.gmap) { 
			var map = Tour.gmap;
			var point = new GPoint(lng,lat);
			var marker = new GMarker(point);
			// when the user clicks
			// pan to point in question, then zoom in after a second.
			// if we are already zoomed in, then just pan immediately
			// afterwards, open an info window with the desired contents.
			var click = function() { 
				var node = document.getElementById(name);
				if (node) node = node.cloneNode(true);
				map.recenterOrPanToLatLng(point);
				window.setTimeout(function() { 
					if (map.getZoomLevel()>zoom ||
					    map.getZoomLevel()<maxzoom) 
						map.centerAndZoom(point,zoom);
					map.openInfoWindow(point,node);
					
				},map.getZoomLevel()>zoom?1000:0);
			}
			GEvent.addListener(marker,"click",click);
			map.addOverlay(marker);
			Tour.locales[name] = { "point": point, "marker": marker, "click" : click };
		}
	    } catch(e) {}
	};

	DOM.Ready.onIdReady("map",function() { 
		try { 
			if (GMap) { 
				var map = new GMap(document.getElementById("map"));
				Tour.gmap = map;
				Tour.map_control = new GLargeMapControl();
				Tour.map_type_control = new GMapTypeControl();
				map.setMapType(G_HYBRID_TYPE);
				map.centerAndZoom(new GPoint(1, 37.4419), 16);
				Tour.show_controls();
			}
			add_locales(); // global callback generated by ColdFusion
		} catch(e) {
			alert(e);
		}
	});
})();

