function MMContextmenu(observe, event)
{		
	this.observe = observe;
	this.event = event;
	
	// container for the contextmenu
	this.div = document.createElement("div");	
	this.div.style.background = "#ffffff";
	this.div.style.borderColor = "#cccccc #676767 #676767 #cccccc";
	this.div.style.borderWidth = "1px";
	this.div.style.borderStyle = "solid";
	this.div.style.cssFloat = "left";
	this.div.style.styleFloat = "left";
	
	
	// pane for appending to the map
	this.pane = document.createElement("div");
	this.pane.style.display = "none";
	this.pane.appendChild(this.div);
	
	this.clickedLatLng;
	this.arguments = new Array();
	this.map;
	
}
MMContextmenu.prototype = new GControl();

// display only one contextmenu at the same time
MMContextmenu.someIsVisible = false;

/** Returns to the map if the control should be printable. */
MMContextmenu.prototype.printable = function(){return false;}


/** Returns to the map if the control contains selectable text. */
MMContextmenu.prototype.selectable = function(){return false;}

/**
 * Returns to the map the position in the map view at which the control appears by default. 
 * This will be overridden by the second argument to GMap2.addControl(). 
 */
MMContextmenu.prototype.getDefaultPosition = function(){
	return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(0,0));	
}

/**
 * Will be called by the map so the control can initialize itself. The control will 
 * use the method GMap2.getContainer() to get hold of the DOM element that contains 
 * the map, and add itself to it. It returns the added element. 
 */
MMContextmenu.prototype.initialize = function(map){

	this.map = map;
	map.getContainer().appendChild(this.pane);     
	
	GEvent.bind(map, "click", this, function(){this.hide();});
	GEvent.bind(map, "movestart", this, function(){this.hide();});
	GEvent.bind(map, "moveend", this, function(){this.hide();});
    
	GEvent.bind(this.observe, "dragstart", this, function(){this.hide();});  	
	
	GEvent.bind(this.observe, this.event, this, function(point, src, overlay) {        
		
			/* store the "pixel" info in case we need it later
			 * adjust the context menu location if near an egde
			 * create a GControlPosition
			 * apply it to the context menu, and make the context menu visible
			 */		
			this.clickedLatLng = map.fromContainerPixelToLatLng(point);
			for(var i=1;i<arguments.length;i++)this.arguments.push(arguments[i]);
			var x = point.x;
			var y = point.y;
			
			if (x > map.getSize().width - 120) { x = map.getSize().width - 120 }
			if (y > map.getSize().height - 100) { y = map.getSize().height - 100 }
			var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));  
			pos.apply(this.pane);
			this.show();
	});

	return this.pane;      
}

MMContextmenu.prototype.hide = function(){
	this.pane.style.display="none";
	MMContextmenu.someIsVisible = false;
}

MMContextmenu.prototype.show = function(){
	if(!MMContextmenu.someIsVisible){
		this.pane.style.display="block";
		MMContextmenu.someIsVisible = true;
	}
}

/**
 * Adds a new MenuItem with its callback method
 * The geographical coordinates of the point that was clicked are passed in the method argument.
 */
MMContextmenu.prototype.addItem = function(title, object, method)
{ 			
	var element = document.createElement("div");	
	element.style.cursor = "pointer";
	element.innerHTML = '<div style="color:#0000cc;font-size:small;font-family:arial,sans-serif;text-decoration:none;">&nbsp;&nbsp;'+ title +'&nbsp;&nbsp;</div>';
		
	this.div.appendChild(element);
	
	GEvent.bindDom(element, "mouseover", this, function(){
		element.style.background = "#d3e3fe";
	});
	GEvent.bindDom(element, "mouseout", this, function(){
		element.style.background = "#ffffff";
	});
	GEvent.bindDom(element, "click", this, function(){
		this.hide();
		
		var args = new Array();
		args.push(this.clickedLatLng);		
		for(var i=0;i<this.arguments.length;i++)args.push(this.arguments[i]);
		return method.apply(object, args);
	});
}

/**
 * Adds a horizontal line
 */
MMContextmenu.prototype.addSeparator = function(){
	var div = document.createElement('div');	
	div.style.borderTop = "1px solid #cccccc";
	div.style.margin = "0px";
	this.div.appendChild(div);
	this.div = div;
}
