

var dbg = null;
if ('undefined' != typeof(console))
    dbg = console;
else if ('undefined' != typeof(Debug))
{
    dbg = 
    {
        log: function() 
        { 
            var outString = '';
            for(var arg = 0; arguments.length > arg; arg++)
                outString += arguments[arg];
            Debug.writeln(outString); 
        }
    }
}
else
{
    dbg =
    {
        log: function()
        {
        }
    }
}



var vscroll =
{
    pauseTime: 5000,
    scrollIncrement: 15,
    scrollFrequency: 50,
    parentTag: null,
    previousTag: null, 
    currentTag: null,
    intervalTimerID: 0,
    mouseX: 0,
    mouseY: 0,
    
    ctor: function(paramParent)
    {
        this.parentTag = document.getElementById(paramParent);
        this.previousTag = null;
        this.currentTag = null;
        
        // Make each child tag 'relative' positioned.
        var maxHeight = 0;
        for (var tag = this.parentTag.firstChild; null != tag; tag = tag.nextSibling)
        {
            if (this.ignoreNode(tag))
                continue;
            if (maxHeight < tag.offsetHeight)
                maxHeight = tag.offsetHeight;
            tag.style.position = 'relative';
        }
        dbg.log('=== MAX HEIGHT is ' + maxHeight);
        if ('undefined' != typeof(this.parentTag.style.maxHeight))
            this.parentTag.style.maxHeight = maxHeight + 'px';
        this.parentTag.style.height = maxHeight + 'px';
        this.parentTag.style.position = 'relative';
        window.vscroll = this;
        if ('undefined' != typeof(window.attachEvent))
            document.attachEvent('onmousemove', this.mouseMove);
        else
            document.addEventListener('mousemove', this.mouseMove, false);
    },
    
    mouseMove: function()
    {
        var _this = window.vscroll;
        var e = null;
        if (0 < arguments.length)
            e = arguments[0];
        else
            e = window.event;
        _this.mouseX = e.clientX;
        _this.mouseY = e.clientY;
        dbg.log('** MOUSE: ' + e.clientX + ', ' + e.clientY);
    },
    
    scrollup: function()
    {
        var _this = window.vscroll;
        if (0 >= (_this.currentTag.offsetTop - _this.scrollIncrement))
        {
            _this.currentTag.style.top = (parseInt(_this.currentTag.style.top) - _this.currentTag.offsetTop) + 'px';
            _this.pause();
            return;
        }
            
        var tagTop = _this.previousTag.style.top;
        var initPreviousTop = _this.previousTag.offsetTop;
        if ((null == tagTop) || ('' == tagTop))
            tagTop = _this.previousTag.offsetTop;
        _this.previousTag.style.top = (parseInt(tagTop) - _this.scrollIncrement) + 'px';
 
        var htmlText = _this.previousTag.innerHTML.substr(0, 35);
        re = /(\n|\r|\t| {2,})/g;
        htmlText = htmlText.replace(re, '');
        
        var tagTop = _this.currentTag.style.top;
        var initCurrentTop = _this.currentTag.offsetTop;
        if ((null == tagTop) || ('' == tagTop))
            tagTop = _this.currentTag.offsetTop;
        _this.currentTag.style.top = (parseInt(tagTop) - _this.scrollIncrement) + 'px';
 
        var htmlText2 = _this.currentTag.innerHTML.substr(0, 35);
        htmlText2 = htmlText2.replace(re, '');
        dbg.log(htmlText + ' [' + _this.previousTag.style.top + ',' + _this.previousTag.offsetTop + 
            ',' + initPreviousTop + '] |***| ' + htmlText2 + ' [' + _this.currentTag.style.top + 
            ',' + _this.currentTag.offsetTop + ',' + initCurrentTop + ']');
            
    },
    
    resume: function()
    {
        var _this = window.vscroll;
        //
        // Pause for more time if mouse is over our scroller.
        //
        if (((_this.parentTag.offsetLeft < _this.mouseX) && 
            ((_this.parentTag.offsetLeft + _this.parentTag.offsetWidth) > _this.mouseX)) &&
            ((_this.parentTag.offsetTop < _this.mouseY) && 
            ((_this.parentTag.offsetTop + _this.parentTag.offsetHeight) > _this.mouseY)))
        {
            dbg.log('** PAUSING');
            setTimeout(_this.resume, 1000);
            return;
        }
        _this.intervalTimerID = setInterval(_this.scrollup, _this.scrollFrequency);
    },
    
    start: function()
    {
        var _this = window.vscroll;
        //
        // Find suitable starting tags for previous and current.
        //
        if (null == _this.previousTag)
        {
            _this.previousTag = _this.parentTag.firstChild;
            while((null != _this.previousTag) && _this.ignoreNode(_this.previousTag))
            {
                _this.previousTag = _this.previousTag.nextSibling;
            }
        }

        if (null == _this.currentTag)
            _this.currentTag = _this.previousTag.nextSibling;
        while((null != _this.currentTag) && _this.ignoreNode(_this.currentTag))
        {
            _this.currentTag = _this.currentTag.nextSibling;
        }
        
        //
        // Start scrolling process with initial pause.
        //
        setTimeout(_this.resume, _this.pauseTime);
    },
    
    ignoreNode: function(node)
    {
        return ('#text' == node.nodeName) || ('script' == node.nodeName.toLowerCase());
    },
    
    pause: function()
    {
        dbg.log('		|**** PAUSE ****|');
        var _this = window.vscroll;
        if (0 != _this.intervalTimerID)
            clearInterval(_this.intervalTimerID);
        _this.previousTag = _this.currentTag;

        do
        {
            _this.currentTag = _this.currentTag.nextSibling;
            if (null == _this.currentTag)
                _this.currentTag = _this.parentTag.firstChild;
        } while (_this.ignoreNode(_this.currentTag))

        if ((null != _this.previousTag) && (null != _this.currentTag))
        {
            _this.currentTag.style.top = (_this.parentTag.offsetHeight + 40) + 'px';
            setTimeout(_this.resume, _this.pauseTime);
        }
    }
};

