function SimpleGMap(el, x, y, zoom, hybrid, el_x, el_y)
{
	var el = $(el);
	
	if(el)
	{
		this.startLocationX = x.toFloat();
		this.startLocationY = y.toFloat();
		this.startZoom = zoom.toInt();
		if(this.startZoom < 1) this.startZoom = 1;
		this.startHybrid = (hybrid == true);
		
		this.el = el;
		this.el_x = $(el_x);
		this.el_y = $(el_y);
	}
}

SimpleGMap.prototype.el = null;
SimpleGMap.prototype.dirEl = null;
SimpleGMap.prototype.el_x = null;
SimpleGMap.prototype.el_y = null;
SimpleGMap.prototype.map = null;
SimpleGMap.prototype.geocoder = null;
SimpleGMap.prototype.gdir = null;
SimpleGMap.prototype.startLocationX = null;
SimpleGMap.prototype.startLocationY = null;
SimpleGMap.prototype.startZoom = 13;
SimpleGMap.prototype.startHybrid = false;
SimpleGMap.prototype.currentMarker = null;
SimpleGMap.prototype.draggable = false;
SimpleGMap.prototype.dragend = null;
SimpleGMap.prototype.centerOnDrag = null;

SimpleGMap.prototype.setupDraggable = function(draggable, centerOnDrag, onDragEnd)
{
	this.draggable = (draggable == true);
	this.centerOnDrag = (centerOnDrag == true);
	this.dragend = onDragEnd;
}

SimpleGMap.prototype.setupDirectionControl = function(el)
{
	this.dirEl = $(el);
}

SimpleGMap.prototype.display = function()
{
	if(this.el)
	{
		this.el.style.display = 'block';
		this.map = new GMap2(this.el);
		
		//Positionne le marqueur
		this.setPosition(new GLatLng(this.startLocationX, this.startLocationY));
		
		this.map.setMapType(this.startHybrid ? G_HYBRID_MAP : G_NORMAL_MAP);
		
		this.map.addControl(new GSmallZoomControl3D());
		this.map.addControl(new GMenuMapTypeControl());
		
		if(this.dirEl)
		{
			this.gdir = new GDirections(this.map, this.dirEl);
			GEvent.addListener(this.gdir, 'error', this.handleErrors.bind(this));
		}
	}
}

SimpleGMap.prototype.setMapType = function(type)
{
	this.map.setMapType(type);
}

SimpleGMap.prototype.getDirectionFrom = function(from)
{
	this.gdir.loadFromWaypoints([from, this.currentMarker.getLatLng()], { locale : 'fr_FR' });
}

SimpleGMap.prototype.getDirectionTo = function(to)
{
	this.gdir.loadFromWaypoints([this.currentMarker.getLatLng(), to], { locale : 'fr_FR' });
}

SimpleGMap.prototype.setMarkerInfo = function(info)
{
	this.currentMarker.bindInfoWindowHtml(info);
}

SimpleGMap.prototype.handleErrors = function()
{
	var code = this.gdir.getStatus().code;
	var msg = '';
	
	if(code == G_GEO_UNKNOWN_ADDRESS)
	{
		msg = 'L\'adresse donnée n\'a pu être localisée, veuillez la vérifier et recommencer.';
	}
	else if(code == G_GEO_BAD_KEY)
	{
		msg = 'Clé Google Map invalide.';
	}
	else if(code == G_GEO_BAD_REQUEST)
	{
		msg = 'La requête a été mal formulée et n\'a pu être traitée.';
	}
	else //G_GEO_SERVER_ERROR || G_GEO_MISSING_QUERY
 	{
		msg = 'Une erreur est survenue lors de la demande de directions.';
 	}
	
	var errorDiv = $('map_error');
	errorDiv.adopt(new Element('p', { text: msg }));
}

SimpleGMap.prototype.saveCoord = function()
{
	if(this.el_x && this.el_y)
	{
		var coord = this.currentMarker.getLatLng();
		
		this.el_x.value = Math.round(coord.lat()*1000000) / 1000000;
		this.el_y.value = Math.round(coord.lng()*1000000) / 1000000;
	}
}

SimpleGMap.prototype.setPosition = function(point, zoom)
{
	if(this.currentMarker != null)
	{
		this.currentMarker.setLatLng(point);
		this.map.panTo(point);
	}
	else
	{
		this.currentMarker = new GMarker(point, {draggable : this.draggable});
		if(this.draggable) GEvent.addListener(this.currentMarker, "dragend", this.onDragEnd.bind(this));
		this.map.addOverlay(this.currentMarker);
		this.map.setCenter(point, this.startZoom);
	}
	
	if(zoom) this.map.setZoom(zoom);
	
	this.saveCoord();
}

SimpleGMap.prototype.onDragEnd = function()
{
	if(this.centerOnDrag == true) this.map.panTo(this.currentMarker.getLatLng());
	if(this.dragend != null) this.dragend();
	this.saveCoord();
}

SimpleGMap.prototype.getAddressCoord = function(address, fn)
{
	if (this.geocoder == null) this.geocoder = new GClientGeocoder();
	
	var self = this;
	this.geocoder.getLatLng(
		address,
		fn
	);
}