function attachNodeEventManager(node)
  {
  if (node.eventList == null)
    {
    node.eventList = {};
    }

  node.removeSharedEvent = function(eventName, index)
    {
    this.eventList[eventName][index] = null;
    }



  node.attachSharedEvent = function(eventName, fn)
    {
    if (this.eventList[eventName] == null)
      {
      this.eventList[eventName] = new Array();//create a new array

      if (this['on' + eventName] != null)//Add any previously set events to our list.
        {
        this.eventList[eventName][this.eventList[eventName].length] = this['on' + eventName];
        }

      this['on' + eventName] = function(eventObj)//set the new default function
        {
        if (eventObj == null)
          {
          eventObj = event;
          }

        if (this.eventList == null || this.eventList[eventName] == null)
          {
          return;
          }

        var ret = null;
        var toRet = null;
        for(var i = 0; i < this.eventList[eventName].length; i++)
          {
          if (this.eventList[eventName][i] == null)
            {
            continue;
            }
          if (this.eventList[eventName][i].constructor == objfn)
            {
            ret = this.eventList[eventName][i].run(eventObj);
            }else
            {
            ret = this.eventList[eventName][i](eventObj);
            }
          if (ret != null)
            {
            toRet = ret;
            }
          }
        if (toRet != null)
          {
          return toRet;
          }
        }

      if (eventName == "drag")
        {
        this.startDragChecking = function(eventObj)
          {
          this.cleanupDragChecking();
          this.dragCheckingData['bodyMouseMoveIndex'] = attachNodeEvent(document.body, 'mousemove', new objfn('monitorDragChecking', this));
//          this.dragCheckingData['bodyMouseOutIndex'] = attachNodeEvent(window, 'mouseout', new objfn('cleanupDragChecking', this));
          this.dragCheckingData['bodyMouseUpIndex'] = attachNodeEvent(document.body, 'mouseup', new objfn('cleanupDragChecking', this));
          this.dragCheckingData['originalClientX'] = eventObj.clientX;
          this.dragCheckingData['originalClientY'] = eventObj.clientY;
          }

        this.monitorDragChecking = function(eventObj)
          {
          var currDragDistance = Math.sqrt(Math.pow(this.dragCheckingData['originalClientX']- eventObj.clientX, 2) + Math.pow(this.dragCheckingData['originalClientY']- eventObj.clientY, 2));
          if (currDragDistance > 3)
            {
            this.cleanupDragChecking();
            this.ondrag(eventObj);
            }
          }

        this.cleanupDragChecking = function()
          {
          if (this.dragCheckingData['bodyMouseMoveIndex'] != null)
            {
            document.body.removeSharedEvent('mousemove', this.dragCheckingData['bodyMouseMoveIndex']);
            this.dragCheckingData['bodyMouseMoveIndex'] = null;
            }
          if (this.dragCheckingData['bodyMouseOutIndex'] != null)
            {
            window.removeSharedEvent('mouseout', this.dragCheckingData['bodyMouseOutIndex']);
            this.dragCheckingData['bodyMouseOutIndex'] = null;
            }
          if (this.dragCheckingData['bodyMouseUpIndex'] != null)
            {
            document.body.removeSharedEvent('mouseup', this.dragCheckingData['bodyMouseUpIndex']);
            this.dragCheckingData['bodyMouseUpIndex'] = null;
            }
          }

        this.attachSharedEvent('mousedown', new objfn('startDragChecking', this));
        this.dragCheckingData = {};
        }

      //IE is not logical
      if (window == this)
        {
        window['on' + eventName] = this['on' + eventName];
        }
      }

    var freeSpot = this.eventList[eventName].length;

    for(var i = 0; i < this.eventList[eventName].length; i++)
      {
      if (this.eventList[eventName][i] == null)
        {
        freeSpot = i;
        break;
        }
      }

    this.eventList[eventName][freeSpot] = fn;
    return freeSpot;
    }


  node.deattachSharedEvent = function(eventName, index)
    {
    this.removeSharedEvent(eventName, index);
    }
  }

function attachNodeEvent(node, event, fn)
  {
  if (node == null)
    {
    return;
    }
  if (node.attachSharedEvent == null)
    {
    attachNodeEventManager(node);
    }
  return node.attachSharedEvent(event, fn);
  }


function detachNodeEvent(node, event, index)
  {
  if (node == null)
    {
    return;
    }
  if (node.attachSharedEvent == null)
    {
    attachNodeEventManager(node);
    }
  return node.detachSharedEvent(event, index);
  }
