//Effect.DefaultOptions.duration = 0.3;

NewsTicker = Class.create();
NewsTicker.prototype = {

    eltTickerPanel: null,
    eltTickerLink : null,

	items: [],
    index: 0,

    executer : null,

    paused : false,

	pauseLength: 5,

	initialize: function() {
        this.eltTickerLink = $('newsTickerLink');
        this.eltTickerPanel = $('panelNewsTicker');
	},
	
    add: function(sText, sUrl) {
        if (typeof(sText) == 'string' && typeof(sUrl) == 'string' && sText.length > 0 && sUrl.length > 0) {
            this.items.push({text:sText,url:sUrl});
        }
    },

    activate : function() {
        if (this.eltTickerLink && this.eltTickerPanel && this.items.length > 0) {
            Event.observe(this.eltTickerLink, 'mouseover', this.handleMouseOver.bindAsEventListener(this), false);
            Event.observe(this.eltTickerLink, 'mouseout', this.handleMouseOut.bindAsEventListener(this), false);
            this.executer = new PeriodicalExecuter(this.handleTick.bind(this), this.pauseLength);
        }
    },

    handleTick : function() {
        if (!this.paused) {
		    new Effect.Fade(this.eltTickerLink, { afterFinish: this.switchData.bind(this) });
        }
    },

    switchData : function() {
        var current = this.items[this.getIndex()];
        this.eltTickerLink.href = current.url;
        Element.update(this.eltTickerLink, current.text);
		new Effect.Appear(this.eltTickerLink);
    },

    getIndex : function() {
        this.index++;
        if (this.index >= this.items.length) this.index = 0;
        return this.index;
    },

    handleMouseOver : function(evnt) {
        this.paused = true;
        if (!Element.hasClassName(this.eltTickerPanel, 'hovered'))
            Element.addClassName(this.eltTickerPanel, 'hovered');
        return true;
    },

    handleMouseOut : function(evnt) {
        this.paused = false;
        if (Element.hasClassName(this.eltTickerPanel, 'hovered'))
            Element.removeClassName(this.eltTickerPanel, 'hovered');
        return true;
    }
    
};

