try
{
    wedebug.addLogger("contextmenu", "ContextMenu");
}
catch(e)
{
    alert(e.message);
}


var ContextMenuController = Class.create();

ContextMenuController.prototype = {
    m_contextMenu : null,
    m_mapController : null,
    
    m_contextMenuItems : [
        {
            caption : "Korteste veg",
            type : "header"
        }, 
        {
            caption : "Herfra",
            type : "trigger",
            callback : function(){alert(arguments);} //Callback-property must be called as function, bound the GUIController this object.
        },
        {
            caption : "Til hit",
            type : "trigger",
            callback : function(){alert("hoi" + arguments.length);}        
        }
    ],
    
    initialize : function(t_mapController) {
        wedebug.log("contextmenu", "ContextMenuController.initialize: " + [t_mapController]);
        this.m_mapController = t_mapController;
        this.m_mapController.addEventListener("oncontextmenu", this._cb_onContextMenu.bind(this));               
    },
    
    /**
    Callback for oncontextmenu event in MapController
    */
    _cb_onContextMenu : function( t_activeMapType, t_pointerX, t_pointerY, t_scrollX, t_scrollY, t_lat, t_lon )
    {
        wedebug.log("contextmenu", "-> _cb_onContextMenu: " + [t_activeMapType,t_pointerX,t_pointerY,t_scrollX,t_scrollY,t_lat,t_lon]);
        this._undoRemoveMenu();
        
        //Set context menu event data. These are used when items are choosen from the context menu
        var t_contextMenuEventValues = {
            controller : this,
            mapType : t_activeMapType,
            pointerX : t_pointerX,
            pointerY : t_pointerY,
            scrollX : t_scrollX,
            scrollY : t_scrollY,
            lat : t_lat,
            lon : t_lon
        };
        
        var t_controlPane = this.m_mapController.getControlPane();
           
        if( this.m_contextMenu != null )
        {
            try
            {
                Element.remove(this.m_contextMenu);
            }
            catch(e)
            {
                wedebug.log("contextmenu", "Element.remove(this.m_contextMenu) threw exception: " + e.message, "warn");
            }
        }
        
        wedebug.log("contextmenu", "Creating context menu panel.");
        this.m_contextMenu = $dom("div", null, null, t_controlPane);
        this.m_contextMenu.id = "contextmenu";
        Event.observe( this.m_contextMenu, "mouseout", this._removeMenu.bind(this, 1000) );
        Event.observe( this.m_contextMenu, "mouseover", this._undoRemoveMenu.bind(this) );
        var lis = [];
        
        
        for( var i=0; i<this.m_contextMenuItems.length; i++ )
        {
            lis.push( $dom("li", this.m_contextMenuItems[i].caption) );
            lis[i].className = this.m_contextMenuItems[i].type;
            if(this.m_contextMenuItems[i].test)this.m_contextMenuItems[i].test();
         
            if( this.m_contextMenuItems[i].type == "trigger" )
            {
                if( typeof this.m_contextMenuItems[i].callback == "undefined" )
                {
                    throw {message : "contextmenu item of type trigger is missing callback function."};
                }
                else
                {
                    //Get reference to callback function triggering the callback function.
                    Event.observe( lis[i], "click", 
                        (function(t_callback, t_contextMenuEventValues)
                        {
                            t_callback.apply(this, arguments) 
                        }).bind(this, this.m_contextMenuItems[i].callback, t_contextMenuEventValues));
                }
            }
        }
        var ul = $dom("ul", null, lis, this.m_contextMenu);            
        
        Element.setStyle(this.m_contextMenu,
            {
                left : parseInt(t_pointerX) + "px",
                top : parseInt(t_pointerY) + "px",
                position : "absolute",
                "background-color" : "White"
            }
        );
            
        
        this._removeMenu(3000);
    },
        
    m_doRemoveMenu : false,
    _removeMenu : function(t_timeout)
    {
        wedebug.log("contextmenu", "_removeMenu: " + t_timeout);
             
        if( !this.m_doRemoveMenu )
        {
            this.m_doRemoveMenu = true;
            setTimeout(
                (function()
                {   
                    wedebug.log("contextmenu", "_removeMenu:trigger " + this.m_doRemoveMenu);      
                    if( this.m_doRemoveMenu )
                    {
                        if(this.m_contextMenu != null)
                        {
                            Element.remove(this.m_contextMenu);
                        }
                        this.m_doRemoveMenu = false;
                    }
                }).bind(this),
                
                t_timeout
            );
        }
    },
    
    _undoRemoveMenu : function()
    {
        wedebug.log("contextmenu", "_undoRemoveMenu");
        
        this.m_doRemoveMenu = false;
    }
}
