function ContextMenu(oMap){this.initialize(oMap);}

//Construct the DOM tree of the menu
ContextMenu.prototype.initLink = function(oMap) {


	var that=this;

	a_link = document.createElement("li");
	a_link.innerHTML="<a href='javascript:void(0);'>&raquo;Zoom In  </a>";
	GEvent.addDomListener(a_link, 'click', function() {
		that.map.zoomIn();
		that.contextmenu.style.display='none';
	});
	this.ul_container.appendChild(a_link);


	a_link = document.createElement("li");
	a_link.innerHTML="<a href='javascript:void(0);'>&raquo;Zoom Out  </a>";
	GEvent.addDomListener(a_link, 'click', function() {
		that.map.zoomOut();
		that.contextmenu.style.display='none';
	});
	this.ul_container.appendChild(a_link);

	a_link = document.createElement("li");
	a_link.innerHTML="<a href='javascript:void(0);'>&raquo;Zoom Here  </a>";
	GEvent.addDomListener(a_link, 'click', function() {
		var point = that.map.fromContainerPixelToLatLng(that.clickedPixel);
		that.map.zoomIn(point,true);
		that.contextmenu.style.display="none";
	});
	this.ul_container.appendChild(a_link);

	a_link = document.createElement("li");
	a_link.innerHTML="<a href='javascript:void(0);'>&raquo;Center Map Here  </a>";
	GEvent.addDomListener(a_link, 'click', function() {
		var point = that.map.fromContainerPixelToLatLng(that.clickedPixel);
		that.map.panTo(point);
		that.contextmenu.style.display="none";
	});
	this.ul_container.appendChild(a_link);

	if(!routeReadOnly){
		a_link = document.createElement('li');
		a_link.innerHTML="<a href='javascript:void(0);'>&raquo;Remove marker  <img id='removeImg' src='../images/empty.gif' border='0'></a>";
		GEvent.addDomListener(a_link, 'click', function() {
			//var point = that.map.fromContainerPixelToLatLng(that.clickedPixel);
			removeMarker(contextOpenedOverMarker);
			that.contextmenu.style.display="none";
		});
		this.ul_container.appendChild(a_link);
	}

/*
	a_link = document.createElement("li");
	a_link.innerHTML="<a href='javascript:void(0);'>&raquo;Altitude  </a>";
	this.activeLabel=null;
	this.timer=null;
//This will manage the request for the altitude: once the result will be shown on a TLabel, and will disappear after 7 seconds.
	GEvent.addDomListener(a_link, 'click', function(){
		var point = that.map.fromContainerPixelToLatLng(that.clickedPixel);
		var url = 'ajax/get_altitude.php?alt_lat='+point.lat()+'&alt_lng='+point.lng();
		that.contextmenu.style.display="none";
		GDownloadUrl(url, function(data, responseCode) {
//This is the returned value if no data is available
			if(data=="-32768"){
				var retval = "N/A";
			}else{
				var retval = data + " m.";
			}
			if(that.activeLabel!=null || that.timer!=null){
				map.removeTLabel(that.activeLabel);
				window.clearTimeout(that.timer);
				that.timer=null;
			}
			var label = new TLabel();
			label.id = 'tlabel_altezza';
			label.anchorLatLng =point;
			label.percentOpacity = 85;
			label.content = '<div style="width:6em;font-weight:bold;border:1px solid blue;background:#E5ECF9;color:#3300FF;padding:4px;">'+retval+'</div>';
			map.addTLabel(label);
			var cb = that.bind(function(){map.removeTLabel(that.activeLabel);that.activeLabel=null;that.timer=null;});
			that.timer=window.setTimeout(cb,7000);
			that.activeLabel=label;
		});
	});
	this.ul_container.appendChild(a_link);
	*/
}
ContextMenu.prototype.bind = function(method) {
	var self = this;
	var opt_args = [].slice.call(arguments, 1);
	return function() {
		var args = opt_args.concat([].slice.call(arguments));
		return method.apply(self, args);
	}
}

//The object 'constructor'
ContextMenu.prototype.initialize = function(oMap){
	this.map = oMap;
	var that=this;

	this.contextmenu = document.createElement("div");
	this.contextmenu.style.display="none";
//CSS class name of the menu
	this.contextmenu.className="contextmenu";
	this.ul_container = document.createElement("ul");
	this.ul_container.id="context_menu_ul";
	this.contextmenu.appendChild(this.ul_container);
	this.initLink();
	this.map.getContainer().appendChild(this.contextmenu);

//Event listeners that will interact with our context menu
	GEvent.addListener(oMap,"singlerightclick",function(pixel,tile) {


		contextOpenedOverMarker = contextIsOverMarker;
		if(!routeReadOnly){
			if(contextIsOverMarker < 0 ) {
				//alert('hide it: '+contextIsOverMarker);
				document.getElementById('context_menu_ul').lastChild.style.display="none";
			}
			else {
					document.getElementById('context_menu_ul').lastChild.style.display = "block";
					document.getElementById('removeImg').src=gmarkersPropertiesArray[contextOpenedOverMarker].img;
			}
		}

		that.clickedPixel = pixel;
		var x=pixel.x;
		var y=pixel.y;
//Prevents the menu to go out of the map margins, in this case the expected menu size is 150x110
		if (x > that.map.getSize().width - 160) { x = that.map.getSize().width - 160 }
		if (y >that.map.getSize().height - 120) { y = that.map.getSize().height - 120 }
		var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));
		pos.apply(that.contextmenu);
		that.contextmenu.style.display = "";

	});
	GEvent.addListener(oMap, "move", function() {
		that.contextmenu.style.display="none";
	});
	GEvent.addListener(oMap, "click", function(overlay,point) {
		that.contextmenu.style.display="none";
	});
}




//context = new ContextMenu(map);
