$(document).ready(function() {
	t3a.init();
});

var t3a = {
	init: function() {
		this.gallery.init();
		this.mapping.init();
	},
	gallery: {
		init: function() {
			$("div.gallery a").slimbox({loop: true}, function(el) {
				// Use a RegExp to pick out the "src" and "lbl" properties...
				var params = /\?src=([^&]*)&lbl=([^&]*)/i.exec(el.href);
				if (params) return [params[1], decodeURIComponent(params[2])];
			});
		}
	},
	mapping: {
		init: function() {
			// Get the first (and only) #gmap element...
			var jel = $("#gmap").eq(0);
			if (jel) {
				var card = $("#cbcard");
				var latlng = new GLatLng(card.find(".latitude").text(), card.find(".longitude").text());
				t3a.mapping.google.createMap(jel, {
					clear:		true,
					center:		latlng,
					zoom:		14,
					markers:	[{caption: "<div class='vcard'>" + card.html() + "</div>", loc: latlng}]
				});
			}
		},
		google: {
			createMap: function(jel, opts) {
				var map = null;
				if (GBrowserIsCompatible()) {
					//Clear the element...
					jel.empty();
					// Clear the element & create the map...
					map = new GMap2(jel.get(0));
					// Center the map...
					map.setCenter(opts.center, opts.zoom);
					// Create and add the markers to the map...
					if (opts.markers) for (var i = 0; i < opts.markers.length; i++) map.addOverlay(this.createMarker(opts.markers[i]));
					// Add the polylines to the map...
					if (opts.polys) for (var i = 0; i < opts.polys.length; i++) map.addOverlay(this.createPolyline(opts.polys[i]));
					// Add the map's controls...
					map.addControl(new GLargeMapControl());
					map.addControl(new GMapTypeControl());
				}
				return map;
			},
			createMarker: function(params) {
				var icon = null;
				if (!(params.base == null)) {
					icon = new GIcon(params.base);
					icon.image = params.img;
				}
				// Create the marker...
				var marker = new GMarker(params.loc, icon);
				// Add the caption to the InfoWindow...
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(params.caption);
				});
				return marker;
			},
			createPolyline: function(params) {
				return new GPolyline.fromEncoded(params);
			}
		}
	}
};
