﻿// Create Object Class
function RssTicker(){
    this.initialize.apply(this, arguments);
}

// Array keep Objects
RssTicker.arrObj = new Array();

// Create Prototype for Object Class
RssTicker.prototype = {
    index: 0,
    seek: 30,
    step: 1,
    isPlaying: false,
    timer: null,
    element: null,
    ctrRss: null,
    indexLinkRss: 0,
    direction: 'Up',
    iheight: 0,
    initialize: function(ctrId,width,height,iheight,direction){
        // Keep this Object into ArrayObj
        this.index = RssTicker.arrObj.length;
        RssTicker.arrObj[this.index] = this;
        
        // Keep configs
        this.direction = direction;
        this.iheight = iheight;
        
        // Get Element container
        this.element = document.getElementById(ctrId);
        this.ctrRss = document.getElementById(ctrId+'_rss');
        if(!this.element) return false;
        
        // Set Style
        this.element.style.width = width + 'px';
        this.element.style.height = height + 'px';
        this.element.style.overflow = 'hidden';
        
        // Remoe Node Text
        for(var i=0; i<this.element.childNodes.length; i++){
            var itm = this.element.childNodes[i];
            if(itm.nodeType==3){
                this.element.removeChild(itm);
                i--;
            }else if(itm.nodeType==1){
                itm.style.height = iheight + 'px';
                itm.style.overflow = 'hidden';
            }
        }
    },
    addItem: function(html){
        var dv = document.createElement('div');
        dv.innerHTML = html;
        dv.style.height = this.iheight + 'px';
        dv.style.overflow = 'hidden';
        this.element.appendChild(dv);
    }
}

RssTicker.play = function(ind,miliSecondDelay){
    var obj = this.arrObj[ind];
    window.clearInterval(obj.timer);
    if(obj.direction == 'Up') window.setInterval("RssTicker.up("+obj.index+")",miliSecondDelay);
    if(obj.direction == 'Down') window.setInterval("RssTicker.down("+obj.index+")",miliSecondDelay);
}

RssTicker.up = function(ind){
    var obj = this.arrObj[ind];
    obj.element.scrollTop += obj.step;
    if(obj.element.scrollTop < obj.iheight){
        obj.isPlaying = true;
        window.setTimeout("RssTicker.up("+ind+")",obj.seek);
    }else{
        obj.isPlaying = false; 
        this.insertAfter(obj.element.firstChild,obj.element.lastChild);
        obj.element.scrollTop = 0;
    }
}

RssTicker.down = function(ind){
    var obj = this.arrObj[ind];
    obj.element.scrollTop -= obj.step;
    if(obj.element.scrollTop > 0){
        obj.isPlaying = true;
        window.setTimeout("RssTicker.down("+ind+")",obj.seek);
    }else{
        obj.isPlaying = false; 
        this.insertBefore(obj.element.lastChild,obj.element.firstChild);
        obj.element.scrollTop = obj.iheight;
    }
}

// Helper
RssTicker.insertAfter = function(newNode,oldNode){
    if(oldNode.nextSibling)
        oldNode.parentNode.insertBefore(newNode,oldNode.nextSibling);
    else oldNode.parentNode.appendChild(newNode);
}
RssTicker.insertBefore = function(newNode,oldNode){
    oldNode.parentNode.insertBefore(newNode,oldNode);
}