// ********************
// Name:	jQuery Googlemap
// Date: 28 augustus 2009
// Version:	1.0
// Author: Pim Hoogendoorn - the people's valley
// Create Google maps in a second
// ********************
//	TO DO: Plaats/Postcode/Adres invullen = Lat & Long automatisch opzoeken
//
//	Zoomlevel (default=12): On a scale from 1 to 20. 1 is far and 20 is close.
//
//	Controls (default=false):
//	 new GLargeMapControl() = Full controls [arrows & zoomlevel-slider ]
//	 new GSmallMapControl() = Small controls [arrows & +/-]
//	 new GSmallZoomControl() = Mini controls [+/-]
//	 new GScaleControl() = Scale controls
//	 new GMapTypeControl() = User can switch maptype
//	 new GOverviewMapControl() = Gives a small overview window in the right corner
// --------------------------------
(function($){
 $.fn.mapit = function(options) {

	// Map options
  var settings = {
		address: '1101 ee, amsterdam',
		zoomLevel: 12, // Zoom level
		mapType: 'normal', // Type of map
		mapOverview: true, // Type of controls
		control: true,
		controlType: new GLargeMapControl(), // Controls to use when control = true
		showLabel: true, // Infobox for marker
		infoText: null, // Text to use in the infoBox when showLabel = true
		errormsg: 'Sorry, er heeft zich een error voorgedaan. De map kan niet worden geladen.'
  };

	var options = $.extend(settings, options);
  return this.each(function() {
		var base = $(this).attr('id'); // Set the base
		var map = null;
    var geocoder = null;
		init(); // Initialize settings and build the map
		
		function init(){
			$('html').attr('xmlns:v', 'urn:schemas-microsoft-com:vml');
			loadMap(settings.address);
		}
		
		function loadMap(address){
			if (GBrowserIsCompatible()) {
				if (document.getElementById(base)) {
					// Display the map, with some controls and set the initial location
					var map = new GMap2(document.getElementById(base));
					geocoder = new GClientGeocoder();
					
					// Check settings
					if(settings.infoText == null){ settings.infoText = settings.address;}
					if(settings.mapType == 'normal'){ mapType = G_NORMAL_MAP; }
					else if (settings.mapType == 'satellite'){ mapType = G_SATELLITE_MAP;}
					else if (settings.mapType == 'hybrid'){ mapType = G_HYBRID_MAP;}
					if(settings.mapOverview==true){ map.addControl(settings.controlType); } // Type of controls
					
					map.setMapType(mapType, settings.zoomLevel); // Type of map
					
					 if (geocoder) {
							geocoder.getLatLng(
								address,
								function(point) {
									if (!point) {
										alert("Adres niet gevonden...");
									} else {
										map.setCenter(point, settings.zoomLevel);
										// Draw the map
										if(settings.showLabel==true){
											marker = createMarker(point, settings.infoText);
											map.addOverlay(marker);
											marker.openInfoWindowHtml(settings.infoText);	
										} else {
											marker = createMarker(point, settings.infoText);
											map.addOverlay(marker);
										}
									}
								}
							);
						}
					
				}
			}
			else { 
				alert(settings.errormsg); // display a warning if the browser was not compatible
			}
		}

		function createMarker(point,html) {
			var marker = new GMarker(point);
			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(html);
			});
			return marker;
		}
		
  });
 };
})(jQuery);