function showMapContainer(containerId, localizeMap) {
	document.getElementById(containerId).style.display = 'block';
	localizeMap.show(); // this is to fix the refresh problem when showing the map
}

function hideMapContainer(containerId) {
	document.getElementById(containerId).style.display = 'none';
}

function geolocalizeAddress(localizeMap, oldX, oldY)
{
    var country = document.getElementById('geoCountry').value;
    var city = document.getElementById('geoCity').value;
    var address = document.getElementById('geoStreet').value;
	var number = document.getElementById('geoStreetNumber').value;
    var strAddr = (number ? number + ' ' : '') 
		+ (address ? address + ' ' : '')
		+ (city ? city + ' ' : '')
		+ (country ? country : '');
		
	var elLongitude = document.getElementById('geoLongitude');
	var elLatitude = document.getElementById('geoLatitude');
	
	if (oldX && oldY) {
		localizeMap.addOnlyOneMarker(oldX, oldY, null, null, null, strAddr).setCenterPoint(oldX, oldY, 7).show();
		elLongitude.value = oldX;
		elLatitude.value = oldY;
		
		return; // use the old coordinates do not make geo request
	}
	
    var addr = new Mappy.api.geolocation.AddressLocation(country, city, address, number);
    var geo = new Mappy.api.geolocation.Geocoder();
    geo.geocode(addr, function (results) {
		if (results.length > 0) {
			var x = results[0].Placemark.Point.coordinates[0];
			var y = results[0].Placemark.Point.coordinates[1];
			//results[0].Placemark.name;
			//alert(results[0].Placemark.ExtendedData["mappy:LocalGeocodeLevel"]["mappy:code"]);
			var bestZoom = results[0].getBestZoom() ? results[0].getBestZoom() : 7;
			
			localizeMap.addOnlyOneMarker(x, y, null, null, null, strAddr).setCenterPoint(x, y, bestZoom).show();
			elLongitude.value = x;
			elLatitude.value = y;
			
		} else {
			localizeMap.reset();
			elLongitude.value = '';
			elLatitude.value = '';
		}
	}, function (error) {
		localizeMap.reset();
		elLongitude.value = '';
		elLatitude.value = '';
	});
}


function LocalizeMap(containerId) {
	this.containerId = containerId;
	
	this.map = new Mappy.api.map.Map({
		container:"#"+containerId
	});
	
	this.map.disableScrollWheelZoom();
	
	this.markerLayer = new Mappy.api.map.layer.MarkerLayer(40);
	this.map.addLayer(this.markerLayer);
	
	// default values
	this.x = this.defaultX = 2.396579;
	this.y = this.defaultY = 46.302892;
	this.z = this.defaultZ = 3;
	
	var that = this;
	
	this.map.addListener("zoomend", function() {
		if (that.markerLayer.markersCount > 0) {
			that.markerLayer.explode();
		}
	});
}
LocalizeMap.prototype.initToolBar = function() {
	this.toolBar = new Mappy.api.map.tools.ToolBar(
		{"move": true, "zoom": true, "slider": true},
		new Mappy.api.map.tools.ToolPosition("rb", new Mappy.api.types.Point(5, 15)),
		"vertical"
	);
	this.map.addTool(this.toolBar);
	
	return this;
}
LocalizeMap.prototype.setCenterPoint = function(x, y, z) {
	this.x = x ? x : this.x;
	this.y = y ? y : this.y;
	this.z = z ? z : this.z;
	
	return this;
}
LocalizeMap.prototype.addOnlyOneMarker = function(x, y, icon, iconLabel, toolTipText, popupText) {
	// show only one marker -> remove the previous
	this.removeMarkers();

	var _icon = new Mappy.api.ui.Icon(icon ? icon : Mappy.api.ui.Icon.DEFAULT);
	_icon.label = iconLabel ? iconLabel : 'x';
	
	var _marker = new Mappy.api.map.Marker(new Mappy.api.geo.Coordinates(x, y), _icon);
	if (toolTipText) {
		_marker.addToolTip(toolTipText);
	}
	if (popupText) {
		_marker.addListener("click", function () {
			_marker.openPopUp(popupText);
		});
	}
	
	var elLongitude = document.getElementById('geoLongitude');
	var elLatitude = document.getElementById('geoLatitude');
	
	this.markerLayer.addMarker(_marker);
	_marker.openPopUp(popupText);
	_marker.addDraggable();
	_marker.addListener("dragstop", function() {
		elLongitude.value = _marker.coordinates.x;
		elLatitude.value =  _marker.coordinates.y;
	});
	
	return this;
}
LocalizeMap.prototype.removeMarkers = function() {
	this.markerLayer.clean();
	
	return this;
}
LocalizeMap.prototype.reset = function() {
	this.removeMarkers().setCenterPoint(this.defaultX, this.defaultY, this.defaultZ).show();
}
LocalizeMap.prototype.show = function() {
	this.map.setCenter(new Mappy.api.geo.Coordinates(this.x, this.y), this.z);
	this.refresh(); // for correct centering
}
LocalizeMap.prototype.refresh = function() {
	//console.log("x: " + this.x + " y:" + this.y);
	this.map.setZoomLevel(this.z);
	this.map.slideTo(new Mappy.api.geo.Coordinates(this.x, this.y));
}

