/* 
Google Maps jQuery Plugin by Dylan Verheul 
http://www.dyve.net/jquery/?googlemaps
modified version (v6.12.13.1)

usage:

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=..." type="text/javascript"></script> // put your API key here (...)
<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript" src="/js/jquery.googlemaps.js"></script>
<script type="text/javascript">
$('#gmaps').googleMap({
	controls: ["GSmallMapControl", "GMapTypeControl"],
	markers: $("#gmaps .geo")
});
</script>
...
<div id="gmaps" title="52.421528,16.935339,9"> // title="lat,lng,zoom"
	<div class="geo"> // marker
		<div class="description"> // marker description
			<h2>Łubudubu</h2>
			<p>Chomiku fikumiku</p>
		</div>
		<abbr class="latitude" title="52.421528">N 57° ...</abbr> // marker latitude
		<abbr class="longitude" title="16.935339">W 16° ...</abbr> // marker longitude
	</div>
</div>
*/

$.googleMap = {
	maps: {},
	marker: function(m) {
		if (!m) {
			return null;
		} else if (m.lat == null && m.lng == null) {
			return $.googleMap.marker($.googleMap.readFromGeo(m));
		} else {
			var marker = new GMarker(new GLatLng(m.lat, m.lng));
			if (m.txt) {
				GEvent.addListener(marker, "click", function() {
    				marker.openInfoWindowHtml(m.txt);
  				});
			}
			return marker;
		}
	},
	readFromGeo: function(elem) {
		var lat = $(".latitude", elem).title();
		var lng = $(".longitude", elem).title();
		var txt = $("div.description",elem).html();
		if (lat && lng) {
			return { lat : parseFloat(lat), lng : parseFloat(lng), txt : txt }
		} else {
			return null;
		}
	},
	mapNum: 1
};

$.fn.googleMap = function(options) {

	// Get lat, lng, zoom from title
	if($(this).title() != '' && $(this).title() != null){
		var mapVars = $(this).title().split(",");
		for (i = 0; i < mapVars.length; i++){
			mapVars[i] = parseFloat(mapVars[i]);
		}
		var lat 	= mapVars[0];
		var lng 	= mapVars[1];
		var zoom 	= mapVars[2];
		$(this).title('');
	}

	// 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;

	// 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])){
				map.addOverlay(marker);
			}
       	}
    });

};