/**
 * Google Map View Plugin
 *
 * Easily interact with Google Maps V3 API with this plugin, this is just a simple layer
 * over the top to hide some event complexity and move to a more literal notation way
 * of creating a new instance.
 *
 * Available methods:
 *	
 *		addEvent 		- Add an event to the maps.
 *		addMarker 		- Attach a marker and associated events
 *		addMarkerEvent	- Add events to a Marker.
 *
 * @version 	0.1
 * @author		Luke Robinson
 */

(function($){
		  
	$.fn.mapView = function(opts){
		
		this.canvas = $(this)[0];
		
		var structure = {
			
			init : function(opts)
			{
				this.map = new google.maps.Map(this.canvas, opts);
			},
			
			addEvent : function(events)
			{
				
				var map = this.map;
				
				$.each(events, function(eventType,eventFunction){
							
					var ref = new google.maps.event.addListener(map, eventType, function(eventData){
						eventFunction(ref,eventData);
					});

				});					
			},
			
			addMarker : function(opts)
			{			
				var struct = this;
				var defaults = { map : this.map }
				var opts = $.extend({}, defaults, opts);

				var marker = new google.maps.Marker(opts);
				
				if($.isPlainObject(opts.events))
				{
					struct.addMarkerEvent(marker,opts.events);
				}
				
				(opts.callback || $.noop)(marker);

				return marker;
			},
			
			addMarkerEvent : function(marker,events)
			{
				$.each(events, function(eventType,eventFunction){
				 
					var eventRef = new google.maps.event.addListener(marker, eventType, function(eventData){
						eventFunction(eventRef,eventData,marker);
					});
					
				});
			}
		}
		
		$.extend(this,structure);
		
		this.init(opts);

		return this;
	
	};

})(jQuery);