VScroller = function(container, iframe)
{
    this.id = container;
    this.iframe = iframe;
    this.init();
}

VScroller.prototype = {
    init: function()
    {
        this.content = document.getElementById(this.id);
        this.icontent = parent.top.document.getElementById(this.iframe);
        // If the container is actually coming "later" via Ajax, wait for it to arrive.
        if (!this.content || !this.icontent) {
            this.timerId = setTimeout(this.init.bind(this), 1000);
            return;
        }

        this.content.style.position = "relative";

        this.topLine = 1;
        this.length = 0;
        this.outerHeight = 0;
        this.scrolling = 1;
        this.delay = 100;

        this.scrollSetup();
    },

    pause: function()
    {
        this.scrolling = 0;
    },

    resume: function()
    {
        this.scrolling = 1;
    },

    scrollSetup: function()
    {
        // There is the possiblity that 'content' will not be set properly by
        // the time this script runs at page load time. So that means length
        // will be zero. If it is, setup a 1 second delay (rather than waiting
        // in a tight loop) then trigger scrollSetup again. This prevents
        // crappy browsers (you know which one I mean...) from complaining that
        // a script is running too slowly...
        this.length = this.content.offsetHeight;
        if (this.length == 0) {
            setTimeout(this.scrollSetup.bind(this), 1000);
            return;
        }

        // This may not be portable.
        this.outerHeight = this.icontent.offsetHeight;

        this.fixLinks();

        if (this.length <= this.outerHeight) {
            this.icontent.style.height = this.length+1;
            return;
        }

        addEvent(this.content, "mouseover", this.pause.bind(this), false);
        addEvent(this.content, "mouseout", this.resume.bind(this), false);

        setTimeout(this.setTop.bind(this), 5000);
    },

    setTop: function()
    {
        if(document.layers){
            this.content.top = this.topLine;
        }
        if(document.all){
            this.content.style.top = this.topLine;
        }
        if (!document.all && document.getElementById) {
            this.content.style.top = this.topLine+"px";
        }

        setTimeout(this.scroll.bind(this), this.delay);
    },

    scroll: function()
    {
//        console.log("scroll: scrolling=%d", this.scrolling);
        if (this.scrolling > 0) {
            this.topLine--;
            if (this.topLine < -(this.length-this.outerHeight)) {
                this.topLine = 1;
            }

            if (this.topLine >= 0)
                setTimeout(this.setTop.bind(this), 5000);
            else
                this.setTop();
        } else {
            setTimeout(this.scroll.bind(this), this.delay);
        }
    },

    handleClick: function(e)
    {
        var target = find_target(e);
        if (!target) return;

        parent.top.document.location = target.href;

        if (window.event) {
            window.event.cancelBubble = true;
            window.event.returnValue = false;
        }
        if (e && e.stopPropagation && e.preventDefault) {
            e.stopPropagation();
            e.preventDefault();
        }
    },

    fixLinks: function()
    {
        if (!document.getElementsByTagName)
            return;
        var all_links = document.getElementsByTagName('a');
        for (var i = 0; i < all_links.length; i++) {
            var link = all_links[i]; 
            addEvent(link, "click", this.handleClick.bind(this), true);
        }
    }
}


