var windowOnLoad = window.onload;
if (typeof window.onload != 'function') {
    window.onload = function() 
    {
        NEWS.load(2, 0);
    }
} else {
    window.onload = function()
    {
        windowOnLoad();
        NEWS.load(2, 0);
    }
}

var NEWS =
{
    limit : 0,
    offset : 0,
    count : 0,
    id : 'news',
    element_id : 'news-nav',
    inEffect : null,
    outEffect : null,
    change : null,
    duration : 15000,
    
    load : function(limit, offset)
    {
        this.setOpacity(0);
        FADE.inEffect();
        if (this.change) {
            window.clearTimeout(this.change);
        }
        if (limit >= 0){
            this.limit = limit;
        }
        if (offset >= 0){
            this.offset = offset;
        }
        var childs = document.getElementById(this.id).getElementsByTagName('div');
        var count = childs.length;
        var a = 0;
        var c = 0;
        for (var i=0;i < count;i++) {
            if (childs[i].className.match(/content-view-line/)) {
                if (this.limit==0 || (a >=this.offset && a < this.offset + this.limit)) {
                    childs[i].style.display='';
                } else {
                    childs[i].style.display='none';
                }
                a++;
            }
        }
        this.count = a;
        var navchilds = document.getElementById(this.element_id).getElementsByTagName('a');
        var navcount = navchilds.length;
        for (i=0;i < navcount;i++) {
            navchilds[i].className=(this.limit > 0 && i==Math.round(this.offset/this.limit)) ? 'selected' : 'normal';
        }
        this.change = setTimeout('NEWS.next()', this.duration);
    },
    
    stop : function()
    {
        this.setOpacity(10);
        if (this.inEffect) {
            window.clearTimeout(this.inEffect);
        }
        this.inEffect = null;
        if (this.outEffect) {
            window.clearTimeout(this.outEffect);
        }
        this.outEffect = null;
        if (this.change) {
            window.clearTimeout(this.change);
        }
        this.change = null;
    },
    
    next : function()
    {
        FADE.outEffect();
        this.offset = (this.offset + this.limit < this.count) ? this.offset + this.limit : this.offset + this.limit - this.count;
        this.load(this.limit, this.offset);
    },
    
    previous : function()
    {
        FADE.outEffect();
        this.offset = (this.offset - this.limit >= 0) ? this.offset - this.limit : this.offset - this.limit + this.count;
        this.load(this.limit, this.offset);
    },
    
    setOpacity : function(val)
    {
        var obj = document.getElementById(this.id);
        obj.style.MozOpacity = val;
        obj.style.opacity = val/10;
        obj.style.filter = 'alpha(opacity=' + val*10 + ')';
    }
}

var FADE =
{
    inEffect : function ()
    {
        if (NEWS.outEffect) {
            window.clearTimeout(NEWS.outEffect);
        }
        NEWS.outEffect = null;
        var obj = document.getElementById(NEWS.id);
        var alpha = 0;
        function a()
        {
            alpha++;
            NEWS.setOpacity(alpha);
            if(alpha < 11) {
                NEWS.inEffect = setTimeout(a, 100);
            }
        }
        NEWS.inEffect = setTimeout(a, 100);
    },
    
    outEffect : function ()
    {
        if (NEWS.inEffect) {
            window.clearTimeout(NEWS.inEffect);
        }
        NEWS.inEffect = null;
        var obj = document.getElementById(NEWS.id);
        var alpha = 10;
        function f()
        {
            alpha--;
            NEWS.setOpacity(alpha);
            if(alpha > -1) {
                NEWS.outEffect = setTimeout(f, 100);
            }
        }
        NEWS.outEffect = setTimeout(f, 100);
    }
}
