if (!dmNewsFeed) var dmNewsFeed = {};
if (!dmNewsFeed.ticker) {
    dmNewsFeed.ticker = function() {

        this.arrNewsItems = [];
        this.intTickSpeed = 5000;
        this.intCursorRate = 10;
        this.intCursorSpeed = 30;
        this.intTickPos = 0;
        this.intCursorPosition = 0;
        this.tickLocked = false;
        this.fadeTimerID = null;
        this.autoTimerID = 0;
        this.intMaxCursorPosition = null;
        this.typingTickerContent = null;
        this.tickLink = null;
        this.cursor = null;
        this.width = 0;

        this.create = function(id) {
            if (!this.arrNewsItems.length) return;

            var that = this;
            var container = document.getElementById(id);

            this.typingTickerContent = document.getElementById("typingTickerContent");
            this.tickLink = document.getElementById("typingTickerLink");
            this.cursor = document.getElementById("typingTickerCursor");

            var kids = container.getElementsByTagName('img');
            for (var i = 0; i < kids.length; i++) {
                kids[i].onclick = function(e) { that.buttonClick(e, this.getAttribute('button')); };
                kids[i].onmousedown = function(e) { that.buttonDown(e, this.id); };
                kids[i].onmouseup = function(e) { that.buttonUp(e, this.id); };
                kids[i].oncontextmenu = function(e) { that.buttonMenu(e); };
            }

            this.typingTickerContent.onmouseover = function() { that.stopTicker(); };
            this.typingTickerContent.onmouseout = function() { that.resumeTicker(); };
            this.setArticle(0);
            this.playTicker();
        };

        this.buttonDown = function(e, id) {
            if (!e) var e = window.event;
            if ((this.tickLocked == false) && (e.button != 2)) {
                document.getElementById(id).style.margin = "2px 0px 0px 2px";
            }
        };

        this.buttonUp = function(e, id) {
            if (!e) var e = window.event;
            if ((this.tickLocked == false) && (e.button != 2)) {
                document.getElementById(id).style.margin = "";
            }
        };

        this.buttonClick = function(e, button) {
            this.delayTicker();
            if (button == "back") {
                this.prevArticle();
            } else if (button == "next") {
                this.nextArticle();
            }
        };

        this.buttonMenu = function(e) {
            return false;
        };

        this.prevArticle = function() {
            if (this.tickLocked == false) {
                if (this.intTickPos == 0) {
                    this.intTickPos = this.arrNewsItems.length - 1;
                } else {
                    this.intTickPos--;
                }
                this.setArticle(this.intTickPos);
            }
        };

        this.nextArticle = function() {
            if (this.tickLocked == false) {
                if (this.intTickPos == this.arrNewsItems.length - 1) {
                    this.intTickPos = 0;
                } else {
                    this.intTickPos++;
                }
                this.setArticle(this.intTickPos);
            }
        };

        this.setArticle = function(intPos) {
            this.tickLocked = true;
            this.intCursorPosition = this.typingTickerContent.offsetLeft;
            this.setCursorPosition(this.intCursorPosition);
            this.typingTickerContent.innerHTML = this.arrNewsItems[intPos];
            this.tickLink.href = this.typingTickerContent.firstChild.href;
            this.tickLink.onclick = function() { window.open(this.href); return false; };
            this.intMaxCursorPosition = this.typingTickerContent.offsetLeft + this.typingTickerContent.offsetWidth;
            this.tickLocked = false;
            this.showCursor();
            this.fadeIn();
        };

        this.fadeIn = function() {
            if (this.intCursorPosition <= this.intMaxCursorPosition) {
                this.intCursorPosition += this.intCursorRate;
                this.setCursorPosition(this.intCursorPosition);
                var that = this;
                this.fadeTimerID = self.setTimeout(function() { that.fadeIn(); }, this.intCursorSpeed);
            } else {
                clearTimeout(this.fadeTimerID);
                this.hideCursor();
            }
        };

        this.hideCursor = function() {
            this.cursor.className = "hidden";
        };

        this.showCursor = function() {
            this.cursor.className = "";
        };

        this.setCursorPosition = function(intCursorPosition) {
            if (!this.width) this.width = this.cursor.offsetWidth;
            this.cursor.style.width = this.width - this.intCursorPosition + 'px';
            this.cursor.style.left = this.intCursorPosition + "px";
        };

        this.playTicker = function() {
            if (this.autoTimerID != 0) {
                this.nextArticle();
            }
            var that = this;
            this.autoTimerID = self.setTimeout(function() { that.playTicker(); }, this.intTickSpeed);
        };

        this.stopTicker = function() {
            clearTimeout(this.autoTimerID);
        };

        this.resumeTicker = function() {
            clearTimeout(this.autoTimerID);
            var that = this;
            this.autoTimerID = self.setTimeout(function() { that.playTicker(); }, this.intTickSpeed);
        };

        this.delayTicker = function() {
            clearTimeout(this.autoTimerID);
            var that = this;
            this.autoTimerID = self.setTimeout(function() { that.playTicker(); }, this.intTickSpeed * 2);
        };

        this.add = function(item) {
            this.arrNewsItems[this.arrNewsItems.length] = item;
        };
    };
}

