$.gMap = {
	maps: {},
	mapNum: 1
};

$.fn.gMap = function(zoom, options) {

	if (!window.GBrowserIsCompatible || !GBrowserIsCompatible())
            return this;

	// Sanitize options
	if (!options || typeof options != 'object') options = {};
	options.mapOptions = options.mapOptions || {};
	options.controls = options.controls || {};

	var lat = $(this).attr("latitude") || 0;
	var lng = $(this).attr("longitude") || 0;
	if (!zoom) zoom = 15;

	// Map all our elements
	return this.each(function() {
		// Make sure we have a valid id
		if (!this.id) this.id = "gMap" + $.gMap.mapNum++;
		// Create a map and a shortcut to it at the same time
		map = $.gMap.maps[this.id] = new GMap2(this,
                    options.mapOptions);

		markerOptions = { clickable:false, draggable:false };

		if($(this).hasClass('multi')) {
			zoom = 1;
			geos = $('.geo');
			maxlat = 1000;
			minlat = 1000;
			maxlon = 1000;
			minlon = 1000;
			map.setCenter(new GLatLng(0, 0), 3, G_PHYSICAL_MAP);
			geos.each(function() {
				thislat = parseFloat($(this).attr("latitude"));
				thislon = parseFloat($(this).attr("longitude"));
				if(maxlat == 1000) { maxlat = thislat; }
				if(minlat == 1000) { minlat = thislat; }
				if(maxlon == 1000) { maxlon = thislon; }
				if(minlon == 1000) { minlon = thislon; }
				if(thislat > maxlat) { maxlat = thislat; }
				if(thislat < minlat) { minlat = thislat; }
				if(thislon > maxlon) { maxlon = thislon; }
				if(thislon < minlon) { minlon = thislon; }
				marker = new GMarker(new GLatLng(thislat, thislon), markerOptions);
				map.addOverlay(marker);				
			});
			newlon = (maxlon + minlon) / 2;
			newlat = (maxlat + minlat) / 2;
			bigrect = new GLatLngBounds(new GLatLng(minlat, minlon), new GLatLng(maxlat, maxlon));
			zoom = map.getBoundsZoomLevel(bigrect);
			map.setCenter(new GLatLng(newlat, newlon), zoom, G_PHYSICAL_MAP);
		}else{
			var latitude = parseFloat(lat);
			var longitude = parseFloat(lng);

    	    // Center and zoom the map
	       	map.setCenter(new GLatLng(latitude, longitude), zoom, G_PHYSICAL_MAP);
	        var marker = new GMarker(new GLatLng(latitude, longitude), markerOptions);
	        map.addOverlay(marker);	
		}

        map.addControl(new GSmallZoomControl());
		var ov = new GOverviewMapControl(new GSize(100,100));
        map.addControl(ov);
//        ov.hide(true);
		
    });
};

$(document).ready(function() {
		$(".gmap").gMap(15, {
		});
});
