﻿function cdsnetAddEvent(el,ev,func){
    if(!el)return;
    if(el.attachEvent)
        el.attachEvent('on'+ev,func);
    else if(el.addEventListener)
        el.addEventListener(ev,func,true);
    else 
        el['on'+ev]=func;
};
function cdsnetVerticalNewsTicker(containerID, width, height, contIn, bFloat){
    var obj = document.getElementById(containerID);
    var delay = 10;
    var tmr = 0;
    var canContinue=true;
    var currentTop = 0;
    var scrollAmount = 5;
    var waitAfterShow = 3000;
    var nextTop = 0;
    var waiting = false;
    if(!obj)return null;
    var maxH = 0;
    var floatT = 1;
    var th = height;
    
    function reset(){
        ___stopIt();
        maxH = 0;
        var chn = obj.childNodes;
        if(contIn)
            for(var z=0;z<chn.length;z++)
                if(chn[z].tagName)
                {
                    chn = chn[z].childNodes;
                    break;
                }
                
        for(var i=0;i<chn.length;i++){
            var cn = chn[i];
            if(cn.tagName){
                if(cn.offsetHeight<height){
                    cn.style.height = height + "px";
                }
                maxH+=height;
            }
        }
        window.setTimeout(
            function(){
                if(bFloat){
                    floatT = Math.floor(obj.offsetWidth / width) ;
                }
            },
            1);
        canContinue = true;
        ___startAnim();
    }
    this.reset = reset;
    function ___doAnim(){
        waiting = false;
        window.clearTimeout(tmr);
        if(nextTop <= -maxH / floatT){
            ___startAnim();
            return;
        }
        if(currentTop == nextTop){
            nextTop -= height;
            tmr = window.setTimeout(___doAnim,waitAfterShow);
            waiting = true;
            return;
        }
        currentTop -= scrollAmount;
        if(currentTop<nextTop)currentTop = nextTop;
        obj.style.top = currentTop + "px";
        tmr = window.setTimeout(___doAnim,delay);
    }
    
    function ___startAnim(){
        if(!canContinue)return;
        obj.style.top = height + "px";
        currentTop = height;
        nextTop = 0;
        tmr = window.setTimeout(___doAnim,delay);
    }
    
    function ___stopIt(){
        canContinue=false;
        window.clearTimeout(tmr);
    }
    function ___startIt(){
        canContinue=true;
        tmr = window.setTimeout(___doAnim,waiting ? waitAfterShow/3 : delay);
    }
    
    cdsnetAddEvent(obj.parentNode,"mouseover",___stopIt);
    cdsnetAddEvent(obj.parentNode,"mouseout",___startIt);
    
    reset();
    return this;
}
