/*
 * jQuery Google Maps plugin
 *
 * Copyright (c) 2007 Joaquin Cuenca Abela
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */

(function($){
	$.fn.gmaps = function(options) {
		var settings = {
			center: new GLatLng(0.0, 0.0),
			zoom: 12,
			map: G_HYBRID_MAP
		};

		$.extend(settings, options);

		return this.each(function() {
			if ($(this).getMap()) return;
			var map = new GMap2(this);
			this.gmap = map;
			$(this).setCenter(settings.center, settings.zoom, settings.map).addOverlay(settings.overlays).addControl(settings.controls);
		});
	};

	$.fn.getMap = function(i) {
		var maps = $.map(this, function(j) {
			return j.gmap;
		});
		return i !== undefined ? (maps[i] ? maps[i] : null) : (maps.length > 0 ? maps : null);
	};

	$.fn.setCenter = function(center, zoom, kind) {
		$.each(this.getMap(), function(i, map) {
			map.setCenter(center, zoom, kind);
		});
		return this;
	};

	$.gmapControls = {
		SMALL_MAP: GSmallMapControl,
		LARGE_MAP: GLargeMapControl,
		SMALL_ZOOM: GSmallZoomControl,
		SCALE: GSmallZoomControl,
		MAP_TYPE: GMapTypeControl
	};

	$.fn.addControl = function(controls) {
		if (!controls) return this;

		if (controls.constructor != Array)
			controls = [controls];

		$.each(this.getMap(), function(i, map) {
			$.each(controls, function(i, n) {
				map.addControl(n.constructor == String ? new $.gmapControls[n] : n);
			})
		});
		return this;
	};

	$.fn.addOverlay = function(overlays) {
		if (!overlays) return this;

		if (overlays.constructor != Array)
			overlays = [overlays];

		$.each(this.getMap(), function(i, map) {
			$.each(overlays, function(i, n) {
				map.addOverlay(n);
			});
		});
		return this;
	};

	$.fn.checkResize = function(overlays) {
		$.each(this.getMap(), function(i, map) {
			map.checkResize();
		});
		return this;
	};
})(jQuery);

