/* jQuery googleMap Copyright Dylan Verheul <dylan@dyve.net>
 * Licensed like jQuery, see http://docs.jquery.com/License
 * Modified September 9, 2009 - Nora Heuer
 */

$.googleMap = {
	maps: {},
	marker: function(m, p) {
		if (!m) {
			return null;
		} else if (m.lat == null && m.lng == null) {
			return $.googleMap.marker($.googleMap.readFromGeo(m), p);
		} else {
			var marker = new GMarker(new GLatLng(m.lat, m.lng), {title: m.rel});
			if (m.txt) {
				if (p) {
					var marker = new GMarker(new GLatLng(m.lat, m.lng), {title: 'click to get ' + m.rel});
					GEvent.addListener(marker, "click", function() {
						Shadowbox.open({
							player: 'iframe'
							,content: m.txt
							,title: m.rel
							,options: {
								overlayOpacity: 1
								,viewportPadding: 30
							}
						});
					});
				} else {
					var icon = new GIcon();
					icon.image = "http://odca.baytides.ca/files/markers/odca.png";
					icon.shadow = "";  /* no marker shadow used in favour of map usability */
					icon.iconSize = new GSize(20, 20);  
					icon.iconAnchor = new GPoint(0, 20);
					icon.infoWindowAnchor = new GPoint(15, 1);
					var marker = new GMarker(new GLatLng(m.lat, m.lng), {title: m.rel, icon: icon});
					//marker.openInfoWindowHtml(m.txt);
					GEvent.addListener(marker, "click", function() {
						marker.openInfoWindowHtml(m.txt); // should add width
					});
  				}
			}
			return marker;
		}
	},
	readFromGeo: function(elem) {
		var latElem = $(".latitude", elem)[0];
		var lngElem = $(".longitude", elem)[0];
		if (latElem && lngElem) {
			return { lat:parseFloat($(latElem).attr("title")), lng:parseFloat($(lngElem).attr("title")), txt:$(elem).attr("title"), rel:$(elem).attr("rel") }
		} else {
			return null;
		}
	},
	mapNum: 1
};

$.fn.googleMap = function(lat, lng, zoom, options, infoWin) {

	// If we aren't supported, we're done
	if (!window.GBrowserIsCompatible || !GBrowserIsCompatible()) return this;

	// Default values make for easy debugging
	if (lat == null) lat = 37.4419;
	if (lng == null) lng = -122.1419;
	if (!zoom) zoom = 13;
	if (!infoWin) infoWin = false; // false=default onclick handling, true=open link embedded in title via shadowbox

	// Sanitize options
	if (!options || typeof options != 'object')	options = {};
	options.mapOptions = options.mapOptions || {};
	options.markers = options.markers || [];
	options.controls = options.controls || {};

	// Map all our elements
	return this.each(function() {
		// Make sure we have a valid id
		if (!this.id) this.id = "gMap" + $.googleMap.mapNum++;
		// Create a map and a shortcut to it at the same time
		var map = $.googleMap.maps[this.id] = new GMap2(this, options.mapOptions);
		// Center and zoom the map
       	map.setCenter(new GLatLng(lat, lng), zoom);
       	// Add controls to our map
       	for (var i = 0; i < options.controls.length; i++) {
	       	var c = options.controls[i];
	       	eval("map.addControl(new " + c + "());");
       	}
       	// If we have markers, put them on the map
       	var marker = null;
       	for (var i = 0; i < options.markers.length; i++) {
	       	if (marker = $.googleMap.marker(options.markers[i], infoWin)) map.addOverlay(marker);
       	}
    });

};

