var map;
var geocoder = new GClientGeocoder();
var container = '';

// Create a new icon for CL
var iconCL = new GIcon();
iconCL.image = "./images/gmap16x16.png";
//iconCL.shadow = "./images/gmap.shadow.png";

iconCL.iconSize = new GSize(16, 16);
iconCL.iconAnchor = new GPoint(8, 15);
iconCL.infoWindowAnchor = new GPoint(0, 0);




function gr_unloadGMap(container) {
	document.getElementById(container).innerHTML = "";
	document.getElementById(container).style.display = none;
	GUnload();
}

function gr_ajouterMarqueur(address,texte){
	geocoder.getLatLng(
    	address,
    	function(point) {
			if (!point) {
        		//alert("Unknown location '"+address+"'. Unable to load the map.");
      		} else {
      			gr_addMarker(point, iconCL, texte.replace(/ *, */g, "<br/>"));
      		}
		}
	);
}

function gr_loadGMapWithAddress(c, address, zoom,centre) {
	if (document.getElementById(c)) {
		container = c;
		if (GBrowserIsCompatible()) {
		    map = new GMap2(document.getElementById(container));
			if (typeof zoom == "undefined")
				zoom = 13;
			if(!centre){
			gr_setCenterWithAddress(address, zoom);}
			map.addControl(new GSmallMapControl());
		} else {
			alert("Your browser is not compatible with Google Maps.");
			gr_unloadGMap(container);
		}
	} else {
		alert("Element '"+c+"' doesn't exist.");
		gr_unloadGMap(c);
	}	
}

function gr_setCenterWithAddress(address, zoom) {
	var position_centre_x=45.460131;
	var position_centre_y=-34.277344;
	geocoder.getLatLng(
    	address,
    	function(point) {
			if (!point) {
        		alert("Unknown location '"+address+"'. Unable to load the map.");
				gr_unloadGMap(container);
      		} else {
      			//map.setCenter(point, zoom);
      			map.setCenter(new GLatLng(position_centre_x, position_centre_y), zoom);
      			gr_addMarker(point, iconCL, address.replace(/ *, */g, "<br/>"));
      		}
		}
	);
}

function gr_addMarker(point, icon, text) {
	var marker = new GMarker(point, {icon: icon, clickable: false, draggable: false, bouncy: false});
	GEvent.addListener(marker, "click", function() {
    	marker.openInfoWindowHtml(text);
  	});
	map.addOverlay(marker);
}

