/*** News Ticker ***/
var newsTicker = Class.create({
    initialize: function (options) {
        this.options = Object.extend({
            elNewsList: $('news-ticker'),
            changeDelay: 1000
        }, options || {});

        this.elNewsItem = this.options.elNewsList.childElements('li');
        this.currentItem = 0;
        this.elNewsItem.invoke('hide');
        this.elNewsItem.invoke('removeClassName', 'invisible');
        ASelf = this;
        this.elNewsItem.each(function (el) {
            el.childElements('a').invoke('observe', 'focus', ASelf.pauseRotation); ;
            el.childElements('a').invoke('observe', 'blur', ASelf.restartRotation.bind(ASelf))
        });
//        this.elNewsItem.invoke('observe', 'focus', self.pauseRotationTest);
//        this.elNewsItem.invoke('observe', 'blur', self.restartRotationBlur.bind(self)); //this.restartRotation.bind(this));
        this.elNewsItem[0].show();
        this.options.elNewsList.onmouseover = this.pauseRotation;
        this.options.elNewsList.onmouseout = this.restartRotation.bind(this);
        window.newsTicker = setTimeout(this.startRotation.bind(this), this.options.changeDelay);
    },
    pauseRotationTest: function (e) {
        alert(1);
    },
    pauseRotation: function (e) {
        clearTimeout(window.newsTicker);
    },
    startRotation: function (elNewsItem) {
        clearTimeout(window.newsTicker);
        window.newsTicker = setTimeout(this.nextNewsItem.bind(this), 1000);
    },
    restartRotation: function (elNewsItem) {
        clearTimeout(window.newsTicker);
        window.newsTicker = setTimeout(this.nextNewsItem.bind(this), this.options.changeDelay);
    },
    restartRotationBlur: function (elNewsItem) {
        clearTimeout(window.newsTicker);
        window.newsTicker = setTimeout(this.testNewsItem.bind(this), 1000);
    },
    testNewsItem: function () {
        alert(this.currentItem);
    },
    nextNewsItem: function () {
        clearTimeout(window.newsTicker);
        this.elNewsItem[this.currentItem].fade({
            duration: 0.25,
            from: 1,
            to: 0
        });
        if (this.currentItem == this.elNewsItem.length - 1) {
            this.currentItem = 0;
        } else {
            this.currentItem++;
        }

        this.elNewsItem[this.currentItem].appear({
            duration: 1
        });

        window.newsTicker = setTimeout(this.nextNewsItem.bind(this), this.options.changeDelay);
    }
});
