﻿function randomId(count)
{
  var fix = "abcdefghijklmnopqrstuvwxyz0123456789";
  var len = fix.length;
  var str = "";
  
  for(i = 0; i < count; i++)
  {
    str += fix.charAt(Math.random() * Len);
  }
  
  return str;
}

var MarqueeIDs = [];

function getMarqueeId(id)
{
  var newId = id == undefined ? randomId(5) : id;
  var names = "," + MarqueeIDs.join(",") + ",";
  
  while(names.indexOf("," + newId + ",") >= 0)
  {
    newId = randomId(5);
  }
  
  return newId;
}

function Marquee(parm)
{
  var _this = this;
  var _parm = parm;

  if (!_parm.id)
    return;
  
  _this.mode = _parm.mode || "x";
  _this.marqueeId = getMarqueeId(_parm.id);
  _this.marqueeObj = document.getElementById(_parm.id);
  _this.height = _this.marqueeObj.offsetHeight;
  _this.speed = _parm.speed || 10;
  _this.delay = _parm.delay == undefined ? 0 : _parm.delay;
  _this.autoStart = _parm.autoStart == undefined ? true : _parm.autoStart;
  _this.mousePause = _parm.mousePause == undefined ? true : _parm.mousePause;
  _this.timer = null;
  _this.pause = false;
  
  _this.init = function()
  {
    if ((_this.marqueeObj.scrollWidth <= _this.marqueeObj.offsetWidth && _this.mode == "x") && (_this.marqueeObj.scrollHeight <= _this.marqueeObj.offsetHeight && _this.mode == "y"))
      return;
    
    MarqueeIDs.push(_this.marqueeId);

    if (_this.mode == "x")
    {
      _this.marqueeObj.innerHTML = 
        "<table width='100%' border='0' align='left' cellpadding='0' cellspace='0'>" +
        " <tr>" +
        "  <td id = 'mq_" + _this.marqueeId + "_1'>" + _this.marqueeObj.innerHTML + "</td>" +
        "  <td id = 'mq_" + _this.marqueeId + "_2'>" + _this.marqueeObj.innerHTML + "</td>" +
        " </tr>" +
        "</table>";
    }
    else
    {
      _this.marqueeObj.innerHTML = 
        "<div id = 'mq_" + _this.marqueeId + "_1'>" + _this.marqueeObj.innerHTML + "</div>" +
        "<div id = 'mq_" + _this.marqueeId + "_2'>" + _this.marqueeObj.innerHTML + "</div>";
    }

    setTimeout(function()
    {
      _this.marqueeObj1 = document.getElementById("mq_" + _this.marqueeId + "_1");
      _this.marqueeObj2 = document.getElementById("mq_" + _this.marqueeId + "_2");
      _this.marqueeWidth = _this.marqueeObj1.scrollWidth;
      _this.marqueeHeight = _this.marqueeObj1.scrollHeight;

      if(_this.autoStart)
        _this.start();
    }, _this.delay);
  };
  
  // 开始滚动
  _this.start = function()
  {
    _this.timer = setInterval((_this.mode == "x" ? _this.scrollX : _this.scrollY), _this.speed);
  
    if (_this.mousePause)
    {
      _this.marqueeObj.onmouseover = function() {_this.pause = true;}
      _this.marqueeObj.onmouseout = function() {_this.pause = false;}
    }
  }

  // 停止滚动
  _this.stop = function()
  {
    clearInterval(_this.timer)
    _this.marqueeObj.onmouseover = function() {}
    _this.marqueeObj.onmouseout = function() {}
  }
  
  // 水平滚动
  _this.scrollX = function()
  {
    if(_this.pause)
      return;

    var left = _this.marqueeObj.scrollLeft;
    
    if (left == _this.marqueeWidth)
    {
      _this.marqueeObj.scrollLeft = 0 ;
    }
    else if(left > _this.mo1Width)
    {
      _this.marqueeObj.scrollLeft = left - _this.marqueeWidth;
    }
    else
    {
      _this.marqueeObj.scrollLeft++;
    }
  };

  // 垂直滚动
  _this.scrollY = function()
  {
    if(_this.pause)
      return;

    var top = _this.marqueeObj.scrollTop;
    
    if (top == _this.marqueeHeight)
    {
      _this.marqueeObj.scrollTop = 0 ;
    }
    else if (top > _this.marqueeHeight)
    {
      _this.marqueeObj.scrollTop = top - _this.marqueeHeight;
    }
    else
    {
      _this.marqueeObj.scrollTop++;
      
      if (_this.delay)
      {
        if ((_this.marqueeObj.scrollTop != 0) && ((_this.marqueeObj.scrollTop % _this.height) == 0))
        {
          _this.stop();
          setTimeout( _this.start, _this.delay);
        }
      }
    }
  };
  
  _this.init();
}

