﻿    
    // ContextMenuBehavior.js 

    Type.registerNamespace('EdgarOnline.UI');

    EdgarOnline.UI.ContextMenuBehavior =  function(element){
    EdgarOnline.UI.ContextMenuBehavior.initializeBase(this,[element]);
    
    // Private fields.
    var _contextMenuHandler;
    var _mousedownHandler;
    this._contextMenuID;
    var _contextMenu;
    var _menuBehavior;
    
  // _menuBehavior = new Sys.UI.PopupBedhavior();
    }
   

    EdgarOnline.UI.ContextMenuBehavior.prototype = {
   initialize:function()
    {
        EdgarOnline.UI.ContextMenuBehavior.callBaseMethod(this, 'initialize');
       
       _contextMenu = new Sys.UI.Control(this._element);
       var element = this.get_element();
       this._promptDiv = document.createElement('div');
       this._menuBehavior = $create(AjaxControlToolkit.PopupBehavior, { parentElement : element }, {}, {}, this._promptDiv);

        _contextMenu.initialize();
      
//    _contextMenuHandler = Function.createDelegate(this, contextMenuHandler);      
//        _mousedownHandler = Function.createDelegate(this, mousedownHandler);       
       $addHandlers(this.get_element(),{
                                        'oncontextmenu':this. contextMenuHandler,
                                        'onmousedown':this.mousedownHandler
                                        }, this);

    },
     // Properties.
    get_contextMenuID : function() {
        return this._contextMenuID;
    },
    
    set_contextMenuID : function(value) {
        if(value != this._contextMenuID) {
            this._contextMenuID = value;
            this.raisePropertyChanged('contextMenuID');
        }
        },
    dispose: function()
    {
        $clearHandlers(this.get_element());
        EdgarOnline.UI.ContextMenuBehavior.callBaseMethod(this, 'dispose');
        
    },
   
    // Handlers.
    contextMenuHandler:function()
    {
        var evt = window.event;
        var target = evt.srcElement;
        
        if (target == this.control.element) {
        
            var bounds = Sys.UI.Control.getBounds(target);
            var labelText = target.innerHTML;
            var labelId = target.id;
            var menuTemplate = document.getElementById('contextMenuTemplate');
            var menuText = menuTemplate.innerHTML;
            var menuDiv = document.getElementById('contextMenuText');
            var menuShadow = document.getElementById('contextMenuShadow');
            
            
            // Process Menu Parameters
            var menuParams = document.getElementsByTagName("input");

            for (i=0; i<menuParams.length; i++)
            {
                if (menuParams[i].className == 'contextMenuParam')
                {
                    menuText = menuText.replace(eval("/#" + menuParams[i].id + "#/g"), menuParams[i].value);
                }
            }

            if (labelId)
                menuText = menuText.replace(/#id#/g, labelId);
                
            if (labelText)
                menuText = menuText.replace(/#text#/g, labelText);
            
            menuDiv.innerHTML = menuText;
                     
            _menuBehavior.set_x(bounds.width);
            _menuBehavior.show();
            
            this.contextMenuShow.invoke(this, Sys.EventArgs.Empty);
            
            if (document.getElementById && !document.all)
            {
                menuShadow.style.height = menuDiv.offsetHeight + 'px';            
                menuShadow.style.width = menuDiv.offsetWidth + 'px';
            }
            else
            {
                menuShadow.style.height = menuDiv.clientHeight;            
                menuShadow.style.width = menuDiv.clientWidth;            
            }
        
            evt.returnValue = false;
        }
        
        // Uncomment the following line to disable the Context Menu in general
        // evt.returnValue = false;
    },
    
   mousedownHandler: function()
    {
          var x = window.event.clientX;
          var y = window.event.clientY;
          
          var bounds = Sys.UI.Control.getBounds(document.getElementById('contextMenuText'));
          
          if(!((x >= bounds.x) && (x <= bounds.x + bounds.width)
           && (y >= bounds.y) && (y <= bounds.y + bounds.height))) {
              this.hideMenu();
          }
    },
   
    // Public methods.
    hideMenu: function() {
        _menuBehavior.hide();
    },
   
    // getDescriptor.
    getDescriptor : function()
    {
        var td = EdgarOnline.UI.ContextMenuBehavior.callBaseMethod(this, 'getDescriptor');
       
        td.addProperty('contextMenuID', String);
        td.addEvent('contextMenuShow', true);
        td.addMethod('hideMenu');
        
        return td;
    }
    }


EdgarOnline.UI.ContextMenuBehavior.registerClass('EdgarOnline.UI.ContextMenuBehavior', Sys.UI.Behavior);
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

//Sys.TypeDescriptor.addType('script', 'contextMenuBehavior', EdgarOnline.UI.ContextMenuBehavior);

