function quitarMenuContextual() {
	if (document.getElementById('menuContext') != null)
		document.getElementById('map').removeChild(document.getElementById('menuContext'));
}
function mostrarMenuContextual(caurrentLatLng, opcion) {
	var projection;
	var contextmenuDir;
	projection = map.getProjection();
	quitarMenuContextual();
	contextmenuDir = document.createElement("div");
	contextmenuDir.className  = 'contextmenu';
	contextmenuDir.id = 'menuContext';
	contextmenuDir.style.position = 'absolute';
	
	if (opcion == 1)
		contextmenuDir.innerHTML = '<a href="javascript:establecerPosicion' + caurrentLatLng + ';" class="estoyAqui">Estoy aquí<\/a>';
	else
		contextmenuDir.innerHTML = '<a href="javascript:cambiarLatLng' + caurrentLatLng + ';" class="estoyAqui">Ubicación<\/a>';
	
	document.getElementById('map').appendChild(contextmenuDir);
	
	setMenuXY(caurrentLatLng);

	contextmenuDir.style.visibility = "visible";
}

function getCanvasXY(caurrentLatLng){
	var scale = Math.pow(2, map.getZoom());
	var nw = new google.maps.LatLng(
		map.getBounds().getNorthEast().lat(),
		map.getBounds().getSouthWest().lng()
	);
	var worldCoordinateNW = map.getProjection().fromLatLngToPoint(nw);
	var worldCoordinate = map.getProjection().fromLatLngToPoint(caurrentLatLng);
	var caurrentLatLngOffset = new google.maps.Point(
		Math.floor((worldCoordinate.x - worldCoordinateNW.x) * scale),
		Math.floor((worldCoordinate.y - worldCoordinateNW.y) * scale)
	);
	return caurrentLatLngOffset;
}

function setMenuXY(caurrentLatLng) {
	var mapWidth = 580;
	var mapHeight = 400;
	var menuWidth = 100;
	var menuHeight = 30;
	var clickedPosition = getCanvasXY(caurrentLatLng);
	var x = clickedPosition.x ;
	var y = clickedPosition.y ;

	if((mapWidth - x ) < menuWidth) //if to close to the map border, decrease x position
		x = x - menuWidth;
	if((mapHeight - y ) < menuHeight) //if to close to the map border, decrease y position
		y = y - menuHeight;

	document.getElementById('menuContext').style.left = x + "px";
	document.getElementById('menuContext').style.top = y + "px";
};

function establecerPosicion(lat, lng) {
	posicionEncontrada(lat, lng, "");
}
