

/* Class Marqee */
function Marquee(id, contents, pxSpeed, width, startLeftPos, contentWidth, paused, bgcolor)
{
  // Variables
  this.id = id;
  this.pxSpeed = pxSpeed;
  this.timeout = null;
  this.paused = paused;
  this.lastX = 0;
  this.dragging = false;
  this.fg = null;
  this.bg = null;
  this.contentWidth = contentWidth;

  // Methods
  this.scroll = scroll;
  this.pauseScroll = pauseScroll;
  this.unPauseScroll = unPauseScroll;
  this.unPauseScroll = unPauseScroll;
  this.setTheTimeout = setTheTimeout;
  this.move = move;
  this.beginDrag = beginDrag;
  this.drag = drag;
  this.endDrag = endDrag;
  this.writeContents = writeContents;
  this.assign = assign;
  this.setFGLeft = setFGLeft;
  this.getFGLeft = getFGLeft; 
  this.attachEventHandlers = attachEventHandlers;

  this.writeContents(bgcolor, startLeftPos, width, contents);

  this.setTheTimeout();
}


function writeContents(bgcolor, startLeftPos, width, contents)
{
  var fgWidth = 2 * this.contentWidth;

  var divbg='<div id="'+this.id+'bg" '+
            'style="position:relative; z-index:5000'+
            'background-color:red; '+
            'width:'+width+'px; '+
            'overflow:hidden; '+
            '">';

  var divEnd='</div>';

  var divfg='<div id="'+this.id+'fg" '+
            'style="position:relative; '+
            'background-color:'+bgcolor+'; '+
            'width:'+fgWidth+'px; '+
            'left:'+startLeftPos+';" '+
            '>';

  document.write(divbg + divfg + contents + contents + divEnd + divEnd);

  this.assign();
  this.attachEventHandlers();
}

function attachEventHandlers()
{
  attachEventAllBrowsers(this.fg, 'mouseover', handleMouseOver, false);
  attachEventAllBrowsers(this.fg, 'mouseout',  handleMouseOut, false);

  attachEventAllBrowsers(this.fg, 'mousedown',  handleMouseDown, false);
  attachEventAllBrowsers(this.fg, 'mouseup',  handleMouseUp, false);
  attachEventAllBrowsers(this.fg, 'mousemove',  handleMouseMove, false);

/*
            'onmouseout="'+this.id+'.unPauseScroll()" '+
            'onmousedown="'+this.id+'.beginDrag()" '+
            'onmouseup="'+this.id+'.endDrag()" '+
            'onmousemove="'+this.id+'.drag()" '+
*/
}

function handleMouseOver(e)
{
  panorama.pauseScroll();
}

function handleMouseOut(e)
{
  panorama.unPauseScroll();
}

function handleMouseDown(e)
{
  panorama.beginDrag(e);
}

function handleMouseUp(e)
{
  panorama.endDrag(e);
}

function handleMouseMove(e)
{
  panorama.drag(e);
}


function attachEventAllBrowsers(obj, eventType, eventHandler, useCapture)
{
  var useCapture = false;
  var success = false;

  if (!useCapture) 
  {
    useCapture = true;   
  }

  if (obj.addEventListener)
  {
    obj.addEventListener(eventType, eventHandler, useCapture);
    success = true;  
  }
  else if (obj.attachEvent)
  { 
    success = obj.attachEvent("on" + eventType, eventHandler);
  }

  return success;
}



function assign()
{
    this.bg = document.getElementById(this.id+'bg');
    this.fg = document.getElementById(this.id+'fg');
}

function beginDrag(e)
{
  if ( (ns && e.which!=1) || (ie && event.button!=1) ) 
    return true; // Other buttons.

  this.lastX = (ns) ? e.pageX : event.x;
  this.dragging = true;

  return false;
}

function drag(e)
{
  if ( (this.dragging == false) || (ns && e.which!=1) || (ie && event.button!=1) )  
    return true; // Other buttons or not dragging.

  var dragX = (ns) ? e.pageX : event.x;
  var amountX = dragX - this.lastX;
  this.move(amountX);

  return false;
}

function endDrag(e)
{
  if ( (ns && e.which!=1) || (ie && event.button!=1) ) 
    return true; // Other buttons.

  var dragX = (ns) ? e.pageX : event.x;
  var amountX = dragX - this.lastX;
  this.move(amountX);
  this.dragging=false;
  this.lastX = dragX - amountX;

  return false;
}

function unPauseScroll()
{
  if (this.paused == true)
  {   
    this.paused = false;
    this.setTheTimeout();
  }
}

function pauseScroll()
{
  if (this.paused == false)
  {

    this.paused = true;
    clearTimeout(this.timeout);
  }
}

function scroll()
{
  this.move(this.pxSpeed);
  this.setTheTimeout();
}

function move(amountX)
{
    var left = this.getFGLeft();
    var nextLeft = left + amountX;
    var offset = nextLeft + this.contentWidth;

    if (offset <= 0) 
    {
      nextLeft = offset;
    }
    else if (nextLeft > 0) 
    {
      nextLeft -= this.contentWidth;
      this.lastX += this.contentWidth;
    }

    this.setFGLeft(nextLeft);
}

function getFGLeft()
{
  var left = 0;

  left = parseInt(this.fg.style.left);

  return left;
}

function setFGLeft(newLeft) 
{
  this.fg.style.left = newLeft  + 'px';
}

function setTheTimeout()
{
  if (this.paused == false)
  {
    globalThis = this;
    this.timeout = setTimeout('globalThis.scroll();', 25);  
  }
}