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;Увеличи  </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;Намали  </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;Увеличи тук  </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;Центрирай  </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(!is_small){
    	
    var a_link_add = document.createElement("li");
    a_link_add.setAttribute("id", "li_add");
    a_link_add.innerHTML="<a href=javascript:void(0);>&raquo;Добави обект  </a>";
    this.activeLabel=null;
    this.timer=null;
    GEvent.addDomListener(a_link_add, "click", function(){
        //var url = get_altitude.php?alt_lat=+point.lat()+&alt_lng=+point.lng();   
        that.contextmenu.style.display="none";
        this.style.display="none";
        if($('#li_cancel_get').is(":visible")) GEvent.trigger(document.getElementById("li_cancel_get"), "click");
        //alert("langtitude = "+point.lat()+" Longtitude = "+point.lng());
		if($("#add_object_btn_on").is(":visible")){
			var point = that.map.fromContainerPixelToLatLng(that.clickedPixel);
	        remove_add_marker();
			var new_obj = follow(point);
			GEvent.trigger(document.getElementById("add_object_btn_on"), "click");
			delete point;
		}
		a_link_cancel.style.display="";
    });
    this.ul_container.appendChild(a_link_add);
    
    //stop adding object
    a_link_cancel = document.createElement("li");
    a_link_cancel.setAttribute("id", "li_cancel");
    a_link_cancel.innerHTML="<a href=javascript:void(0);>&raquo;Откажи  </a>";
    this.activeLabel=null;
    this.timer=null;
    a_link_cancel.style.display="none";
    GEvent.addDomListener(a_link_cancel, "click", function(){
        that.contextmenu.style.display="none";
        this.style.display="none";
        //GEvent.trigger(map, "click");
		if($("#add_object_btn_off").is(":visible")){
			remove_add_marker();
			GEvent.trigger(document.getElementById("add_object_btn_off"), "click");
		}
		a_link_add.style.display="";
    });
    this.ul_container.appendChild(a_link_cancel);
    
    
    var a_link_get = document.createElement("li");
    a_link_get.setAttribute("id", "li_add_link");
    a_link_get.innerHTML="<a href=javascript:void(0);>&raquo;Вземи линк  </a>";
    this.activeLabel=null;
    this.timer=null;
    GEvent.addDomListener(a_link_get, "click", function(){
        //var url = get_altitude.php?alt_lat=+point.lat()+&alt_lng=+point.lng();   
        that.contextmenu.style.display="none";
        this.style.display="none";
        //alert("langtitude = "+point.lat()+" Longtitude = "+point.lng());
        if($('#li_cancel').is(":visible")) GEvent.trigger(document.getElementById("li_cancel"), "click");
        if($("#get_link_on").is(":visible")){ 
			var point = that.map.fromContainerPixelToLatLng(that.clickedPixel);
		    remove_add_marker();
			var new_obj = follow_get_link(point);
			GEvent.trigger(document.getElementById("get_link_on"), "click");
			delete point;
			
        }
		a_link_cancel_get.style.display="";
		
    });
    this.ul_container.appendChild(a_link_get);
    
    //stop get link
    a_link_cancel_get = document.createElement("li");
    a_link_cancel_get.setAttribute("id", "li_cancel_get");
    a_link_cancel_get.innerHTML="<a href=javascript:void(0);>&raquo;Откажи  </a>";
    this.activeLabel=null;
    this.timer=null;
    a_link_cancel_get.style.display="none";
    GEvent.addDomListener(a_link_cancel_get, "click", function(){
        that.contextmenu.style.display="none";
        this.style.display="none";
        //GEvent.trigger(map, "click");
		
		if($("#get_link_off").is(":visible")){
			remove_add_marker();
			GEvent.trigger(document.getElementById("get_link_off"), "click");
		}
		
		a_link_get.style.display="";
    });
    this.ul_container.appendChild(a_link_cancel_get);
    
    
	    if(usertype == 2){
		    a_link = document.createElement("li");
		    a_link.innerHTML="<a href=javascript:void(0);>&raquo;Изчертай квартал  </a>";
		    GEvent.addDomListener(a_link, "click", function() {
		        if(polygon != 0){ 
		        	map.removeOverlay(polygon);
		        	GEvent.removeListener(polygonClickListener);
		        	for(t in polylineArr){
						map.removeOverlay(polylineArr[t]);
					}
					arrayP = [];
					polylineArr = [];
		        }
		        draw_polygon();
		        that.contextmenu.style.display="none";
		    });
		    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) {
        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";
    });   
}