/*
  -------------------------------------------------------------------------------------------------

  : marquee.js
  : ben miller : http://hyl.co.uk
  :
  : scrolling marquee
  : required files: util.js

  -------------------------------------------------------------------------------------------------
*/

var hyl = hyl || {};

var __hylMarquee = [];

AEL(window, 'load', function()
{
  for(var i=0;i<__hylMarquee.length;i++)
  {
    __hylMarquee[i].init();
  }
}, false);

hyl.Marquee = function(
  _id,
  _interval)
{
  if(!document.getElementById){ return; }

  this.index = __hylMarquee.length;
  __hylMarquee.push(this);

  this.id = _id;
  this.interval = _interval || 20;
  this.timer = null;
  this.el = document.createElement('span');
  this.threshold = 0;

  this.loaded = false;
}

hyl.Marquee.prototype.init = function()
{
  if(this.loaded) { return; }

  this.loaded = true;

  var box = $(this.id);

  box.style.overflow = 'hidden';
  box.style.whiteSpace = 'nowrap';
  box.style.display = 'block';

  this.el.style.position   = 'relative';
  this.el.style.whiteSpace = 'nowrap';
  this.el.style.background = 'transparent';

  this.el.innerHTML = box.innerHTML;

  box.innerHTML = '';
  box.appendChild(this.el);

  this.el.style.left = box.offsetWidth + 'px';

  var marq = this;

  AEL(box, 'mouseover', function() { marq.stop(); }, false);
  AEL(box, 'mouseout', function() { marq.start(); }, false);

  this.start();
}

hyl.Marquee.prototype.scroll = function()
{
  this.stop();

  var edge = this.el.offsetLeft - 1;

  if(Math.abs(edge) >= this.el.offsetWidth)
  {
    edge = this.el.parentNode.offsetWidth;
  }

  this.el.style.left = edge + 'px';

  this.start();
}

hyl.Marquee.prototype.stop = function()
{
  if(this.timer != null)
  {
    window.clearTimeout(this.timer);
    this.timer = null;
  }
}

hyl.Marquee.prototype.start = function()
{
  this.timer = window.setTimeout('__hylMarquee['+ this.index +'].scroll()', this.interval);
}