LocalizeMap.prototype.addMarker = function(x, y, icon, iconLabel, toolTipText, popupText, popupOpened) {

	var _popupOpened = popupOpened == true ? true : false;

	var _icon = new Mappy.api.ui.Icon(icon ? icon : Mappy.api.ui.Icon.DEFAULT);
	_icon.label = iconLabel ? iconLabel : 'x';
	
	var _marker = new Mappy.api.map.Marker(new Mappy.api.geo.Coordinates(x, y), _icon);
	if (toolTipText) {
		_marker.addToolTip(toolTipText);
	}
	if (popupText) {
		_marker.addListener("click", function () {
			_marker.openPopUp(popupText);
		});
	}
	
	var elLongitude = document.getElementById('geoLongitude');
	var elLatitude = document.getElementById('geoLatitude');
	
	this.markerLayer.addMarker(_marker);
	
	if (_popupOpened == true && popupText) {
		_marker.openPopUp(popupText, false);
	}
	
	return this;
}
// Try to center and zoom the map, so that all the markers are shown/visible
LocalizeMap.prototype.showInBounds = function(preferedZoom) {
	var goeBounds = this.markerLayer.getBounds();

	var x = goeBounds ? goeBounds.center.x : this.x;
	var y = goeBounds ? goeBounds.center.y : this.y;
	var z = goeBounds ? this.map.getBoundsZoomLevel(goeBounds) : this.z;
	
	z =  preferedZoom ? preferedZoom : z;

	this.map.setCenter(new Mappy.api.geo.Coordinates(x, y), z);
	this.markerLayer.explode();
	//this.refresh(); // for correct centering
}


// Regions Map
function RegionsMap(containerId, kmlUrl, searchUrl) {
	this.containerId = containerId;
	this.kmlUrl = kmlUrl;
	this.searchUrl = searchUrl;
	
	this.map = new Mappy.api.map.Map({
		container:"#"+containerId
	});
	
	var that = this;
	
	//this.map.disableDraggable();
    this.map.disableScrollWheelZoom();
    this.map.disableDblClickZoom();
	

    this.shapeLayer =  new Mappy.api.map.layer.ShapeLayer(80);
    this.map.addLayer(this.shapeLayer);
	
	jQuery.get(this.kmlUrl, function (data) {
		that.shapeLayer.clean();
		var json = Mappy.api.utils.xml2json(data);
		var kmlReader = new Mappy.api.map.shape.kml.KmlReader();
		var shapes = kmlReader.getShapes(json.kml);

		for (var i = 1; i < shapes.length; i += 1) {
			that.setStyle(shapes[i], that.containerId, that.searchUrl);
			that.shapeLayer.addShape(shapes[i]);
		}
		
		that.map.setCenter(new Mappy.api.geo.Coordinates(2.37, 46.5), 2);
	});
}
RegionsMap.prototype.setStyle = function (shape, containerId, searchUrl, xitiLevel2) {
	
	var style = shape.getStyle();
	var newFillSyle = "7F00FF00";
	var oldFillStyle = style.getFillStyle();
	
	shape.addListener("click", function () {
		var arrNameId = shape.Placemark.name.split('#');
		if (arrNameId[1]) {
			///////////////////////////// xiti click
			var regName = arrNameId[0].replace(/ /g,"_");
			var xitiPage = '';
			
			if (xitiLevel2 == 2) {
				xitiPage = "recherche_offre::recherche_Carte::"+regName;
			}
			else { // xitiLevel2 == 3
				xitiPage = "recherche_CV::recherche_Carte::"+regName;
			}
			//alert(xitiPage);
			xt_med('C', xitiLevel2, xitiPage, 'N');
			window.location.href = searchUrl + arrNameId[1];
		}
	});
	
	shape.addListener("mouseover", function () {
		jQuery("#"+containerId+" div").css("cursor", "pointer");
		shape.setStyle(style.setFillStyle(newFillSyle));
	});
	
	shape.addListener("mouseout", function () {
		jQuery("#"+containerId+" div").css("cursor", "default");
		shape.setStyle(style.setFillStyle(oldFillStyle));
	});
 }
