/* jQuery googleMap Copyright Dylan Verheul <dylan@dyve.net>
 * Licensed like jQuery, see http://docs.jquery.com/License
 */


(function($){
$.googleMap = {
	maps: {},
	marker: function(m,i) {
		if (!m) {
			return null;
		} else if (m.lat == null && m.lng == null) {
			return $.googleMap.marker($.googleMap.readFromGeo(m,i),i);
		} else {
			var marker = new google.maps.Marker(new google.maps.LatLng(m.lat, m.lng));
			var icon = marker.getIcon();
			icon.image = "/img/map/marker_"+i+".png";
			//icon.iconSize = new GSize(40,68);
			//icon.iconAnchor = new GPoint(20,68);
			
			if (m.title) {
				google.maps.Event.addListener(marker, "click", function() {
    				marker.openInfoWindowHtml("<a href='"+m.url+"'>"+m.title+"</a>");
  				});
			} else {
				google.maps.Event.addListener(marker, "click", function() {
					showMarkerPopup(m.uid,marker.getLatLng())
				});
			}
			return marker;
		}
	},
	readFromGeo: function(elem,i) {
		var latElem = $(".latitude", elem)[0];
		var lngElem = $(".longitude", elem)[0];
		if (latElem && lngElem) {
			itemBlock($(elem).attr("uid")).find("a.btnPanTo").before('<img align="middle" height="24" border=0 src="/img/map/marker_'+i+'.png"/>&nbsp;');
			return { 
					lat:parseFloat($(latElem).attr("title")), 
					lng:parseFloat($(lngElem).attr("title")), 
					title:$(elem).attr("title"),
					url:$(elem).attr("url"),
					uid:$(elem).attr("uid"),
					color:$(elem).attr("color"),
					letter:$(elem).attr("letter") 
					}
		} else {
			return null;
		}
	},
	mapNum: 1
};

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

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

	// Default values make for easy debugging
	if (lat == null) //lat = 37.4419;
	if (lng == null) //lng = -122.1419;
	if (!zoom) //zoom = 13;

	// 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 google.maps.Map2(this, options.mapOptions);
       	google.maps.Event.addListener(map, "movestart", function() {
    				Popup.close();
    				if (mapDisplayed)
	    				$("#searchInMapButton").show();
  		});
  		google.maps.Event.addListener(map, "zoomend", function() {
    				Popup.close();
    				if (mapDisplayed)
	    				$("#searchInMapButton").show();
  		});
       	var center = null;
       	if ((lat!=null) && (lng !=null) && (zoom != null)) {
       		center = new google.maps.LatLng(lat, lng);
	       	map.setCenter(center, zoom);
       	} else {
       		map.setCenter(new google.maps.LatLng(0,0),12);
       	}

       	// 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;
       	var bounds = null;
       	for (var i = 0; i < options.markers.length; i++) {
	       	if (marker = $.googleMap.marker(options.markers[i],i)){
	       		 map.addOverlay(marker);
	       		if (!bounds){
	       			bounds = new google.maps.LatLngBounds(marker.getPoint(),  marker.getPoint());	
	       		} else {
	       			bounds.extend(marker.getPoint());
	       		}
	       	}
       	}
       	if (bounds && (center==null)){
			center = bounds.getCenter();
			zoom = map.getBoundsZoomLevel(bounds);
			// Center and zoom the map
       		map.setCenter(center, zoom-1);	
		}
		mapDisplayed=true;
		
    });

};
})(jQuery);

