/* jQuery googleMap Copyright Dylan Verheul <dylan@dyve.net>
 * Licensed like jQuery, see http://docs.jquery.com/License
 */
 
/* Extended by John Elliott @ Evosite Ltd. 22nd September 2009 
 * Ammened Code to use the address element content to provide 
 * the data for the info window rather than the title attribute 
 * of the map-data div
 */

var bounds;

$.googleMap = {
	maps: {},
	marker: function(m) {
		if (!m) {
			return null;
		} else if (m.lat == null && m.lng == null) {
			return $.googleMap.marker($.googleMap.readFromGeo(m));
		} else {
			point = new GLatLng(m.lat, m.lng);
			var marker = new GMarker(point);
			
			// ==== Each time a point is found, extent the bounds ato include it =====
            
           bounds.extend(point);
            
            
          
            
			if (m.txt) {
				GEvent.addListener(marker, "click", function() {
    				marker.openInfoWindowHtml(m.txt);
  				});
  				
  				
			}
			return marker;
		}
	},
	readFromGeo: function(elem) {
		var latElem = $(".latitude", elem)[0];
		var lngElem = $(".longitude", elem)[0];
		var addressElem = $("address", elem)[0]
		if (latElem && lngElem) {
			return { lat:parseFloat($(latElem).attr("title")), lng:parseFloat($(lngElem).attr("title")), txt:$(addressElem).html() }
		} else {
			return null;
		}
	},
	mapNum: 1
};

$.fn.googleMap = function(lat, lng, zoom, options) {

	// If we aren't supported, we're done
	if (!window.GBrowserIsCompatible || !GBrowserIsCompatible()) return this;

	// Default values make for easy debugging
	if (lat == null) {lat = 0};
	if (lng == null) {lng = 0};
	if (zoom == null){zoom = 7};
	
	
	

	// 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);
		
		
       	 map.setCenter(new GLatLng(37.4419, -122.1419), zoom);

       	// Add controls to our map
       	map.setMapType(G_NORMAL_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;
       	
       	bounds = new GLatLngBounds();

		var j = options.markers.length;
       	
       	for (var i = 0; i < j; i++) {
	       	if (marker = $.googleMap.marker(options.markers[i])) {
	       		map.addOverlay(marker);
	       	}
	       	
	      
	       
       	}
       	
       	 // ===== determine the zoom level from the bounds =====
       	 
       	
       	 
       	 //alert(map.getBoundsZoomLevel(bounds));
       	 
       	 if(j > 1) {
         	map.setZoom(map.getBoundsZoomLevel(bounds));
         }

          var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
      	  var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
      	  map.setCenter(new GLatLng(clat,clng));
          

          
       	
       	
       	
    });

};


