function cTrail()
{
  this.offsetfrommouse = [10, 10]; //image x,y offsets from cursor position in pixels. Enter 0, 0 for no offset
  this.object = null;
  this.currentElementId = null;
  this.display = false;
  this.hold = false;
  this.keep = false;
  this.xLastCoord = 0;
  this.yLastCoord = 0;
  this.orginalObjectWidth = 0;
  this.orginalObjectHeight = 0;
  //this.isMouseOnObject = false;
  this.isObjectVisible = false;
  this.objectsToHide = [];
  
  this.savedPositions;
  
  this.init = function()
  {
    this.savedPositions = new Object();
  }
  
  this.show = function(elementId, e)
  {
    if (this.hold == false)
    {
      this.currentElementId = elementId;
      //dbg('show');
    
      var obj = null;
      while (obj = this.objectsToHide.pop())
        obj.style.display = "none";
      
      this.isObjectVisible = true;
      
      this.object = document.getElementById(elementId);
      this.object.onmouseover = this.objectMouseOver;
      this.object.onmouseout = this.objectMouseOut;
      
      this.object.style.display = "";
      this.orginalObjectWidth = this.object.clientWidth;
      this.orginalObjectHeight = this.object.clientHeight;
      this.object.style.display = "none";

      this.followmouse(e);
      document.onmousemove = this.followmouse;
      
      this.objectsToHide.push(this.object);
      //dbg(this.currentElementId);
    }
  }

  this.hide = function()
  {
    if (this.hold == false)
      this.forceHide();   
  }

  this.forceHide = function()
  {
    this.isObjectVisible = false;
  }
  
  this.toggleHold = function()
  {
    this.hold = !this.hold;
  }
  
  this.objectMouseOver = function()
  {
    cTrailGlobal.isObjectVisible = true;
  }

  this.objectMouseOut = function()
  {
    cTrailGlobal.isObjectVisible = false;
  }

  this.followmouse = function(e)
  {
    if (cTrailGlobal.hold == false)
    {
      var xcoord = 0;
      var ycoord = 0;

      var scrollLeft = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft;
      var scrollTop = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
      
      
      var cursorX = 0;
      var cursorY = 0;
      if (typeof e != "undefined" && typeof e.pageX != "undefined" && typeof e.pageY != "undefined")
      {
        cursorX = e.pageX - scrollLeft;
        cursorY = e.pageY - scrollTop;
      }
      else if (typeof window.event != "undefined")
      {
        cursorX = window.event.clientX;
        cursorY = window.event.clientY;
      }

      xcoord += cursorX + scrollLeft;
      ycoord += cursorY + scrollTop;

      var windowWidth = document.documentElement.clientWidth;
      var windowHeight = document.documentElement.clientHeight;
      
      var leftOfCursor = false;
      if (windowWidth / 2 < cursorX)
        leftOfCursor = true;

      var overCursor = false;
      if (windowHeight / 2 < cursorY)
        overCursor = true;

      if (cTrailGlobal.savedPositions[cTrailGlobal.currentElementId + 'left'] != undefined)
      {
        leftOfCursor = cTrailGlobal.savedPositions[cTrailGlobal.currentElementId + 'left'];
        overCursor = cTrailGlobal.savedPositions[cTrailGlobal.currentElementId + 'over'];
      }
      else
      {
        cTrailGlobal.savedPositions[cTrailGlobal.currentElementId + 'left'] = leftOfCursor;
        cTrailGlobal.savedPositions[cTrailGlobal.currentElementId + 'over'] = overCursor;
      }

        
      if (leftOfCursor)
      {
        xcoord -= cTrailGlobal.orginalObjectWidth;
        xcoord -= cTrailGlobal.offsetfrommouse[0];
      }
      else
      {
        xcoord += cTrailGlobal.offsetfrommouse[0];
      }
      
      if (overCursor)
      {
        ycoord -= cTrailGlobal.orginalObjectHeight;
        ycoord -= cTrailGlobal.offsetfrommouse[1];
      }
      else
      {
        ycoord += cTrailGlobal.offsetfrommouse[1];
      }

      var pageWindowWidth = scrollLeft + document.documentElement.clientWidth;
      var pageWindowHeight = scrollTop + document.documentElement.clientHeight;
      
      if (xcoord + cTrailGlobal.orginalObjectWidth >= pageWindowWidth - 10)
        xcoord = pageWindowWidth - cTrailGlobal.orginalObjectWidth - 10;
      else if (xcoord < scrollLeft)
        xcoord = scrollLeft + 10;

      if (ycoord + cTrailGlobal.orginalObjectHeight >= pageWindowHeight - 10)
        ycoord = pageWindowHeight - cTrailGlobal.orginalObjectHeight - 10;
      else if (ycoord < scrollTop)
        ycoord = scrollTop + 10;
      
      cTrailGlobal.object.style.left = xcoord + "px";
      cTrailGlobal.object.style.top = ycoord + "px";


      if (cTrailGlobal.isObjectVisible == false)
      {
        cTrailGlobal.object.style.display = "none";
        document.onmousemove = ""
      }
      else
      {
        cTrailGlobal.object.style.display = "";
      }
    }

  }
  
  this.hookEvent = function(element, eventName, callback)
  {
    if (typeof(element) == "string")
      element = document.getElementById(element);
    
    if (element == null)
      return;
    
    if (element.addEventListener)
    {
      if (eventName == 'mousewheel')
        element.addEventListener('DOMMouseScroll', callback, false);  
      
      element.addEventListener(eventName, callback, false);
    }
    else if (element.attachEvent)
    {
      element.attachEvent("on" + eventName, callback);
    }
  }

  cTrailGlobal = this;
  window.onresize = this.init;

  this.init();
  this.hookEvent(document.getElementsByTagName('body')[0], 'mousewheel', function()
  {
    cTrailGlobal.init();
  });
//  this.hookEvent(document.getElementsByName('body')[0], 'mousewheel', this.followmouse);
}
