﻿var StringBuffer = function()
{ this.buffer = new Array(); }

StringBuffer.prototype.append = function(str)
{ this.buffer[this.buffer.length] = str; }

StringBuffer.prototype.toString = function()
{ return this.buffer.join(""); }
if (!Array.prototype.push) {
    Array.prototype.push = function() {
        var startLength = this.length;
        for (var i = 0; i < arguments.length; i++)
            this[startLength + i] = arguments[i];

        return this.length;
    }
}

if (!String.prototype.trim) {
    String.prototype.trim = function()
    { return this.replace(new RegExp("(^\\s*)|(\\s*$)", "g"), ""); }
}

if (!String.prototype.replaceAll) {
    String.prototype.replaceAll = function(source, target) {
        source = source.replace(new RegExp("(\\W)", "g"), "\\$1");
        target = target.replace(new RegExp("\\$", "g"), "$$$$");
        return this.replace(new RegExp(source, "gm"), target);
    }
}

if (!String.prototype.count) {
    String.prototype.count = function(str) {
        var matches = this.match(new RegExp(str.replace(new RegExp("(\\W)", "g"), "\\$1"), "g"));
        return matches ? matches.length : 0;
    }
}

if (!String.prototype.htmlspecialchars) {
    String.prototype.htmlspecialchars = function()
    { return this.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll("<", "&gt;"); }
}

if (!String.prototype.unhtmlspecialchars) {
    String.prototype.unhtmlspecialchars = function()
    { return this.replaceAll("&amp;", "&").replaceAll("&lt;", "<").replaceAll("&gt;", ">"); }
}


function RollObj(objName, itemArray, intvalTime) {
    var ins = this;

    this.RollObj = document.getElementById(objName);
    this.item = new Array();
    this.top = new Array();
    this.itemCount = itemArray.length;
    this.heightSize = 34;
    this.Speed = 2;
    this.rollDeley = 0.1;
    if (intvalTime) this.intvalTime = intvalTime;
    else this.intvalTime = 2500;

    this.Count = 0;
    this.run = true;

    var strHtmlText = new StringBuffer();

    for (i = 0; i < this.itemCount; i++) {
        strHtmlText.append("<div id=\"" + objName + i + "\" style=\"top: " + (i * this.heightSize) + "px; position:absolute;\">");
        strHtmlText.append(decodeURIComponent(itemArray[i]));
        strHtmlText.append('</div>');
        this.top[i] = i * this.heightSize;
    }

    var oStyle = {
//        height: '14px',
        height: '30px',
        overflow: 'hidden',
        position: 'relative'
    };

    Element.setStyle(this.RollObj, oStyle);
    this.RollObj.innerHTML = strHtmlText.toString();

    for (i = 0; i < this.itemCount; i++) {
        this.item[i] = document.getElementById(objName + i);

        this.item[i].ins = this;
        this.item[i].onmouseover = function() {
            this.ins.run = false;
        }
        this.item[i].onmouseout = function() {
            this.ins.run = true;
        }
    }

    setTimeout(function() { ins.next() }, 5000);
}
RollObj.prototype.next = function() {
    var ins = this;

    if (!this.run) {
        setTimeout(function() { ins.next() }, 1000);
        return null;
    }

    for (i = 0; i < this.itemCount; i++) {
        if ((this.top[i] + this.heightSize) <= 0) {
            this.top[i] = this.heightSize * (this.itemCount - 1);
        }
        this.top[i] = this.top[i] - this.Speed;
        this.item[i].style.top = this.top[i] + 'px';
    }

    if ((this.Count++ * this.Speed + this.Speed) < this.heightSize) {
        setTimeout(function() { ins.next() }, this.rollDeley);
    }
    else {
        this.Count = 0;
        setTimeout(function() { ins.next() }, this.intvalTime);
    }
}
