var NewsTicker = new Class({

	/*Implements: [Options],*/
	
	options: {
		'characterDelay': 100,
		'resumeDelay': 1000,
		'changeDelay': 3000,
        'maxWidth': 450
	},
    
    setOptions: function(options){
        if(options){
            this.options = options;
        }
    },
    
	initialize: function(container, options) {
		this.container = $(container);
		this.setOptions(options);
		
		// Remove the headlines from the DOM and store them on this object
		var ul = this.container.getElements('ul.headlines');
		this.headlines = ul.getElements('a')[0];
        ul[0].remove();
        
		// If there are no headlines, hide the entire news bar
		if (this.headlines.length == 0) {
			this.container.remove();
			return;
		}

		// Initialise the play/pause, next and back buttons		
		var buttons = this.container.getElements('.news_controls img');
		this.playPayseButton = buttons[0];
		this.playPayseButton.addEvent('click', function() {
			if (this.paused) {
				this.addTickerEvents();
				this.resume();
			} else {
				this.removeTickerEvents();
				this.pause();
			}
		}.bind(this));
		buttons[1].addEvent('click', function() {
			this.pause();
			this.previous();
		}.bind(this));
		buttons[2].addEvent('click', function() {
			this.pause();
			this.next();
		}.bind(this));
		
		this.paused = false;
		this.boundPause = this.pause.bind(this);
		this.boundResume = this.resume.bind(this);
		
		// Create the ticker element itself
		this.ticker = new Element('a').injectInside(this.container.getElement('.news'));
		this.addTickerEvents();
		
		this.index = -1;
		this.next();
	},
	
	/**
	 * Advance to the next headline.
	 */
	next: function() {
		var index = this.index + 1;
        if(index > this.headlines.length) index = this.headlines.length;
        this.set(index);
	},

	/**
	 * Return to the previous headline.
	 */	
	previous: function() {
        var index = this.index - 1;
        if(index < 0) index = 0;
		this.set(index);
	},
	
	/**
	 * Jump to a specific headline.
	 */
	set: function(index) {
		this.index = index % this.headlines.length;
		this.ticker.href =  this.headlines[this.index].href;
        this.ticker.target = "_blank";
		if (this.paused) {
			this.ticker.setText(this.headlines[this.index].getText());
		} else {
			this.ticker.setText('_');
			this.tick();
		}
	},
	
	/**
	 * Advance the animation of the headline ticker.
	 */
	tick: function() {
		$clear(this.timer);
		var oldText = this.ticker.getText();
		var fullText = this.headlines[this.index].getText();
		if (oldText == fullText) {
            if(this.ticker.offsetWidth < this.ticker.scrollWidth){
                var difference = this.ticker.offsetWidth - this.ticker.scrollWidth;
                var fx = new Fx.Style(this.ticker, 'text-indent', {duration:1500});
                fx.addEvent('onComplete', function(){
                    var fx2 = new Fx.Style(this.ticker, 'text-indent', {duration:1500});
                    fx2.addEvent('onComplete', function(){
                        this.next.bind(this).delay(this.options.resumeDelay);
                    }.bind(this));
                    fx2.start.delay(1000, fx2, 0);
                }.bind(this));
                fx.start(difference - 10);
            }else{
                this.next.bind(this).delay(this.options.changeDelay);
            }
		} else {
			var newText = fullText.substring(0, oldText.length);
			if (newText != fullText) newText += '_';
			this.ticker.setText(newText);
			this.timer = this.tick.bind(this).delay(this.options.characterDelay);
		}
	},
	
	addTickerEvents: function() {
		this.removeTickerEvents();
		this.ticker.addEvent('mouseover', this.boundPause);
		this.ticker.addEvent('mouseout', this.boundResume);
	},
	
	removeTickerEvents: function() {
		this.ticker.removeEvent('mouseover', this.boundPause);
		this.ticker.removeEvent('mouseout', this.boundResume);
	},
	
	pause: function() {
		$clear(this.timer);
		this.ticker.setText(this.headlines[this.index].getText());
		this.playPayseButton.src = this.playPayseButton.src.replace('pause', 'play');
		this.paused = true;
	},
	
	resume: function() {
		$clear(this.timer);
		this.playPayseButton.src = this.playPayseButton.src.replace('play', 'pause');
		this.next.bind(this).delay(this.options.resumeDelay);
		this.paused = false;
	}

}); 


window.addEvent('domready', function() {
	new NewsTicker('news_ticker');
});