/**
 * Plugin: jquery.zGoogleMap
 * 
 * Version: 1.0.0
 * (c) Copyright 2010, Zazar Ltd
 * 
 * Description: jQuery plugin for displaying maps via Google Maps API v3
 * 
 * History:
 * 
 **/

(function($){
    
	var current = null; 
    
	$.fn.GoogleMap = function(addresses, titles, details, options) {
        
        	// Set current instance
		var self = this;
		if (!current) {
			current = self;
		}
		
		// Set pluign defaults
		var defaults = {  
			type: 0,
			width: '627px',
			height: '400px',
			zoom: 10,
			clickable: true,
			tooltip: true,
			tipsuffix: ' (click for more)'
		};  
		var options = $.extend(defaults, options); 

		// Select map type
		var maptype = getGoogleMapID(options.type);
		
		// Set Map defaults
		var apiDefaults = {
			mapTypeId: maptype,
			zoom: options.zoom
		}

		// Initialise Map variables
		var apiCenter = new google.maps.LatLng(0,0);
		var apiMap = new google.maps.Map(this.get(0), $.extend(apiDefaults, { center: apiCenter }));
		var apiGeocoder = new google.maps.Geocoder();
		
		// Functions
		$.extend(self, {
            
            		initialise: function() {
		
				// Add map class to user div
				if (!current.hasClass('mapGoogle')) {
					current.addClass('mapGoogle');
					current.css('width',options.width);
					current.css('height',options.height);
				}

				// Loop through addresses adding markers
				if (addresses) {
					if (addresses.length > 0) {
						var i = 0;
						while (i < addresses.length) {
							
							// Plot location
							self.geocode(i++);
						}
					}
				}
			},
			geocode: function(index) {

				// Check for valid address array
				if (addresses && index >= 0) {
				
					// Get Lat/Long values from address
					apiGeocoder.geocode({ address: addresses[index] }, function(results, status) {
			
						if (status == google.maps.GeocoderStatus.OK && results.length) {
				
							if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
								
								// Center in map on location
								apiMap.setCenter(results[0].geometry.location);
								
								// Create tooltip text
								var title = '';
								if (options.tooltip) title = titles[index] + options.tipsuffix;

								// Add marker to map
								var apiMarker = new google.maps.Marker({
									position: results[0].geometry.location,
									map: apiMap,
									title: title
								});
					
								// Create info window
								var apiInfoWindow = new google.maps.InfoWindow({
									content: details[index]
								});
					
								// Create 'click' event and attach info window to marker
								if (options.clickable)
									//google.maps.event.addListener(apiMarker, 'click', function() {
										apiInfoWindow.open(apiMap, apiMarker);
									//});
							}
						}
					});
				}
			}
		});

		// Convert map type to Google Map ID
		function getGoogleMapID(map) {
			var mapid = google.maps.MapTypeId.ROADMAP;

			switch (map) {
				case 1:
					mapid = google.maps.MapTypeId.SATELLITE;
					break;
				case 2:
					mapid = google.maps.MapTypeId.HYBRID;
					break;
				case 3:
					mapid = google.maps.MapTypeId.TERRAIN;
					break;
			}

			return mapid;
        	}
		
		// Load the map		
		function load() {
			self.initialise();
			return self;
        	}
        
        	load();
	};
})(jQuery);

