﻿if (typeof NewsTicker == "undefined") {
    NewsTicker = function(scrollerID, lineHeight, pauseInterval, frameInterval, increment) {
        this.instanceID = NewsTicker.Instances.length;
        NewsTicker.Instances[this.id] = this;
        this.init(scrollerID, lineHeight, pauseInterval, frameInterval, increment);
    };

    NewsTicker.prototype = {
        increment: -1,
        frameInterval: 20,
        pauseInterval: 10000,
        lineHeight: 30,
        scroller: null,
        yOffset: 0,
        intervalID: null
    };

    NewsTicker.Instances = new Array();

    NewsTicker.prototype.init = function(scrollerID, _lineHeight, _pauseInterval, _frameInterval, _increment) {
        if (_lineHeight !== undefined) { this.lineHeight = _lineHeight };
        if (_pauseInterval !== undefined) { this.pauseInterval = _pauseInterval };
        if (_frameInterval !== undefined) { this.frameInterval = _frameInterval };
        if (_increment !== undefined) { this.increment = _increment };
        this.scroller = document.getElementById(scrollerID);
        if (!this.scroller) {
            alert('News ticker object not found! ');
            return (false);
        }
    };

    NewsTicker.prototype.moveScroll = function() {
        if ((this.increment < 0 && this.yOffset < (0 - this.scroller.offsetHeight)) || (this.increment > 0 && this.yOffset > this.scroller.offsetHeight)) {
            this.yOffset = this.lineHeight;
        } else {
            this.yOffset += this.increment;
            if (this.yOffset % this.lineHeight == 0) {
                this.pauseScroll();
            }
        }
        this.scroller.style.top = this.yOffset + 'px';
    };

    NewsTicker.prototype.pauseScroll = function() {
        if (this.intervalID) { this.stopScroll(this.intervalID) };
        setTimeout('NewsTicker.Instances[' + this.id + '].startScroll()', this.pauseInterval);
    };

    NewsTicker.prototype.stopScroll = function() {
        clearInterval(this.intervalID);
    };

    NewsTicker.prototype.startScroll = function() {
        /* don't scroll unless there is more than one line of content. */
        if (this.scroller.offsetHeight >= this.lineHeight) {
            this.intervalID = setInterval('NewsTicker.Instances[' + this.id + '].moveScroll()', this.frameInterval);
        }
    };
};

