﻿var NewsTicker = {
    timer: 5,
    interval: null,
    animation: 500,
    working: false,
    tickerContainer: null,
    newsItems: null,
    init: function() {
        if (!NewsTicker.interval) {
            $("#ticker-noticias").mouseover(function() {
                NewsTicker.stopScroll();
            }).mouseout(function() {
                NewsTicker.startScroll();
            });

            NewsTicker.tickerContainer = $("#ticker-noticias").length > 0 ? $("#ticker-noticias")[0] : null;
            if (NewsTicker.tickerContainer) {
                NewsTicker.newsItems = NewsTicker.tickerContainer.children;
                NewsTicker.startScroll();
            }
        }
    },

    TickerUpdate: function() {
        if (NewsTicker.working)
            return;

        if (!NewsTicker.tickerContainer)
            return;

        NewsTicker.working = true;

        // clone first child and append it as children tail
        NewsTicker.tickerContainer.appendChild(NewsTicker.tickerContainer.children[0].cloneNode(true));

        // animate
        $(NewsTicker.newsItems[0]).animate({ height: 'hide', opacity: 'hide' }, NewsTicker.animation, function() {
            // remove first (already invisible) child
            $(NewsTicker.newsItems[0]).remove();
            NewsTicker.working = false;
        });
    },

    startScroll: function() {
        if (!this.interval) {
            this.interval = setInterval("NewsTicker.TickerUpdate()", this.timer * 1000);
        }
    },

    stopScroll: function() {
        if (this.interval) {
            clearInterval(this.interval);
            this.interval = false;
        }
    }
}

$(NewsTicker.init);
