(function($) {

	$.fn.gMaps = function(options) {

		debug(this);

		// build main options before element iteration
		var opts = $.extend({}, $.fn.gMaps.defaults, options);

		// iterate and reformat each matched element
		return this.each(function() {

			var $this = $(this);

			// build element specific options
			var o = $.meta ? $.extend({}, opts, $this.data()) : opts;

			// build map
		    var myLatlng		= new google.maps.LatLng(o.latitude, o.longitude);
		    var myOptions		= {
		      zoom: o.zoom,
		      center: myLatlng,
			  disableDefaultUI: o.disableDefaultUI,
		      mapTypeId: google.maps.MapTypeId[o.mapTypeId]
		    };

			function addMarker(m) {
				var nm, ni, infowindow, marker;
				nm			= {
					icon		: m.icon || { image : null },
					latitude	: m.latitude || o.latitude,
					longitude	: m.longitude || o.longitude,
					infowindow	: m.infowindow || false
				};
				marker = new google.maps.Marker({
					icon		: nm.icon.image,
					position	: new google.maps.LatLng(nm.latitude, nm.longitude),
			        map			: map,
			        title		: nm.infowindow.title || 'Title'
			    });
				if (nm.infowindow) {
					ni			= '<div id="marker-content">'+
					    '<h2>' + nm.infowindow.title + '</h2>'+
					    '<div id="marker-body">' + nm.infowindow.text + '</div>' +
					    '</div>';
					infowindow	= new google.maps.InfoWindow({
						content		: ni,
						maxWidth	: nm.infowindow.maxWidth || 300,
						maxHeigt	: nm.infowindow.maxHeigt || 300
					});
					if (nm.infowindow.open) {
						infowindow.open(map,marker);
					}
				    google.maps.event.addListener(marker, 'click', function() {
				      infowindow.open(map, marker);
				    });
				}
			}

		    var map = new google.maps.Map($this[0], myOptions);

			for (var i=0; i < o.markers.length; i++) {
				addMarker(o.markers[i]);
			}	
			
			// 
			// // update element styles
			// $this.css({
			//	backgroundColor: o.background,
			//	color: o.foreground
			// });
			// 
			// var markup = $this.html();
			// 
			// // call our format function
			// markup = $.fn.gMaps.format(markup);
			// $this.html(markup);
			// if (address) {
			//   var geocoder = new google.maps.Geocoder();
			//   geocoder.geocode({ address: address }, function(results, status) {
			//     if (status == google.maps.GeocoderStatus.OK && results.length) {
			//       if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
			//         map.set_center(results[0].geometry.location);
			//         var marker = new google.maps.Marker({
			//             position: results[0].geometry.location,
			//             map: map
			//         });
			//       }
			//     }
			//   });
			// }

		});
		
	};

	//
	// private function for debugging
	//
	function debug($obj) {
		if (window.console && window.console.log) {
			window.console.log($obj);
		}
	}

	// $.fn.gMaps.addMarker = function(marker) {
	//	addMarker(marker);
	// };

	//
	// plugin defaults
	//
	$.fn.gMaps.defaults = {
	    latitude			: 45.448,
	    longitude			: 9.178994,
	    zoom				: 14,
		disableDefaultUI	: true,
	    mapTypeId			: 'ROADMAP',
		markers				: [{
									latitude	: 45.442848,
									longitude	: 9.178994,
									infowindow	: {
										title		: "Qwent&egrave;s Italia S.r.l",
										text		: "Via Meda 25, 20136<br />Milano<br />Italy",
										maxWidth	: 300,
										maxHeigt	: 300,
										open		: true
									}
								}]
	};

})(jQuery);
