(function($) {

		
	/*jQuery.fn.jGMap = {
		
		//settings = jQuery.extend({}, jQuery.fn.DialogLink.defaultSettings, settings);
		create : function(elem_id) {
			return new GMap2(elem_id);
		}
		//return this;
	}
	*/
	
	jGMap = function(elem_id, settings) {
		
		var map;
		
		/**
		 * Settings
		 * 
		 * mapType : map | sat | hybrid
		 * mapCenter : [lat, lng]
		 * mapZoom 1,20
		 * mapControl : large |  | 
		 * mapEnableDragging : 
		 * mapEnableType : 
		 * mapEnableInfoWindows : 
		 * mapEnableDoubleClickZoom : 
		 * mapEnableScrollZoom : 
		 * mapEnableSmoothZoom : 
		 * mapEnableGoogleBar : 
		 * mapEnableScaleControl : 
		 * mapShowjMapsIcon : 
		 */
		this.defaultSettings = {
			'mapType':'map',
			'mapCenter':[43.604510, 1.439896],
			'mapZoom': 15, 
			'mapControl' : 'small',
			'mapEnableDragging' : true,
			'mapEnableType': true,
			'mapEnableInfoWindows' : true,
			'mapEnableDoubleClickZoom' : true,
			'mapEnableScrollZoom' : false,
			'mapEnableSmoothZoom' : true,
			'mapEnableGoogleBar' : false,
			'mapEnableScaleControl' : true,
			'mapEnableOverview' : false,
			'mapShowjMapsIcon' : false
		}
		
		
		
		// Création
		this.id = 'jGMap-' + elem_id;
		this.map = new GMap2(document.getElementById(elem_id));
		this.settings = $.extend(this.defaultSettings, settings);
		this.geocoder = new GClientGeocoder();
		this.geocoder.setBaseCountryCode('fr');
		this.map.Geocoder = this.geocoder;
				
		this.Initialize();
		
	}
	
	jGMap.prototype = {
		
		
		Initialize : function() {
			
			settings = this.settings;
			
			this.icon = null;
			this.markers = [];
			
			// Centre
			this.map.setCenter(
				new GLatLng(settings.mapCenter[0], settings.mapCenter[1]), 
				settings.mapZoom, 
				this.GMapType(settings.mapType)
			);
			
			if(!settings.mapEnableDragging) { this.map.disableDragging(); }
			if(!settings.mapEnableInfoWindows) { this.map.disableInfoWindow(); }
			if(!settings.mapEnableDoubleClickZoom) { this.map.disableDoubleClickZoom(); }
			if(settings.mapEnableScrollZoom) { this.map.enableScrollWheelZoom(); }
			if(settings.mapEnableSmoothZoom) { this.map.enableContinuousZoom(); }
			if(settings.mapEnableGoogleBar) { this.map.enableGoogleBar(); }
			if(settings.mapEnableScrollZoom) { this.map.enableScrollWheelZoom(); }
			
			
			// OverviewMapControl
			if(settings.mapEnableOverview) {
				this.overviewControl = new GOverviewMapControl();
				this.map.addControl(this.overviewControl);
			}
			// ScaleControl
			if(settings.mapEnableScaleControl) {
			}
			
			// TypeControl
			if(settings.mapEnableType) {
				this.typeControl = new GMapTypeControl();
				this.map.addControl(this.typeControl); 
			}
			
			// MapControl
			switch(settings.mapControl) {
				case 'large' : 
					this.control = new GLargeMapControl(); 
					this.map.addControl(this.control); 
				break;
				case 'small' : 
					this.control = new GSmallMapControl(); 
					this.map.addControl(this.control); 
				break;
			}
		},
		
		/**
		 * 
		 */
		GMapType : function(type) {
			switch(type) {
				case 'map':	// Normal Map
					mapType = G_NORMAL_MAP;
				break;
				case 'sat':	// Satallite Imagery
					mapType = G_SATELLITE_MAP;
				break;
				case 'hybrid':	//Hybrid Map
					mapType = G_HYBRID_MAP;
				break;
			};
			return mapType;
		},
		
		/**
		 * 
		 */
		GetTravelMode : function ( mode ) {
			switch(mode) {
				case 'driving':	
					travelMode = G_TRAVEL_MODE_DRIVING;
				break;
				case 'walking':	
					travelMode = G_TRAVEL_MODE_WALKING;
				break;
			};
			return travelMode;
		},
		
		
		/*AddressToLatLng : function(address, instance) {
			this.geocoder.getLatLng(address, instance.
			
		}*/
		
		setCenter : function(address, instance) {//alert(instance);
			if(!instance) {
				var instance = this;
			}
			
			this.map.Geocoder.getLatLng(
				address,
				function(point ) {
					instance.map.setCenter(point);
				}
			);
		},
		
		
		
		AddMarker : function(opt, instance) {
			
			if(!instance) {//alert('iu');
				var instance = this;//alert(instance.id);
			}
			
			defaults = {
				'id' : '',
				'address' : '',
				'HTML' : '',
				'centerMap' : true,
				'autoOpen' : false
			}
			
			opt = $.extend(defaults, opt);
			markerOpt = {};
			
			if(this.icon != null) {
				markerOpt.icon = this.GIcon();
			}
			
			this.geocoder.getLatLng(
				opt.address,
				function(point ) {
					if(point) { //alert(point);
					//coords = new GLatLng(point.Coordinates[1], point.Coordinates[0]);
					var marker = new GMarker(point, markerOpt);//alert(instance.id);
					instance.map.addOverlay(marker);
					if(opt.autoOpen) {
						marker.openInfoWindowHtml(opt.HTML);
					}
					if(opt.centerMap) {
						instance.map.setCenter(point, 13);
					}
					GEvent.addListener(marker, 'click', function(){
						instance.map.setCenter(point);
						marker.openInfoWindowHtml(opt.HTML);
					});
					instance.markers[opt.id] = marker;//alert(instance.markers[opt.id]);
					}
				}
			);
			
			
		},
		
		RemoveMarker : function(id) {alert(this.markers.length);
			if(this.markers[id] != null) {
				this.map.removeOverlay(this.markers[id]);
				this.markers[id] = null;
			}
		},
		
		GIcon : function() {
			if(this.icon != null) {
				icon = new GIcon();
				icon.image = this.icon.uri;
				icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
				icon.iconSize = new GSize(this.icon.w, this.icon.h);
				icon.shadowSize = new GSize(40, 20);
				icon.iconAnchor = new GPoint(16, 16);
				icon.infoWindowAnchor = new GPoint(15, 1);
				return icon;
			}
		},
		
		DefaultIcon : function(uri, w, h) {
			if(uri && w && h) {
				this.icon = {
					'uri' : uri,
					'w' : w,
					'h' : h
				}
			}
		},
		
		Unload : function() {
			GUnload();
		}
		
	}
	
})(jQuery);







	
	function jGIcon(uri, w, h) {
		icon = new GIcon();
		icon.image = uri;
		icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
		icon.iconSize = new GSize(w, h);
		icon.shadowSize = new GSize(40, 20);
		icon.iconAnchor = new GPoint(16, 16);
		icon.infoWindowAnchor = new GPoint(15, 1);
		return icon;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	


