
TickerBox = new Class({
    Implements: [Options,Events],

	options: {
        showCount:5,
        showHeight:75,
        tickerTimeout:10000,
        lineColor:'#aaaaaa'
    },
    initialize: function(name, origin, target, options) {
        this.name = name;
        this.setOptions(options);
        this.origin = $(origin);
        this.target = $(target);
        this.nscroll;
        this.currCDiv = 0;
        this.allNews = $$("#"+origin+" div.scrollitem");
        this.allNews.each(function(news) {
            news.hide();
        });
        this.newsCount = this.allNews.length;
        this.divCount = this.options.showCount;
        this.divHeight = this.options.showHeight;
        this.target.style.height= (this.divCount*this.divHeight)-1;
        this.newpos = (-1) * this.divHeight;
        this.boxCount = 5+2;
        this.tickerTimeout = this.options.tickerTimeout;
        this.scrollTimeout = 10;
        this.direction = "up";
        this.nostop = false;
        this.rscroll = new Element("div",
        {
            styles:{
                //height:(this.divCount*this.divHeight)+"px",
                width:this.target.style.width,
                position:'absolute',
                top:'0px'
            },
            onmouseover: this.name+".stopNscroll()",
            onmouseout: this.name+".startNscroll()"
        });
        this.rscroll.inject(this.target);
        this.elements = new Array();
        for (i=0;i<=this.divCount+1; i++) {
            this.elements[i] = new Element("div",
            {
                id: this.origin.id + i,
                styles: {
                    margin:'0px',
                    position:'relative',
                    top:'0px',
                    overflow:'hidden',
                    'border-bottom':'1px '+this.options.lineColor+' solid',
                    height:(this.divHeight-1)+'px',
                    width:this.target.style.width
                }
            }).inject(this.rscroll);
            
        }
        this.rscrollHeight = this.rscroll.getDimensions().y;
        this.start();
    },
    start: function() {
        this.elements[0].innerHTML = this.allNews[this.newsCount-1].innerHTML;
        this.elements[1].innerHTML = this.allNews[0].innerHTML;
        for (i=0; i<this.boxCount; i++) {
            n = i;
            if (i>=this.newsCount) n=0;
            if (this.elements[i+1]) this.elements[i+1].innerHTML = this.allNews[n].innerHTML;
        }
        this.rscroll.style.top = "-"+this.divHeight+"px";
        if (this.direction == "down") {
            this.nscroll = window.setTimeout(this.name+'.scrollvDown()',this.tickerTimeout);
        } else {
            if (this.tickerTimeout != 0) this.nscroll = window.setTimeout(this.name+'.scrollv()',this.tickerTimeout);
        }

    },
    scrollv: function () {
        if (this.direction == "down") {
            return this.scrollvDown();
        }
        this.newpos=this.newpos-3;
        //$('debug').innerHTML = this.name+" "+this.rscroll+" "+this.newpos;
        if (this.newpos % this.divHeight == 0) {
            this.nContent();
            this.rscroll.style.top = "-"+this.divHeight+"px";
            this.newpos=(-1)*this.divHeight;
            if (this.nostop) { this.nscroll = window.setTimeout(this.name+'.scrollv()',this.scrollTimeout); }
            else {if (this.tickerTimeout != 0) this.nscroll = window.setTimeout(this.name+'.scrollv()',this.tickerTimeout);}
        } else {
            if (this.newpos > (-1)*this.rscrollHeight) {
                this.rscroll.style.top = this.newpos+"px";
            } else {
                this.rscroll.style.top = "-"+this.divHeight+"px";
                this.newpos = (-1)*this.divHeight;
            }
            this.nscroll = window.setTimeout(this.name+'.scrollv()',this.scrollTimeout);
        }
    },
    scrollvDown: function() {
        if (this.direction == "up") {
            return this.scrollv();
        }
        this.newpos=this.newpos+3;
        if (this.newpos % this.divHeight == 0) {
            this.nContentDown();
            this.rscroll.style.top = "-"+this.divHeight+"px";
            this.newpos = (-1)*this.divHeight;
            if (this.nostop) this.nscroll = window.setTimeout(this.name+'.scrollv()',this.scrollTimeout);
            else {if (this.tickerTimeout != 0) this.nscroll = window.setTimeout(this.name+'.scrollv()',this.tickerTimeout)};
        } else {
            if (this.newpos > (-1)*this.rscrollHeight) {
                this.rscroll.style.top = this.newpos+"px";
            } else {
                this.rscroll.style.top = "-"+this.divHeight+"px";
                this.newpos = (-1)*this.divHeight;
            }
            this.nscroll = window.setTimeout(this.name+'.scrollv()',this.scrollTimeout);
        }
    },
    nContent: function() {
        this.elements[0].innerHTML = this.allNews[this.currCDiv].innerHTML;
        this.currCDiv = this.currCDiv+1;
        if (this.currCDiv >= this.newsCount) this.currCDiv = 0;
        this.elements[1].innerHTML = this.allNews[this.currCDiv].innerHTML;
        cDiv = this.currCDiv;
        for (i=1; i<this.boxCount; i++) {
            cDiv++;
            if (cDiv >= this.newsCount) cDiv = 0;
            if (this.elements[i+1]) this.elements[i+1].innerHTML = this.allNews[cDiv].innerHTML;
        }

    },
    nContentDown: function() {
        this.currCDiv = this.currCDiv-1;
        if (this.currCDiv < 0) this.currCDiv = this.newsCount-1;
        this.elements[1].innerHTML = this.allNews[this.currCDiv].innerHTML;
        n0Div = this.currCDiv-1;
        if (n0Div < 0) n0Div = this.newsCount-1;
        this.elements[0].innerHTML = this.allNews[n0Div].innerHTML;
        cDiv = this.currCDiv;
        for (i=1; i<this.boxCount; i++) {
            cDiv++;
            if (cDiv >= this.newsCount) cDiv = 0;
            //$('debug').innerHTML = ($('debug').innerHTML) + "<br/>n"+(i+1)+" "+cDiv;
            if (this.elements[i+1]) this.elements[i+1].innerHTML = this.allNews[cDiv].innerHTML;
        }
    },
    stopNscroll: function() {
        clearTimeout(this.nscroll);
    },

    startNscroll: function() {
        if (this.newpos % this.divHeight == 0) {
            if (this.tickerTimeout != 0) this.nscroll = window.setTimeout(this.name+'.scrollv()',this.tickerTimeout);
        } else {
            this.nscroll = window.setTimeout(this.name+'.scrollv()',1000);
        }
    },

    setDirection: function(val) {
        this.direction = val;
    },
    setNoStop: function(val) {
        this.nostop = val;
        if (this.nostop == true) {
            if (this.direction == "down") {
                clearTimeout(this.nscroll);
                this.nscroll = setTimeout(this.name+'.scrollvDown()',10);
            } else {
                clearTimeout(this.nscroll);
                this.nscroll = setTimeout(this.name+'.scrollv()',10);
            }
        }
    }
});
